PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Agent / TagTemplateParts.php

TagTemplateParts.php in Extendify 3.0.5, at app/Agent/TagTemplateParts.php

138 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Extendify\Agent;
4
5 defined('ABSPATH') || die('No direct access.');
6
7 /**
8 * Tag blocks inside template parts (header/footer/etc.) with deterministic IDs.
9 * - Only runs inside core/template-part scopes.
10 * - Preorder numbering per template-part.
11 */
12 class TagTemplateParts
13 {
14 private static $frames = [];
15 private static $blockStack = [];
16
17 public static function init()
18 {
19 // Reset per-request state
20 add_action('template_redirect', function () {
21 self::$frames = [];
22 self::$blockStack = [];
23 });
24 // Assign IDs at the block-data layer (pre-order, stable)
25 add_filter('render_block_data', [self::class, 'onRenderBlockData'], 10, 2);
26 // Turn those IDs into DOM attributes & manage scope lifecycle
27 add_filter('render_block', [self::class, 'onRenderBlock'], 10, 2);
28 }
29
30 private static function isTemplatePart(array $b): bool
31 {
32 return (($b['blockName'] ?? '') === 'core/template-part');
33 }
34
35 private static function currentFrame()
36 {
37 return self::$frames ? self::$frames[count(self::$frames) - 1] : null;
38 }
39
40 private static function setCurrentFrame(array $frame)
41 {
42 self::$frames[count(self::$frames) - 1] = $frame;
43 }
44
45 private static function labelForPart(array $b): string
46 {
47 if (!empty($b['attrs']['area'])) {
48 return (string) $b['attrs']['area'];
49 }
50 if (!empty($b['attrs']['slug'])) {
51 return (string) $b['attrs']['slug'];
52 }
53 return 'template-part';
54 }
55
56 /** Assign IDs/labels into attrs before render (pre-order) */
57 public static function onRenderBlockData(array $block, array $source): array
58 {
59 $name = $block['blockName'] ?? '';
60 if ($name === '') {
61 return $block;
62 }
63
64 // OPEN a template-part scope (do not number the wrapper itself)
65 if (self::isTemplatePart($block)) {
66 $label = self::labelForPart($block);
67 $slug = $block['attrs']['slug'] ?? '';
68 self::$frames[] = [
69 'label' => $label,
70 'slug' => $slug,
71 'seq' => 0, // per-part counter
72 ];
73 // mark so we know to pop when this wrapper finishes rendering
74 $block['attrs']['__extendify_scope_open'] = 1;
75 return $block;
76 }
77
78 // Not inside a template-part? Ignore (prevents tagging post content).
79 if (empty(self::$frames)) {
80 return $block;
81 }
82
83 // Inside a template part
84 $frame = self::currentFrame();
85 // Any other block inside the part → number it
86 if ($frame) {
87 $frame['seq']++;
88 self::$blockStack[] = [
89 'id' => $frame['seq'],
90 'label' => $frame['label'],
91 'slug' => $frame['slug'] ?? '',
92 ];
93 self::setCurrentFrame($frame);
94 }
95
96 return $block;
97 }
98
99 /** Inject DOM attributes and close scopes after render */
100 public static function onRenderBlock(string $html, array $block): string
101 {
102 $name = $block['blockName'] ?? '';
103 if ($name === '') {
104 return $html;
105 }
106
107 // When the template-part wrapper finishes rendering, POP the frame
108 if (self::isTemplatePart($block) && !empty($block['attrs']['__extendify_scope_open'])) {
109 // wrapper itself isn’t tagged; just close the scope
110 array_pop(self::$frames);
111 return $html;
112 }
113
114 // If we’re not inside a template part, do nothing
115 if (empty(self::$frames)) {
116 return $html;
117 }
118
119 // Inject attributes for blocks we numbered
120 $info = !empty(self::$blockStack)
121 ? array_pop(self::$blockStack)
122 : null;
123
124 if ($info && $html) {
125 $tp = new \WP_HTML_Tag_Processor($html);
126 if ($tp->next_tag()) {
127 $tp->set_attribute('data-extendify-part-block-id', (string) (int) $info['id']);
128 $tp->set_attribute('data-extendify-part', $info['label']);
129 if (!empty($info['slug'])) {
130 $tp->set_attribute('data-extendify-part-slug', $info['slug']);
131 }
132 $html = $tp->get_updated_html();
133 }
134 }
135 return $html;
136 }
137 }
138