-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
theme.php
597 lines (505 loc) · 18.1 KB
/
theme.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
<?php declare(strict_types = 1);
/**
* Template and theme collector.
*
* @package query-monitor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @extends QM_DataCollector<QM_Data_Theme>
*/
class QM_Collector_Theme extends QM_DataCollector {
/**
* @var string
*/
public $id = 'response';
/**
* @var bool
*/
protected $got_theme_compat = false;
/**
* @var array<int, mixed>
*/
protected $requested_template_parts = array();
/**
* @var array<int, mixed>
*/
protected $requested_template_part_posts = array();
/**
* @var array<int, mixed>
*/
protected $requested_template_part_files = array();
/**
* @var array<int, mixed>
*/
protected $requested_template_part_nopes = array();
/**
* @var ?WP_Block_Template
*/
protected $block_template = null;
public function get_storage(): QM_Data {
return new QM_Data_Theme();
}
/**
* @return void
*/
public function set_up() {
parent::set_up();
add_filter( 'body_class', array( $this, 'filter_body_class' ), 9999 );
add_filter( 'timber/output', array( $this, 'filter_timber_output' ), 9999, 3 );
add_action( 'template_redirect', array( $this, 'action_template_redirect' ) );
add_action( 'get_template_part', array( $this, 'action_get_template_part' ), 10, 3 );
add_action( 'get_header', array( $this, 'action_get_position' ) );
add_action( 'get_sidebar', array( $this, 'action_get_position' ) );
add_action( 'get_footer', array( $this, 'action_get_position' ) );
add_action( 'render_block_core_template_part_post', array( $this, 'action_render_block_core_template_part_post' ), 10, 3 );
add_action( 'render_block_core_template_part_file', array( $this, 'action_render_block_core_template_part_file' ), 10, 3 );
add_action( 'render_block_core_template_part_none', array( $this, 'action_render_block_core_template_part_none' ), 10, 3 );
add_action( 'gutenberg_render_block_core_template_part_post', array( $this, 'action_render_block_core_template_part_post' ), 10, 3 );
add_action( 'gutenberg_render_block_core_template_part_file', array( $this, 'action_render_block_core_template_part_file' ), 10, 3 );
add_action( 'gutenberg_render_block_core_template_part_none', array( $this, 'action_render_block_core_template_part_none' ), 10, 3 );
}
/**
* @return void
*/
public function tear_down() {
remove_filter( 'body_class', array( $this, 'filter_body_class' ), 9999 );
remove_filter( 'timber/output', array( $this, 'filter_timber_output' ), 9999 );
remove_action( 'template_redirect', array( $this, 'action_template_redirect' ) );
remove_action( 'get_template_part', array( $this, 'action_get_template_part' ), 10 );
remove_action( 'get_header', array( $this, 'action_get_position' ) );
remove_action( 'get_sidebar', array( $this, 'action_get_position' ) );
remove_action( 'get_footer', array( $this, 'action_get_position' ) );
remove_action( 'render_block_core_template_part_post', array( $this, 'action_render_block_core_template_part_post' ), 10 );
remove_action( 'render_block_core_template_part_file', array( $this, 'action_render_block_core_template_part_file' ), 10 );
remove_action( 'render_block_core_template_part_none', array( $this, 'action_render_block_core_template_part_none' ), 10 );
remove_action( 'gutenberg_render_block_core_template_part_post', array( $this, 'action_render_block_core_template_part_post' ), 10 );
remove_action( 'gutenberg_render_block_core_template_part_file', array( $this, 'action_render_block_core_template_part_file' ), 10 );
remove_action( 'gutenberg_render_block_core_template_part_none', array( $this, 'action_render_block_core_template_part_none' ), 10 );
parent::tear_down();
}
/**
* Fires before the header/sidebar/footer template file is loaded.
*
* @param string|null $name Name of the specific file to use. Null for the default.
* @return void
*/
public function action_get_position( $name ) {
$filter = current_filter();
$trace = new QM_Backtrace( array(
'ignore_hook' => array(
$filter => true,
),
) );
$position = str_replace( 'get_', '', $filter );
$templates = array();
if ( '' !== (string) $name ) {
$templates[] = "{$position}-{$name}.php";
}
$templates[] = "{$position}.php";
$data = array(
'slug' => $position,
'name' => $name,
'templates' => $templates,
'caller' => $trace->get_caller(),
);
$this->requested_template_parts[] = $data;
}
/**
* @return array<int, string>
*/
public function get_concerned_actions() {
return array(
'template_redirect',
);
}
/**
* @return array<int, string>
*/
public function get_concerned_filters() {
$filters = array(
'stylesheet',
'stylesheet_directory',
'template',
'template_directory',
'template_include',
);
foreach ( self::get_query_filter_names() as $filter ) {
$filters[] = $filter;
$filters[] = "{$filter}_hierarchy";
}
return $filters;
}
/**
* @return array<int, string>
*/
public function get_concerned_options() {
return array(
'stylesheet',
'template',
);
}
/**
* @return array<int|string, string>
*/
public static function get_query_template_names() {
$names = array();
$names['embed'] = 'is_embed';
$names['404'] = 'is_404';
$names['search'] = 'is_search';
$names['front_page'] = 'is_front_page';
$names['home'] = 'is_home';
$names['privacy_policy'] = 'is_privacy_policy';
$names['post_type_archive'] = 'is_post_type_archive';
$names['taxonomy'] = 'is_tax';
$names['attachment'] = 'is_attachment';
$names['single'] = 'is_single';
$names['page'] = 'is_page';
$names['singular'] = 'is_singular';
$names['category'] = 'is_category';
$names['tag'] = 'is_tag';
$names['author'] = 'is_author';
$names['date'] = 'is_date';
$names['archive'] = 'is_archive';
$names['index'] = '__return_true';
return $names;
}
/**
* @return array<int|string, string>
*/
public static function get_query_filter_names() {
$names = array();
$names['embed'] = 'embed_template';
$names['404'] = '404_template';
$names['search'] = 'search_template';
$names['front_page'] = 'frontpage_template';
$names['home'] = 'home_template';
$names['privacy_policy'] = 'privacypolicy_template';
$names['taxonomy'] = 'taxonomy_template';
$names['attachment'] = 'attachment_template';
$names['single'] = 'single_template';
$names['page'] = 'page_template';
$names['singular'] = 'singular_template';
$names['category'] = 'category_template';
$names['tag'] = 'tag_template';
$names['author'] = 'author_template';
$names['date'] = 'date_template';
$names['archive'] = 'archive_template';
$names['index'] = 'index_template';
return $names;
}
/**
* @return void
*/
public function action_template_redirect() {
add_filter( 'template_include', array( $this, 'filter_template_include' ), PHP_INT_MAX );
foreach ( self::get_query_template_names() as $template => $conditional ) {
// If a matching theme-compat file is found, further conditional checks won't occur in template-loader.php
if ( $this->got_theme_compat ) {
break;
}
$get_template = "get_{$template}_template";
if ( function_exists( $conditional ) && function_exists( $get_template ) && call_user_func( $conditional ) ) {
$filter = str_replace( '_', '', "{$template}" );
add_filter( "{$filter}_template_hierarchy", array( $this, 'filter_template_hierarchy' ), PHP_INT_MAX );
add_filter( "{$filter}_template", array( $this, 'filter_template' ), PHP_INT_MAX, 3 );
call_user_func( $get_template );
remove_filter( "{$filter}_template_hierarchy", array( $this, 'filter_template_hierarchy' ), PHP_INT_MAX );
remove_filter( "{$filter}_template", array( $this, 'filter_template' ), PHP_INT_MAX );
}
}
}
/**
* Fires before a template part is loaded.
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialized template or an empty
* string if there is none.
* @param array<int, string> $templates Array of template files to search for, in order.
* @return void
*/
public function action_get_template_part( $slug, $name, $templates ) {
$part = compact( 'slug', 'name', 'templates' );
$trace = new QM_Backtrace( array(
'ignore_hook' => array(
current_filter() => true,
),
) );
$part['caller'] = $trace->get_caller();
$this->requested_template_parts[] = $part;
}
/**
* Fires when a post is loaded for a template part block.
*
* @param string $template_part_id
* @param mixed[] $attributes
* @param WP_Post $post
* @return void
*/
public function action_render_block_core_template_part_post( $template_part_id, $attributes, WP_Post $post ) {
$part = array(
'id' => $template_part_id,
'attributes' => $attributes,
'post' => $post->ID,
);
$this->requested_template_part_posts[] = $part;
}
/**
* Fires when a file is loaded for a template part block.
*
* @param string $template_part_id
* @param mixed[] $attributes
* @param string $template_part_file_path
* @return void
*/
public function action_render_block_core_template_part_file( $template_part_id, $attributes, $template_part_file_path ) {
$part = array(
'id' => $template_part_id,
'attributes' => $attributes,
'path' => $template_part_file_path,
);
$this->requested_template_part_files[] = $part;
}
/**
* Fires when neither a post nor file is found for a template part block.
*
* @param string $template_part_id
* @param mixed[] $attributes
* @param string $template_part_file_path
* @return void
*/
public function action_render_block_core_template_part_none( $template_part_id, $attributes, $template_part_file_path ) {
$part = array(
'id' => $template_part_id,
'attributes' => $attributes,
'path' => $template_part_file_path,
);
$this->requested_template_part_nopes[] = $part;
}
/**
* @param array<int, string> $templates
* @return array<int, string>
*/
public function filter_template_hierarchy( array $templates ) {
if ( ! isset( $this->data->template_hierarchy ) ) {
$this->data->template_hierarchy = array();
}
foreach ( $templates as $template_name ) {
if ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$this->got_theme_compat = true;
break;
}
}
if ( self::wp_is_block_theme() ) {
$block_theme_folders = self::wp_get_block_theme_folders();
foreach ( $templates as $template ) {
if ( str_ends_with( $template, '.php' ) ) {
// Standard PHP template, inject the HTML version:
$this->data->template_hierarchy[] = $block_theme_folders['wp_template'] . '/' . str_replace( '.php', '.html', $template );
$this->data->template_hierarchy[] = $template;
} else {
// Block theme custom template (eg. from `customTemplates` in theme.json), doesn't have a suffix:
$this->data->template_hierarchy[] = $block_theme_folders['wp_template'] . '/' . $template . '.html';
}
}
} else {
$this->data->template_hierarchy = array_merge( $this->data->template_hierarchy, $templates );
}
return $templates;
}
/**
* @param string $template Path to the template. See locate_template().
* @param string $type Sanitized filename without extension.
* @param array<int, string> $templates A list of template candidates, in descending order of priority.
* @return string Full path to template file.
*/
public function filter_template( $template, $type, $templates ) {
if ( $this->data->block_template instanceof \WP_Block_Template ) {
return $template;
}
$block_template = self::wp_resolve_block_template( $type, $templates, $template );
if ( $block_template ) {
$this->data->block_template = $block_template;
}
return $template;
}
/**
* @param array<int, string> $class
* @return array<int, string>
*/
public function filter_body_class( array $class ) {
$this->data->body_class = $class;
return $class;
}
/**
* @param string $template_path
* @return string
*/
public function filter_template_include( $template_path ) {
$this->data->template_path = $template_path;
return $template_path;
}
/**
* @param mixed[] $output
* @param mixed $data
* @param string $file
* @return mixed[]
*/
public function filter_timber_output( $output, $data = null, $file = null ) {
if ( $file ) {
$this->data->timber_files[] = $file;
}
return $output;
}
/**
* @return void
*/
public function process() {
$stylesheet_directory = QM_Util::standard_dir( get_stylesheet_directory() );
$template_directory = QM_Util::standard_dir( get_template_directory() );
$theme_directory = QM_Util::standard_dir( get_theme_root() );
if ( isset( $this->data->template_hierarchy ) ) {
$this->data->template_hierarchy = array_unique( $this->data->template_hierarchy );
}
if ( ! empty( $this->requested_template_parts ) ) {
$this->data->template_parts = array();
$this->data->theme_template_parts = array();
$this->data->count_template_parts = array();
foreach ( $this->requested_template_parts as $part ) {
$file = locate_template( $part['templates'] );
if ( ! $file ) {
$this->data->unsuccessful_template_parts[] = $part;
continue;
}
$file = QM_Util::standard_dir( $file );
if ( isset( $this->data->count_template_parts[ $file ] ) ) {
$this->data->count_template_parts[ $file ]++;
continue;
}
$this->data->count_template_parts[ $file ] = 1;
$filename = str_replace( array(
$stylesheet_directory,
$template_directory,
), '', $file );
$display = trim( $filename, '/' );
$theme_display = trim( str_replace( $theme_directory, '', $file ), '/' );
$this->data->template_parts[ $file ] = $display;
$this->data->theme_template_parts[ $file ] = $theme_display;
}
}
if (
! empty( $this->requested_template_part_posts ) ||
! empty( $this->requested_template_part_files ) ||
! empty( $this->requested_template_part_nopes )
) {
$this->data->template_parts = array();
$this->data->theme_template_parts = array();
$this->data->count_template_parts = array();
$posts = ! empty( $this->requested_template_part_posts ) ? $this->requested_template_part_posts : array();
$files = ! empty( $this->requested_template_part_files ) ? $this->requested_template_part_files : array();
$nopes = ! empty( $this->requested_template_part_nopes ) ? $this->requested_template_part_nopes : array();
$all = array_merge( $posts, $files, $nopes );
foreach ( $all as $part ) {
$file = $part['path'] ?? $part['post'];
if ( isset( $this->data->count_template_parts[ $file ] ) ) {
$this->data->count_template_parts[ $file ]++;
continue;
}
$this->data->count_template_parts[ $file ] = 1;
if ( isset( $part['post'] ) ) {
$display = $part['id'];
$theme_display = $display;
} else {
$file = QM_Util::standard_dir( $file );
$filename = str_replace( array(
$stylesheet_directory,
$template_directory,
), '', $file );
$display = trim( $filename, '/' );
$theme_display = trim( str_replace( $theme_directory, '', $file ), '/' );
}
$this->data->template_parts[ $file ] = $display;
$this->data->theme_template_parts[ $file ] = $theme_display;
}
}
if ( ! empty( $this->data->template_path ) ) {
$template_path = QM_Util::standard_dir( $this->data->template_path );
$template_file = str_replace( array( $stylesheet_directory, $template_directory, ABSPATH ), '', $template_path );
$template_file = ltrim( $template_file, '/' );
$theme_template_file = str_replace( array( $theme_directory, ABSPATH ), '', $template_path );
$theme_template_file = ltrim( $theme_template_file, '/' );
$this->data->template_path = $template_path;
$this->data->template_file = $template_file;
$this->data->theme_template_file = $theme_template_file;
}
$this->data->stylesheet = get_stylesheet();
$this->data->template = get_template();
$this->data->is_child_theme = ( $this->data->stylesheet !== $this->data->template );
$this->data->theme_dirs = array(
$this->data->stylesheet => $stylesheet_directory,
$this->data->template => $template_directory,
);
$this->data->theme_folders = self::wp_get_block_theme_folders();
$stylesheet_theme_json = $stylesheet_directory . '/theme.json';
$template_theme_json = $template_directory . '/theme.json';
if ( is_readable( $stylesheet_theme_json ) ) {
$this->data->stylesheet_theme_json = $stylesheet_theme_json;
}
if ( is_readable( $template_theme_json ) ) {
$this->data->template_theme_json = $template_theme_json;
}
if ( isset( $this->data->body_class ) ) {
asort( $this->data->body_class );
}
}
/**
* @return bool
*/
protected static function wp_is_block_theme() {
// WP 5.9
return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
}
/**
* @return array<string, string>
*/
protected static function wp_get_block_theme_folders() {
// WP 5.9
if ( ! function_exists( 'get_block_theme_folders' ) ) {
return array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
);
}
return get_block_theme_folders();
}
/**
* @param string $template_type The current template type.
* @param array<int, string> $template_hierarchy The current template hierarchy, ordered by priority.
* @param string $fallback_template A PHP fallback template to use if no matching block template is found.
* @return WP_Block_Template|null template A template object, or null if none could be found.
*/
protected static function wp_resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) {
// WP 5.8
if ( ! function_exists( 'resolve_block_template' ) ) {
return null;
}
if ( ! current_theme_supports( 'block-templates' ) ) {
return null;
}
return resolve_block_template( $template_type, $template_hierarchy, $fallback_template );
}
}
/**
* @param array<string, QM_Collector> $collectors
* @param QueryMonitor $qm
* @return array<string, QM_Collector>
*/
function register_qm_collector_theme( array $collectors, QueryMonitor $qm ) {
$collectors['response'] = new QM_Collector_Theme();
return $collectors;
}
if ( ! is_admin() ) {
add_filter( 'qm/collectors', 'register_qm_collector_theme', 10, 2 );
}