New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic function to determine if URL is a store page #45299
Changes from 7 commits
185b3b8
8d473bc
18657a9
263ea48
f2a3e11
490f5b5
7bf2c09
d84d9f2
095c963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add is_store_page helper function |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -122,4 +122,115 @@ public static function is_site_fresh() { | |||||
|
||||||
return $date > $month_ago; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Test if a URL is a store page. This function ignores the domain and protocol of the URL and only checks the path and query string. | ||||||
* | ||||||
* Store pages are defined as: | ||||||
* | ||||||
* - My Account | ||||||
* - Shop | ||||||
* - Cart | ||||||
* - Checkout | ||||||
* - Privacy Policy | ||||||
* - Terms and Conditions | ||||||
* | ||||||
* Additionally, the following autogenerated pages should be included: | ||||||
* - Product pages | ||||||
* - Product Category pages | ||||||
* - Product Tag pages | ||||||
* | ||||||
* @param string $url URL to check. If not provided, the current URL will be used. | ||||||
* @return bool Whether or not the URL is a store page. | ||||||
*/ | ||||||
public static function is_store_page( $url = '' ) { | ||||||
$url = $url ? $url : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); | ||||||
|
||||||
if ( ! $url ) { | ||||||
return false; | ||||||
} | ||||||
$normalized_path = self::get_normalized_url_path( $url ); | ||||||
|
||||||
// WC store pages. | ||||||
$store_pages = array( | ||||||
'myaccount' => wc_get_page_id( 'myaccount' ), | ||||||
'shop' => wc_get_page_id( 'shop' ), | ||||||
'cart' => wc_get_page_id( 'cart' ), | ||||||
'checkout' => wc_get_page_id( 'checkout' ), | ||||||
'privacy' => wc_privacy_policy_page_id(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how big of an impact it is on performance but there's a get_option() call in wc_privacy_policy_page_id() |
||||||
'terms' => wc_terms_and_conditions_page_id(), | ||||||
); | ||||||
|
||||||
foreach ( $store_pages as $page => $page_id ) { | ||||||
if ( 0 >= $page_id ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$permalink = get_permalink( $page_id ); | ||||||
if ( ! $permalink ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
if ( 0 === strpos( $normalized_path, self::get_normalized_url_path( $permalink ) ) ) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
|
||||||
// Check product, category and tag pages. | ||||||
$permalink_structure = wc_get_permalink_structure(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how big of an impact it is on performance but there's a get_option() call in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. I think it's a minor impact. If it becomes a problem, we can optimize it later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From ChatGPT: The impact of calling
Overall, calling |
||||||
$permalink_keys = array( | ||||||
'category_base', | ||||||
'tag_base', | ||||||
'product_base', | ||||||
); | ||||||
|
||||||
foreach ( $permalink_keys as $key ) { | ||||||
if ( ! isset( $permalink_structure[ $key ] ) || ! is_string( $permalink_structure[ $key ] ) ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
// Check if the URL path starts with the matching base. | ||||||
if ( 0 === strpos( $normalized_path, trim( $permalink_structure[ $key ], '/' ) ) ) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
// If the permalink structure contains placeholders, we need to check if the URL matches the structure using regex. | ||||||
if ( strpos( $permalink_structure[ $key ], '%' ) !== false ) { | ||||||
global $wp_rewrite; | ||||||
$rules = $wp_rewrite->generate_rewrite_rule( $permalink_structure[ $key ] ); | ||||||
|
||||||
if ( is_array( $rules ) && ! empty( $rules ) ) { | ||||||
// rule key is the regex pattern. | ||||||
$rule = array_keys( $rules )[0]; | ||||||
$rule = '#^' . str_replace( '?$', '', $rule ) . '#'; | ||||||
|
||||||
if ( preg_match( $rule, $normalized_path ) ) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get normalized URL path. | ||||||
* 1. Only keep the path and query string (if any). | ||||||
* 2. Remove wp home path from the URL path if WP is installed in a subdirectory. | ||||||
* 3. Remove leading and trailing slashes. | ||||||
* | ||||||
* For example: | ||||||
* | ||||||
* - https://example.com/wordpress/shop/uncategorized/test/?add-to-cart=123 => shop/uncategorized/test/?add-to-cart=123 | ||||||
* | ||||||
* @param string $url URL to normalize. | ||||||
*/ | ||||||
private static function get_normalized_url_path( $url ) { | ||||||
$quey = wp_parse_url( $url, PHP_URL_QUERY ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in d84d9f2 |
||||||
$path = wp_parse_url( $url, PHP_URL_PATH ) . ( $quey ? '?' . $quey : '' ); | ||||||
$home_path = wp_parse_url( site_url(), PHP_URL_PATH ); | ||||||
$normalized_path = trim( substr( $path, strlen( $home_path ) ), '/' ); | ||||||
return $normalized_path; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can add a filter here for extensibility, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Added a filter here 095c963 🙂