forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock-template.php
More file actions
583 lines (511 loc) · 20.1 KB
/
block-template.php
File metadata and controls
583 lines (511 loc) · 20.1 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
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
<?php
/**
* Tests for the block template loading algorithm.
*
* @package WordPress
*
* @group block-templates
*/
class Tests_Block_Template extends WP_UnitTestCase {
private static $post;
private static $template_canvas_path = ABSPATH . WPINC . '/template-canvas.php';
public function set_up() {
parent::set_up();
switch_theme( 'block-theme' );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
}
public function tear_down() {
global $_wp_current_template_id, $_wp_current_template_content;
unset( $_wp_current_template_id, $_wp_current_template_content );
parent::tear_down();
}
public function test_page_home_block_template_takes_precedence_over_less_specific_block_templates() {
global $_wp_current_template_content;
$type = 'page';
$templates = array(
'page-home.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-home.html', $_wp_current_template_content );
}
public function test_page_block_template_takes_precedence() {
global $_wp_current_template_content;
$type = 'page';
$templates = array(
'page-slug-doesnt-exist.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page.html', $_wp_current_template_content );
}
public function test_block_template_takes_precedence_over_equally_specific_php_template() {
global $_wp_current_template_content;
$type = 'index';
$templates = array(
'index.php',
);
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/index.html', $_wp_current_template_content );
}
/**
* In a hybrid theme, a PHP template of higher specificity will take precedence over a block template
* with lower specificity.
*
* Covers https://github.com/WordPress/gutenberg/pull/29026.
*/
public function test_more_specific_php_template_takes_precedence_over_less_specific_block_template() {
$page_id_template = 'page-1.php';
$page_id_template_path = get_stylesheet_directory() . '/' . $page_id_template;
$type = 'page';
$templates = array(
'page-slug-doesnt-exist.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $page_id_template_path, $type, $templates );
$this->assertSame( $page_id_template_path, $resolved_template_path );
}
/**
* If a theme is a child of a block-based parent theme but has php templates for some of its pages,
* a php template of the child will take precedence over the parent's block template if they have
* otherwise equal specificity.
*
* Covers https://github.com/WordPress/gutenberg/pull/31123.
* Covers https://core.trac.wordpress.org/ticket/54515.
*
*/
public function test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template() {
switch_theme( 'block-theme-child' );
$page_slug_template = 'page-home.php';
$page_slug_template_path = get_stylesheet_directory() . '/' . $page_slug_template;
$type = 'page';
$templates = array(
'page-home.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $page_slug_template_path, $type, $templates );
$this->assertSame( $page_slug_template_path, $resolved_template_path );
}
public function test_child_theme_block_template_takes_precedence_over_equally_specific_parent_theme_php_template() {
global $_wp_current_template_content;
switch_theme( 'block-theme-child' );
$page_template = 'page-1.php';
$parent_theme_page_template_path = get_template_directory() . '/' . $page_template;
$type = 'page';
$templates = array(
'page-slug-doesnt-exist.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $parent_theme_page_template_path, $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-1.html', $_wp_current_template_content );
}
/**
* Regression: https://github.com/WordPress/gutenberg/issues/31399.
*/
public function test_custom_page_php_template_takes_precedence_over_all_other_templates() {
$custom_page_template = 'templates/full-width.php';
$custom_page_template_path = get_stylesheet_directory() . '/' . $custom_page_template;
$type = 'page';
$templates = array(
$custom_page_template,
'page-slug.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $custom_page_template_path, $type, $templates );
$this->assertSame( $custom_page_template_path, $resolved_template_path );
}
/**
* Covers: https://github.com/WordPress/gutenberg/pull/30438.
*/
public function test_custom_page_block_template_takes_precedence_over_all_other_templates() {
global $_wp_current_template_content;
// Set up custom template post.
$args = array(
'post_type' => 'wp_template',
'post_name' => 'wp-custom-template-my-block-template',
'post_title' => 'My Custom Block Template',
'post_content' => 'Content',
'post_excerpt' => 'Description of my block template',
'tax_input' => array(
'wp_theme' => array(
get_stylesheet(),
),
),
);
$post = self::factory()->post->create_and_get( $args );
wp_set_post_terms( $post->ID, get_stylesheet(), 'wp_theme' );
$custom_page_block_template = 'wp-custom-template-my-block-template';
$page_template_path = get_stylesheet_directory() . '/' . 'page.php';
$type = 'page';
$templates = array(
$custom_page_block_template,
'page-slug.php',
'page-1.php',
'page.php',
);
$resolved_template_path = locate_block_template( $page_template_path, $type, $templates );
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
$this->assertSame( $post->post_content, $_wp_current_template_content );
wp_delete_post( $post->ID );
}
/**
* Regression: https://github.com/WordPress/gutenberg/issues/31652.
*/
public function test_template_remains_unchanged_if_templates_array_is_empty() {
$resolved_template_path = locate_block_template( '', 'search', array() );
$this->assertSame( '', $resolved_template_path );
}
/**
* Tests that `get_the_block_template_html()` wraps block parsing into the query loop when on a singular template.
*
* This is necessary since block themes do not include the necessary blocks to trigger the main query loop, and
* since there is only a single post in the main query loop in such cases anyway.
*
* @ticket 58154
* @ticket 59736
* @covers ::get_the_block_template_html
*/
public function test_get_the_block_template_html_enforces_singular_query_loop() {
global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
// Register test block to log `in_the_loop()` results.
$in_the_loop_logs = array();
$this->register_in_the_loop_logger_block( $in_the_loop_logs );
// Set main query to single post.
$post_id = self::factory()->post->create( array( 'post_title' => 'A single post' ) );
$wp_query = new WP_Query( array( 'p' => $post_id ) );
$wp_the_query = $wp_query;
// Force a template ID that is for the current stylesheet.
$_wp_current_template_id = get_stylesheet() . '//single';
// Use block template that just renders post title and the above test block.
$_wp_current_template_content = '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
$expected = '<div class="wp-site-blocks">';
$expected .= '<h2 class="wp-block-post-title">A single post</h2>';
$expected .= '</div>';
$output = get_the_block_template_html();
$this->unregister_in_the_loop_logger_block();
$this->assertSame( $expected, $output, 'Unexpected block template output' );
$this->assertSame( array( true ), $in_the_loop_logs, 'Main query loop was not triggered' );
}
/**
* Tests that `get_the_block_template_html()` does not start the main query loop generally.
*
* @ticket 58154
* @covers ::get_the_block_template_html
*/
public function test_get_the_block_template_html_does_not_generally_enforce_loop() {
global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
// Register test block to log `in_the_loop()` results.
$in_the_loop_logs = array();
$this->register_in_the_loop_logger_block( $in_the_loop_logs );
// Set main query to a general post query (i.e. not for a specific post).
$post_id = self::factory()->post->create(
array(
'post_title' => 'A single post',
'post_content' => 'The content.',
)
);
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
)
);
$wp_the_query = $wp_query;
// Force a template ID that is for the current stylesheet.
$_wp_current_template_id = get_stylesheet() . '//home';
/*
* Use block template that renders the above test block, followed by a main query loop.
* `get_the_block_template_html()` should not start the loop, but the `core/query` and `core/post-template`
* blocks should.
*/
$_wp_current_template_content = '<!-- wp:test/in-the-loop-logger /-->';
$_wp_current_template_content .= '<!-- wp:query {"query":{"inherit":true}} -->';
$_wp_current_template_content .= '<!-- wp:post-template -->';
$_wp_current_template_content .= '<!-- wp:post-title /-->';
$_wp_current_template_content .= '<!-- wp:post-content /--><!-- wp:test/in-the-loop-logger /-->';
$_wp_current_template_content .= '<!-- /wp:post-template -->';
$_wp_current_template_content .= '<!-- /wp:query -->';
$expected = '<div class="wp-site-blocks">';
$expected .= '<ul class="wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
$expected .= '<li class="wp-block-post post-' . $post_id . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">A single post</h2>';
$expected .= '<div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . wpautop( 'The content.' ) . '</div>';
$expected .= '</li>';
$expected .= '</ul>';
$expected .= '</div>';
$output = get_the_block_template_html();
$this->unregister_in_the_loop_logger_block();
$this->assertSame( $expected, $output, 'Unexpected block template output' );
$this->assertSame( array( false, true ), $in_the_loop_logs, 'Main query loop was triggered incorrectly' );
}
/**
* Tests that `get_the_block_template_html()` does not start the main query loop when on a template that is not from the current theme.
*
* @ticket 58154
* @ticket 59736
* @covers ::get_the_block_template_html
*/
public function test_get_the_block_template_html_skips_singular_query_loop_when_non_theme_template() {
global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
// Register test block to log `in_the_loop()` results.
$in_the_loop_logs = array();
$this->register_in_the_loop_logger_block( $in_the_loop_logs );
// Set main query to single post.
$post_id = self::factory()->post->create( array( 'post_title' => 'A single post' ) );
$wp_query = new WP_Query( array( 'p' => $post_id ) );
$wp_the_query = $wp_query;
// Force a template ID that is not for the current stylesheet.
$_wp_current_template_id = 'some-plugin-slug//single';
// Use block template that just renders post title and the above test block.
$_wp_current_template_content = '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
$output = get_the_block_template_html();
$this->unregister_in_the_loop_logger_block();
$this->assertSame( array( false ), $in_the_loop_logs, 'Main query loop was triggered despite a custom block template outside the current theme being used' );
}
/**
* Tests that `get_the_block_template_html()` adds a skip link when a MAIN element is present.
*
* @ticket 64361
* @covers ::get_the_block_template_html
*/
public function test_get_the_block_template_html_adds_skip_link_when_main_present() {
global $_wp_current_template_id, $_wp_current_template_content;
$_wp_current_template_id = get_stylesheet() . '//index';
$_wp_current_template_content = '<main>Content</main>';
$output = get_the_block_template_html();
$this->assertStringContainsString(
'<a class="skip-link screen-reader-text" id="wp-skip-link"',
$output,
'Expected skip link was not added to the block template HTML.'
);
}
/**
* Tests that `get_the_block_template_html()` does not add a skip link when the skip-link action is unhooked.
*
* @ticket 64361
* @covers ::get_the_block_template_html
*/
public function test_get_the_block_template_html_does_not_add_skip_link_when_action_unhooked() {
global $_wp_current_template_id, $_wp_current_template_content;
$_wp_current_template_id = get_stylesheet() . '//index';
$_wp_current_template_content = '<main>Content</main>';
$was_hooked = (bool) has_action( 'wp_footer', 'the_block_template_skip_link' );
remove_action( 'wp_footer', 'the_block_template_skip_link' );
try {
$output = get_the_block_template_html();
} finally {
if ( $was_hooked ) {
add_action( 'wp_footer', 'the_block_template_skip_link' );
}
}
$this->assertStringNotContainsString(
'<a class="skip-link screen-reader-text" id="wp-skip-link"',
$output,
'Unexpected skip link was added to the block template HTML when the action was unhooked.'
);
}
/**
* @ticket 58319
*
* @covers ::get_block_theme_folders
*
* @dataProvider data_get_block_theme_folders
*
* @param string $theme The theme's stylesheet.
* @param string[] $expected The expected associative array of block theme folders.
*/
public function test_get_block_theme_folders( $theme, $expected ) {
$wp_theme = wp_get_theme( $theme );
$wp_theme->cache_delete(); // Clear cache.
$this->assertSame( $expected, get_block_theme_folders( $theme ), 'Incorrect block theme folders were retrieved.' );
$reflection = new ReflectionMethod( $wp_theme, 'cache_get' );
if ( PHP_VERSION_ID < 80100 ) {
$reflection->setAccessible( true );
}
$theme_cache = $reflection->invoke( $wp_theme, 'theme' );
$cached_value = $theme_cache['block_template_folders'];
if ( PHP_VERSION_ID < 80100 ) {
$reflection->setAccessible( false );
}
$this->assertSame( $expected, $cached_value, 'The cached value is incorrect.' );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_get_block_theme_folders() {
return array(
'block-theme' => array(
'block-theme',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'block-theme-deprecated-path' => array(
'block-theme-deprecated-path',
array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
),
),
'block-theme-child' => array(
'block-theme-child',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'block-theme-child-deprecated-path' => array(
'block-theme-child-deprecated-path',
array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
),
),
'this-is-an-invalid-theme' => array(
'this-is-an-invalid-theme',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'null' => array(
null,
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
);
}
/**
* Tests `_get_block_templates_paths()` for an invalid directory.
*
* @ticket 58196
*
* @covers ::_get_block_templates_paths
*/
public function test_get_block_templates_paths_dir_exists() {
$theme_dir = $this->normalizeDirectorySeparatorsInPath( get_template_directory() );
// Templates in the current theme.
$templates = array(
'parts/small-header.html',
'templates/custom-hero-template.html',
'templates/custom-single-post-template.html',
'templates/index.html',
'templates/page-home.html',
'templates/page.html',
'templates/single.html',
);
$expected_template_paths = array_map(
static function ( $template ) use ( $theme_dir ) {
return $theme_dir . '/' . $template;
},
$templates
);
$template_paths = _get_block_templates_paths( $theme_dir );
$template_paths = array_map( array( $this, 'normalizeDirectorySeparatorsInPath' ), _get_block_templates_paths( $theme_dir ) );
$this->assertSameSets( $expected_template_paths, $template_paths );
}
/**
* Test _get_block_templates_paths() for a invalid dir.
*
* @ticket 58196
*
* @covers ::_get_block_templates_paths
*/
public function test_get_block_templates_paths_dir_doesnt_exists() {
// Should return empty array for invalid path.
$template_paths = _get_block_templates_paths( '/tmp/random-invalid-theme-path' );
$this->assertSame( array(), $template_paths );
}
/**
* Tests that get_block_templates() returns plugin-registered templates.
*
* @ticket 61804
*
* @covers ::get_block_templates
*/
public function test_get_block_templates_from_registry() {
$template_name = 'test-plugin//test-template';
register_block_template( $template_name );
$templates = get_block_templates();
$this->assertArrayHasKey( $template_name, $templates );
unregister_block_template( $template_name );
}
/**
* Tests that get_block_template() returns plugin-registered templates.
*
* @ticket 61804
*
* @covers ::get_block_template
*/
public function test_get_block_template_from_registry() {
$template_name = 'test-plugin//test-template';
$args = array(
'title' => 'Test Template',
);
register_block_template( $template_name, $args );
$template = get_block_template( 'block-theme//test-template' );
$this->assertSame( 'Test Template', $template->title );
unregister_block_template( $template_name );
}
/**
* Tests that unregister_block_template() returns a WP_Error when trying to unregister
* a non-registered template, and that the error message includes the template name.
*
* @ticket 64072
*
* @covers ::unregister_block_template
*/
public function test_unregister_block_template_error_message_includes_template_name() {
$template_name = 'test-plugin//unregistered-template';
// Ensure template is not registered.
unregister_block_template( $template_name );
// Expect _doing_it_wrong() notice.
$this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::unregister' );
// Try to unregister again, should return WP_Error.
$error = unregister_block_template( $template_name );
$this->assertInstanceOf( 'WP_Error', $error );
$this->assertStringContainsString(
$template_name,
$error->get_error_message(),
'Error message should include the template name.'
);
}
/**
* Registers a test block to log `in_the_loop()` results.
*
* @param array $in_the_loop_logs Array to log function results in. Passed by reference.
*/
private function register_in_the_loop_logger_block( array &$in_the_loop_logs ) {
register_block_type(
'test/in-the-loop-logger',
array(
'render_callback' => function () use ( &$in_the_loop_logs ) {
$in_the_loop_logs[] = in_the_loop();
return '';
},
)
);
}
/**
* Unregisters the test block registered by the `register_in_the_loop_logger_block()` method.
*/
private function unregister_in_the_loop_logger_block() {
unregister_block_type( 'test/in-the-loop-logger' );
}
}