forked from nlemoine/page-for-custom-post-type
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
executable file
·282 lines (239 loc) · 8.43 KB
/
Copy pathPlugin.php
File metadata and controls
executable file
·282 lines (239 loc) · 8.43 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
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType;
use n5s\PageForCustomPostType\Admin\Admin;
use n5s\PageForCustomPostType\Core\Api;
use n5s\PageForCustomPostType\Core\RewriteManager;
use n5s\PageForCustomPostType\Frontend\Handler;
use n5s\PageForCustomPostType\Frontend\QueryFilter;
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
use n5s\PageForCustomPostType\Integration\Autodescription;
use n5s\PageForCustomPostType\Integration\IntegrationInterface;
use n5s\PageForCustomPostType\Integration\Polylang;
use n5s\PageForCustomPostType\Integration\WordPressSeo;
use n5s\PageForCustomPostType\Integration\Wpml;
use n5s\PageForCustomPostType\Lifecycle\LifecycleManager;
use n5s\PageForCustomPostType\Lifecycle\Migrator;
use n5s\PageForCustomPostType\PostType\PostType;
/**
* Main plugin class - thin orchestrator that wires everything together.
*/
final class Plugin
{
// Legacy constants for backward compatibility
/** @deprecated Use Api::QUERY_VAR_IS_PFCPT instead */
public const QUERY_VAR_IS_PFCPT = Api::QUERY_VAR_IS_PFCPT;
/** @deprecated Use Api::OPTION_PREFIX instead */
public const OPTION_PREFIX = Api::OPTION_PREFIX;
/** @deprecated Use Api::OPTION_PAGE_IDS instead */
public const OPTION_PAGE_IDS = Api::OPTION_PAGE_IDS;
private static ?self $instance = null;
private Container $container;
private bool $initialized = false;
private function __construct()
{
$this->container = new Container();
}
/**
* Get singleton instance.
*/
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the plugin by registering hooks.
*
* This method is separate from the constructor to avoid side effects
* during object instantiation and to make testing easier.
*/
public function init(): self
{
if ($this->initialized) {
return $this;
}
$this->container->get(Migrator::class)->migrate();
$this->registerHooks();
$this->initialized = true;
return $this;
}
/**
* Get the container.
*/
public function getContainer(): Container
{
return $this->container;
}
/**
* Get the API service.
*/
public function getApi(): Api
{
return $this->container->get(Api::class);
}
/**
* Register all WordPress hooks.
*/
private function registerHooks(): void
{
$this->registerAdminHooks();
$this->registerFrontendHooks();
$this->registerCommonHooks();
}
/**
* Register admin-only hooks.
*/
private function registerAdminHooks(): void
{
if (!is_admin()) {
return;
}
$admin = $this->container->get(Admin::class);
add_action('admin_menu', [$admin, 'addPostTypeSubmenus']);
add_action('admin_init', [$admin, 'addReadingSettings']);
add_action('admin_bar_menu', [$admin, 'addAdminBarArchiveLink'], 80);
add_action('enqueue_block_editor_assets', [$admin, 'enqueueBlockEditorAssets']);
add_action('admin_enqueue_scripts', [$admin, 'enqueueQuickEditAssets']);
add_filter('display_post_states', [$admin, 'displayPostStates'], 100, 2);
}
/**
* Register frontend-only hooks.
*/
private function registerFrontendHooks(): void
{
if (is_admin()) {
return;
}
$handler = $this->container->get(Handler::class);
$queryFilter = $this->container->get(QueryFilter::class);
add_action('parse_query', [$handler, 'withQueryProperties'], 1);
add_filter('posts_where', [$queryFilter, 'filterPostsWhere'], 10, 2);
add_filter('wp_nav_menu_objects', [$queryFilter, 'withCurrentAncestor'], 10, 2);
}
/**
* Register hooks that apply to both admin and frontend.
*/
private function registerCommonHooks(): void
{
$lifecycle = $this->container->get(LifecycleManager::class);
$postType = $this->container->get(PostType::class);
// Post type registration hooks
add_filter('register_post_type_args', [$postType, 'updatePostTypeArgs'], 10, 2);
add_action('registered_post_type', [$postType, 'addPaginationRewriteTags'], 10, 2);
// Option lifecycle hooks (watch for each post type)
add_action('registered_post_type', [$lifecycle, 'watchOptions'], 10, 2);
// Post lifecycle hooks
add_action('transition_post_status', [$lifecycle, 'onTransitionPostStatus'], 10, 3);
add_action('delete_post', [$lifecycle, 'onDeletedPost']);
add_action('wp_trash_post', [$lifecycle, 'onDeletedPost']);
add_action('post_updated', [$lifecycle, 'onPageUpdated'], 10, 3);
// Template redirect
add_action('template_redirect', [$this, 'onTemplateRedirect']);
}
/**
* Handle template redirect.
*/
public function onTemplateRedirect(): void
{
$api = $this->container->get(Api::class);
if (!$api->isPageForCustomPostType()) {
return;
}
do_action('pfcpt/template_redirect');
}
/**
* Get all registered integration class names.
*
* @return list<class-string<IntegrationInterface>>
*/
public function getIntegrations(): array
{
return [
AdvancedCustomFields\AdvancedCustomFields::class,
Polylang\Polylang::class,
WordPressSeo\WordPressSeo::class,
Wpml\Wpml::class,
Autodescription\Autodescription::class,
];
}
// =========================================================================
// Legacy compatibility methods (delegate to Api/RewriteManager)
// =========================================================================
/**
* @deprecated Use getApi()->isPageForCustomPostType() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function is_page_for_custom_post_type(?string $postType = null): bool
{
return $this->getApi()->isPageForCustomPostType($postType);
}
/**
* @deprecated Use getApi()->isQueryPageForCustomPostType() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function is_query_page_for_custom_post_type(): bool
{
return $this->getApi()->isQueryPageForCustomPostType();
}
/**
* @deprecated Use getApi()->getPostTypeFromPageId() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function get_post_type_from_page_id(int $pageId): ?string
{
return $this->getApi()->getPostTypeFromPageId($pageId);
}
/**
* @deprecated Use getApi()->getPageIdFromPostType() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function get_page_id_from_post_type(string $postType, bool $applyFilters = true): ?int
{
return $this->getApi()->getPageIdFromPostType($postType, $applyFilters);
}
/**
* @deprecated Use getApi()->getPageIds() instead
*
* @return array<string, int>
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function get_page_ids(bool $applyFilters = true): array
{
return $this->getApi()->getPageIds($applyFilters);
}
/**
* @deprecated Use getApi()->getOptionName() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function get_option_name(string $postType): string
{
return $this->getApi()->getOptionName($postType);
}
/**
* @deprecated Use RewriteManager::getPageSlug() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function get_page_slug(int $pageId): ?string
{
return $this->container->get(RewriteManager::class)->getPageSlug($pageId);
}
/**
* @deprecated Use RewriteManager::flushRewriteRules() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function flush_rewrite_rules(string $postType): void
{
$this->container->get(RewriteManager::class)->flushRewriteRules($postType);
}
/**
* @deprecated Use getInstance() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public static function get_instance(): self
{
return self::getInstance();
}
}