class-woocommerce-shop-page.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend
  6. */
  7. /**
  8. * Represents the logic to determine if the current page is a WooCommerce shop page.
  9. */
  10. class WPSEO_WooCommerce_Shop_Page implements WPSEO_WordPress_Integration {
  11. /**
  12. * Registers the hooks
  13. *
  14. * @return void
  15. */
  16. public function register_hooks() {
  17. add_filter( 'wpseo_frontend_page_type_simple_page_id', array( $this, 'get_page_id' ) );
  18. }
  19. /**
  20. * Returns the ID of the WooCommerce shop page when the currently opened page is the shop page.
  21. *
  22. * @param int $page_id The page id.
  23. *
  24. * @return int The Page ID of the shop.
  25. */
  26. public function get_page_id( $page_id ) {
  27. if ( ! $this->is_shop_page() ) {
  28. return $page_id;
  29. }
  30. return $this->get_shop_page_id();
  31. }
  32. /**
  33. * Checks if the current page is the shop page.
  34. *
  35. * @return bool Whether the current page is the WooCommerce shop page.
  36. */
  37. public function is_shop_page() {
  38. if ( function_exists( 'is_shop' ) && function_exists( 'wc_get_page_id' ) ) {
  39. return is_shop() && ! is_search();
  40. }
  41. return false;
  42. }
  43. /**
  44. * Returns the id of the set WooCommerce shop page.
  45. *
  46. * @return int The ID of the set page.
  47. */
  48. public function get_shop_page_id() {
  49. static $shop_page_id;
  50. if ( ! $shop_page_id ) {
  51. $shop_page_id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : ( -1 );
  52. }
  53. return $shop_page_id;
  54. }
  55. }