/includes/wc-page-functions.php

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