From 28b903fe2dc0a206d803b7f2de843eafc668bf2a Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 29 Nov 2026 16:06:13 -0500 Subject: [PATCH] Resolve catalog product description links through the route table --- .../src/WordPressSitePlan/SitePlanRun.php | 23 +++++++++++++++++++ .../contract/caller-products-catalog.php | 8 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/php-transformer/src/WordPressSitePlan/SitePlanRun.php b/php-transformer/src/WordPressSitePlan/SitePlanRun.php index 372831aec..415264e32 100644 --- a/php-transformer/src/WordPressSitePlan/SitePlanRun.php +++ b/php-transformer/src/WordPressSitePlan/SitePlanRun.php @@ -367,6 +367,7 @@ public function plan(): array $runtimeDeclarations = $shellDeclarations; $this->timer?->start('compose.site_plan.templates'); $runtimeDeclarations = $this->entityBindings->canonical($runtimeDeclarations, $runtimeEntityRecords, $this->references, $pages, array_merge($parts, $bindingTemplates), $paginationOwners); + $runtimeDeclarations = $this->productDescriptionLinks($runtimeDeclarations); BindingAnchors::assertAnchored($runtimeDeclarations, $pages, array_merge($parts, $bindingTemplates), $assets); $pages = $this->postChrome->extractCommentForms($pages); $blockThemeChrome = $this->postChrome->extractBlockThemeChrome($pages, $runtimeDeclarations, $runtimeEntityRecords); @@ -1029,4 +1030,26 @@ private static function markups(array $documents): array { return array_values(array_filter(array_column($documents, 'canonical_block_markup'), 'is_string')); } + + /** + * Product descriptions are source HTML like page content: their links to + * captured routes resolve through the same route table, against the + * product's own page or, for catalog-only rows, the entrypoint. + */ + private function productDescriptionLinks(array $declarations): array + { + $entry = ''; + foreach ($this->initialDocuments as $document) if (!empty($document['entrypoint'])) $entry = (string) $document['source_path']; + foreach ($declarations as $index => $declaration) { + if ('entity_collection' !== ($declaration['kind'] ?? null) || 'products' !== ($declaration['type'] ?? null)) continue; + foreach ($declaration['payload']['entities'] ?? array() as $row => $product) { + foreach (array('description', 'short_description') as $field) { + if (!is_array($product) || !is_string($product[$field] ?? null) || '' === $product[$field]) continue; + $origin = is_string($product['source_path'] ?? null) && '' !== $product['source_path'] ? $product['source_path'] : $entry; + $declarations[$index]['payload']['entities'][$row][$field] = $this->links->markup($product[$field], $origin); + } + } + } + return $declarations; + } } diff --git a/php-transformer/tests/contract/caller-products-catalog.php b/php-transformer/tests/contract/caller-products-catalog.php index 49b2ba6c4..31274163e 100644 --- a/php-transformer/tests/contract/caller-products-catalog.php +++ b/php-transformer/tests/contract/caller-products-catalog.php @@ -14,12 +14,13 @@ $catalog[] = array('slug' => $slug, 'name' => 'Catalog ' . strtoupper($slug), 'regular_price' => '99.00', 'sale_price' => '89.00', 'sku' => 'CAT-' . $slug, 'image' => 'https://example.test/catalog.jpg', 'description' => 'Catalog description', 'images' => array(array('src' => 'https://example.test/catalog.jpg', 'alt' => 'Catalog')), 'tags' => array('catalog'), 'category' => 'Catalog category', 'categories' => array('Catalog category', 'Sale'), 'brands' => array('Acme'), 'category_paths' => array(array('Shop', 'Drives', 'SAS'), array('Shop', 'Sale')), 'stock_status' => 'outofstock', 'variants' => array()); } $catalog[0]['currency'] = 'PEN'; +$catalog[2]['description'] = '

See A and our policies.

'; $card = static fn (string $slug): string => '
Product ' . $slug . '

Product ' . $slug . '

$10.00

'; $ld = array('@context' => 'https://schema.org', '@type' => 'Product', 'name' => 'Detected A', 'url' => 'https://example.test/products/a.html', 'sku' => 'DETECTED', 'description' => 'Detected description', 'offers' => array('@type' => 'Offer', 'price' => '12.00', 'priceCurrency' => 'USD')); $artifact = array('entrypoint' => 'index.html', 'files' => array( 'index.html' => '

Shop

Cart
' . $card('a') . $card('b') . '
', 'products/a.html' => '

Detected A

Detail page

', -), 'runtime_declarations' => array(array('kind' => 'entity_collection', 'type' => 'products', 'source_path' => 'catalog.json', 'payload' => array('schema' => 'generic/products/v1', 'entities' => $catalog)))); +), 'provenance' => array('source_url' => 'https://example.test/'), 'runtime_declarations' => array(array('kind' => 'entity_collection', 'type' => 'products', 'source_path' => 'catalog.json', 'payload' => array('schema' => 'generic/products/v1', 'entities' => $catalog)))); $result = (new ArtifactCompiler())->compile($artifact)->toArray(); $plan = $result['source_reports']['wordpress_site_plan'] ?? array(); $assert(array() !== $plan, 'Compilation produces a site plan.'); @@ -31,7 +32,10 @@ $assert(array('a', 'b', 'c') === array_keys($products), 'The merged collection contains A, B and C.'); $roles = array_column($products['a']['bindings'] ?? array(), 'role'); $assert(in_array('commerce_add_to_cart_form', $roles, true) && count($roles) >= 2, 'A retains detail-page and card bindings.'); -foreach ($catalog as $row) foreach ($row as $field => $value) $assert($value == ($products[$row['slug']][$field] ?? null), 'Catalog owns ' . $row['slug'] . '.' . $field); +foreach ($catalog as $row) foreach ($row as $field => $value) if ('description' !== $field) $assert($value == ($products[$row['slug']][$field] ?? null), 'Catalog owns ' . $row['slug'] . '.' . $field); +// Description links resolve like page links: a captured page becomes local, an uncaptured route stays on the source. +$described = (string) ($products['c']['description'] ?? ''); +$assert(!str_contains($described, 'https://example.test/products/a.html') && str_contains($described, 'https://example.test/policies/'), 'Catalog description links to captured pages resolve locally: ' . $described); $assert('products/a.html' === $products['a']['source_path'] && str_contains($products['a']['url'], '/products/a.html'), 'A retains its compiled detail-page identity.'); $assert('blocks-engine/whole-page-candidate/v1' === ($products['a']['whole_page_candidate'] ?? null), 'A retains its whole-page candidate.'); $assert(!empty($products['a']['controls']) && !empty($products['a']['presentation_graph']), 'A retains purchase controls and presentation.');