PluginProbe
Extendify / 2.2.2
Extendify v2.2.2
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 0.7.0 All 126 releases
extendify / app / Agent / TagBlocks.php

TagBlocks.php in Extendify 2.2.2, at app/Agent/TagBlocks.php

129 lines 4.2 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 class TagBlocks
8 {
9 // Blocks to ignore completely (block + its children)
10 private static $ignored = ['core/query', 'core/post-template', 'core/post-content'];
11
12 public static function init()
13 {
14 \add_filter('the_content', [self::class, 'enterScope'], 0);
15 \add_filter('the_content', [self::class, 'leaveScope'], PHP_INT_MAX);
16
17 \add_filter('pre_render_block', [self::class, 'pre'], 10, 2);
18 \add_filter('render_block', [self::class, 'post'], 10, 2);
19 }
20
21 public static function enterScope($content)
22 {
23 if (is_admin()) {
24 return $content;
25 }
26
27 if (empty($GLOBALS['extendify_agent_scope'])) {
28 $GLOBALS['extendify_agent_scope'] = [
29 'depth' => 0,
30 'frames' => [],
31 ];
32 }
33 $GLOBALS['extendify_agent_scope']['depth']++;
34 // Each scope has: seq, id_stack, pushed_stack, skip_depth
35 $GLOBALS['extendify_agent_scope']['frames'][] = [
36 'seq' => 0,
37 'id_stack' => [],
38 'pushed_stack' => [],
39 'skip_depth' => 0, // >0 while inside an ignored subtree
40 ];
41 return $content;
42 }
43
44 public static function leaveScope($content)
45 {
46 if (is_admin()) {
47 return $content;
48 }
49 $S =& $GLOBALS['extendify_agent_scope'];
50 if (!empty($S['depth'])) {
51 $S['depth']--;
52 array_pop($S['frames']);
53 }
54 return $content;
55 }
56
57 public static function pre($pre, $parsed_block)
58 {
59 if (is_admin() || !is_array($parsed_block) || empty($parsed_block['blockName'])) {
60 return $pre;
61 }
62
63 $S = $GLOBALS['extendify_agent_scope'] ?? null;
64 if (!$S || ($S['depth'] ?? 0) !== 1 || empty($S['frames'])) {
65 return $pre;
66 } // only outer the_content
67
68 $i = count($S['frames']) - 1;
69 $frame = $S['frames'][$i];
70
71 $name = $parsed_block['blockName'];
72
73 // If this block starts an ignored subtree, enter skip mode
74 if (in_array($name, self::$ignored, true)) {
75 $frame['skip_depth']++;
76 $frame['pushed_stack'][] = false; // we didn't assign an id to this block
77 } elseif ($frame['skip_depth'] > 0) {
78 // Already skipping? (we're inside an ignored subtree)
79 $frame['pushed_stack'][] = false; // no id for anything under ignored
80 } else {
81 // Normal counting
82 $frame['seq']++;
83 $id = $frame['seq'];
84 $frame['id_stack'][] = $id;
85 $frame['pushed_stack'][] = true;
86 }
87
88 $GLOBALS['extendify_agent_scope']['frames'][$i] = $frame;
89 return $pre;
90 }
91
92 public static function post($content, $parsed_block)
93 {
94 $S = $GLOBALS['extendify_agent_scope'] ?? null;
95 if (!$S || empty($S['frames'])) {
96 return $content;
97 }
98
99 $i = count($S['frames']) - 1;
100 $frame = $S['frames'][$i];
101
102 $name = is_array($parsed_block) ? ($parsed_block['blockName'] ?? null) : null;
103
104 // Pop pushed flag & optional id (ALWAYS pop to stay balanced)
105 $pushed = !empty($frame['pushed_stack']) ? array_pop($frame['pushed_stack']) : false;
106 $id = ($pushed && !empty($frame['id_stack'])) ? array_pop($frame['id_stack']) : null;
107
108 // Inject only when: outer scope, we counted this block, html present, not admin
109 if (!is_admin() && ($S['depth'] ?? 0) === 1 && $pushed && $id && $content && $name) {
110 $tp = new \WP_HTML_Tag_Processor($content);
111 $value = (string) (int) $id;
112
113 // Move cursor to the first start tag in the fragment
114 if ($tp->next_tag()) {
115 $tp->set_attribute('data-extendify-agent-block-id', $value);
116 $content = $tp->get_updated_html();
117 }
118 }
119
120 // If this block ends an ignored subtree, exit skip mode
121 if ($name && in_array($name, self::$ignored, true) && $frame['skip_depth'] > 0) {
122 $frame['skip_depth']--;
123 }
124
125 $GLOBALS['extendify_agent_scope']['frames'][$i] = $frame;
126 return $content;
127 }
128 }
129