Commit fbd1d1e1c1e for woocommerce

commit fbd1d1e1c1e3cdc374910d26cb64108e0ba3b013
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date:   Sun Nov 22 20:53:58 2026 +0100

    Agent readiness: run the sitemap and public page checks with plain permalinks (#71284)

    * Agent readiness: run the sitemap and public page checks with plain permalinks

    With plain permalinks, /robots.txt answers with the home page, the
    WordPress sitemap is at /?sitemap=index, and the shop page redirects to
    /?post_type=product, which fetches don't follow. All three checks said
    couldn't check on every scan.

    * Fix plain-permalink product and shop URL checks

    Recognize a product from its query argument in any position, and keep checking a shop permalink a filter has replaced.

    * Update the plain-permalink changelog for merchants

    * Limit the plain-permalink shop swap to the page_id redirect

    * Keep the plain-permalink changelog out of the release notes

    * Turn the product archive off in the plain-permalink shop test

    CI's block theme registers one, so the test was checking the archive instead of the shop page.

diff --git a/plugins/woocommerce/changelog/fix-agent-readiness-plain-permalinks-robots b/plugins/woocommerce/changelog/fix-agent-readiness-plain-permalinks-robots
new file mode 100644
index 00000000000..8743736ef9d
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-agent-readiness-plain-permalinks-robots
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Agent readiness reads the sitemap and shop page with plain permalinks; behind a hidden flag.
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php b/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
index 750f6126979..b88d65084c7 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
@@ -678,7 +678,8 @@ class StoreOutputChecks {
 	 * The robots.txt crawlers get. '' when the store answered that there is none.
 	 * Null when it could not be read: the request failed, or the body is a page.
 	 *
-	 * Loaded from the host root, also when WordPress is in a subdirectory.
+	 * Loaded from the host root, also when WordPress is in a subdirectory. With plain permalinks
+	 * WordPress answers /robots.txt with a page, so crawlers get no rules: that reads as none.
 	 *
 	 * @param StoreResponses $responses Store responses.
 	 */
@@ -690,8 +691,11 @@ class StoreOutputChecks {
 		if ( in_array( $response['status'], array( 404, 410 ), true ) ) {
 			return '';
 		}
+		if ( ! str_starts_with( ltrim( $response['body'] ), '<' ) ) {
+			return $response['body'];
+		}
 		// A theme or a login wall that answers every URL with a page is not a robots.txt.
-		return str_starts_with( ltrim( $response['body'] ), '<' ) ? null : $response['body'];
+		return '' === (string) get_option( 'permalink_structure' ) ? '' : null;
 	}

 	/**
@@ -708,7 +712,8 @@ class StoreOutputChecks {
 		);
 		$defaults = array( '/sitemap_index.xml', '/sitemap.xml' );
 		if ( function_exists( 'wp_sitemaps_get_server' ) && wp_sitemaps_get_server()->sitemaps_enabled() ) {
-			$defaults[] = '/wp-sitemap.xml';
+			// /?sitemap=index with plain permalinks.
+			$defaults[] = (string) get_sitemap_url( 'index' );
 		}
 		return array_values( array_unique( array_merge( $listed, $defaults ) ) );
 	}
@@ -798,6 +803,16 @@ class StoreOutputChecks {
 		if ( false === stripos( $xml, '<sitemapindex' ) ) {
 			return array();
 		}
+		return self::loc_urls( $xml );
+	}
+
+	/**
+	 * The decoded URL in each loc element.
+	 *
+	 * @param string $xml Sitemap body.
+	 * @return array<int, string>
+	 */
+	private static function loc_urls( string $xml ): array {
 		$urls   = array();
 		$offset = 0;
 		$start  = stripos( $xml, '<loc>', $offset );
@@ -1673,8 +1688,8 @@ class StoreOutputChecks {

 	/**
 	 * Whether a sitemap index names a product sitemap (product-sitemap.xml,
-	 * wp-sitemap-posts-product-1.xml), or a urlset lists a product page. A category
-	 * sitemap and a page that isn't a sitemap never count.
+	 * wp-sitemap-posts-product-1.xml, or ?sitemap-subtype=product), or a urlset lists a product page.
+	 * A category sitemap and a page that isn't a sitemap never count.
 	 *
 	 * @param string $xml Sitemap body.
 	 */
@@ -1685,11 +1700,37 @@ class StoreOutputChecks {
 		if ( 1 === preg_match( '#<loc>[^<]*(?:product-sitemap|wp-sitemap-posts-product-)[^<]*\.xml\s*</loc>#i', $xml ) ) {
 			return true;
 		}
+		if ( '' === (string) get_option( 'permalink_structure' ) && self::plain_permalink_names_products( $xml ) ) {
+			return true;
+		}
 		// The fixed part of the product base, before a tag such as %product_cat%.
 		$base = trim( (string) strtok( (string) ( wc_get_permalink_structure()['product_base'] ?? '' ), '%' ), '/' );
 		return '' !== $base && 1 === preg_match( '#<loc>[^<]*/' . preg_quote( $base, '#' ) . '/[^<]+</loc>#i', $xml );
 	}

+	/**
+	 * Whether a plain-permalink sitemap names products. The product sitemap's subtype is
+	 * `product`, and a product page has a `product` query argument, in any position.
+	 *
+	 * @param string $xml Sitemap body.
+	 */
+	private static function plain_permalink_names_products( string $xml ): bool {
+		foreach ( self::loc_urls( $xml ) as $url ) {
+			$query = wp_parse_url( $url, PHP_URL_QUERY );
+			if ( ! is_string( $query ) || '' === $query ) {
+				continue;
+			}
+			$args = array();
+			wp_parse_str( $query, $args );
+			$subtype = $args['sitemap-subtype'] ?? '';
+			$product = $args['product'] ?? '';
+			if ( ( is_string( $subtype ) && 'product' === $subtype ) || ( is_string( $product ) && '' !== $product ) ) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 	/**
 	 * The page's robots meta tags and X-Robots-Tag header in lowercase, rules for one crawler included.
 	 *
@@ -1747,7 +1788,21 @@ class StoreOutputChecks {
 	 */
 	private static function store_page_urls( StoreResponses $responses ): array {
 		$product = $responses->product();
-		return array_values( array_filter( array( home_url( '/' ), wc_get_page_permalink( 'shop', '' ), null === $product ? '' : (string) $product->get_permalink() ) ) );
+		$shop    = wc_get_page_permalink( 'shop', '' );
+		$query   = wp_parse_url( $shop, PHP_URL_QUERY );
+		$args    = array();
+		if ( is_string( $query ) ) {
+			wp_parse_str( $query, $args );
+		}
+		// wc_template_redirect sends /?page_id=<shop> to the product archive. A front page, a filtered permalink, or a missing page has no page_id, so it stays.
+		$page_id = isset( $args['page_id'] ) && is_scalar( $args['page_id'] ) ? (int) $args['page_id'] : 0;
+		if ( 0 < $page_id && wc_get_page_id( 'shop' ) === $page_id && '' === (string) get_option( 'permalink_structure' ) ) {
+			$archive = get_post_type_archive_link( 'product' );
+			if ( is_string( $archive ) && '' !== $archive ) {
+				$shop = $archive;
+			}
+		}
+		return array_values( array_filter( array( home_url( '/' ), $shop, null === $product ? '' : (string) $product->get_permalink() ) ) );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
index 0cbbfb4919c..00ed88eaa71 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
@@ -54,7 +54,7 @@ class StoreOutputChecksTest extends WC_Unit_Test_Case {
 	/**
 	 * Answer requests by path from the home URL; a null response fails like a timeout, and any other path is a 404.
 	 *
-	 * @param array<string, array{0:int,1:string}|null> $responses Status and body by path.
+	 * @param array<string, array{0:int,1:string,2?:array<string,string>}|null> $responses Status, body and headers by path.
 	 */
 	private function serve( array $responses ): void {
 		add_filter(
@@ -67,7 +67,7 @@ class StoreOutputChecksTest extends WC_Unit_Test_Case {
 				}
 				$response = $responses[ $path ] ?? array( 404, '' );
 				return array(
-					'headers'  => array(),
+					'headers'  => new \WpOrg\Requests\Utility\CaseInsensitiveDictionary( $response[2] ?? array() ),
 					'body'     => $response[1],
 					'response' => array(
 						'code'    => $response[0],
@@ -117,6 +117,182 @@ class StoreOutputChecksTest extends WC_Unit_Test_Case {
 		$this->assertTrue( $this->results()['sitemap_lists_products'] );
 	}

+	/**
+	 * @testdox With plain permalinks, /robots.txt answers with a page and the sitemap is at /?sitemap=index: both checks still run.
+	 */
+	public function test_plain_permalinks(): void {
+		$this->set_permalink_structure( '' );
+		// The shop page redirects to the product archive only when the product type has one.
+		$product_type = get_post_type_object( 'product' );
+		$had_archive  = $product_type ? $product_type->has_archive : false;
+		if ( $product_type ) {
+			$product_type->has_archive = true;
+		}
+		try {
+			$product = \WC_Helper_Product::create_simple_product();
+			$shop    = wp_insert_post(
+				array(
+					'post_type'   => 'page',
+					'post_status' => 'publish',
+					'post_title'  => 'Shop',
+				)
+			);
+			update_option( 'woocommerce_shop_page_id', $shop );
+			$page = array( 200, '<!doctype html><title>Shop</title>' );
+			$this->serve(
+				array(
+					'/robots.txt'             => $page,
+					'/'                       => $page,
+					'/?post_type=product'     => $page,
+					self::path_of( $product ) => $page,
+					'/?sitemap=index'         => array( 200, '<sitemapindex><sitemap><loc>' . home_url( '/?sitemap=posts&amp;sitemap-subtype=product_cat&amp;paged=1' ) . '</loc></sitemap></sitemapindex>' ),
+					'/?sitemap=posts&sitemap-subtype=product_cat&paged=1' => array( 200, '<urlset><url><loc>' . home_url( '/?product=beanie' ) . '</loc><lastmod>2026-11-01</lastmod></url></urlset>' ),
+					"/?page_id=$shop"         => array( 302, '', array( 'location' => home_url( '/?post_type=product' ) ) ),
+				)
+			);
+
+			$results = $this->results();
+			$this->assertTrue( $results['sitemap_lists_products'] );
+			$this->assertTrue( $results['sitemap_lastmod'] );
+			$this->assertTrue( $results['pages_public'] );
+
+			$this->assertFalse( $this->names_products_in( '<urlset><url><loc>' . home_url( '/?product_cat=hats' ) . '</loc></url></urlset>' ) );
+			$this->assertTrue( $this->names_products_in( '<sitemapindex><sitemap><loc>' . home_url( '/?sitemap=posts&amp;sitemap-subtype=product&amp;paged=1' ) . '</loc></sitemap></sitemapindex>' ) );
+			$this->assertFalse( $this->names_products_in( '<sitemapindex><sitemap><loc>' . home_url( '/?sitemap=taxonomies&amp;sitemap-subtype=product_brand&amp;paged=1' ) . '</loc></sitemap></sitemapindex>' ) );
+			$this->assertTrue( $this->names_products_in( '<urlset><url><loc>' . home_url( '/?product=beanie&amp;lang=fr' ) . '</loc></url></urlset>' ) );
+		} finally {
+			if ( $product_type ) {
+				$product_type->has_archive = $had_archive;
+			}
+		}
+	}
+
+	/**
+	 * @testdox With plain permalinks, a page at /robots.txt counts as no robots file, so a missing sitemap fails.
+	 */
+	public function test_plain_permalinks_html_robots_is_no_file(): void {
+		$this->set_permalink_structure( '' );
+		$this->serve( array( '/robots.txt' => array( 200, '<!doctype html><title>Home</title>' ) ) );
+
+		$this->assertFalse( $this->results()['sitemap_lists_products'] );
+	}
+
+	/**
+	 * @testdox With plain permalinks and no product archive, the shop page itself is what gets checked.
+	 */
+	public function test_plain_permalinks_shop_without_archive(): void {
+		$this->set_permalink_structure( '' );
+		$product_type = get_post_type_object( 'product' );
+		$had_archive  = $product_type ? $product_type->has_archive : false;
+		try {
+			$shop = wp_insert_post(
+				array(
+					'post_type'   => 'page',
+					'post_status' => 'publish',
+					'post_title'  => 'Shop',
+				)
+			);
+			update_option( 'woocommerce_shop_page_id', $shop );
+			// A block theme registers a product archive. This case is the shop page with that archive off.
+			if ( $product_type ) {
+				$product_type->has_archive = false;
+			}
+			$this->serve(
+				array(
+					'/'                   => array( 200, '<!doctype html><title>Home</title>' ),
+					"/?page_id=$shop"     => array( 302, '', array( 'location' => site_url( '/wp-login.php' ) ) ),
+					'/wp-login.php'       => array( 200, '<html>Log in</html>' ),
+					'/?post_type=product' => array( 200, '<!doctype html><title>Shop</title>' ),
+				)
+			);
+
+			$this->assertFalse( $this->results()['pages_public'] );
+		} finally {
+			if ( $product_type ) {
+				$product_type->has_archive = $had_archive;
+			}
+		}
+	}
+
+	/**
+	 * @testdox With plain permalinks, the shop as the front page is not replaced by the product archive.
+	 */
+	public function test_plain_permalinks_front_page_shop(): void {
+		$this->set_permalink_structure( '' );
+		$product_type = get_post_type_object( 'product' );
+		$had_archive  = $product_type ? $product_type->has_archive : false;
+		if ( $product_type ) {
+			$product_type->has_archive = true;
+		}
+		try {
+			$shop = wp_insert_post(
+				array(
+					'post_type'   => 'page',
+					'post_status' => 'publish',
+					'post_title'  => 'Shop',
+				)
+			);
+			update_option( 'woocommerce_shop_page_id', $shop );
+			update_option( 'show_on_front', 'page' );
+			update_option( 'page_on_front', $shop );
+			$this->serve(
+				array(
+					'/'                   => array( 200, '<!doctype html><title>Home</title>' ),
+					'/?post_type=product' => array( 302, '', array( 'location' => site_url( '/wp-login.php' ) ) ),
+					'/wp-login.php'       => array( 200, '<html>Log in</html>' ),
+				)
+			);
+
+			$this->assertTrue( $this->results()['pages_public'] );
+		} finally {
+			if ( $product_type ) {
+				$product_type->has_archive = $had_archive;
+			}
+		}
+	}
+
+	/**
+	 * @testdox With plain permalinks, a custom shop permalink is the page that is checked.
+	 */
+	public function test_plain_permalinks_custom_shop_permalink(): void {
+		$this->set_permalink_structure( '' );
+		$shop = wp_insert_post(
+			array(
+				'post_type'   => 'page',
+				'post_status' => 'publish',
+				'post_title'  => 'Shop',
+			)
+		);
+		update_option( 'woocommerce_shop_page_id', $shop );
+		add_filter(
+			'woocommerce_get_shop_page_permalink',
+			static function () {
+				return home_url( '/?custom-shop=1' );
+			}
+		);
+		$this->serve(
+			array(
+				'/'                   => array( 200, '<!doctype html><title>Home</title>' ),
+				'/?custom-shop=1'     => array( 302, '', array( 'location' => site_url( '/wp-login.php' ) ) ),
+				'/wp-login.php'       => array( 200, '<html>Log in</html>' ),
+				'/?post_type=product' => array( 200, '<!doctype html><title>Shop</title>' ),
+			)
+		);
+
+		$this->assertFalse( $this->results()['pages_public'] );
+	}
+
+	/**
+	 * Whether the sitemap check passes when /?sitemap=index serves this XML and nothing else answers.
+	 *
+	 * @param string $xml Sitemap XML.
+	 */
+	private function names_products_in( string $xml ): bool {
+		remove_all_filters( 'pre_http_request' );
+		$this->serve( array( '/?sitemap=index' => array( 200, $xml ) ) );
+		return true === $this->results()['sitemap_lists_products'];
+	}
+
 	/**
 	 * @testdox With the WordPress sitemap off, a product sitemap listed in robots.txt or at /sitemap_index.xml passes.
 	 */