PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/wc-page-functions.php

https://gitlab.com/hunt9310/ras
PHP | 215 lines | 109 code | 39 blank | 67 comment | 41 complexity | c451e7c2e5f8289794515da14a124019 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Page Functions
  4. *
  5. * Functions related to pages and menus.
  6. *
  7. * @author WooThemes
  8. * @category Core
  9. * @package WooCommerce/Functions
  10. * @version 2.6.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * Replace a page title with the endpoint title.
  17. * @param string $title
  18. * @return string
  19. */
  20. function wc_page_endpoint_title( $title ) {
  21. global $wp_query;
  22. if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
  23. $endpoint = WC()->query->get_current_endpoint();
  24. if ( $endpoint_title = WC()->query->get_endpoint_title( $endpoint ) ) {
  25. $title = $endpoint_title;
  26. }
  27. remove_filter( 'the_title', 'wc_page_endpoint_title' );
  28. }
  29. return $title;
  30. }
  31. add_filter( 'the_title', 'wc_page_endpoint_title' );
  32. /**
  33. * Retrieve page ids - used for myaccount, edit_address, shop, cart, checkout, pay, view_order, terms. returns -1 if no page is found.
  34. *
  35. * @param string $page
  36. * @return int
  37. */
  38. function wc_get_page_id( $page ) {
  39. if ( $page == 'pay' || $page == 'thanks' ) {
  40. _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.' );
  41. $page = 'checkout';
  42. }
  43. if ( $page == 'change_password' || $page == 'edit_address' || $page == 'lost_password' ) {
  44. _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.' );
  45. $page = 'myaccount';
  46. }
  47. $page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option('woocommerce_' . $page . '_page_id' ) );
  48. return $page ? absint( $page ) : -1;
  49. }
  50. /**
  51. * Retrieve page permalink.
  52. *
  53. * @param string $page
  54. * @return string
  55. */
  56. function wc_get_page_permalink( $page ) {
  57. $page_id = wc_get_page_id( $page );
  58. $permalink = 0 < $page_id ? get_permalink( $page_id ) : get_home_url();
  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
  67. * @param string $value
  68. * @param string $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. $endpoint = ! empty( WC()->query->query_vars[ $endpoint ] ) ? WC()->query->query_vars[ $endpoint ] : $endpoint;
  78. $value = ( 'edit-address' == $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
  79. if ( get_option( 'permalink_structure' ) ) {
  80. if ( strstr( $permalink, '?' ) ) {
  81. $query_string = '?' . parse_url( $permalink, PHP_URL_QUERY );
  82. $permalink = current( explode( '?', $permalink ) );
  83. } else {
  84. $query_string = '';
  85. }
  86. $url = trailingslashit( $permalink ) . $endpoint . '/' . $value . $query_string;
  87. } else {
  88. $url = add_query_arg( $endpoint, $value, $permalink );
  89. }
  90. return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink );
  91. }
  92. /**
  93. * Hide menu items conditionally.
  94. *
  95. * @param array $items
  96. * @return array
  97. */
  98. function wc_nav_menu_items( $items ) {
  99. if ( ! is_user_logged_in() ) {
  100. $customer_logout = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
  101. if ( ! empty( $customer_logout ) ) {
  102. foreach ( $items as $key => $item ) {
  103. $path = parse_url( $item->url, PHP_URL_PATH );
  104. $query = parse_url( $item->url, PHP_URL_QUERY );
  105. if ( strstr( $path, $customer_logout ) || strstr( $query, $customer_logout ) ) {
  106. unset( $items[ $key ] );
  107. }
  108. }
  109. }
  110. }
  111. return $items;
  112. }
  113. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
  114. /**
  115. * Fix active class in nav for shop page.
  116. *
  117. * @param array $menu_items
  118. * @return array
  119. */
  120. function wc_nav_menu_item_classes( $menu_items ) {
  121. if ( ! is_woocommerce() ) {
  122. return $menu_items;
  123. }
  124. $shop_page = (int) wc_get_page_id('shop');
  125. $page_for_posts = (int) get_option( 'page_for_posts' );
  126. foreach ( (array) $menu_items as $key => $menu_item ) {
  127. $classes = (array) $menu_item->classes;
  128. // Unset active class for blog page
  129. if ( $page_for_posts == $menu_item->object_id ) {
  130. $menu_items[$key]->current = false;
  131. if ( in_array( 'current_page_parent', $classes ) ) {
  132. unset( $classes[ array_search('current_page_parent', $classes) ] );
  133. }
  134. if ( in_array( 'current-menu-item', $classes ) ) {
  135. unset( $classes[ array_search('current-menu-item', $classes) ] );
  136. }
  137. // Set active state if this is the shop page link
  138. } elseif ( is_shop() && $shop_page == $menu_item->object_id && 'page' === $menu_item->object ) {
  139. $menu_items[ $key ]->current = true;
  140. $classes[] = 'current-menu-item';
  141. $classes[] = 'current_page_item';
  142. // Set parent state if this is a product page
  143. } elseif ( is_singular( 'product' ) && $shop_page == $menu_item->object_id ) {
  144. $classes[] = 'current_page_parent';
  145. }
  146. $menu_items[ $key ]->classes = array_unique( $classes );
  147. }
  148. return $menu_items;
  149. }
  150. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
  151. /**
  152. * Fix active class in wp_list_pages for shop page.
  153. *
  154. * https://github.com/woothemes/woocommerce/issues/177.
  155. *
  156. * @author Jessor, Peter Sterling
  157. * @param string $pages
  158. * @return string
  159. */
  160. function wc_list_pages( $pages ) {
  161. if ( is_woocommerce() ) {
  162. // Remove current_page_parent class from any item.
  163. $pages = str_replace( 'current_page_parent', '', $pages );
  164. // Find shop_page_id through woocommerce options.
  165. $shop_page = 'page-item-' . wc_get_page_id( 'shop' );
  166. if ( is_shop() ) {
  167. // Add current_page_item class to shop page.
  168. $pages = str_replace( $shop_page, $shop_page . ' current_page_item', $pages );
  169. } else {
  170. // Add current_page_parent class to shop page.
  171. $pages = str_replace( $shop_page, $shop_page . ' current_page_parent', $pages );
  172. }
  173. }
  174. return $pages;
  175. }
  176. add_filter( 'wp_list_pages', 'wc_list_pages' );