-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpattern-transformer.php
More file actions
439 lines (364 loc) · 13.9 KB
/
Copy pathpattern-transformer.php
File metadata and controls
439 lines (364 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* Pattern Transformer Functions
*
* Load block patterns and replace placeholder content with actual data.
* Handles pattern resolution (wp:pattern references) and transformations.
*
* @package HM\Rehydrator
*/
namespace HM\Rehydrator\Pattern_Transformer;
use WP_Block_Patterns_Registry;
use WP_HTML_Tag_Processor;
/**
* Get pattern content by slug using WordPress pattern registry.
*
* @param string $slug Pattern slug (e.g., 'theme/hero-internal').
* @return string Pattern markup, or empty string if not found.
*/
function get_pattern_by_slug( string $slug ) : string {
// Check if running in WordPress context.
if ( ! class_exists( WP_Block_Patterns_Registry::class ) ) {
return '';
}
// Get pattern from registry.
$pattern = WP_Block_Patterns_Registry::get_instance()->get_registered( $slug );
if ( ! $pattern ) {
return '';
}
return $pattern['content'] ?? '';
}
/**
* Resolve pattern references and tag blocks with their source pattern.
*
* Finds wp:pattern blocks and replaces them with the actual pattern content.
* Tags each block with _source_pattern metadata for transformation targeting.
*
* @param array $blocks Parsed blocks.
* @param string $source_pattern Current source pattern slug.
* @return array Blocks with patterns resolved and source tagged.
*/
function resolve_and_tag_patterns( array $blocks, string $source_pattern = '' ) : array {
$result = [];
foreach ( $blocks as $block ) {
// Tag block with its source pattern.
if ( ! empty( $source_pattern ) ) {
$block['_source_pattern'] = $source_pattern;
}
// Check if this is a pattern reference.
if ( ( $block['blockName'] ?? '' ) === 'core/pattern' ) {
$slug = $block['attrs']['slug'] ?? '';
// Get pattern content from WordPress registry.
$pattern_markup = get_pattern_by_slug( $slug );
if ( empty( $pattern_markup ) ) {
// Pattern not found, keep the reference block as-is.
$result[] = $block;
continue;
}
$pattern_blocks = parse_blocks( $pattern_markup );
// Recursively resolve with this pattern as the source.
$resolved_blocks = resolve_and_tag_patterns( $pattern_blocks, $slug );
// Add resolved blocks to result (replacing the pattern reference).
foreach ( $resolved_blocks as $resolved_block ) {
$result[] = $resolved_block;
}
continue;
}
// Process inner blocks.
if ( ! empty( $block['innerBlocks'] ) ) {
$block['innerBlocks'] = resolve_and_tag_patterns(
$block['innerBlocks'],
$source_pattern
);
// Filter out null/whitespace blocks (these are preserved in innerContent, not innerBlocks).
$block['innerBlocks'] = array_values( array_filter(
$block['innerBlocks'],
function( $inner_block ) {
return ! empty( $inner_block['blockName'] );
}
) );
// Rebuild innerContent to match the new innerBlocks structure after resolution.
$block = rebuild_inner_content( $block );
}
$result[] = $block;
}
return $result;
}
/**
* Recursively tag blocks with a source pattern if they don't have one.
*
* This ensures nested blocks inherit their parent's pattern context.
*
* @param array $blocks Blocks to tag.
* @param string $source_pattern Pattern to tag with.
* @return array Tagged blocks.
*/
function tag_blocks_recursively( array $blocks, string $source_pattern ) : array {
foreach ( $blocks as &$block ) {
// Only tag if block doesn't already have a source pattern.
if ( empty( $block['_source_pattern'] ) && empty( $block['attrs']['metadata']['patternName'] ?? '' ) ) {
$block['_source_pattern'] = $source_pattern;
}
// Recurse into inner blocks.
if ( ! empty( $block['innerBlocks'] ) ) {
$block['innerBlocks'] = tag_blocks_recursively( $block['innerBlocks'], $source_pattern );
}
}
return $blocks;
}
/**
* Apply transformations to blocks based on their source pattern.
*
* @param array $blocks Blocks with _source_pattern metadata.
* @param array $transformations Transformations per pattern.
* @param array $block_type_counters Counter array (for internal recursion).
* @return array Transformed blocks.
*/
function apply_pattern_transformations( array $blocks, array $transformations, array &$block_type_counters = [] ) : array {
$result = [];
foreach ( $blocks as $block ) {
// Check both _source_pattern (from resolved pattern references) and
// metadata.patternName (from inline content with semantic pattern grouping).
$source_pattern = $block['_source_pattern'] ?? $block['attrs']['metadata']['patternName'] ?? '';
$block_name = $block['blockName'] ?? '';
// Track occurrences of each block type within each pattern.
$counter_key = $source_pattern . '::' . $block_name;
if ( ! isset( $block_type_counters[ $counter_key ] ) ) {
$block_type_counters[ $counter_key ] = 0;
}
$occurrence = $block_type_counters[ $counter_key ];
$block_type_counters[ $counter_key ]++;
$should_delete = false;
// Check if we have transformations for this pattern.
if ( ! empty( $source_pattern ) && isset( $transformations[ $source_pattern ] ) ) {
$pattern_transforms = $transformations[ $source_pattern ];
// Check if there's a callback for the whole pattern.
if ( isset( $pattern_transforms['callback'] ) && is_callable( $pattern_transforms['callback'] ) ) {
$block = $pattern_transforms['callback']( $block );
}
// Apply block-type specific transformations.
if ( ! empty( $block_name ) && isset( $pattern_transforms[ $block_name ] ) ) {
$block_transform = $pattern_transforms[ $block_name ];
// Apply per-occurrence transformation if one exists.
if ( isset( $block_transform[ $occurrence ] ) ) {
$transform = $block_transform[ $occurrence ];
// Check if this is a deletion transformation.
if ( isset( $transform['_delete'] ) && $transform['_delete'] === true ) {
$should_delete = true;
} else {
$block = apply_block_transformation( $block, $transform );
}
}
// Apply block-type-level callback (applies to all occurrences).
// This runs in addition to any per-occurrence transform above.
if ( ! $should_delete && isset( $block_transform['callback'] ) && is_callable( $block_transform['callback'] ) ) {
$block = $block_transform['callback']( $block );
}
// If no per-occurrence or callback transform matched, try as a
// single transformation for all occurrences (non-callback keys
// like _delete, textContent, etc. at the block-type level).
if ( ! $should_delete && ! isset( $block_transform[ $occurrence ] ) && ! isset( $block_transform['callback'] ) && ! isset( $block_transform[0] ) ) {
if ( isset( $block_transform['_delete'] ) && $block_transform['_delete'] === true ) {
$should_delete = true;
} else {
$block = apply_block_transformation( $block, $block_transform );
}
}
}
}
// Skip this block if marked for deletion.
if ( $should_delete ) {
continue;
}
// Clean up metadata.
unset( $block['_source_pattern'] );
// Process inner blocks.
if ( ! empty( $block['innerBlocks'] ) ) {
$original_inner_count = count( $block['innerBlocks'] );
// Tag inner blocks with parent's source pattern if they don't have one.
// This ensures transformations targeting a pattern also apply to nested blocks.
// Do this recursively for ALL descendant blocks.
if ( ! empty( $source_pattern ) ) {
$block['innerBlocks'] = tag_blocks_recursively( $block['innerBlocks'], $source_pattern );
}
$block['innerBlocks'] = apply_pattern_transformations(
$block['innerBlocks'],
$transformations,
$block_type_counters
);
// If blocks were deleted, rebuild innerContent to match new innerBlocks count.
if ( count( $block['innerBlocks'] ) !== $original_inner_count ) {
$block = rebuild_inner_content( $block );
}
}
$result[] = $block;
}
return $result;
}
/**
* Apply a transformation to a single block.
*
* @param array $block Block to modify.
* @param array $transformation Transformation configuration.
* @return array Modified block.
*/
function apply_block_transformation( array $block, array $transformation ) : array {
// Replace entire innerHTML.
if ( isset( $transformation['innerHTML'] ) ) {
$block['innerHTML'] = $transformation['innerHTML'];
$block['innerContent'] = [ $transformation['innerHTML'] ];
}
// Replace or merge attributes.
if ( isset( $transformation['attrs'] ) ) {
$block['attrs'] = array_merge( $block['attrs'] ?? [], $transformation['attrs'] );
}
// Replace just the text content, preserving HTML structure.
if ( isset( $transformation['textContent'] ) ) {
$block = update_block_text_content( $block, $transformation['textContent'] );
}
// Search and replace in innerHTML.
if ( isset( $transformation['search'] ) && isset( $transformation['replace'] ) ) {
$block['innerHTML'] = str_replace(
$transformation['search'],
$transformation['replace'],
$block['innerHTML']
);
// Also update innerContent.
if ( ! empty( $block['innerContent'] ) ) {
foreach ( $block['innerContent'] as &$content ) {
if ( is_string( $content ) ) {
$content = str_replace(
$transformation['search'],
$transformation['replace'],
$content
);
}
}
}
}
// Callback for custom replacement logic.
if ( isset( $transformation['callback'] ) && is_callable( $transformation['callback'] ) ) {
$block = $transformation['callback']( $block );
}
return $block;
}
/**
* Update just the text content of a block, preserving HTML structure.
*
* Uses WP_HTML_Tag_Processor to safely update text while keeping all attributes.
*
* @param array $block Block to modify.
* @param string $new_text New text content.
* @return array Modified block.
*/
function update_block_text_content( array $block, string $new_text ) : array {
$html = $block['innerHTML'] ?? '';
if ( empty( $html ) ) {
return $block;
}
$processor = new WP_HTML_Tag_Processor( $html );
// Find the first HTML tag and extract its details.
if ( ! $processor->next_tag() ) {
return $block;
}
$tag_name = $processor->get_tag();
$attributes = '';
// Collect all attributes.
foreach ( $processor->get_attribute_names_with_prefix( '' ) as $attr_name ) {
$attr_value = $processor->get_attribute( $attr_name );
if ( is_string( $attr_value ) ) {
$attributes .= sprintf( ' %s="%s"', $attr_name, esc_attr( $attr_value ) );
} else {
// Boolean attribute (true) or missing value (null).
$attributes .= sprintf( ' %s', $attr_name );
}
}
// Rebuild the HTML with the new text inside the tag.
// Use lowercase tag names for consistency with WordPress standards.
$tag_name_lower = strtolower( $tag_name );
$html = sprintf( '<%s%s>%s</%s>', $tag_name_lower, $attributes, $new_text, $tag_name_lower );
$block['innerHTML'] = $html;
$block['innerContent'] = [ $html ];
return $block;
}
/**
* Rebuild innerContent array to match innerBlocks structure.
*
* WordPress block serialization requires innerContent to have null placeholders
* for each inner block. This rebuilds it when innerBlocks has been modified.
*
* Tries to use wrapper chunks the parser already recorded in innerContent, then
* falls back to reconstructing a single wrapper tag with WP_HTML_Tag_Processor.
*
* Special handling for cover blocks to preserve image and overlay elements.
*
* @param array $block Block with potentially modified innerBlocks.
* @return array Block with corrected innerContent.
*/
function rebuild_inner_content( array $block ) : array {
if ( empty( $block['innerBlocks'] ) ) {
// No inner blocks - innerContent should just be innerHTML.
if ( isset( $block['innerHTML'] ) ) {
$block['innerContent'] = [ $block['innerHTML'] ];
}
return $block;
}
$inner_html = $block['innerHTML'] ?? '';
$block_name = $block['blockName'] ?? '';
// Special handling for cover blocks - preserve image and overlay HTML.
if ( $block_name === 'core/cover' && ! empty( $inner_html ) ) {
// For cover blocks, preserve the original innerContent structure.
// The innerHTML contains the image, overlay, and inner-container div.
// We need to keep this structure intact.
return $block;
}
// If there's no wrapper HTML, innerContent is just nulls.
if ( empty( $inner_html ) ) {
$block['innerContent'] = array_fill( 0, count( $block['innerBlocks'] ), null );
return $block;
}
// Reuse wrapper chunks recorded by the block parser when a block has them.
// This captures more varieties of wrapping markup around nested inner blocks
// than we can infer from the top node of innerHTML alone.
$existing = (array) ( $block['innerContent'] ?? [] );
$opening = $existing[0] ?? null;
$closing = count( $existing ) > 1 ? end( $existing ) : null;
if ( is_string( $opening ) && is_string( $closing ) ) {
$block['innerContent'] = array_merge(
[ $opening ],
array_fill( 0, count( $block['innerBlocks'] ), null ),
[ $closing ]
);
return $block;
}
// Use WP_HTML_Tag_Processor to extract the tag name and attributes.
$processor = new WP_HTML_Tag_Processor( $inner_html );
if ( ! $processor->next_tag() ) {
// No valid HTML tag found - just use nulls.
$block['innerContent'] = array_fill( 0, count( $block['innerBlocks'] ), null );
return $block;
}
// Reconstruct opening tag with all attributes.
// Use lowercase tag names for consistency with WordPress standards.
$tag_name = strtolower( $processor->get_tag() );
$attributes = '';
foreach ( $processor->get_attribute_names_with_prefix( '' ) as $attr_name ) {
$attr_value = $processor->get_attribute( $attr_name );
if ( is_string( $attr_value ) ) {
$attributes .= sprintf( ' %s="%s"', $attr_name, esc_attr( $attr_value ) );
} else {
// Boolean attribute (true) or missing value (null).
$attributes .= sprintf( ' %s', $attr_name );
}
}
$opening_tag = "<{$tag_name}{$attributes}>";
$closing_tag = "</{$tag_name}>";
// Build innerContent: opening tag, null for each block, closing tag.
$inner_content = [ $opening_tag ];
foreach ( $block['innerBlocks'] as $inner_block ) {
$inner_content[] = null;
}
$inner_content[] = $closing_tag;
$block['innerContent'] = $inner_content;
return $block;
}