Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions php-transformer/src/WordPressSitePlan/SitePlanRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions php-transformer/tests/contract/caller-products-catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<p>See <a href="https://example.test/products/a.html">A</a> and our <a href="https://example.test/policies/#returns">policies</a>.</p>';
$card = static fn (string $slug): string => '<article><a href="/products/' . $slug . '.html"><img src="https://example.test/' . $slug . '.jpg" alt="Product ' . $slug . '"><h2>Product ' . $slug . '</h2></a><p>$10.00</p><button type="button">Add to cart</button></article>';
$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' => '<main><h1>Shop</h1><a href="/cart/">Cart</a><label>Sort<select name="sort"><option value="price-asc">Price: low to high</option><option value="price-desc">Price: high to low</option></select></label><div class="grid">' . $card('a') . $card('b') . '</div></main>',
'products/a.html' => '<script type="application/ld+json">' . json_encode($ld, JSON_THROW_ON_ERROR) . '</script><main><h1>Detected A</h1><p>Detail page</p><form action="/cart/" method="post"><label>Quantity<input type="number" name="quantity" value="1"></label><button type="submit" name="add-to-cart" value="a">Add to cart</button></form></main>',
), '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.');
Expand All @@ -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.');
Expand Down
Loading