2013-08-09 16:11:15 +00:00
< ? php
/**
* WooCommerce Page Functions
*
* Functions related to pages and menus .
*
* @ author WooThemes
* @ category Core
* @ package WooCommerce / Functions
* @ version 2.1 . 0
*/
2014-12-05 13:42:19 +00:00
/**
* Replace a page title with the endpoint title
* @ param string $title
* @ return string
*/
function wc_page_endpoint_title ( $title ) {
if ( is_main_query () && in_the_loop () && is_page () && is_wc_endpoint_url () ) {
$endpoint = WC () -> query -> get_current_endpoint ();
if ( $endpoint_title = WC () -> query -> get_endpoint_title ( $endpoint ) ) {
$title = $endpoint_title ;
}
}
return $title ;
}
add_filter ( 'the_title' , 'wc_page_endpoint_title' );
2013-08-09 16:11:15 +00:00
/**
* Retrieve page ids - used for myaccount , edit_address , shop , cart , checkout , pay , view_order , terms . returns - 1 if no page is found
*
* @ param string $page
* @ return int
*/
2013-11-25 14:07:22 +00:00
function wc_get_page_id ( $page ) {
2013-08-09 16:11:15 +00:00
if ( $page == 'pay' || $page == 'thanks' ) {
2013-11-01 22:00:09 +00:00
_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.' );
2013-08-09 16:11:15 +00:00
$page = 'checkout' ;
}
if ( $page == 'change_password' || $page == 'edit_address' || $page == 'lost_password' ) {
2013-11-25 14:07:22 +00:00
_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.' );
2013-08-09 16:11:15 +00:00
$page = 'myaccount' ;
}
$page = apply_filters ( 'woocommerce_get_' . $page . '_page_id' , get_option ( 'woocommerce_' . $page . '_page_id' ) );
2014-07-03 14:14:00 +00:00
return $page ? absint ( $page ) : - 1 ;
2013-08-09 16:11:15 +00:00
}
2015-02-15 19:13:22 +00:00
/**
* Retrieve page permalink
*
* @ param string $page
* @ return string
*/
function wc_get_page_permalink ( $page ) {
$permalink = get_permalink ( wc_get_page_id ( $page ) );
return apply_filters ( 'woocommerce_get_' . $page . '_page_permalink' , $permalink );
}
2013-08-09 16:11:15 +00:00
/**
* Get endpoint URL
*
* Gets the URL for an endpoint , which varies depending on permalink settings .
*
* @ return string
*/
2013-11-25 14:07:22 +00:00
function wc_get_endpoint_url ( $endpoint , $value = '' , $permalink = '' ) {
2013-08-09 16:11:15 +00:00
if ( ! $permalink )
$permalink = get_permalink ();
2013-09-03 15:14:56 +00:00
// Map endpoint to options
$endpoint = isset ( WC () -> query -> query_vars [ $endpoint ] ) ? WC () -> query -> query_vars [ $endpoint ] : $endpoint ;
2014-04-24 21:18:41 +00:00
$value = ( 'edit-address' == $endpoint ) ? wc_edit_address_i18n ( $value ) : $value ;
2013-09-03 15:14:56 +00:00
2014-03-03 11:54:49 +00:00
if ( get_option ( 'permalink_structure' ) ) {
if ( strstr ( $permalink , '?' ) ) {
$query_string = '?' . parse_url ( $permalink , PHP_URL_QUERY );
$permalink = current ( explode ( '?' , $permalink ) );
} else {
$query_string = '' ;
}
$url = trailingslashit ( $permalink ) . $endpoint . '/' . $value . $query_string ;
} else {
2013-08-09 16:11:15 +00:00
$url = add_query_arg ( $endpoint , $value , $permalink );
2014-03-03 11:54:49 +00:00
}
2013-08-09 16:11:15 +00:00
2014-10-01 15:50:22 +00:00
return apply_filters ( 'woocommerce_get_endpoint_url' , $url , $endpoint , $value , $permalink );
2013-08-09 16:11:15 +00:00
}
2014-04-24 21:18:41 +00:00
/**
* Get the edit address slug translation .
*
* @ param string $id Address ID .
* @ param bool $flip Flip the array to make it possible to retrieve the values from both sides .
*
* @ return string Address slug i18n .
*/
function wc_edit_address_i18n ( $id , $flip = false ) {
$slugs = apply_filters ( 'woocommerce_edit_address_slugs' , array (
2014-10-22 10:26:09 +00:00
'billing' => sanitize_title ( _x ( 'billing' , 'edit-address-slug' , 'woocommerce' ) ),
'shipping' => sanitize_title ( _x ( 'shipping' , 'edit-address-slug' , 'woocommerce' ) )
2014-04-24 21:18:41 +00:00
) );
if ( $flip ) {
$slugs = array_flip ( $slugs );
}
if ( ! isset ( $slugs [ $id ] ) ) {
return $id ;
}
return $slugs [ $id ];
}
2013-08-09 16:11:15 +00:00
/**
2013-11-25 14:07:22 +00:00
* Returns the url to the lost password endpoint url
2013-08-09 16:11:15 +00:00
*
* @ access public
2013-11-29 18:50:31 +00:00
* @ return string
2013-08-09 16:11:15 +00:00
*/
2013-12-02 11:34:27 +00:00
function wc_lostpassword_url () {
2015-02-15 19:13:22 +00:00
return wc_get_endpoint_url ( 'lost-password' , '' , wc_get_page_permalink ( 'myaccount' ) );
2013-08-09 16:11:15 +00:00
}
2013-12-02 11:34:27 +00:00
add_filter ( 'lostpassword_url' , 'wc_lostpassword_url' , 10 , 0 );
2013-08-09 16:11:15 +00:00
/**
* Get the link to the edit account details page
*
* @ return string
*/
2013-11-25 14:07:22 +00:00
function wc_customer_edit_account_url () {
2015-02-15 19:13:22 +00:00
$edit_account_url = wc_get_endpoint_url ( 'edit-account' , '' , wc_get_page_permalink ( 'myaccount' ) );
2013-08-09 16:11:15 +00:00
return apply_filters ( 'woocommerce_customer_edit_account_url' , $edit_account_url );
}
/**
2013-08-23 15:41:02 +00:00
* Hide menu items conditionally
2013-08-09 16:11:15 +00:00
*
* @ param array $items
* @ return array
*/
2014-11-20 00:43:09 +00:00
function wc_nav_menu_items ( $items ) {
2013-08-09 16:11:15 +00:00
if ( ! is_user_logged_in () ) {
2014-11-20 00:43:09 +00:00
$customer_logout = get_option ( 'woocommerce_logout_endpoint' , 'customer-logout' );
2013-08-09 16:11:15 +00:00
foreach ( $items as $key => $item ) {
2014-11-20 00:43:09 +00:00
if ( strstr ( $item -> url , $customer_logout ) ) {
2013-08-09 16:11:15 +00:00
unset ( $items [ $key ] );
2014-11-20 00:43:09 +00:00
}
2013-08-09 16:11:15 +00:00
}
}
2014-11-20 00:43:09 +00:00
2013-08-09 16:11:15 +00:00
return $items ;
}
2014-11-20 00:43:09 +00:00
add_filter ( 'wp_nav_menu_objects' , 'wc_nav_menu_items' , 10 );
2013-08-09 16:11:15 +00:00
/**
* Fix active class in nav for shop page .
*
* @ param array $menu_items
* @ return array
*/
2014-11-20 00:43:09 +00:00
function wc_nav_menu_item_classes ( $menu_items ) {
2013-08-09 16:11:15 +00:00
2014-11-20 00:43:09 +00:00
if ( ! is_woocommerce () ) {
return $menu_items ;
}
2013-08-09 16:11:15 +00:00
2013-11-25 14:07:22 +00:00
$shop_page = ( int ) wc_get_page_id ( 'shop' );
2013-08-09 16:11:15 +00:00
$page_for_posts = ( int ) get_option ( 'page_for_posts' );
foreach ( ( array ) $menu_items as $key => $menu_item ) {
$classes = ( array ) $menu_item -> classes ;
// Unset active class for blog page
if ( $page_for_posts == $menu_item -> object_id ) {
$menu_items [ $key ] -> current = false ;
2014-04-24 21:18:41 +00:00
2014-11-20 00:43:09 +00:00
if ( in_array ( 'current_page_parent' , $classes ) ) {
2013-10-07 13:47:39 +00:00
unset ( $classes [ array_search ( 'current_page_parent' , $classes ) ] );
2014-11-20 00:43:09 +00:00
}
2014-04-24 21:18:41 +00:00
2014-11-20 00:43:09 +00:00
if ( in_array ( 'current-menu-item' , $classes ) ) {
2013-10-07 13:47:39 +00:00
unset ( $classes [ array_search ( 'current-menu-item' , $classes ) ] );
2014-11-20 00:43:09 +00:00
}
2013-08-09 16:11:15 +00:00
// Set active state if this is the shop page link
} elseif ( is_shop () && $shop_page == $menu_item -> object_id ) {
2014-11-20 00:43:09 +00:00
$menu_items [ $key ] -> current = true ;
2013-08-09 16:11:15 +00:00
$classes [] = 'current-menu-item' ;
$classes [] = 'current_page_item' ;
// Set parent state if this is a product page
} elseif ( is_singular ( 'product' ) && $shop_page == $menu_item -> object_id ) {
$classes [] = 'current_page_parent' ;
}
2014-11-20 00:43:09 +00:00
$menu_items [ $key ] -> classes = array_unique ( $classes );
2013-08-09 16:11:15 +00:00
}
return $menu_items ;
}
2014-11-20 00:43:09 +00:00
add_filter ( 'wp_nav_menu_objects' , 'wc_nav_menu_item_classes' , 2 );
2013-08-09 16:11:15 +00:00
/**
* Fix active class in wp_list_pages for shop page .
*
* https :// github . com / woothemes / woocommerce / issues / 177
*
* @ author Jessor , Peter Sterling
* @ param string $pages
* @ return string
*/
2013-11-25 14:07:22 +00:00
function wc_list_pages ( $pages ) {
2013-08-09 16:11:15 +00:00
if ( is_woocommerce ()) {
$pages = str_replace ( 'current_page_parent' , '' , $pages ); // remove current_page_parent class from any item
2013-11-25 14:07:22 +00:00
$shop_page = 'page-item-' . wc_get_page_id ( 'shop' ); // find shop_page_id through woocommerce options
2013-08-09 16:11:15 +00:00
if ( is_shop ()) :
$pages = str_replace ( $shop_page , $shop_page . ' current_page_item' , $pages ); // add current_page_item class to shop page
else :
$pages = str_replace ( $shop_page , $shop_page . ' current_page_parent' , $pages ); // add current_page_parent class to shop page
endif ;
}
return $pages ;
}
2013-11-25 14:07:22 +00:00
add_filter ( 'wp_list_pages' , 'wc_list_pages' );