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
45 changes: 33 additions & 12 deletions php-transformer/src/WordPressSitePlan/ShellExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2052,18 +2052,22 @@ public function sharedShells(array $pages, array $reservedSlugs = array(), array
}
// A shared landmark can have route-local presentation. Keep those
// variants editable without substituting the dominant route's paint.
// A matching source anchor proves shell ownership; unrelated article
// headers and unlabelled route content must not be promoted.
// A matching source anchor or authored landmark wrapper proves shell
// ownership; unrelated article headers must not be promoted.
foreach ($parts as $part) {
if ('header' !== ($part['area'] ?? null)) continue;
$partIndex = new \Automattic\BlocksEngine\PhpTransformer\Support\BlockMarkupIndex($part['canonical_block_markup']);
$anchors = array();
$wrappers = array();
foreach ($partIndex->tokens() as $token) {
if ($token['closing']) continue;
$attrs = json_decode($token['attributes'], true);
if ('header' === ($attrs['tagName'] ?? null) && !empty($attrs['anchor'])) $anchors[$attrs['anchor']] = array($attrs['style'] ?? array(), $attrs['className'] ?? '');
if ('header' !== ($attrs['tagName'] ?? null)) continue;
$wrapper = array($attrs['style'] ?? array(), $attrs['className'] ?? '');
if (!empty($attrs['anchor'])) $anchors[$attrs['anchor']] = $wrapper;
elseif (!empty($attrs['className'])) $wrappers[] = $wrapper;
}
if (array() === $anchors) continue;
if (array() === $anchors && array() === $wrappers) continue;
foreach ($pages as $index => $page) {
// Do not promote a second occurrence on a route that already
// contributed its shell, or one retained for unsafe placement.
Expand All @@ -2075,18 +2079,29 @@ public function sharedShells(array $pages, array $reservedSlugs = array(), array
$row = $rows[0];
$tokens = (new \Automattic\BlocksEngine\PhpTransformer\Support\BlockMarkupIndex($row['markup']))->tokens();
$attrs = json_decode($tokens[0]['attributes'] ?? '', true);
if (!isset($anchors[$attrs['anchor'] ?? ''])) continue;
// Descendant paint and navigation selection remain route-owned.
// Layout differences, however, can describe a distinct shell
// even when its landmark wrapper has identical attributes.
if (array($attrs['style'] ?? array(), $attrs['className'] ?? '') === $anchors[$attrs['anchor']]
&& self::descendantShellLayout($row['markup']) === self::descendantShellLayout($part['canonical_block_markup'])) continue;
$wrapper = array($attrs['style'] ?? array(), $attrs['className'] ?? '');
$sharedWrapper = $anchors[$attrs['anchor'] ?? ''] ?? null;
// An unanchored landmark can still prove route-shell ownership
// through the same authored wrapper and copy. Keep its layout
// in a route-local part rather than freezing the donor's paint.
if (null === $sharedWrapper && empty($attrs['anchor']) && in_array($wrapper, $wrappers, true)) $sharedWrapper = $wrapper;
if (null === $sharedWrapper) continue;
// Navigation-only exclusions remain route-owned unless layout
// differs. A bound control, however, must move with its shell
// into an editable part, even when block layout is identical.
if (self::textRuns($row['markup']) !== self::textRuns($part['canonical_block_markup'])) continue;
$bindings = $this->runtimeBindingsInRanges($runtimeDeclarations, $page, array($row));
if ($bindings['blocked'] || array() !== $bindings['refs']) continue;
if ($bindings['blocked']) continue;
if ((!empty($attrs['anchor']) || (array() === $bindings['refs'] && str_contains($row['markup'], '<!-- wp:navigation '))) && $wrapper === $sharedWrapper
&& self::descendantShellLayout($row['markup']) === self::descendantShellLayout($part['canonical_block_markup'])) continue;
$hoist = array() === $bindings['refs'] ? null : $this->sharedShellBindingHoist($runtimeDeclarations, $pages, array($index), array($index => $bindings['refs']), $row + array('template_part_markup' => $row['markup']), 'header');
if (array() !== $bindings['refs'] && null === $hoist) continue;
$edges = array($index => array($row + array('identity' => $row['identity_markup'], 'nested' => true)));
$slug = 'header-' . substr(hash('sha256', $page['source_path']), 0, 12);
$variants = $this->edgeVariantParts($pages, $edges, array(array('indexes' => array($index), 'length' => 1)), 'header', array_fill_keys(array_column($parts, 'slug'), true), $runtimeDeclarations, $slug);
$partSourcePath = 'wordpress-site-plan/shared/' . $slug . '#header';
$variantDeclarations = null === $hoist ? $runtimeDeclarations : self::applySharedShellBindingHoist($runtimeDeclarations, $hoist, $partSourcePath);
$variants = $this->edgeVariantParts($pages, $edges, array(array('indexes' => array($index), 'length' => 1)), 'header', array_fill_keys(array_column($parts, 'slug'), true), $variantDeclarations, $slug);
if (array() !== $variants['parts']) $runtimeDeclarations = $variantDeclarations;
$pages = $variants['pages'];
array_push($parts, ...$variants['parts']);
array_push($diagnostics, ...$variants['diagnostics']);
Expand All @@ -2106,6 +2121,12 @@ private static function descendantShellLayout(string $markup): array
$attrs = json_decode($token['attributes'], true);
if (!self::isGroupBlock($token['token']) || 'header' === ($attrs['tagName'] ?? '')) continue;
$style = array_intersect_key($attrs['style'] ?? array(), array_flip(array('spacing', 'dimensions', 'layout', 'position', 'shadow')));
// CSS-owned layout is represented by a scoped class rather than
// block style attributes. Preserve that layout identity too.
$classes = preg_split('/\s+/', trim($attrs['className'] ?? ''));
if (in_array('blocks-engine-css-owned-layout', $classes, true)) {
$style['css_layout_classes'] = array_values(array_filter($classes, static fn(string $class): bool => str_starts_with($class, 'route-class-')));
}
if (array() !== $style) $layout[] = $style;
}
return $layout;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\ArtifactCompiler;

$files = array();
foreach (array('index', 'about', 'work', 'shop') as $name) {
$layout = 'shop' === $name ? 'column' : 'row';
$files[$name . '.html'] = '<html><head><title>' . $name . '</title><style>header.chrome{background:#eee;padding:10px}.bar{display:flex;flex-direction:' . $layout . ';gap:12px}main{min-height:200px}</style></head><body><header class="chrome"><div class="bar"><a href="/">Shared brand</a><a href="/shop.html">Shop</a><form><label>Email<input type="email" name="email"></label><button type="submit">Subscribe</button></form></div></header><main><h1>' . $name . '</h1><p>Route content.</p></main></body></html>';
}
$plan = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => $files))->toArray()['source_reports']['wordpress_site_plan'];
$headers = array_filter($plan['template_parts'], static fn(array $part): bool => 'header' === ($part['area'] ?? null));
if (2 !== count($headers)) throw new RuntimeException('The unanchored route layout must have its own editable header part.');
foreach ($plan['pages'] as $page) {
if (str_contains($page['canonical_block_markup'], '<header')) throw new RuntimeException($page['source_path'] . ' retains page-owned chrome.');
}
$parts = array_column($plan['template_parts'], null, 'source_path');
foreach ($plan['runtime_declarations'] as $declaration) {
foreach ($declaration['payload']['entities'] ?? array() as $entity) {
foreach ($entity['bindings'] ?? array() as $binding) {
$part = $parts[$binding['source_path']] ?? null;
if (null === $part) throw new RuntimeException('Header bindings must move with their route-local part.');
$position = $binding['position'];
if ($binding['search_block_markup'] !== substr($part['canonical_block_markup'], $position['offset'], $position['length'])) throw new RuntimeException('Hoisted binding must resolve inside the part.');
}
}
}
echo "unanchored-header-layout-variant: passed\n";
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\ArtifactCompiler;
use Automattic\BlocksEngine\PhpTransformer\WordPressSitePlan\WordPressSitePlan;

// An excluded navigation design is not evidence of a distinct header layout.
// The identical unanchored landmark must not turn that navigation into another
// template part merely because its copy differs from the shared menu.
$files = array();
foreach (array('index.html' => 'Home', 'about/index.html' => 'About', 'services/index.html' => 'Services') as $path => $title) {
$links = '';
foreach (array('services' => 'Services', 'about' => 'About', 'contact' => 'Contact') as $slug => $label) {
$current = $title === $label;
if ('Services' === $title && 'about' === $slug) $label = 'Our team';
$links .= '<a ' . ($current ? 'aria-current="page" class="text-fg"' : 'class="text-muted hover:text-fg"') . ' href="/' . $slug . '/">' . $label . '</a>';
}
$files[$path] = '<html><head><title>' . $title . '</title><style>@layer utilities{.flex{display:flex}.gap-6{gap:1.5rem}.text-muted{color:#888}.text-fg{color:#111}.hover\\:text-fg:hover{color:#111}.p-4{padding:1rem}}</style></head><body><header class="p-4"><nav aria-label="Main"><div class="flex gap-6"><a href="/">Brand</a><div class="actions"><div class="hidden">' . $links . '</div><div class="tools"><button type="button" aria-label="Switch theme"></button></div></div></div></nav></header><main><h1>' . $title . '</h1></main></body></html>';
}
$plan = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => $files))->toArray()['source_reports']['wordpress_site_plan'];
WordPressSitePlan::assertValid($plan);
$headers = array_filter($plan['template_parts'], static fn(array $part): bool => 'header' === ($part['area'] ?? null));
if (1 !== count($headers)) throw new RuntimeException('Navigation-only exclusions must not create route-local header parts.');
foreach ($plan['pages'] as $page) {
$retained = str_contains($page['canonical_block_markup'], '<header');
if ($retained !== ('services/index.html' === $page['source_path'])) throw new RuntimeException('Only the non-equivalent navigation remains route-owned.');
}
echo "unanchored-header-navigation-exclusion: passed\n";
Loading