-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathFunctions.php
659 lines (564 loc) · 18.2 KB
/
Functions.php
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
<?php
namespace P4\MasterTheme\Migrations\Utils;
use WP_Block_Parser;
use P4\MasterTheme\BlockReportSearch\BlockSearch;
use P4\MasterTheme\BlockReportSearch\Block\Query\Parameters;
/**
* Utility functions for the migration scripts.
*/
class Functions
{
/**
* Execute a block migration.
*
* @param string $block_name - The name of the block to be migrated.
* @param callable $block_check_callback - Callback function to check if block is valid for migration.
* @param callable $record block_transformation_callback - Callback function to transform a block.
* phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- interface implementation
*/
public static function execute_block_migration(
string $block_name,
callable $block_check_callback,
callable $block_transformation_callback
): void {
try {
// Get the list of posts using the specified block.
$posts = self::get_posts_using_specific_block(
$block_name,
Constants::ALL_POST_TYPES,
Constants::POST_STATUS_LIST
);
// If there are no posts, abort.
if (!$posts) {
return;
}
echo $block_name . " migration in progress...\n"; // phpcs:ignore
$parser = new WP_Block_Parser();
foreach ($posts as $post) {
if (empty($post->post_content)) {
continue;
}
$current_post_id = $post->ID; // Store the current post ID
echo 'Parsing post ', $current_post_id, "\n"; // phpcs:ignore
// Parse the blocks from the post content.
$blocks = $parser->parse($post->post_content);
if (!is_array($blocks)) {
throw new \Exception("Invalid block structure for post #" . $current_post_id);
}
// Process blocks recursively.
$blocks = self::process_blocks_recursive(
$blocks,
$block_check_callback,
$block_transformation_callback
);
// Serialize the blocks content.
$new_content = serialize_blocks($blocks);
if ($post->post_content === $new_content) {
continue;
}
$post_update = array(
'ID' => $current_post_id,
'post_content' => $new_content,
);
// Update the post with the replaced blocks.
$result = wp_update_post($post_update);
if ($result === 0) {
throw new \Exception("There was an error trying to update the post #" . $current_post_id);
}
echo "Migration successful\n";
}
} catch (\ErrorException $e) {
// Catch any exceptions and display the post ID if available
echo "Migration wasn't executed for post ID: ", $current_post_id ?? 'unknown', "\n";
echo $e->getMessage(), "\n";
}
}
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
/**
* Recursively process blocks and their inner blocks.
*
* @param string $block_name - The name of the block to be migrated.
* @param callable $block_check_callback - Callback function to check if block is valid for migration.
* @param callable $record block_transformation_callback - Callback function to transform a block.
*/
private static function process_blocks_recursive(
array $blocks,
callable $block_check_callback,
callable $block_transformation_callback
): array {
foreach ($blocks as &$block) {
if ($block_check_callback($block)) {
$block = $block_transformation_callback($block);
}
// Check for innerBlocks and process recursively.
if (empty($block['innerBlocks']) || !is_array($block['innerBlocks'])) {
continue;
}
$block['innerBlocks'] = self::process_blocks_recursive(
$block['innerBlocks'],
$block_check_callback,
$block_transformation_callback
);
}
// Unset the reference to avoid issues.
unset($block);
return $blocks;
}
/**
* Get all the posts using a specific type of block.
*
* @param string $block_name - The name of the block to be searched.
* @param array $post_types - The list of post types to look for.
* @param array $post_status - The list of post status to look for.
* @return mixed - The posts using a type of block or null if no posts are found.
*/
public static function get_posts_using_specific_block(
string $block_name,
array $post_types,
?array $post_status = null
): mixed {
$search = new BlockSearch();
$params = ( new Parameters() )->with_name($block_name);
if ($post_status) {
$params = $params->with_post_status($post_status);
}
$post_ids = $search->get_posts($params);
if (empty($post_ids)) {
return null;
}
$args = [
'include' => $post_ids,
'post_type' => $post_types,
];
if ($post_status) {
$args['post_status'] = 'any';
}
$posts = get_posts($args) ?? [];
if (empty($posts)) {
return null;
}
return $posts;
}
/**
* Generate the code for the block attributes "innerHtml" and "innerContent".
*
* IMPORTANT!
* The format of the html code must not be changed.
* Any small change can affect the way the blocks are rendered and make them stop working.
*
* @param string $classname - A CSS classname.
* @return array - The code for the block attributes.
*/
public static function generate_html_content(string $classname): array
{
$html = "
<div class=\"$classname\">
</div>
";
$content = [
0 => "
<div class=\"$classname\">",
1 => null,
2 => "
",
3 => null,
4 => '</div>
',
];
return [
'html' => $html,
'content' => $content,
];
}
/**
* Create a new block.
*
* @param string $name - The name of the block.
* @param array $attrs - The attributes of the block (optional).
* @param array $inner_blocks - The internal blocks (optional).
* @param string $inner_html - The internal HTML (optional).
* @param array $inner_content - The internal content (optional).
* @return array - The new block.
*/
public static function create_new_block(
string $name,
?array $attrs = [],
?array $inner_blocks = [],
?string $inner_html = '',
?array $inner_content = []
): array {
return [
'blockName' => $name,
'attrs' => $attrs,
'innerBlocks' => $inner_blocks,
'innerHTML' => $inner_html,
'innerContent' => $inner_content,
];
}
/**
* Create a new heading block.
*
* @param array $attrs - The attributes of the block.
* @param string $text - The heading text.
* @return array - The new heading block.
*/
public static function create_block_heading(array $attrs, string $text): array
{
$level = isset($attrs['level']) ? strval($attrs['level']) : '2';
$html = '<h' . $level . ' class="wp-block-heading">';
$html .= $text;
$html .= '</h' . $level . '>';
return self::create_new_block(
Constants::BLOCK_HEADING,
$attrs,
[],
$html,
[$html]
);
}
/**
* Create a new paragraph block.
*
* @param array $attrs - The attributes of the block.
* @param string $content - The paragraph text.
* @return array - The new paragraph block.
*/
public static function create_block_paragraph(array $attrs, string $content): array
{
$margin = $attrs['style']['spacing']['margin'];
$styles =
isset($margin) ?
'margin-top: ' . $margin['top'] . '; margin-bottom: ' . $margin['bottom'] . ';' :
'';
$html = '<p style="' . $styles . '">' . $content . '</p>';
return self::create_new_block(
Constants::BLOCK_PARAGRAPH,
$attrs,
[],
$html,
[$html]
);
}
/**
* Create a new column block.
*
* @param array $attrs - The attributes of the block.
* @param array $inner_blocks - The inner blocks.
* @return array - The new column block.
*/
public static function create_block_single_column(array $attrs, array $inner_blocks): array
{
$classname =
isset($attrs['verticalAlignment']) ?
'wp-block-column is-vertically-aligned-center' :
'wp-block-column';
$html_content = self::generate_html_content($classname);
return self::create_new_block(
Constants::BLOCK_SINGLE_COLUMN,
$attrs,
$inner_blocks,
$html_content['html'],
$html_content['content']
);
}
/**
* Create a new columns block.
*
* @param array $attrs - The attributes of the block.
* @param array $inner_blocks - The inner blocks.
* @return array - The new columns block.
*/
public static function create_block_columns(array $attrs, array $inner_blocks): array
{
$html_content = self::generate_html_content("wp-block-columns");
return self::create_new_block(
Constants::BLOCK_CORE_COLUMNS,
$attrs,
$inner_blocks,
$html_content['html'],
$html_content['content']
);
}
/**
* Create a new columns block.
*
* @param array $attrs - The attributes of the block.
* @return array - The new columns block.
*/
public static function create_block_p4_columns(array $attrs): array
{
return self::create_new_block(
Constants::BLOCK_P4_COLUMNS,
$attrs,
[],
'',
[]
);
}
/**
* Create a new button block.
*
* @param array $attrs - The attributes of the block.
* @param string $text - The button label.
* @param string|null $link - The button link (optional).
* @return array - The new button block.
*/
public static function create_block_single_button(array $attrs, string $text, ?string $link = null): array
{
$classname = isset($attrs['className']) ? $attrs['className'] : '';
// phpcs:disable Generic.Files.LineLength.MaxExceeded
if (!$link) {
$html = '
<div class="wp-block-button ' . $classname . '"><a class="wp-block-button__link wp-element-button">' . $text . '</a></div>
';
} else {
$html = '
<div class="wp-block-button ' . $classname . '"><a target="_self" href="' . $link . '" class="wp-block-button__link wp-element-button">' . $text . '</a></div>
';
}
// phpcs:enable Generic.Files.LineLength.MaxExceeded
return self::create_new_block(
Constants::BLOCK_SINGLE_BUTTON,
$attrs,
[],
$html,
[$html]
);
}
/**
* Create a new buttons block.
*
* @param array $attrs - The attributes of the block.
* @param array $inner_blocks - The inner blocks.
* @return array - The new buttons block.
*/
public static function create_block_buttons(array $attrs, array $inner_blocks): array
{
$classname =
isset($attrs['className']) ?
'wp-block-buttons ' . $attrs['className'] :
'wp-block-buttons';
$html_content = self::generate_html_content($classname);
return self::create_new_block(
Constants::BLOCK_BUTTONS,
$attrs,
$inner_blocks,
$html_content['html'],
$html_content['content']
);
}
/**
* Create a new media & text block.
*
* @param array $attrs - The attributes of the block.
* @param array $inner_blocks - The inner blocks.
* @param string $img_url - The image URL.
* @param string $img_id - The image ID.
* @return array - The new media & text block.
*/
public static function create_media_text_block(
array $attrs,
array $inner_blocks,
string $img_url,
int $img_id
): array {
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$html = array (
0 => '
<div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="' . $img_url . '" alt="" class="wp-image-' . $img_id . ' size-full"/></figure><div class="wp-block-media-text__content">',
1 => null,
2 => '
',
3 => null,
4 => '
',
5 => null,
6 => '</div></div>
',
);
// phpcs:enable Generic.Files.LineLength.MaxExceeded
return self::create_new_block(
Constants::BLOCK_MEDIA_TEXT,
$attrs,
$inner_blocks,
$html[0] . $html[6],
$html
);
}
/**
* Create a new Embed block.
*
* @param string $social_media_url - The Social Media URL.
* @param string $type - The embed type.
* @param $provider - The embed provider.
*
* @return array - The new Embed block.
*/
public static function create_embed_block(
string $social_media_url,
string $type,
string $provider,
): array {
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$html_content = '<figure class="wp-block-embed is-type-' . $type . ' is-provider-' . $provider . ' wp-block-embed-' . $provider . '"><div class="wp-block-embed__wrapper">
' . $social_media_url . '
</div></figure>';
// phpcs:enable Generic.Files.LineLength.MaxExceeded
$attrs = [];
$attrs['type'] = $type;
$attrs['providerNameSlug'] = $provider;
$attrs['metadata']['name'] = $provider;
$attrs['url'] = $social_media_url;
$block['innerHTML'] = $html_content;
$block['innerContent'][0] = $html_content;
return self::create_new_block(
Constants::BLOCK_EMBED,
$attrs,
[],
$html_content,
[$html_content],
);
}
/**
* Create a new Query Loop block.
*
* @param array $inner_blocks - The inner blocks.
* @param array $attrs - The block attributes.
* @param string $classname - The block CSS class name.
*
* @return array - The new Post Query block.
*/
public static function create_block_query(array $inner_blocks, array $attrs, string $classname): array
{
$html = '
<div class="wp-block-query posts-list p4-query-loop is-custom-layout-' . $classname . '">
</div>
';
$content = array (
0 => '
<div class="wp-block-query posts-list p4-query-loop is-custom-layout-' . $classname . '">',
1 => null,
2 => '
',
3 => null,
4 => '
',
5 => null,
6 => '
',
7 => null,
8 => '
',
9 => null,
10 => '
',
11 => null,
12 => '</div>
',
);
return self::create_new_block(
Constants::BLOCK_QUERY,
$attrs,
$inner_blocks,
$html,
$content,
);
}
/**
* Create a new Query No Results block.
*
* @param array $inner_blocks - The inner blocks.
* @param array $attrs - The block attributes.
*
* @return array - The new Query No Results block.
*/
public static function create_block_query_no_results(array $inner_blocks, array $attrs): array
{
$html = '
';
$content = array (
0 => '
',
1 => null,
2 => '
',
);
return self::create_new_block(
Constants::BLOCK_QUERY_NO_RESULTS,
$attrs,
$inner_blocks,
$html,
$content,
);
}
/**
* Create a new Group block.
*
* @param array $inner_blocks - The block's inner blocks.
* @param array $attrs - The block's attributes.
*
* @return array - The new Group block.
*/
public static function create_group_block(array $inner_blocks, array $attrs): array
{
$classname =
isset($attrs['className']) ?
'wp-block-group ' . $attrs['className'] :
'wp-block-group';
// IMPORTANT: DO NOT MODIFY THIS FORMAT!
$inner_html =
'<div class="' . $classname . '">
</div>';
// IMPORTANT: DO NOT MODIFY THIS FORMAT!
$inner_content = array (
0 => '
<div class="' . $classname . '">',
1 => null,
2 => '
',
3 => null,
4 => '
',
5 => null,
6 => '
',
7 => null,
8 => '</div>
',
);
return self::create_new_block(
Constants::BLOCK_GROUP,
$attrs,
$inner_blocks,
$inner_html,
$inner_content,
);
}
/**
* Create a new Post template.
*
* @param array $inner_blocks - The template inner blocks.
* @param array $attrs - The template attributes.
*
* @return array - The new Post template.
*/
public static function create_post_template(array $inner_blocks, array $attrs): array
{
$html = '
';
$content = array (
0 => '
',
1 => null,
2 => '
',
);
return self::create_new_block(
Constants::BLOCK_POST_TEMPLATE,
$attrs,
$inner_blocks,
$html,
$content,
);
}
}