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

https://gitlab.com/webkod3r/tripolis · PHP · 261 lines · 132 code · 46 blank · 83 comment · 44 complexity · b0268219da8eac7fbffba55eee4c658e 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. 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 = $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. // Map endpoint to options
  76. $endpoint = ! empty( WC()->query->query_vars[ $endpoint ] ) ? WC()->query->query_vars[ $endpoint ] : $endpoint;
  77. $value = ( 'edit-address' == $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
  78. if ( get_option( 'permalink_structure' ) ) {
  79. if ( strstr( $permalink, '?' ) ) {
  80. $query_string = '?' . parse_url( $permalink, PHP_URL_QUERY );
  81. $permalink = current( explode( '?', $permalink ) );
  82. } else {
  83. $query_string = '';
  84. }
  85. $url = trailingslashit( $permalink ) . $endpoint . '/' . $value . $query_string;
  86. } else {
  87. $url = add_query_arg( $endpoint, $value, $permalink );
  88. }
  89. return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink );
  90. }
  91. /**
  92. * Get the edit address slug translation.
  93. *
  94. * @param string $id Address ID.
  95. * @param bool $flip Flip the array to make it possible to retrieve the values ​​from both sides.
  96. *
  97. * @return string Address slug i18n.
  98. */
  99. function wc_edit_address_i18n( $id, $flip = false ) {
  100. $slugs = apply_filters( 'woocommerce_edit_address_slugs', array(
  101. 'billing' => sanitize_title( _x( 'billing', 'edit-address-slug', 'woocommerce' ) ),
  102. 'shipping' => sanitize_title( _x( 'shipping', 'edit-address-slug', 'woocommerce' ) )
  103. ) );
  104. if ( $flip ) {
  105. $slugs = array_flip( $slugs );
  106. }
  107. if ( ! isset( $slugs[ $id ] ) ) {
  108. return $id;
  109. }
  110. return $slugs[ $id ];
  111. }
  112. /**
  113. * Returns the url to the lost password endpoint url.
  114. *
  115. * @access public
  116. * @param string $default_url
  117. * @return string
  118. */
  119. function wc_lostpassword_url( $default_url = '' ) {
  120. $wc_password_reset_url = wc_get_page_permalink( 'myaccount' );
  121. if ( false !== $wc_password_reset_url ) {
  122. return wc_get_endpoint_url( 'lost-password', '', $wc_password_reset_url );
  123. } else {
  124. return $default_url;
  125. }
  126. }
  127. add_filter( 'lostpassword_url', 'wc_lostpassword_url', 10, 1 );
  128. /**
  129. * Get the link to the edit account details page.
  130. *
  131. * @return string
  132. */
  133. function wc_customer_edit_account_url() {
  134. $edit_account_url = wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) );
  135. return apply_filters( 'woocommerce_customer_edit_account_url', $edit_account_url );
  136. }
  137. /**
  138. * Hide menu items conditionally.
  139. *
  140. * @param array $items
  141. * @return array
  142. */
  143. function wc_nav_menu_items( $items ) {
  144. if ( ! is_user_logged_in() ) {
  145. $customer_logout = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
  146. if ( ! empty( $customer_logout ) ) {
  147. foreach ( $items as $key => $item ) {
  148. if ( strstr( $item->url, $customer_logout ) ) {
  149. unset( $items[ $key ] );
  150. }
  151. }
  152. }
  153. }
  154. return $items;
  155. }
  156. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
  157. /**
  158. * Fix active class in nav for shop page.
  159. *
  160. * @param array $menu_items
  161. * @return array
  162. */
  163. function wc_nav_menu_item_classes( $menu_items ) {
  164. if ( ! is_woocommerce() ) {
  165. return $menu_items;
  166. }
  167. $shop_page = (int) wc_get_page_id('shop');
  168. $page_for_posts = (int) get_option( 'page_for_posts' );
  169. foreach ( (array) $menu_items as $key => $menu_item ) {
  170. $classes = (array) $menu_item->classes;
  171. // Unset active class for blog page
  172. if ( $page_for_posts == $menu_item->object_id ) {
  173. $menu_items[$key]->current = false;
  174. if ( in_array( 'current_page_parent', $classes ) ) {
  175. unset( $classes[ array_search('current_page_parent', $classes) ] );
  176. }
  177. if ( in_array( 'current-menu-item', $classes ) ) {
  178. unset( $classes[ array_search('current-menu-item', $classes) ] );
  179. }
  180. // Set active state if this is the shop page link
  181. } elseif ( is_shop() && $shop_page == $menu_item->object_id && 'page' === $menu_item->object ) {
  182. $menu_items[ $key ]->current = true;
  183. $classes[] = 'current-menu-item';
  184. $classes[] = 'current_page_item';
  185. // Set parent state if this is a product page
  186. } elseif ( is_singular( 'product' ) && $shop_page == $menu_item->object_id ) {
  187. $classes[] = 'current_page_parent';
  188. }
  189. $menu_items[ $key ]->classes = array_unique( $classes );
  190. }
  191. return $menu_items;
  192. }
  193. add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
  194. /**
  195. * Fix active class in wp_list_pages for shop page.
  196. *
  197. * https://github.com/woothemes/woocommerce/issues/177.
  198. *
  199. * @author Jessor, Peter Sterling
  200. * @param string $pages
  201. * @return string
  202. */
  203. function wc_list_pages( $pages ) {
  204. if (is_woocommerce()) {
  205. $pages = str_replace( 'current_page_parent', '', $pages); // remove current_page_parent class from any item
  206. $shop_page = 'page-item-' . wc_get_page_id('shop'); // find shop_page_id through woocommerce options
  207. if (is_shop()) :
  208. $pages = str_replace($shop_page, $shop_page . ' current_page_item', $pages); // add current_page_item class to shop page
  209. else :
  210. $pages = str_replace($shop_page, $shop_page . ' current_page_parent', $pages); // add current_page_parent class to shop page
  211. endif;
  212. }
  213. return $pages;
  214. }
  215. add_filter( 'wp_list_pages', 'wc_list_pages' );