abstracts
5 years ago
admin
5 years ago
blocks
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
4 years ago
emails
5 years ago
export
5 years ago
gateways
4 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
wc-page-functions.php
228 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Page Functions |
| 4 | * |
| 5 | * Functions related to pages and menus. |
| 6 | * |
| 7 | * @package WooCommerce\Functions |
| 8 | * @version 2.6.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Replace a page title with the endpoint title. |
| 15 | * |
| 16 | * @param string $title Post title. |
| 17 | * @return string |
| 18 | */ |
| 19 | function wc_page_endpoint_title( $title ) { |
| 20 | global $wp_query; |
| 21 | |
| 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 | $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 25 | $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action ); |
| 26 | $title = $endpoint_title ? $endpoint_title : $title; |
| 27 | |
| 28 | remove_filter( 'the_title', 'wc_page_endpoint_title' ); |
| 29 | } |
| 30 | |
| 31 | return $title; |
| 32 | } |
| 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 Page slug. |
| 40 | * @return int |
| 41 | */ |
| 42 | function wc_get_page_id( $page ) { |
| 43 | if ( 'pay' === $page || 'thanks' === $page ) { |
| 44 | 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.' ); |
| 45 | |
| 46 | $page = 'checkout'; |
| 47 | } |
| 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 | |
| 54 | $page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option( 'woocommerce_' . $page . '_page_id' ) ); |
| 55 | |
| 56 | return $page ? absint( $page ) : -1; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Retrieve page permalink. |
| 61 | * |
| 62 | * @param string $page page slug. |
| 63 | * @param string|bool $fallback Fallback URL if page is not set. Defaults to home URL. @since 3.4.0. |
| 64 | * @return string |
| 65 | */ |
| 66 | function wc_get_page_permalink( $page, $fallback = null ) { |
| 67 | $page_id = wc_get_page_id( $page ); |
| 68 | $permalink = 0 < $page_id ? get_permalink( $page_id ) : ''; |
| 69 | |
| 70 | if ( ! $permalink ) { |
| 71 | $permalink = is_null( $fallback ) ? get_home_url() : $fallback; |
| 72 | } |
| 73 | |
| 74 | return apply_filters( 'woocommerce_get_' . $page . '_page_permalink', $permalink ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get endpoint URL. |
| 79 | * |
| 80 | * Gets the URL for an endpoint, which varies depending on permalink settings. |
| 81 | * |
| 82 | * @param string $endpoint Endpoint slug. |
| 83 | * @param string $value Query param value. |
| 84 | * @param string $permalink Permalink. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | function wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' ) { |
| 89 | if ( ! $permalink ) { |
| 90 | $permalink = get_permalink(); |
| 91 | } |
| 92 | |
| 93 | // Map endpoint to options. |
| 94 | $query_vars = WC()->query->get_query_vars(); |
| 95 | $endpoint = ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint; |
| 96 | $value = ( get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ) === $endpoint ) ? wc_edit_address_i18n( $value ) : $value; |
| 97 | |
| 98 | if ( get_option( 'permalink_structure' ) ) { |
| 99 | if ( strstr( $permalink, '?' ) ) { |
| 100 | $query_string = '?' . wp_parse_url( $permalink, PHP_URL_QUERY ); |
| 101 | $permalink = current( explode( '?', $permalink ) ); |
| 102 | } else { |
| 103 | $query_string = ''; |
| 104 | } |
| 105 | $url = trailingslashit( $permalink ); |
| 106 | |
| 107 | if ( $value ) { |
| 108 | $url .= trailingslashit( $endpoint ) . user_trailingslashit( $value ); |
| 109 | } else { |
| 110 | $url .= user_trailingslashit( $endpoint ); |
| 111 | } |
| 112 | |
| 113 | $url .= $query_string; |
| 114 | } else { |
| 115 | $url = add_query_arg( $endpoint, $value, $permalink ); |
| 116 | } |
| 117 | |
| 118 | return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Hide menu items conditionally. |
| 123 | * |
| 124 | * @param array $items Navigation items. |
| 125 | * @return array |
| 126 | */ |
| 127 | function wc_nav_menu_items( $items ) { |
| 128 | if ( ! is_user_logged_in() ) { |
| 129 | $customer_logout = get_option( 'woocommerce_logout_endpoint', 'customer-logout' ); |
| 130 | |
| 131 | if ( ! empty( $customer_logout ) && ! empty( $items ) && is_array( $items ) ) { |
| 132 | foreach ( $items as $key => $item ) { |
| 133 | if ( empty( $item->url ) ) { |
| 134 | continue; |
| 135 | } |
| 136 | $path = wp_parse_url( $item->url, PHP_URL_PATH ); |
| 137 | $query = wp_parse_url( $item->url, PHP_URL_QUERY ); |
| 138 | |
| 139 | if ( strstr( $path, $customer_logout ) || strstr( $query, $customer_logout ) ) { |
| 140 | unset( $items[ $key ] ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return $items; |
| 147 | } |
| 148 | add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 ); |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Fix active class in nav for shop page. |
| 153 | * |
| 154 | * @param array $menu_items Menu items. |
| 155 | * @return array |
| 156 | */ |
| 157 | function wc_nav_menu_item_classes( $menu_items ) { |
| 158 | if ( ! is_woocommerce() ) { |
| 159 | return $menu_items; |
| 160 | } |
| 161 | |
| 162 | $shop_page = wc_get_page_id( 'shop' ); |
| 163 | $page_for_posts = (int) get_option( 'page_for_posts' ); |
| 164 | |
| 165 | if ( ! empty( $menu_items ) && is_array( $menu_items ) ) { |
| 166 | foreach ( $menu_items as $key => $menu_item ) { |
| 167 | $classes = (array) $menu_item->classes; |
| 168 | $menu_id = (int) $menu_item->object_id; |
| 169 | |
| 170 | // Unset active class for blog page. |
| 171 | if ( $page_for_posts === $menu_id ) { |
| 172 | $menu_items[ $key ]->current = false; |
| 173 | |
| 174 | if ( in_array( 'current_page_parent', $classes, true ) ) { |
| 175 | unset( $classes[ array_search( 'current_page_parent', $classes, true ) ] ); |
| 176 | } |
| 177 | |
| 178 | if ( in_array( 'current-menu-item', $classes, true ) ) { |
| 179 | unset( $classes[ array_search( 'current-menu-item', $classes, true ) ] ); |
| 180 | } |
| 181 | } elseif ( is_shop() && $shop_page === $menu_id && 'page' === $menu_item->object ) { |
| 182 | // Set active state if this is the shop page link. |
| 183 | $menu_items[ $key ]->current = true; |
| 184 | $classes[] = 'current-menu-item'; |
| 185 | $classes[] = 'current_page_item'; |
| 186 | |
| 187 | } elseif ( is_singular( 'product' ) && $shop_page === $menu_id ) { |
| 188 | // Set parent state if this is a product page. |
| 189 | $classes[] = 'current_page_parent'; |
| 190 | } |
| 191 | |
| 192 | $menu_items[ $key ]->classes = array_unique( $classes ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return $menu_items; |
| 197 | } |
| 198 | add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 ); |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Fix active class in wp_list_pages for shop page. |
| 203 | * |
| 204 | * See details in https://github.com/woocommerce/woocommerce/issues/177. |
| 205 | * |
| 206 | * @param string $pages Pages list. |
| 207 | * @return string |
| 208 | */ |
| 209 | function wc_list_pages( $pages ) { |
| 210 | if ( ! is_woocommerce() ) { |
| 211 | return $pages; |
| 212 | } |
| 213 | |
| 214 | // Remove current_page_parent class from any item. |
| 215 | $pages = str_replace( 'current_page_parent', '', $pages ); |
| 216 | // Find shop_page_id through woocommerce options. |
| 217 | $shop_page = 'page-item-' . wc_get_page_id( 'shop' ); |
| 218 | |
| 219 | if ( is_shop() ) { |
| 220 | // Add current_page_item class to shop page. |
| 221 | return str_replace( $shop_page, $shop_page . ' current_page_item', $pages ); |
| 222 | } |
| 223 | |
| 224 | // Add current_page_parent class to shop page. |
| 225 | return str_replace( $shop_page, $shop_page . ' current_page_parent', $pages ); |
| 226 | } |
| 227 | add_filter( 'wp_list_pages', 'wc_list_pages' ); |
| 228 |