2013-08-09 16:11:15 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Page Functions
2013-08-09 16:11:15 +00:00
*
* Functions related to pages and menus .
*
2017-11-20 22:19:09 +00:00
* @ package WooCommerce\Functions
2016-01-14 19:48:30 +00:00
* @ version 2.6 . 0
2013-08-09 16:11:15 +00:00
*/
2018-03-08 19:15:31 +00:00
defined ( 'ABSPATH' ) || exit ;
2015-11-06 09:22:19 +00:00
2014-12-05 13:42:19 +00:00
/**
2015-11-03 13:31:20 +00:00
* Replace a page title with the endpoint title .
2017-11-20 22:19:09 +00:00
*
* @ param string $title Post title .
2014-12-05 13:42:19 +00:00
* @ return string
*/
function wc_page_endpoint_title ( $title ) {
2015-04-19 23:46:04 +00:00
global $wp_query ;
if ( ! is_null ( $wp_query ) && ! is_admin () && is_main_query () && in_the_loop () && is_page () && is_wc_endpoint_url () ) {
2017-11-20 22:19:09 +00:00
$endpoint = WC () -> query -> get_current_endpoint ();
$endpoint_title = WC () -> query -> get_endpoint_title ( $endpoint );
$title = $endpoint_title ? $endpoint_title : $title ;
2015-04-19 23:46:04 +00:00
2015-02-19 10:37:57 +00:00
remove_filter ( 'the_title' , 'wc_page_endpoint_title' );
2014-12-05 13:42:19 +00:00
}
2015-04-19 23:46:04 +00:00
2014-12-05 13:42:19 +00:00
return $title ;
}
2015-04-19 23:46:04 +00:00
2014-12-05 13:42:19 +00:00
add_filter ( 'the_title' , 'wc_page_endpoint_title' );
2013-08-09 16:11:15 +00:00
/**
2015-11-03 13:31:20 +00:00
* Retrieve page ids - used for myaccount , edit_address , shop , cart , checkout , pay , view_order , terms . returns - 1 if no page is found .
2013-08-09 16:11:15 +00:00
*
2017-11-20 22:19:09 +00:00
* @ param string $page Page slug .
2013-08-09 16:11:15 +00:00
* @ return int
*/
2013-11-25 14:07:22 +00:00
function wc_get_page_id ( $page ) {
2017-11-20 22:19:09 +00:00
if ( 'pay' === $page || 'thanks' === $page ) {
2016-11-23 16:15:00 +00:00
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.' );
2013-08-09 16:11:15 +00:00
$page = 'checkout' ;
}
2016-09-07 22:32:24 +00:00
if ( 'change_password' === $page || 'edit_address' === $page || 'lost_password' === $page ) {
2016-11-23 16:15:00 +00:00
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.' );
2013-08-09 16:11:15 +00:00
$page = 'myaccount' ;
}
2016-09-02 01:51:31 +00:00
$page = apply_filters ( 'woocommerce_get_' . $page . '_page_id' , get_option ( 'woocommerce_' . $page . '_page_id' ) );
2013-08-09 16:11:15 +00:00
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
/**
2015-11-03 13:31:20 +00:00
* Retrieve page permalink .
2015-02-15 19:13:22 +00:00
*
2018-04-06 10:43:30 +00:00
* @ param string $page page slug .
* @ param string | bool $fallback Fallback URL if page is not set . Defaults to home URL . @ since 3.4 . 0.
2015-02-15 19:13:22 +00:00
* @ return string
*/
2018-04-06 10:43:30 +00:00
function wc_get_page_permalink ( $page , $fallback = null ) {
2015-07-13 09:59:34 +00:00
$page_id = wc_get_page_id ( $page );
2018-04-06 10:43:30 +00:00
$permalink = 0 < $page_id ? get_permalink ( $page_id ) : '' ;
if ( ! $permalink ) {
$permalink = is_null ( $fallback ) ? get_home_url () : $fallback ;
}
2015-02-19 10:37:57 +00:00
return apply_filters ( 'woocommerce_get_' . $page . '_page_permalink' , $permalink );
2015-02-15 19:13:22 +00:00
}
2013-08-09 16:11:15 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get endpoint URL .
2013-08-09 16:11:15 +00:00
*
* Gets the URL for an endpoint , which varies depending on permalink settings .
*
2017-11-20 22:19:09 +00:00
* @ param string $endpoint Endpoint slug .
* @ param string $value Query param value .
* @ param string $permalink Permalink .
2016-01-06 16:11:09 +00:00
*
2013-08-09 16:11:15 +00:00
* @ return string
*/
2013-11-25 14:07:22 +00:00
function wc_get_endpoint_url ( $endpoint , $value = '' , $permalink = '' ) {
2016-02-19 17:14:23 +00:00
if ( ! $permalink ) {
2013-08-09 16:11:15 +00:00
$permalink = get_permalink ();
2016-02-19 17:14:23 +00:00
}
2013-08-09 16:11:15 +00:00
2017-11-20 16:40:56 +00:00
// Map endpoint to options.
$query_vars = WC () -> query -> get_query_vars ();
2017-11-20 22:19:09 +00:00
$endpoint = ! empty ( $query_vars [ $endpoint ] ) ? $query_vars [ $endpoint ] : $endpoint ;
2017-11-20 16:40:56 +00:00
$value = ( get_option ( 'woocommerce_myaccount_edit_address_endpoint' , '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 , '?' ) ) {
2017-11-20 22:19:09 +00:00
$query_string = '?' . wp_parse_url ( $permalink , PHP_URL_QUERY );
2014-03-03 11:54:49 +00:00
$permalink = current ( explode ( '?' , $permalink ) );
} else {
$query_string = '' ;
}
2019-07-31 23:57:55 +00:00
$url = trailingslashit ( $permalink );
2018-02-19 13:14:07 +00:00
if ( $value ) {
2019-07-31 23:57:55 +00:00
$url .= trailingslashit ( $endpoint ) . user_trailingslashit ( $value );
} else {
$url .= user_trailingslashit ( $endpoint );
2018-02-19 13:14:07 +00:00
}
$url .= $query_string ;
2014-03-03 11:54:49 +00:00
} 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
}
/**
2015-11-03 13:31:20 +00:00
* Hide menu items conditionally .
2013-08-09 16:11:15 +00:00
*
2017-10-02 10:41:44 +00:00
* @ param array $items Navigation items .
2013-08-09 16:11:15 +00:00
* @ 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
2017-11-06 16:05:19 +00:00
if ( ! empty ( $customer_logout ) && ! empty ( $items ) && is_array ( $items ) ) {
2015-10-02 08:04:18 +00:00
foreach ( $items as $key => $item ) {
2017-10-02 10:41:44 +00:00
if ( empty ( $item -> url ) ) {
continue ;
}
2017-11-20 22:19:09 +00:00
$path = wp_parse_url ( $item -> url , PHP_URL_PATH );
$query = wp_parse_url ( $item -> url , PHP_URL_QUERY );
2017-10-02 10:41:44 +00:00
2016-05-31 06:46:05 +00:00
if ( strstr ( $path , $customer_logout ) || strstr ( $query , $customer_logout ) ) {
2015-10-02 08:04:18 +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
2016-07-11 14:56:35 +00:00
return $items ;
2013-08-09 16:11:15 +00:00
}
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 .
*
2017-11-20 22:19:09 +00:00
* @ param array $menu_items Menu items .
2013-08-09 16:11:15 +00:00
* @ return array
*/
2014-11-20 00:43:09 +00:00
function wc_nav_menu_item_classes ( $menu_items ) {
if ( ! is_woocommerce () ) {
return $menu_items ;
}
2013-08-09 16:11:15 +00:00
2018-04-04 15:12:05 +00:00
$shop_page = wc_get_page_id ( 'shop' );
2013-08-09 16:11:15 +00:00
$page_for_posts = ( int ) get_option ( 'page_for_posts' );
2017-11-06 16:05:19 +00:00
if ( ! empty ( $menu_items ) && is_array ( $menu_items ) ) {
foreach ( $menu_items as $key => $menu_item ) {
$classes = ( array ) $menu_item -> classes ;
2017-11-20 22:19:09 +00:00
$menu_id = ( int ) $menu_item -> object_id ;
2013-08-09 16:11:15 +00:00
2017-11-20 22:19:09 +00:00
// Unset active class for blog page.
if ( $page_for_posts === $menu_id ) {
2017-11-06 16:05:19 +00:00
$menu_items [ $key ] -> current = false ;
2014-04-24 21:18:41 +00:00
2017-11-20 22:19:09 +00:00
if ( in_array ( 'current_page_parent' , $classes , true ) ) {
unset ( $classes [ array_search ( 'current_page_parent' , $classes , true ) ] );
2017-11-06 16:05:19 +00:00
}
2013-08-09 16:11:15 +00:00
2017-11-20 22:19:09 +00:00
if ( in_array ( 'current-menu-item' , $classes , true ) ) {
unset ( $classes [ array_search ( 'current-menu-item' , $classes , true ) ] );
2017-11-06 16:05:19 +00:00
}
2017-11-20 22:19:09 +00:00
} elseif ( is_shop () && $shop_page === $menu_id && 'page' === $menu_item -> object ) {
// Set active state if this is the shop page link.
2017-11-06 16:05:19 +00:00
$menu_items [ $key ] -> current = true ;
2018-03-08 19:15:31 +00:00
$classes [] = 'current-menu-item' ;
$classes [] = 'current_page_item' ;
2013-08-09 16:11:15 +00:00
2017-11-20 22:19:09 +00:00
} elseif ( is_singular ( 'product' ) && $shop_page === $menu_id ) {
// Set parent state if this is a product page.
2017-11-06 16:05:19 +00:00
$classes [] = 'current_page_parent' ;
}
2013-08-09 16:11:15 +00:00
2017-11-06 16:05:19 +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 .
*
2017-11-20 22:19:09 +00:00
* See details in https :// github . com / woocommerce / woocommerce / issues / 177.
2013-08-09 16:11:15 +00:00
*
2017-11-20 22:19:09 +00:00
* @ param string $pages Pages list .
2013-08-09 16:11:15 +00:00
* @ return string
*/
2013-11-25 14:07:22 +00:00
function wc_list_pages ( $pages ) {
2018-05-15 02:08:18 +00:00
if ( ! is_woocommerce () ) {
return $pages ;
}
// Remove current_page_parent class from any item.
$pages = str_replace ( 'current_page_parent' , '' , $pages );
// Find shop_page_id through woocommerce options.
$shop_page = 'page-item-' . wc_get_page_id ( 'shop' );
if ( is_shop () ) {
// Add current_page_item class to shop page.
return str_replace ( $shop_page , $shop_page . ' current_page_item' , $pages );
2016-07-11 14:56:35 +00:00
}
2018-05-15 02:08:18 +00:00
// Add current_page_parent class to shop page.
return str_replace ( $shop_page , $shop_page . ' current_page_parent' , $pages );
2013-08-09 16:11:15 +00:00
}
2013-11-25 14:07:22 +00:00
add_filter ( 'wp_list_pages' , 'wc_list_pages' );