1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
<?php /** * WooCommerce Page Functions * * Functions related to pages and menus. * * @package WooCommerce\Functions * @version 2.6.0 */
defined( 'ABSPATH' ) || exit;
/** * Replace a page title with the endpoint title. * * @param string $title Post title. * @return string */ function wc_page_endpoint_title( $title ) { global $wp_query;
if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) { $endpoint = WC()->query->get_current_endpoint(); $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action ); $title = $endpoint_title ? $endpoint_title : $title;
remove_filter( 'the_title', 'wc_page_endpoint_title' ); }
return $title; }
add_filter( 'the_title', 'wc_page_endpoint_title' );
/** * Replace the title part of the document title. * * @param array $title { * The document title parts. * * @type string $title Title of the viewed page. * @type string $page Optional. Page number if paginated. * @type string $tagline Optional. Site description when on home page. * @type string $site Optional. Site title when not on home page. * } * @return array */ function wc_page_endpoint_document_title_parts( $title ) { global $wp_query;
if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && is_page() && is_wc_endpoint_url() ) { $endpoint = WC()->query->get_current_endpoint(); $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action ); $title['title'] = $endpoint_title ? $endpoint_title : $title['title'];
remove_filter( 'document_title_parts', 'wc_page_endpoint_document_title_parts' ); }
return $title; }
add_filter( 'document_title_parts', 'wc_page_endpoint_document_title_parts' );
/** * 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 Page slug. * @return int */ function wc_get_page_id( $page ) { if ( 'pay' === $page || 'thanks' === $page ) { 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.' );
$page = 'checkout'; } if ( 'change_password' === $page || 'edit_address' === $page || 'lost_password' === $page ) { 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.' );
$page = 'myaccount'; }
$page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option( 'woocommerce_' . $page . '_page_id' ) );
return $page ? absint( $page ) : -1; }
/** * Retrieve page permalink. * * @param string $page page slug. * @param string|bool $fallback Fallback URL if page is not set. Defaults to home URL. @since 3.4.0. * @return string */ function wc_get_page_permalink( $page, $fallback = null ) { $page_id = wc_get_page_id( $page ); $permalink = 0 < $page_id ? get_permalink( $page_id ) : '';
if ( ! $permalink ) { $permalink = is_null( $fallback ) ? get_home_url() : $fallback; }
return apply_filters( 'woocommerce_get_' . $page . '_page_permalink', $permalink ); }
/** * Get endpoint URL. * * Gets the URL for an endpoint, which varies depending on permalink settings. * * @param string $endpoint Endpoint slug. * @param string $value Query param value. * @param string $permalink Permalink. * * @return string */ function wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' ) { if ( ! $permalink ) { $permalink = get_permalink(); }
// Map endpoint to options. $query_vars = WC()->query->get_query_vars(); $endpoint = ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint; $value = ( get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ) === $endpoint ) ? wc_edit_address_i18n( $value ) : $value;
if ( get_option( 'permalink_structure' ) ) { if ( strstr( $permalink, '?' ) ) { $query_string = '?' . wp_parse_url( $permalink, PHP_URL_QUERY ); $permalink = current( explode( '?', $permalink ) ); } else { $query_string = ''; } $url = trailingslashit( $permalink );
if ( $value ) { $url .= trailingslashit( $endpoint ) . user_trailingslashit( $value ); } else { $url .= user_trailingslashit( $endpoint ); }
$url .= $query_string; } else { $url = add_query_arg( $endpoint, $value, $permalink ); }
return apply_filters( 'woocommerce_get_endpoint_url', $url, $endpoint, $value, $permalink ); }
/** * Hide or adjust menu items conditionally. * * @param array $items Navigation items. * @return array */ function wc_nav_menu_items( $items ) { $logout_endpoint = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
if ( ! empty( $logout_endpoint ) && ! empty( $items ) && is_array( $items ) ) { foreach ( $items as $key => $item ) { if ( empty( $item->url ) ) { continue; }
$path = wp_parse_url( $item->url, PHP_URL_PATH ) ?? ''; $query = wp_parse_url( $item->url, PHP_URL_QUERY ) ?? ''; $is_logout_link = strstr( $path, $logout_endpoint ) || strstr( $query, $logout_endpoint );
if ( ! $is_logout_link ) { continue; }
if ( is_user_logged_in() ) { $items[ $key ]->url = wp_nonce_url( $item->url, 'customer-logout' ); } else { unset( $items[ $key ] ); } } }
return $items; } add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_items', 10 );
/** * Hide menu items in navigation blocks conditionally. * * Does the same thing as wc_nav_menu_items but for block themes. * * @since 9.3.0 * @param \WP_Block_list $inner_blocks Inner blocks. * @return \WP_Block_list */ function wc_nav_menu_inner_blocks( $inner_blocks ) { $logout_endpoint = get_option( 'woocommerce_logout_endpoint', 'customer-logout' );
if ( ! empty( $logout_endpoint ) && $inner_blocks ) { foreach ( $inner_blocks as $inner_block_key => $inner_block ) { $url = $inner_block->parsed_block['attrs']['url'] ?? ''; $path = wp_parse_url( $url, PHP_URL_PATH ) ?? ''; $query = wp_parse_url( $url, PHP_URL_QUERY ) ?? ''; $is_logout_link = strstr( $path, $logout_endpoint ) || strstr( $query, $logout_endpoint );
if ( ! $is_logout_link ) { continue; }
if ( is_user_logged_in() ) { $inner_block->parsed_block['attrs']['url'] = wp_nonce_url( $inner_block->parsed_block['attrs']['url'], 'customer-logout' ); } else { unset( $inner_blocks[ $inner_block_key ] ); } } }
return $inner_blocks; } add_filter( 'block_core_navigation_render_inner_blocks', 'wc_nav_menu_inner_blocks' );
/** * Fix active class in nav for shop page. * * @param array $menu_items Menu items. * @return array */ function wc_nav_menu_item_classes( $menu_items ) { if ( ! is_woocommerce() ) { return $menu_items; }
$shop_page = wc_get_page_id( 'shop' ); $page_for_posts = (int) get_option( 'page_for_posts' );
if ( ! empty( $menu_items ) && is_array( $menu_items ) ) { foreach ( $menu_items as $key => $menu_item ) { $classes = (array) $menu_item->classes; $menu_id = (int) $menu_item->object_id;
// Unset active class for blog page. if ( $page_for_posts === $menu_id && isset( $menu_item->object ) && 'page' === $menu_item->object ) { $menu_items[ $key ]->current = false;
if ( in_array( 'current_page_parent', $classes, true ) ) { unset( $classes[ array_search( 'current_page_parent', $classes, true ) ] ); }
if ( in_array( 'current-menu-item', $classes, true ) ) { unset( $classes[ array_search( 'current-menu-item', $classes, true ) ] ); } } elseif ( is_shop() && $shop_page === $menu_id && 'page' === $menu_item->object ) { // Set active state if this is the shop page link. $menu_items[ $key ]->current = true; $classes[] = 'current-menu-item'; $classes[] = 'current_page_item';
} elseif ( is_singular( 'product' ) && $shop_page === $menu_id ) { // Set parent state if this is a product page. $classes[] = 'current_page_parent'; }
$menu_items[ $key ]->classes = array_unique( $classes ); } }
return $menu_items; } add_filter( 'wp_nav_menu_objects', 'wc_nav_menu_item_classes', 2 );
/** * Fix active class in wp_list_pages for shop page. * * See details in https://github.com/woocommerce/woocommerce/issues/177. * * @param string $pages Pages list. * @return string */ function wc_list_pages( $pages ) { 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 ); }
// Add current_page_parent class to shop page. return str_replace( $shop_page, $shop_page . ' current_page_parent', $pages ); } add_filter( 'wp_list_pages', 'wc_list_pages' );
|