Sin descripción

wc-page-functions.php 6.9KB

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