wc-page-functions.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * WooCommerce Page Functions
  4. *
  5. * Functions related to pages and menus.
  6. *
  7. * @package WooCommerce\Functions
  8. * @version 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Replace a page title with the endpoint title.
  13. *
  14. * @param string $title Post title.
  15. * @return string
  16. */
  17. function wc_page_endpoint_title( $title ) {
  18. global $wp_query;
  19. if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
  20. $endpoint = WC()->query->get_current_endpoint();
  21. $endpoint_title = WC()->query->get_endpoint_title( $endpoint );
  22. $title = $endpoint_title ? $endpoint_title : $title;
  23. remove_filter( 'the_title', 'wc_page_endpoint_title' );
  24. }
  25. return $title;
  26. }
  27. add_filter( 'the_title', 'wc_page_endpoint_title' );
  28. /**
  29. * Retrieve page ids - used for myaccount, edit_address, shop, cart, checkout, pay, view_order, terms. returns -1 if no page is found.
  30. *
  31. * @param string $page Page slug.
  32. * @return int
  33. */
  34. function wc_get_page_id( $page ) {
  35. if ( 'pay' === $page || 'thanks' === $page ) {
  36. wc_deprecated_argument( __FUNCTION__, '2.1', 'The "pay" and "thanks" pages are no-longer used - an endpoint is added to the checkout instead. To get a valid link use the WC_Order::get_checkout_payment_url() or WC_Order::get_checkout_order_received_url() methods instead.' );
  37. $page = 'checkout';
  38. }
  39. if ( 'change_password' === $page || 'edit_address' === $page || 'lost_password' === $page ) {
  40. wc_deprecated_argument( __FUNCTION__, '2.1', 'The "change_password", "edit_address" and "lost_password" pages are no-longer used - an endpoint is added to the my-account instead. To get a valid link use the wc_customer_edit_account_url() function instead.' );
  41. $page = 'myaccount';
  42. }
  43. $page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option( 'woocommerce_' . $page . '_page_id' ) );
  44. return $page ? absint( $page ) : -1;
  45. }
  46. /**
  47. * Retrieve page permalink.
  48. *
  49. * @param string $page page slug.
  50. * @param string|bool $fallback Fallback URL if page is not set. Defaults to home URL. @since 3.4.0.
  51. * @return string
  52. */
  53. function wc_get_page_permalink( $page, $fallback = null ) {
  54. $page_id = wc_get_page_id( $page );
  55. $permalink = 0 < $page_id ? get_permalink( $page_id ) : '';
  56. if ( ! $permalink ) {
  57. $permalink = is_null( $fallback ) ? get_home_url() : $fallback;
  58. }
  59. return apply_filters( 'woocommerce_get_' . $page . '_page_permalink', $permalink );
  60. }
  61. /**
  62. * Get endpoint URL.
  63. *
  64. * Gets the URL for an endpoint, which varies depending on permalink settings.
  65. *
  66. * @param string $endpoint Endpoint slug.
  67. * @param string $value Query param value.
  68. * @param string $permalink Permalink.
  69. *
  70. * @return string
  71. */
  72. function wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' ) {
  73. if ( ! $permalink ) {
  74. $permalink = get_permalink();
  75. }
  76. // Map endpoint to options.
  77. $query_vars = WC()->query->get_query_vars();
  78. $endpoint = ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint;
  79. $value = ( get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ) === $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
  80. if ( get_option( 'permalink_structure' ) ) {
  81. if ( strstr( $permalink, '?' ) ) {
  82. $query_string = '?' . wp_parse_url( $permalink, PHP_URL_QUERY );
  83. $permalink = current( explode( '?', $permalink ) );
  84. } else {
  85. $query_string = '';
  86. }
  87. $url = trailingslashit( $permalink ) . trailingslashit( $endpoint );
  88. if ( $value ) {
  89. $url .= trailingslashit( $value );
  90. }
  91. $url .= $query_string;
  92. } else {
  93. $url = add_query_arg( $endpoint, $value, $permalink );
  94. }
  95. return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink );
  96. }
  97. /**
  98. * Hide menu items conditionally.
  99. *
  100. * @param array $items Navigation items.
  101. * @return array
  102. */
  103. function wc_nav_menu_items( $items ) {
  104. if ( ! is_user_logged_in() ) {
  105. $customer_logout = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
  106. if ( ! empty( $customer_logout ) && ! empty( $items ) && is_array( $items ) ) {
  107. foreach ( $items as $key => $item ) {
  108. if ( empty( $item->url ) ) {
  109. continue;
  110. }
  111. $path = wp_parse_url( $item->url, PHP_URL_PATH );
  112. $query = wp_parse_url( $item->url, PHP_URL_QUERY );
  113. if ( strstr( $path, $customer_logout ) || strstr( $query, $customer_logout ) ) {
  114. unset( $items[ $key ] );
  115. }
  116. }
  117. }
  118. }
  119. return $items;
  120. }
  121. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
  122. /**
  123. * Fix active class in nav for shop page.
  124. *
  125. * @param array $menu_items Menu items.
  126. * @return array
  127. */
  128. function wc_nav_menu_item_classes( $menu_items ) {
  129. if ( ! is_woocommerce() ) {
  130. return $menu_items;
  131. }
  132. $shop_page = wc_get_page_id( 'shop' );
  133. $page_for_posts = (int) get_option( 'page_for_posts' );
  134. if ( ! empty( $menu_items ) && is_array( $menu_items ) ) {
  135. foreach ( $menu_items as $key => $menu_item ) {
  136. $classes = (array) $menu_item->classes;
  137. $menu_id = (int) $menu_item->object_id;
  138. // Unset active class for blog page.
  139. if ( $page_for_posts === $menu_id ) {
  140. $menu_items[ $key ]->current = false;
  141. if ( in_array( 'current_page_parent', $classes, true ) ) {
  142. unset( $classes[ array_search( 'current_page_parent', $classes, true ) ] );
  143. }
  144. if ( in_array( 'current-menu-item', $classes, true ) ) {
  145. unset( $classes[ array_search( 'current-menu-item', $classes, true ) ] );
  146. }
  147. } elseif ( is_shop() && $shop_page === $menu_id && 'page' === $menu_item->object ) {
  148. // Set active state if this is the shop page link.
  149. $menu_items[ $key ]->current = true;
  150. $classes[] = 'current-menu-item';
  151. $classes[] = 'current_page_item';
  152. } elseif ( is_singular( 'product' ) && $shop_page === $menu_id ) {
  153. // Set parent state if this is a product page.
  154. $classes[] = 'current_page_parent';
  155. }
  156. $menu_items[ $key ]->classes = array_unique( $classes );
  157. }
  158. }
  159. return $menu_items;
  160. }
  161. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
  162. /**
  163. * Fix active class in wp_list_pages for shop page.
  164. *
  165. * See details in https://github.com/woocommerce/woocommerce/issues/177.
  166. *
  167. * @param string $pages Pages list.
  168. * @return string
  169. */
  170. function wc_list_pages( $pages ) {
  171. if ( is_woocommerce() ) {
  172. // Remove current_page_parent class from any item.
  173. $pages = str_replace( 'current_page_parent', '', $pages );
  174. // Find shop_page_id through woocommerce options.
  175. $shop_page = 'page-item-' . wc_get_page_id( 'shop' );
  176. if ( is_shop() ) {
  177. // Add current_page_item class to shop page.
  178. $pages = str_replace( $shop_page, $shop_page . ' current_page_item', $pages );
  179. } else {
  180. // Add current_page_parent class to shop page.
  181. $pages = str_replace( $shop_page, $shop_page . ' current_page_parent', $pages );
  182. }
  183. }
  184. return $pages;
  185. }
  186. add_filter( 'wp_list_pages', 'wc_list_pages' );