forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.php
More file actions
719 lines (653 loc) · 19.6 KB
/
template.php
File metadata and controls
719 lines (653 loc) · 19.6 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
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
<?php
/**
* test wp-includes/template.php
*
* @group themes
*/
class Tests_Template extends WP_UnitTestCase {
protected $hierarchy = array();
protected static $page_on_front;
protected static $page_for_posts;
protected static $page;
protected static $post;
/**
* Page For Privacy Policy.
*
* @since 5.2.0
*
* @var WP_Post $page_for_privacy_policy
*/
protected static $page_for_privacy_policy;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$page_on_front = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_name' => 'page-on-front-😀',
)
);
self::$page_for_posts = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_name' => 'page-for-posts-😀',
)
);
self::$page = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_name' => 'page-name-😀',
)
);
add_post_meta( self::$page->ID, '_wp_page_template', 'templates/page.php' );
self::$post = $factory->post->create_and_get(
array(
'post_type' => 'post',
'post_name' => 'post-name-😀',
'post_date' => '1984-02-25 12:34:56',
)
);
set_post_format( self::$post, 'quote' );
add_post_meta( self::$post->ID, '_wp_page_template', 'templates/post.php' );
self::$page_for_privacy_policy = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => 'Privacy Policy',
)
);
}
public function set_up() {
parent::set_up();
register_post_type(
'cpt',
array(
'public' => true,
)
);
register_taxonomy(
'taxo',
'post',
array(
'public' => true,
'hierarchical' => true,
)
);
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
}
public function tear_down() {
unregister_post_type( 'cpt' );
unregister_taxonomy( 'taxo' );
$this->set_permalink_structure( '' );
parent::tear_down();
}
public function test_404_template_hierarchy() {
$url = add_query_arg(
array(
'p' => '-1',
),
home_url()
);
$this->assertTemplateHierarchy(
$url,
array(
'404.php',
)
);
}
public function test_author_template_hierarchy() {
$author = self::factory()->user->create_and_get(
array(
'user_nicename' => 'foo',
)
);
$this->assertTemplateHierarchy(
get_author_posts_url( $author->ID ),
array(
'author-foo.php',
"author-{$author->ID}.php",
'author.php',
'archive.php',
)
);
}
public function test_category_template_hierarchy() {
$term = self::factory()->term->create_and_get(
array(
'taxonomy' => 'category',
'slug' => 'foo-😀',
)
);
$this->assertTemplateHierarchy(
get_term_link( $term ),
array(
'category-foo-😀.php',
'category-foo-%f0%9f%98%80.php',
"category-{$term->term_id}.php",
'category.php',
'archive.php',
)
);
}
public function test_tag_template_hierarchy() {
$term = self::factory()->term->create_and_get(
array(
'taxonomy' => 'post_tag',
'slug' => 'foo-😀',
)
);
$this->assertTemplateHierarchy(
get_term_link( $term ),
array(
'tag-foo-😀.php',
'tag-foo-%f0%9f%98%80.php',
"tag-{$term->term_id}.php",
'tag.php',
'archive.php',
)
);
}
public function test_taxonomy_template_hierarchy() {
$term = self::factory()->term->create_and_get(
array(
'taxonomy' => 'taxo',
'slug' => 'foo-😀',
)
);
$this->assertTemplateHierarchy(
get_term_link( $term ),
array(
'taxonomy-taxo-foo-😀.php',
'taxonomy-taxo-foo-%f0%9f%98%80.php',
'taxonomy-taxo.php',
'taxonomy.php',
'archive.php',
)
);
}
public function test_date_template_hierarchy_for_year() {
$this->assertTemplateHierarchy(
get_year_link( 1984 ),
array(
'date.php',
'archive.php',
)
);
}
public function test_date_template_hierarchy_for_month() {
$this->assertTemplateHierarchy(
get_month_link( 1984, 2 ),
array(
'date.php',
'archive.php',
)
);
}
public function test_date_template_hierarchy_for_day() {
$this->assertTemplateHierarchy(
get_day_link( 1984, 2, 25 ),
array(
'date.php',
'archive.php',
)
);
}
public function test_search_template_hierarchy() {
$url = add_query_arg(
array(
's' => 'foo',
),
home_url()
);
$this->assertTemplateHierarchy(
$url,
array(
'search.php',
)
);
}
public function test_front_page_template_hierarchy_with_posts_on_front() {
$this->assertSame( 'posts', get_option( 'show_on_front' ) );
$this->assertTemplateHierarchy(
home_url(),
array(
'front-page.php',
'home.php',
'index.php',
)
);
}
public function test_front_page_template_hierarchy_with_page_on_front() {
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', self::$page_on_front->ID );
update_option( 'page_for_posts', self::$page_for_posts->ID );
$this->assertTemplateHierarchy(
home_url(),
array(
'front-page.php',
'page-page-on-front-😀.php',
'page-page-on-front-%f0%9f%98%80.php',
'page-' . self::$page_on_front->ID . '.php',
'page.php',
'singular.php',
)
);
}
public function test_home_template_hierarchy_with_page_on_front() {
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', self::$page_on_front->ID );
update_option( 'page_for_posts', self::$page_for_posts->ID );
$this->assertTemplateHierarchy(
get_permalink( self::$page_for_posts ),
array(
'home.php',
'index.php',
)
);
}
public function test_page_template_hierarchy() {
$this->assertTemplateHierarchy(
get_permalink( self::$page ),
array(
'templates/page.php',
'page-page-name-😀.php',
'page-page-name-%f0%9f%98%80.php',
'page-' . self::$page->ID . '.php',
'page.php',
'singular.php',
)
);
}
/**
* @ticket 44005
* @group privacy
*/
public function test_privacy_template_hierarchy() {
update_option( 'wp_page_for_privacy_policy', self::$page_for_privacy_policy->ID );
$this->assertTemplateHierarchy(
get_permalink( self::$page_for_privacy_policy->ID ),
array(
'privacy-policy.php',
'page-privacy-policy.php',
'page-' . self::$page_for_privacy_policy->ID . '.php',
'page.php',
'singular.php',
)
);
}
/**
* @ticket 18375
*/
public function test_single_template_hierarchy_for_post() {
$this->assertTemplateHierarchy(
get_permalink( self::$post ),
array(
'templates/post.php',
'single-post-post-name-😀.php',
'single-post-post-name-%f0%9f%98%80.php',
'single-post.php',
'single.php',
'singular.php',
)
);
}
public function test_single_template_hierarchy_for_custom_post_type() {
$cpt = self::factory()->post->create_and_get(
array(
'post_type' => 'cpt',
'post_name' => 'cpt-name-😀',
)
);
$this->assertTemplateHierarchy(
get_permalink( $cpt ),
array(
'single-cpt-cpt-name-😀.php',
'single-cpt-cpt-name-%f0%9f%98%80.php',
'single-cpt.php',
'single.php',
'singular.php',
)
);
}
/**
* @ticket 18375
*/
public function test_single_template_hierarchy_for_custom_post_type_with_template() {
$cpt = self::factory()->post->create_and_get(
array(
'post_type' => 'cpt',
'post_name' => 'cpt-name-😀',
)
);
add_post_meta( $cpt->ID, '_wp_page_template', 'templates/cpt.php' );
$this->assertTemplateHierarchy(
get_permalink( $cpt ),
array(
'templates/cpt.php',
'single-cpt-cpt-name-😀.php',
'single-cpt-cpt-name-%f0%9f%98%80.php',
'single-cpt.php',
'single.php',
'singular.php',
)
);
}
public function test_attachment_template_hierarchy() {
$attachment = self::factory()->attachment->create_and_get(
array(
'post_name' => 'attachment-name-😀',
'file' => 'image.jpg',
'post_mime_type' => 'image/jpeg',
)
);
$this->assertTemplateHierarchy(
get_permalink( $attachment ),
array(
'image-jpeg.php',
'jpeg.php',
'image.php',
'attachment.php',
'single-attachment-attachment-name-😀.php',
'single-attachment-attachment-name-%f0%9f%98%80.php',
'single-attachment.php',
'single.php',
'singular.php',
)
);
}
/**
* @ticket 18375
*/
public function test_attachment_template_hierarchy_with_template() {
$attachment = self::factory()->attachment->create_and_get(
array(
'post_name' => 'attachment-name-😀',
'file' => 'image.jpg',
'post_mime_type' => 'image/jpeg',
)
);
add_post_meta( $attachment, '_wp_page_template', 'templates/cpt.php' );
$this->assertTemplateHierarchy(
get_permalink( $attachment ),
array(
'image-jpeg.php',
'jpeg.php',
'image.php',
'attachment.php',
'single-attachment-attachment-name-😀.php',
'single-attachment-attachment-name-%f0%9f%98%80.php',
'single-attachment.php',
'single.php',
'singular.php',
)
);
}
public function test_embed_template_hierarchy_for_post() {
$this->assertTemplateHierarchy(
get_post_embed_url( self::$post ),
array(
'embed-post-quote.php',
'embed-post.php',
'embed.php',
'templates/post.php',
'single-post-post-name-😀.php',
'single-post-post-name-%f0%9f%98%80.php',
'single-post.php',
'single.php',
'singular.php',
)
);
}
public function test_embed_template_hierarchy_for_page() {
$this->assertTemplateHierarchy(
get_post_embed_url( self::$page ),
array(
'embed-page.php',
'embed.php',
'templates/page.php',
'page-page-name-😀.php',
'page-page-name-%f0%9f%98%80.php',
'page-' . self::$page->ID . '.php',
'page.php',
'singular.php',
)
);
}
/**
* @ticket 17851
* @covers ::add_settings_section
*/
public function test_add_settings_section() {
add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page' );
global $wp_settings_sections;
$this->assertIsArray( $wp_settings_sections, 'List of sections is not initialized.' );
$this->assertArrayHasKey( 'test-page', $wp_settings_sections, 'List of sections for the test page has not been added to sections list.' );
$this->assertIsArray( $wp_settings_sections['test-page'], 'List of sections for the test page is not initialized.' );
$this->assertArrayHasKey( 'test-section', $wp_settings_sections['test-page'], 'Test section has not been added to the list of sections for the test page.' );
$this->assertEqualSetsWithIndex(
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '',
'after_section' => '',
'section_class' => '',
),
$wp_settings_sections['test-page']['test-section'],
'Test section data does not match the expected dataset.'
);
}
/**
* @ticket 17851
*
* @param array $extra_args Extra arguments to pass to function `add_settings_section()`.
* @param array $expected_section_data Expected set of section data.
* @param string $expected_before_section_html Expected HTML markup to be rendered before the settings section.
* @param string $expected_after_section_html Expected HTML markup to be rendered after the settings section.
*
* @covers ::add_settings_section
* @covers ::do_settings_sections
*
* @dataProvider data_extra_args_for_add_settings_section
*/
public function test_add_settings_section_with_extra_args( $extra_args, $expected_section_data, $expected_before_section_html, $expected_after_section_html ) {
add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page', $extra_args );
add_settings_field( 'test-field', 'Field title', '__return_false', 'test-page', 'test-section' );
global $wp_settings_sections;
$this->assertIsArray( $wp_settings_sections, 'List of sections is not initialized.' );
$this->assertArrayHasKey( 'test-page', $wp_settings_sections, 'List of sections for the test page has not been added to sections list.' );
$this->assertIsArray( $wp_settings_sections['test-page'], 'List of sections for the test page is not initialized.' );
$this->assertArrayHasKey( 'test-section', $wp_settings_sections['test-page'], 'Test section has not been added to the list of sections for the test page.' );
$this->assertEqualSetsWithIndex(
$expected_section_data,
$wp_settings_sections['test-page']['test-section'],
'Test section data does not match the expected dataset.'
);
ob_start();
do_settings_sections( 'test-page' );
$output = ob_get_clean();
$this->assertStringContainsString( $expected_before_section_html, $output, 'Test page output does not contain the custom markup to be placed before the section.' );
$this->assertStringContainsString( $expected_after_section_html, $output, 'Test page output does not contain the custom markup to be placed after the section.' );
}
/**
* Data provider for `test_add_settings_section_with_extra_args()`.
*
* @return array
*/
public function data_extra_args_for_add_settings_section() {
return array(
'class placeholder section_class present' => array(
array(
'before_section' => '<div class="%s">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => 'test-section-wrap',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="%s">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => 'test-section-wrap',
),
'<div class="test-section-wrap">',
'</div><!-- end of the test section -->',
),
'missing class placeholder section_class' => array(
array(
'before_section' => '<div class="testing-section-wrapper">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => 'test-section-wrap',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="testing-section-wrapper">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => 'test-section-wrap',
),
'<div class="testing-section-wrapper">',
'</div><!-- end of the test section -->',
),
'empty section_class' => array(
array(
'before_section' => '<div class="test-section-container">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => '',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="test-section-container">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => '',
),
'<div class="test-section-container">',
'</div><!-- end of the test section -->',
),
'section_class missing' => array(
array(
'before_section' => '<div class="wp-whitelabel-section">',
'after_section' => '</div><!-- end of the test section -->',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="wp-whitelabel-section">',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => '',
),
'<div class="wp-whitelabel-section">',
'</div><!-- end of the test section -->',
),
'disallowed tag in before_section' => array(
array(
'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/" />',
'after_section' => '</div><!-- end of the test section -->',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/" />',
'after_section' => '</div><!-- end of the test section -->',
'section_class' => '',
),
'<div class="video-settings-section">',
'</div><!-- end of the test section -->',
),
'disallowed tag in after_section' => array(
array(
'before_section' => '<div class="video-settings-section">',
'after_section' => '</div><iframe src="https://www.wordpress.org/" />',
),
array(
'id' => 'test-section',
'title' => 'Section title',
'callback' => '__return_false',
'before_section' => '<div class="video-settings-section">',
'after_section' => '</div><iframe src="https://www.wordpress.org/" />',
'section_class' => '',
),
'<div class="video-settings-section">',
'</div>',
),
);
}
/**
* Tests that `locate_template()` uses the current theme even after switching the theme.
*
* @ticket 18298
*
* @covers ::locate_template
*/
public function test_locate_template_uses_current_theme() {
$themes = wp_get_themes();
// Look for parent themes with an index.php template.
$relevant_themes = array();
foreach ( $themes as $theme ) {
if ( $theme->get_stylesheet() !== $theme->get_template() ) {
continue;
}
$php_templates = $theme['Template Files'];
if ( ! isset( $php_templates['index.php'] ) ) {
continue;
}
$relevant_themes[] = $theme;
}
if ( count( $relevant_themes ) < 2 ) {
$this->markTestSkipped( 'Test requires at least two parent themes with an index.php template.' );
}
$template_names = array( 'index.php' );
$old_theme = $relevant_themes[0];
$new_theme = $relevant_themes[1];
switch_theme( $old_theme->get_stylesheet() );
$this->assertSame( $old_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in initial theme.' );
switch_theme( $new_theme->get_stylesheet() );
$this->assertSame( $new_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in theme after switch.' );
}
public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {
$this->go_to( $url );
$hierarchy = $this->get_template_hierarchy();
$this->assertSame( $expected, $hierarchy, $message );
}
protected static function get_query_template_conditions() {
return array(
'embed' => 'is_embed',
'404' => 'is_404',
'search' => 'is_search',
'front_page' => 'is_front_page',
'home' => 'is_home',
'privacy_policy' => 'is_privacy_policy',
'post_type_archive' => 'is_post_type_archive',
'taxonomy' => 'is_tax',
'attachment' => 'is_attachment',
'single' => 'is_single',
'page' => 'is_page',
'singular' => 'is_singular',
'category' => 'is_category',
'tag' => 'is_tag',
'author' => 'is_author',
'date' => 'is_date',
'archive' => 'is_archive',
'paged' => 'is_paged',
);
}
protected function get_template_hierarchy() {
foreach ( self::get_query_template_conditions() as $type => $condition ) {
if ( call_user_func( $condition ) ) {
$filter = str_replace( '_', '', $type );
add_filter( "{$filter}_template_hierarchy", array( $this, 'log_template_hierarchy' ) );
call_user_func( "get_{$type}_template" );
remove_filter( "{$filter}_template_hierarchy", array( $this, 'log_template_hierarchy' ) );
}
}
$hierarchy = $this->hierarchy;
$this->hierarchy = array();
return $hierarchy;
}
public function log_template_hierarchy( array $hierarchy ) {
$this->hierarchy = array_merge( $this->hierarchy, $hierarchy );
return $hierarchy;
}
}