Automattic\WooCommerce\Admin

WCAdminHelper::is_store_page()public staticWC 1.0

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:

  • Shop
  • Cart
  • Checkout
  • Privacy Policy
  • Terms and Conditions

Additionally, the following autogenerated pages should be included:

  • Product pages
  • Product Category pages
  • Product Tag pages

Method of the class: WCAdminHelper{}

Hooks from the method

Return

true|false. Whether or not the URL is a store page.

Usage

$result = WCAdminHelper::is_store_page( $url );
$url(string)
URL to check. If not provided, the current URL will be used.
Default: ''

WCAdminHelper::is_store_page() code WC 9.0.1

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(
		'shop'        => wc_get_page_id( 'shop' ),
		'cart'        => wc_get_page_id( 'cart' ),
		'checkout'    => wc_get_page_id( 'checkout' ),
		'privacy'     => wc_privacy_policy_page_id(),
		'terms'       => wc_terms_and_conditions_page_id(),
		'coming_soon' => wc_get_page_id( 'coming_soon' ),
	);

	/**
	 * Filter the store pages array to check if a URL is a store page.
	 *
	 * @since 8.8.0
	 * @param array $store_pages The store pages array. The keys are the page slugs and the values are the page IDs.
	 */
	$store_pages = apply_filters( 'woocommerce_store_pages', $store_pages );

	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();
	$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;
}