/includes/wc-page-functions.php

https://github.com/crowdfavorite/woocommerce · PHP · 239 lines · 120 code · 41 blank · 78 comment · 37 complexity · e4c63a5336ccd99d7806e18437ce4d91 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.1.0
  11. */
  12. /**
  13. * Replace a page title with the endpoint title
  14. * @param string $title
  15. * @return string
  16. */
  17. function wc_page_endpoint_title( $title ) {
  18. if ( is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
  19. $endpoint = WC()->query->get_current_endpoint();
  20. if ( $endpoint_title = WC()->query->get_endpoint_title( $endpoint ) ) {
  21. $title = $endpoint_title;
  22. }
  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
  32. * @return int
  33. */
  34. function wc_get_page_id( $page ) {
  35. if ( $page == 'pay' || $page == 'thanks' ) {
  36. _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 ( $page == 'change_password' || $page == 'edit_address' || $page == 'lost_password' ) {
  40. _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
  50. * @return string
  51. */
  52. function wc_get_page_permalink( $page ) {
  53. $permalink = get_permalink( wc_get_page_id( $page ) );
  54. return apply_filters( 'woocommerce_get_' . $page . '_page_permalink', $permalink );
  55. }
  56. /**
  57. * Get endpoint URL
  58. *
  59. * Gets the URL for an endpoint, which varies depending on permalink settings.
  60. *
  61. * @return string
  62. */
  63. function wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' ) {
  64. if ( ! $permalink )
  65. $permalink = get_permalink();
  66. // Map endpoint to options
  67. $endpoint = isset( WC()->query->query_vars[ $endpoint ] ) ? WC()->query->query_vars[ $endpoint ] : $endpoint;
  68. $value = ( 'edit-address' == $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
  69. if ( get_option( 'permalink_structure' ) ) {
  70. if ( strstr( $permalink, '?' ) ) {
  71. $query_string = '?' . parse_url( $permalink, PHP_URL_QUERY );
  72. $permalink = current( explode( '?', $permalink ) );
  73. } else {
  74. $query_string = '';
  75. }
  76. $url = trailingslashit( $permalink ) . $endpoint . '/' . $value . $query_string;
  77. } else {
  78. $url = add_query_arg( $endpoint, $value, $permalink );
  79. }
  80. return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink );
  81. }
  82. /**
  83. * Get the edit address slug translation.
  84. *
  85. * @param string $id Address ID.
  86. * @param bool $flip Flip the array to make it possible to retrieve the values ​​from both sides.
  87. *
  88. * @return string Address slug i18n.
  89. */
  90. function wc_edit_address_i18n( $id, $flip = false ) {
  91. $slugs = apply_filters( 'woocommerce_edit_address_slugs', array(
  92. 'billing' => sanitize_title( _x( 'billing', 'edit-address-slug', 'woocommerce' ) ),
  93. 'shipping' => sanitize_title( _x( 'shipping', 'edit-address-slug', 'woocommerce' ) )
  94. ) );
  95. if ( $flip ) {
  96. $slugs = array_flip( $slugs );
  97. }
  98. if ( ! isset( $slugs[ $id ] ) ) {
  99. return $id;
  100. }
  101. return $slugs[ $id ];
  102. }
  103. /**
  104. * Returns the url to the lost password endpoint url
  105. *
  106. * @access public
  107. * @return string
  108. */
  109. function wc_lostpassword_url() {
  110. return wc_get_endpoint_url( 'lost-password', '', wc_get_page_permalink( 'myaccount' ) );
  111. }
  112. add_filter( 'lostpassword_url', 'wc_lostpassword_url', 10, 0 );
  113. /**
  114. * Get the link to the edit account details page
  115. *
  116. * @return string
  117. */
  118. function wc_customer_edit_account_url() {
  119. $edit_account_url = wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) );
  120. return apply_filters( 'woocommerce_customer_edit_account_url', $edit_account_url );
  121. }
  122. /**
  123. * Hide menu items conditionally
  124. *
  125. * @param array $items
  126. * @return array
  127. */
  128. function wc_nav_menu_items( $items ) {
  129. if ( ! is_user_logged_in() ) {
  130. $customer_logout = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
  131. foreach ( $items as $key => $item ) {
  132. if ( strstr( $item->url, $customer_logout ) ) {
  133. unset( $items[ $key ] );
  134. }
  135. }
  136. }
  137. return $items;
  138. }
  139. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
  140. /**
  141. * Fix active class in nav for shop page.
  142. *
  143. * @param array $menu_items
  144. * @return array
  145. */
  146. function wc_nav_menu_item_classes( $menu_items ) {
  147. if ( ! is_woocommerce() ) {
  148. return $menu_items;
  149. }
  150. $shop_page = (int) wc_get_page_id('shop');
  151. $page_for_posts = (int) get_option( 'page_for_posts' );
  152. foreach ( (array) $menu_items as $key => $menu_item ) {
  153. $classes = (array) $menu_item->classes;
  154. // Unset active class for blog page
  155. if ( $page_for_posts == $menu_item->object_id ) {
  156. $menu_items[$key]->current = false;
  157. if ( in_array( 'current_page_parent', $classes ) ) {
  158. unset( $classes[ array_search('current_page_parent', $classes) ] );
  159. }
  160. if ( in_array( 'current-menu-item', $classes ) ) {
  161. unset( $classes[ array_search('current-menu-item', $classes) ] );
  162. }
  163. // Set active state if this is the shop page link
  164. } elseif ( is_shop() && $shop_page == $menu_item->object_id ) {
  165. $menu_items[ $key ]->current = true;
  166. $classes[] = 'current-menu-item';
  167. $classes[] = 'current_page_item';
  168. // Set parent state if this is a product page
  169. } elseif ( is_singular( 'product' ) && $shop_page == $menu_item->object_id ) {
  170. $classes[] = 'current_page_parent';
  171. }
  172. $menu_items[ $key ]->classes = array_unique( $classes );
  173. }
  174. return $menu_items;
  175. }
  176. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
  177. /**
  178. * Fix active class in wp_list_pages for shop page.
  179. *
  180. * https://github.com/woothemes/woocommerce/issues/177
  181. *
  182. * @author Jessor, Peter Sterling
  183. * @param string $pages
  184. * @return string
  185. */
  186. function wc_list_pages( $pages ) {
  187. if (is_woocommerce()) {
  188. $pages = str_replace( 'current_page_parent', '', $pages); // remove current_page_parent class from any item
  189. $shop_page = 'page-item-' . wc_get_page_id('shop'); // find shop_page_id through woocommerce options
  190. if (is_shop()) :
  191. $pages = str_replace($shop_page, $shop_page . ' current_page_item', $pages); // add current_page_item class to shop page
  192. else :
  193. $pages = str_replace($shop_page, $shop_page . ' current_page_parent', $pages); // add current_page_parent class to shop page
  194. endif;
  195. }
  196. return $pages;
  197. }
  198. add_filter( 'wp_list_pages', 'wc_list_pages' );