WC_Product_Collection_Block_Tracking::parse_editor_location_contextprivateWC 1.0

Parse editor's location context from WP Post.

Possible contexts:

  • post
  • page
  • single-product
  • product-archive
  • cart
  • checkout
  • product-catalog
  • order-confirmation

Method of the class: WC_Product_Collection_Block_Tracking{}

No Hooks.

Returns

String. Returns the context.

Usage

// private - for code of main (parent) class only
$result = $this->parse_editor_location_context( $post );
$post(WP_Post) (required)
The Post instance.

WC_Product_Collection_Block_Tracking::parse_editor_location_context() code WC 9.9.4

private function parse_editor_location_context( $post ) {
	$context = 'other';

	if ( ! $post instanceof \WP_Post ) {
		return $context;
	}

	$post_type = $post->post_type;
	if ( ! in_array( $post_type, array( 'post', 'page', 'wp_template', 'wp_template_part', 'wp_block' ), true ) ) {
		return $context;
	}

	if ( 'wp_template' === $post_type ) {
		$name = $post->post_name;
		if ( false !== strpos( $name, SingleProductTemplate::SLUG ) ) {
			$context = 'single-product';
		} elseif ( ProductAttributeTemplate::SLUG === $name ) {
			$context = 'product-archive';
		} elseif ( false !== strpos( $name, 'taxonomy-' ) ) { // Including the '-' in the check to avoid false positives.
			$taxonomy           = str_replace( 'taxonomy-', '', $name );
			$product_taxonomies = get_object_taxonomies( 'product', 'names' );
			if ( in_array( $taxonomy, $product_taxonomies, true ) ) {
				$context = 'product-archive';
			}
		} elseif ( in_array( $name, array( CartTemplate::SLUG, MiniCartTemplate::SLUG ), true ) ) {
			$context = 'cart';
		} elseif ( CheckoutTemplate::SLUG === $name ) {
			$context = 'checkout';
		} elseif ( ProductCatalogTemplate::SLUG === $name ) {
			$context = 'product-catalog';
		} elseif ( OrderConfirmationTemplate::SLUG === $name ) {
			$context = 'order-confirmation';
		}
	}

	if ( in_array( $post_type, array( 'wp_block', 'wp_template_part' ), true ) ) {
		$context = 'isolated';
	}

	if ( 'page' === $post_type ) {
		$context = 'page';
	}
	if ( 'post' === $post_type ) {
		$context = 'post';
	}

	return $context;
}