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