-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHandler.php
More file actions
142 lines (119 loc) · 4.59 KB
/
Copy pathHandler.php
File metadata and controls
142 lines (119 loc) · 4.59 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
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType\Frontend;
use n5s\PageForCustomPostType\Core\Api;
use WP_Post_Type;
use WP_Query;
/**
* Handles query property modification and template hierarchy for PFCPT pages.
*/
final class Handler
{
public function __construct(
private readonly Api $api
) {
}
/**
* Add conditional properties to WP_Query object.
*
* Two properties are added:
* - is_page_for_custom_post_type (string|false) Either the post type or false
* - is_{$post_type}_page (bool) Whether the current page is a page for the post type
*/
public function withQueryProperties(WP_Query $query): void
{
if (!$query->is_main_query()) {
return;
}
$currentPageId = $this->api->getPageIdFromQuery($query);
if (!$currentPageId) {
return;
}
// Set default conditionals for all post types
foreach ($this->api->getPostTypes() as $postType) {
$query->{$this->api->getConditionalName($postType)} = false;
// @phpstan-ignore property.notFound
$query->{Api::QUERY_VAR_IS_PFCPT} = false;
}
$pageIds = $this->api->getPageIds();
if (!\in_array($currentPageId, $pageIds, true)) {
return;
}
$postType = array_search($currentPageId, $pageIds, true);
if (empty($postType)) {
return;
}
// Defensive fallback: if the CPT is not registered at all this request
// (e.g. the plugin that registers it is deactivated), no
// registered_post_type hook fires, so the cleanup in LifecycleManager
// can't delete the stale option. Bail here to avoid hijacking the page.
$postTypeObject = get_post_type_object($postType);
if (!$postTypeObject instanceof WP_Post_Type || !$this->api->shouldConsiderPostType($postTypeObject)) {
return;
}
// Modify query conditionals
$query->is_singular = false;
$query->is_page = false;
$query->is_home = true;
$query->is_posts_page = true;
$query->{$this->api->getConditionalName($postType)} = true;
// @phpstan-ignore property.notFound
$query->{Api::QUERY_VAR_IS_PFCPT} = $postType;
$query->set('post_type', $postType);
// Prevent WP from mistakenly thinking this is a front page
// when 'posts' is set as show_on_front
// @see https://github.com/WordPress/wordpress-develop/blob/781953641607c4d5b0743a6924af0e820fd54871/src/wp-includes/class-wp-query.php#L4323-L4325
if (get_option('show_on_front') === 'posts') {
add_filter('pre_option_show_on_front', static fn (): null => null);
}
add_filter('home_template_hierarchy', [$this, 'withHomeTemplateHierarchy']);
add_filter('frontpage_template_hierarchy', '__return_empty_array');
add_filter('body_class', [$this, 'filterBodyClass']);
}
/**
* Replace 'blog' body class with 'home' and 'home-for-{post_type}' on PFCPT pages.
*
* WordPress core adds 'blog' when is_home() is true, but that class should
* only apply to the actual page_for_posts. For custom post type pages, we
* replace it with more specific classes.
*
* @param string[] $classes
* @return string[]
*/
public function filterBodyClass(array $classes): array
{
$wpQuery = $GLOBALS['wp_query'] ?? null;
if (!$wpQuery instanceof WP_Query) {
return $classes;
}
$postType = $wpQuery->{Api::QUERY_VAR_IS_PFCPT} ?? null;
if (!\is_string($postType)) {
return $classes;
}
$classes = array_diff($classes, ['blog']);
$classes[] = 'home';
$classes[] = "home-for-{$postType}";
return array_values($classes);
}
/**
* Change the template hierarchy on pages for custom post type.
*
* @param string[] $templates
* @return string[]
*/
public function withHomeTemplateHierarchy(array $templates): array
{
$wpQuery = $GLOBALS['wp_query'] ?? null;
if (!$wpQuery instanceof WP_Query) {
return $templates;
}
$postType = $wpQuery->{Api::QUERY_VAR_IS_PFCPT} ?? null;
if (!\is_string($postType)) {
return $templates;
}
// Match extension format of incoming templates (classic vs block themes)
$first = reset($templates);
$extension = \is_string($first) && str_ends_with($first, '.php') ? '.php' : '';
return array_merge(["home-{$postType}{$extension}"], $templates);
}
}