-
Notifications
You must be signed in to change notification settings - Fork 12.6k
/
block-template-utils.php
1719 lines (1533 loc) · 58.7 KB
/
block-template-utils.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Utilities used to fetch and create templates and template parts.
*
* @package WordPress
* @since 5.8.0
*/
// Define constants for supported wp_template_part_area taxonomy.
if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) {
define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' );
}
if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) {
define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' );
}
if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) {
define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' );
}
if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
}
/**
* For backward compatibility reasons,
* block themes might be using block-templates or block-template-parts,
* this function ensures we fallback to these folders properly.
*
* @since 5.9.0
*
* @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root.
*
* @return string[] {
* Folder names used by block themes.
*
* @type string $wp_template Theme-relative directory name for block templates.
* @type string $wp_template_part Theme-relative directory name for block template parts.
* }
*/
function get_block_theme_folders( $theme_stylesheet = null ) {
$theme = wp_get_theme( (string) $theme_stylesheet );
if ( ! $theme->exists() ) {
// Return the default folders if the theme doesn't exist.
return array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
);
}
return $theme->get_block_template_folders();
}
/**
* Returns a filtered list of allowed area values for template parts.
*
* @since 5.9.0
*
* @return array[] {
* The allowed template part area values.
*
* @type array ...$0 {
* Data for the allowed template part area.
*
* @type string $area Template part area name.
* @type string $label Template part area label.
* @type string $description Template part area description.
* @type string $icon Template part area icon.
* @type string $area_tag Template part area tag.
* }
* }
*/
function get_allowed_block_template_part_areas() {
$default_area_definitions = array(
array(
'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
'label' => _x( 'General', 'template part area' ),
'description' => __(
'General templates often perform a specific role like displaying post content, and are not tied to any particular area.'
),
'icon' => 'layout',
'area_tag' => 'div',
),
array(
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'label' => _x( 'Header', 'template part area' ),
'description' => __(
'The Header template defines a page area that typically contains a title, logo, and main navigation.'
),
'icon' => 'header',
'area_tag' => 'header',
),
array(
'area' => WP_TEMPLATE_PART_AREA_FOOTER,
'label' => _x( 'Footer', 'template part area' ),
'description' => __(
'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.'
),
'icon' => 'footer',
'area_tag' => 'footer',
),
);
/**
* Filters the list of allowed template part area values.
*
* @since 5.9.0
*
* @param array[] $default_area_definitions {
* The allowed template part area values.
*
* @type array ...$0 {
* Data for the template part area.
*
* @type string $area Template part area name.
* @type string $label Template part area label.
* @type string $description Template part area description.
* @type string $icon Template part area icon.
* @type string $area_tag Template part area tag.
* }
* }
*/
return apply_filters( 'default_wp_template_part_areas', $default_area_definitions );
}
/**
* Returns a filtered list of default template types, containing their
* localized titles and descriptions.
*
* @since 5.9.0
*
* @return array[] {
* The default template types.
*
* @type array ...$0 {
* Data for the template type.
*
* @type string $title Template type title.
* @type string $description Template type description.
* }
* }
*/
function get_default_block_template_types() {
$default_template_types = array(
'index' => array(
'title' => _x( 'Index', 'Template name' ),
'description' => __( 'Used as a fallback template for all pages when a more specific template is not defined.' ),
),
'home' => array(
'title' => _x( 'Blog Home', 'Template name' ),
'description' => __( 'Displays the latest posts as either the site homepage or as the "Posts page" as defined under reading settings. If it exists, the Front Page template overrides this template when posts are shown on the homepage.' ),
),
'front-page' => array(
'title' => _x( 'Front Page', 'Template name' ),
'description' => __( 'Displays your site\'s homepage, whether it is set to display latest posts or a static page. The Front Page template takes precedence over all templates.' ),
),
'singular' => array(
'title' => _x( 'Single Entries', 'Template name' ),
'description' => __( 'Displays any single entry, such as a post or a page. This template will serve as a fallback when a more specific template (e.g. Single Post, Page, or Attachment) cannot be found.' ),
),
'single' => array(
'title' => _x( 'Single Posts', 'Template name' ),
'description' => __( 'Displays a single post on your website unless a custom template has been applied to that post or a dedicated template exists.' ),
),
'page' => array(
'title' => _x( 'Pages', 'Template name' ),
'description' => __( 'Displays a static page unless a custom template has been applied to that page or a dedicated template exists.' ),
),
'archive' => array(
'title' => _x( 'All Archives', 'Template name' ),
'description' => __( 'Displays any archive, including posts by a single author, category, tag, taxonomy, custom post type, and date. This template will serve as a fallback when more specific templates (e.g. Category or Tag) cannot be found.' ),
),
'author' => array(
'title' => _x( 'Author Archives', 'Template name' ),
'description' => __( 'Displays a single author\'s post archive. This template will serve as a fallback when a more specific template (e.g. Author: Admin) cannot be found.' ),
),
'category' => array(
'title' => _x( 'Category Archives', 'Template name' ),
'description' => __( 'Displays a post category archive. This template will serve as a fallback when a more specific template (e.g. Category: Recipes) cannot be found.' ),
),
'taxonomy' => array(
'title' => _x( 'Taxonomy', 'Template name' ),
'description' => __( 'Displays a custom taxonomy archive. Like categories and tags, taxonomies have terms which you use to classify things. For example: a taxonomy named "Art" can have multiple terms, such as "Modern" and "18th Century." This template will serve as a fallback when a more specific template (e.g. Taxonomy: Art) cannot be found.' ),
),
'date' => array(
'title' => _x( 'Date Archives', 'Template name' ),
'description' => __( 'Displays a post archive when a specific date is visited (e.g., example.com/2023/).' ),
),
'tag' => array(
'title' => _x( 'Tag Archives', 'Template name' ),
'description' => __( 'Displays a post tag archive. This template will serve as a fallback when a more specific template (e.g. Tag: Pizza) cannot be found.' ),
),
'attachment' => array(
'title' => __( 'Attachment Pages' ),
'description' => __( 'Displays when a visitor views the dedicated page that exists for any media attachment.' ),
),
'search' => array(
'title' => _x( 'Search Results', 'Template name' ),
'description' => __( 'Displays when a visitor performs a search on your website.' ),
),
'privacy-policy' => array(
'title' => __( 'Privacy Policy' ),
'description' => __( 'Displays your site\'s Privacy Policy page.' ),
),
'404' => array(
'title' => _x( 'Page: 404', 'Template name' ),
'description' => __( 'Displays when a visitor views a non-existent page, such as a dead link or a mistyped URL.' ),
),
);
/**
* Filters the list of default template types.
*
* @since 5.9.0
*
* @param array[] $default_template_types {
* The default template types.
*
* @type array ...$0 {
* Data for the template type.
*
* @type string $title Template type title.
* @type string $description Template type description.
* }
* }
*/
return apply_filters( 'default_template_types', $default_template_types );
}
/**
* Checks whether the input 'area' is a supported value.
* Returns the input if supported, otherwise returns the 'uncategorized' value.
*
* @since 5.9.0
* @access private
*
* @param string $type Template part area name.
* @return string Input if supported, else the uncategorized value.
*/
function _filter_block_template_part_area( $type ) {
$allowed_areas = array_map(
static function ( $item ) {
return $item['area'];
},
get_allowed_block_template_part_areas()
);
if ( in_array( $type, $allowed_areas, true ) ) {
return $type;
}
$warning_message = sprintf(
/* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */
__( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ),
$type,
WP_TEMPLATE_PART_AREA_UNCATEGORIZED
);
wp_trigger_error( __FUNCTION__, $warning_message );
return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}
/**
* Finds all nested template part file paths in a theme's directory.
*
* @since 5.9.0
* @access private
*
* @param string $base_directory The theme's file path.
* @return string[] A list of paths to all template part files.
*/
function _get_block_templates_paths( $base_directory ) {
static $template_path_list = array();
if ( isset( $template_path_list[ $base_directory ] ) ) {
return $template_path_list[ $base_directory ];
}
$path_list = array();
if ( is_dir( $base_directory ) ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
$nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
foreach ( $nested_html_files as $path => $file ) {
$path_list[] = $path;
}
}
$template_path_list[ $base_directory ] = $path_list;
return $path_list;
}
/**
* Retrieves the template file from the theme for a given slug.
*
* @since 5.9.0
* @access private
*
* @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'.
* @param string $slug Template slug.
* @return array|null {
* Array with template metadata if $template_type is one of 'wp_template' or 'wp_template_part',
* null otherwise.
*
* @type string $slug Template slug.
* @type string $path Template file path.
* @type string $theme Theme slug.
* @type string $type Template type.
* @type string $area Template area. Only for 'wp_template_part'.
* @type string $title Optional. Template title.
* @type string[] $postTypes Optional. List of post types that the template supports. Only for 'wp_template'.
* }
*/
function _get_block_template_file( $template_type, $slug ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
return null;
}
$themes = array(
get_stylesheet() => get_stylesheet_directory(),
get_template() => get_template_directory(),
);
foreach ( $themes as $theme_slug => $theme_dir ) {
$template_base_paths = get_block_theme_folders( $theme_slug );
$file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
if ( file_exists( $file_path ) ) {
$new_template_item = array(
'slug' => $slug,
'path' => $file_path,
'theme' => $theme_slug,
'type' => $template_type,
);
if ( 'wp_template_part' === $template_type ) {
return _add_block_template_part_area_info( $new_template_item );
}
if ( 'wp_template' === $template_type ) {
return _add_block_template_info( $new_template_item );
}
return $new_template_item;
}
}
return null;
}
/**
* Retrieves the template files from the theme.
*
* @since 5.9.0
* @since 6.3.0 Added the `$query` parameter.
* @access private
*
* @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'.
* @param array $query {
* Arguments to retrieve templates. Optional, empty by default.
*
* @type string[] $slug__in List of slugs to include.
* @type string[] $slug__not_in List of slugs to skip.
* @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).
* @type string $post_type Post type to get the templates for.
* }
*
* @return array|null Template files on success, null if `$template_type` is not matched.
*/
function _get_block_templates_files( $template_type, $query = array() ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
return null;
}
$default_template_types = array();
if ( 'wp_template' === $template_type ) {
$default_template_types = get_default_block_template_types();
}
// Prepare metadata from $query.
$slugs_to_include = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
$slugs_to_skip = isset( $query['slug__not_in'] ) ? $query['slug__not_in'] : array();
$area = isset( $query['area'] ) ? $query['area'] : null;
$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
$stylesheet = get_stylesheet();
$template = get_template();
$themes = array(
$stylesheet => get_stylesheet_directory(),
);
// Add the parent theme if it's not the same as the current theme.
if ( $stylesheet !== $template ) {
$themes[ $template ] = get_template_directory();
}
$template_files = array();
foreach ( $themes as $theme_slug => $theme_dir ) {
$template_base_paths = get_block_theme_folders( $theme_slug );
$theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
foreach ( $theme_template_files as $template_file ) {
$template_base_path = $template_base_paths[ $template_type ];
$template_slug = substr(
$template_file,
// Starting position of slug.
strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
// Subtract ending '.html'.
-5
);
// Skip this item if its slug doesn't match any of the slugs to include.
if ( ! empty( $slugs_to_include ) && ! in_array( $template_slug, $slugs_to_include, true ) ) {
continue;
}
// Skip this item if its slug matches any of the slugs to skip.
if ( ! empty( $slugs_to_skip ) && in_array( $template_slug, $slugs_to_skip, true ) ) {
continue;
}
/*
* The child theme items (stylesheet) are processed before the parent theme's (template).
* If a child theme defines a template, prevent the parent template from being added to the list as well.
*/
if ( isset( $template_files[ $template_slug ] ) ) {
continue;
}
$new_template_item = array(
'slug' => $template_slug,
'path' => $template_file,
'theme' => $theme_slug,
'type' => $template_type,
);
if ( 'wp_template_part' === $template_type ) {
$candidate = _add_block_template_part_area_info( $new_template_item );
if ( ! isset( $area ) || ( isset( $area ) && $area === $candidate['area'] ) ) {
$template_files[ $template_slug ] = $candidate;
}
}
if ( 'wp_template' === $template_type ) {
$candidate = _add_block_template_info( $new_template_item );
$is_custom = ! isset( $default_template_types[ $candidate['slug'] ] );
if (
! $post_type ||
( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) )
) {
$template_files[ $template_slug ] = $candidate;
}
// The custom templates with no associated post types are available for all post types.
if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) {
$template_files[ $template_slug ] = $candidate;
}
}
}
}
return array_values( $template_files );
}
/**
* Attempts to add custom template information to the template item.
*
* @since 5.9.0
* @access private
*
* @param array $template_item Template to add information to (requires 'slug' field).
* @return array Template item.
*/
function _add_block_template_info( $template_item ) {
if ( ! wp_theme_has_theme_json() ) {
return $template_item;
}
$theme_data = wp_get_theme_data_custom_templates();
if ( isset( $theme_data[ $template_item['slug'] ] ) ) {
$template_item['title'] = $theme_data[ $template_item['slug'] ]['title'];
$template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes'];
}
return $template_item;
}
/**
* Attempts to add the template part's area information to the input template.
*
* @since 5.9.0
* @access private
*
* @param array $template_info Template to add information to (requires 'type' and 'slug' fields).
* @return array Template info.
*/
function _add_block_template_part_area_info( $template_info ) {
if ( wp_theme_has_theme_json() ) {
$theme_data = wp_get_theme_data_template_parts();
}
if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
$template_info['title'] = $theme_data[ $template_info['slug'] ]['title'];
$template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
} else {
$template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}
return $template_info;
}
/**
* Returns an array containing the references of
* the passed blocks and their inner blocks.
*
* @since 5.9.0
* @access private
*
* @param array $blocks array of blocks.
* @return array block references to the passed blocks and their inner blocks.
*/
function _flatten_blocks( &$blocks ) {
$all_blocks = array();
$queue = array();
foreach ( $blocks as &$block ) {
$queue[] = &$block;
}
while ( count( $queue ) > 0 ) {
$block = &$queue[0];
array_shift( $queue );
$all_blocks[] = &$block;
if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
}
}
}
return $all_blocks;
}
/**
* Injects the active theme's stylesheet as a `theme` attribute
* into a given template part block.
*
* @since 6.4.0
* @access private
*
* @param array $block a parsed block.
*/
function _inject_theme_attribute_in_template_part_block( &$block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = get_stylesheet();
}
}
/**
* Removes the `theme` attribute from a given template part block.
*
* @since 6.4.0
* @access private
*
* @param array $block a parsed block.
*/
function _remove_theme_attribute_from_template_part_block( &$block ) {
if (
'core/template-part' === $block['blockName'] &&
isset( $block['attrs']['theme'] )
) {
unset( $block['attrs']['theme'] );
}
}
/**
* Builds a unified template object based on a theme file.
*
* @since 5.9.0
* @since 6.3.0 Added `modified` property to template objects.
* @access private
*
* @param array $template_file Theme file.
* @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'.
* @return WP_Block_Template Template.
*/
function _build_block_template_result_from_file( $template_file, $template_type ) {
$default_template_types = get_default_block_template_types();
$theme = get_stylesheet();
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = file_get_contents( $template_file['path'] );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
$template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
$template->status = 'publish';
$template->has_theme_file = true;
$template->is_custom = true;
$template->modified = null;
if ( 'wp_template' === $template_type ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
if ( $registered_template ) {
$template->plugin = $registered_template->plugin;
$template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
$template->description = empty( $template->description ) ? $registered_template->description : $template->description;
}
}
if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
$template->is_custom = false;
}
if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}
if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) {
$template->area = $template_file['area'];
}
if ( 'wp_template_part' === $template->type ) {
/*
* In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
* we need to wrap its content a mock template part block and traverse it.
*/
$content = get_comment_delimited_block_content(
'core/template-part',
array(),
$template->content
);
$content = apply_block_hooks_to_content(
$content,
$template,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
$template->content = remove_serialized_parent_block( $content );
} else {
$template->content = apply_block_hooks_to_content(
$template->content,
$template,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
}
return $template;
}
/**
* Builds the title and description of a post-specific template based on the underlying referenced post.
*
* Mutates the underlying template object.
*
* @since 6.1.0
* @access private
*
* @param string $post_type Post type, e.g. page, post, product.
* @param string $slug Slug of the post, e.g. a-story-about-shoes.
* @param WP_Block_Template $template Template to mutate adding the description and title computed.
* @return bool Returns true if the referenced post was found and false otherwise.
*/
function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) {
$post_type_object = get_post_type_object( $post_type );
$default_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => 1,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
$args = array(
'name' => $slug,
);
$args = wp_parse_args( $args, $default_args );
$posts_query = new WP_Query( $args );
if ( empty( $posts_query->posts ) ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */
__( 'Not found: %1$s (%2$s)' ),
$post_type_object->labels->singular_name,
$slug
);
return false;
}
$post_title = $posts_query->posts[0]->post_title;
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Post type singular name, 2: Post title. */
__( '%1$s: %2$s' ),
$post_type_object->labels->singular_name,
$post_title
);
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Post title. */
__( 'Template for %s' ),
$post_title
);
$args = array(
'title' => $post_title,
);
$args = wp_parse_args( $args, $default_args );
$posts_with_same_title_query = new WP_Query( $args );
if ( count( $posts_with_same_title_query->posts ) > 1 ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */
__( '%1$s (%2$s)' ),
$template->title,
$slug
);
}
return true;
}
/**
* Builds the title and description of a taxonomy-specific template based on the underlying entity referenced.
*
* Mutates the underlying template object.
*
* @since 6.1.0
* @access private
*
* @param string $taxonomy Identifier of the taxonomy, e.g. category.
* @param string $slug Slug of the term, e.g. shoes.
* @param WP_Block_Template $template Template to mutate adding the description and title computed.
* @return bool True if the term referenced was found and false otherwise.
*/
function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) {
$taxonomy_object = get_taxonomy( $taxonomy );
$default_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'update_term_meta_cache' => false,
);
$term_query = new WP_Term_Query();
$args = array(
'number' => 1,
'slug' => $slug,
);
$args = wp_parse_args( $args, $default_args );
$terms_query = $term_query->query( $args );
if ( empty( $terms_query ) ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */
__( 'Not found: %1$s (%2$s)' ),
$taxonomy_object->labels->singular_name,
$slug
);
return false;
}
$term_title = $terms_query[0]->name;
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Taxonomy singular name, 2: Term title. */
__( '%1$s: %2$s' ),
$taxonomy_object->labels->singular_name,
$term_title
);
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Term title. */
__( 'Template for %s' ),
$term_title
);
$term_query = new WP_Term_Query();
$args = array(
'number' => 2,
'name' => $term_title,
);
$args = wp_parse_args( $args, $default_args );
$terms_with_same_title_query = $term_query->query( $args );
if ( count( $terms_with_same_title_query ) > 1 ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title, 2: Term slug. */
__( '%1$s (%2$s)' ),
$template->title,
$slug
);
}
return true;
}
/**
* Builds a block template object from a post object.
*
* This is a helper function that creates a block template object from a given post object.
* It is self-sufficient in that it only uses information passed as arguments; it does not
* query the database for additional information.
*
* @since 6.5.3
* @access private
*
* @param WP_Post $post Template post.
* @param array $terms Additional terms to inform the template object.
* @param array $meta Additional meta fields to inform the template object.
* @return WP_Block_Template|WP_Error Template or error object.
*/
function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) {
if ( empty( $terms['wp_theme'] ) ) {
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
}
$theme = $terms['wp_theme'];
$default_template_types = get_default_block_template_types();
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
$has_theme_file = get_stylesheet() === $theme && null !== $template_file;
$template = new WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->source = 'custom';
$template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null;
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = $has_theme_file;
$template->is_custom = empty( $meta['is_wp_suggestion'] );
$template->author = $post->post_author;
$template->modified = $post->post_modified;
if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}
if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
$template->is_custom = false;
}
if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) {
$template->area = $terms['wp_template_part_area'];
}
return $template;
}
/**
* Builds a unified template object based a post Object.
*
* @since 5.9.0
* @since 6.3.0 Added `modified` property to template objects.
* @since 6.4.0 Added support for a revision post to be passed to this function.
* @access private
*
* @param WP_Post $post Template post.
* @return WP_Block_Template|WP_Error Template or error object.
*/
function _build_block_template_result_from_post( $post ) {
$post_id = wp_is_post_revision( $post );
if ( ! $post_id ) {
$post_id = $post;
}
$parent_post = get_post( $post_id );
$post->post_name = $parent_post->post_name;
$post->post_type = $parent_post->post_type;
$terms = get_the_terms( $parent_post, 'wp_theme' );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( ! $terms ) {
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
}
$terms = array(
'wp_theme' => $terms[0]->name,
);
if ( 'wp_template_part' === $parent_post->post_type ) {
$type_terms = get_the_terms( $parent_post, 'wp_template_part_area' );
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
$terms['wp_template_part_area'] = $type_terms[0]->name;
}
}
$meta = array(
'origin' => get_post_meta( $parent_post->ID, 'origin', true ),
'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ),
);
$template = _build_block_template_object_from_post_object( $post, $terms, $meta );
if ( is_wp_error( $template ) ) {
return $template;
}
// Check for a block template without a description and title or with a title equal to the slug.
if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
$matches = array();
// Check for a block template for a single author, page, post, tag, category, custom post type, or custom taxonomy.
if ( preg_match( '/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches ) ) {
$type = $matches[1];
$slug_remaining = $matches[2];
switch ( $type ) {
case 'author':
$nice_name = $slug_remaining;
$users = get_users(
array(
'capability' => 'edit_posts',
'search' => $nice_name,
'search_columns' => array( 'user_nicename' ),
'fields' => 'display_name',
)
);
if ( empty( $users ) ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor, referencing a deleted author. %s: Author nicename. */
__( 'Deleted author: %s' ),
$nice_name
);
} else {
$author_name = $users[0];
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. %s: Author name. */
__( 'Author: %s' ),
$author_name
);
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Author name. */
__( 'Template for %s' ),
$author_name
);
$users_with_same_name = get_users(
array(
'capability' => 'edit_posts',
'search' => $author_name,
'search_columns' => array( 'display_name' ),
'fields' => 'display_name',
)
);
if ( count( $users_with_same_name ) > 1 ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title of an author template, 2: Author nicename. */
__( '%1$s (%2$s)' ),
$template->title,
$nice_name
);
}
}
break;
case 'page':
_wp_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template );
break;
case 'single':
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
$post_type_length = strlen( $post_type ) + 1;
// If $slug_remaining starts with $post_type followed by a hyphen.
if ( 0 === strncmp( $slug_remaining, $post_type . '-', $post_type_length ) ) {
$slug = substr( $slug_remaining, $post_type_length, strlen( $slug_remaining ) );
$found = _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, $template );
if ( $found ) {
break;
}
}
}
break;
case 'tag':
_wp_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template );
break;
case 'category':
_wp_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template );
break;
case 'taxonomy':
$taxonomies = get_taxonomies();