Automattic\WooCommerce\Internal\OrderReviews

Endpoint::maybe_create_host_pagepublicWC 10.8.0

Create or adopt the Review Order host page on every feature-on init.

Idempotent and self-healing: re-aligns the stored option with whichever row WP's permalink routing would resolve /review-order/ to, so the page id gate_request() checks always matches the page that add_rewrite_rule() at. Leftover duplicates from prior activation/disable cycles no longer cause asset enqueueing to silently skip.

Method of the class: Endpoint{}

No Hooks.

Returns

null. Nothing (null).

Usage

$Endpoint = new Endpoint();
$Endpoint->maybe_create_host_page(): void;

Changelog

Since 10.8.0 Introduced.

Endpoint::maybe_create_host_page() code WC 10.8.1

public function maybe_create_host_page(): void {
	// Fast path: the stored option already points at a published page
	// that still embeds our shortcode. `get_post()` is served from the
	// posts cache so this short-circuit costs ~nothing per request and
	// avoids the slug `wp_posts` lookup the reconciliation path runs.
	$option_id   = (int) wc_get_page_id( self::PAGE_KEY );
	$option_page = $option_id > 0 ? get_post( $option_id ) : null;
	if ( $option_page instanceof WP_Post
		&& 'page' === $option_page->post_type
		&& 'publish' === $option_page->post_status
		&& false !== strpos( (string) $option_page->post_content, '[' . self::SHORTCODE . ']' ) ) {
		return;
	}

	// Reconcile: adopt the slug-routed page when it also embeds our
	// shortcode. The combined signal avoids hijacking a merchant page
	// that happens to share either the slug or the shortcode alone.
	$canonical = $this->find_canonical_host_page();
	if ( $canonical instanceof WP_Post ) {
		$needs_save = false;

		if ( $option_id !== (int) $canonical->ID ) {
			update_option( 'woocommerce_review_order_page_id', (int) $canonical->ID );
			$needs_save = true;
		}
		if ( 'publish' !== $canonical->post_status ) {
			wp_update_post(
				array(
					'ID'          => (int) $canonical->ID,
					'post_status' => 'publish',
				)
			);
			$needs_save = true;
		}
		if ( $needs_save ) {
			update_option( 'woocommerce_review_order_flush_rewrite_pending', 'yes' );
		}
		return;
	}

	// No slug-canonical page. If the merchant renamed the host page away
	// from our default slug but the stored option still resolves to a
	// non-trashed page, respect it and only republish a draft we own.
	if ( $option_page instanceof WP_Post && 'page' === $option_page->post_type && 'trash' !== $option_page->post_status ) {
		if ( 'publish' !== $option_page->post_status ) {
			wp_update_post(
				array(
					'ID'          => (int) $option_page->ID,
					'post_status' => 'publish',
				)
			);
			update_option( 'woocommerce_review_order_flush_rewrite_pending', 'yes' );
		}
		return;
	}

	// No managed page anywhere. The permanent `woocommerce_create_pages`
	// filter (registered in `init()`) makes the call inject our entry.
	\WC_Install::create_pages();

	// Defer the rewrite flush to wp_loaded; rewrite_rule fires later on init.
	update_option( 'woocommerce_review_order_flush_rewrite_pending', 'yes' );
}