-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAdmin.php
More file actions
477 lines (399 loc) · 15.4 KB
/
Copy pathAdmin.php
File metadata and controls
477 lines (399 loc) · 15.4 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
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType\Admin;
use n5s\PageForCustomPostType\Core\Api;
use n5s\PageForCustomPostType\PostType\PostType;
use WP_Admin_Bar;
use WP_Post;
use WP_Post_Type;
use WP_Screen;
/**
* Handles admin UI: settings, menus, and post states.
*/
final class Admin
{
public function __construct(
private readonly Api $api,
private readonly PostType $postType
) {
}
/**
* Enqueue the Quick Edit warning on the pages list screen when at least
* one page is assigned to a CPT with `use_slug` enabled.
*
* The script wraps `inlineEditPost.save` to surface a `confirm()` dialog
* if the user changes the slug of a protected page via Quick Edit.
*/
public function enqueueQuickEditAssets(string $hook): void
{
if ($hook !== 'edit.php') {
return;
}
$screen = get_current_screen();
if (!$screen instanceof WP_Screen || $screen->post_type !== 'page') {
return;
}
$protected = $this->getProtectedPages();
if (empty($protected)) {
return;
}
$pluginRoot = \dirname(__DIR__, 2);
$assetFile = $pluginRoot . '/build/admin/quick-edit-warning/index.asset.php';
$asset = file_exists($assetFile)
? require $assetFile
: ['dependencies' => [], 'version' => false];
$deps = \is_array($asset['dependencies'] ?? null) ? $asset['dependencies'] : [];
if (!\in_array('inline-edit-post', $deps, true)) {
$deps[] = 'inline-edit-post';
}
wp_enqueue_script(
'pfcpt-quick-edit-warning',
plugins_url('build/admin/quick-edit-warning/index.js', $pluginRoot . '/plugin.php'),
$deps,
\is_string($asset['version'] ?? null) ? $asset['version'] : false,
true
);
wp_add_inline_script(
'pfcpt-quick-edit-warning',
'const pfcptQuickEdit = ' . wp_json_encode(['protectedPages' => $protected]) . ';',
'before'
);
wp_set_script_translations('pfcpt-quick-edit-warning', 'pfcpt', $pluginRoot . '/languages');
}
/**
* Build the map of pages that should trigger the slug-change warning.
*
* @return array<int, string> page ID => plural CPT label
*/
private function getProtectedPages(): array
{
$protected = [];
foreach ($this->api->getPageIds() as $cpt => $pageId) {
if (!$this->api->shouldUsePageSlug($cpt)) {
continue;
}
$obj = get_post_type_object($cpt);
if (!$obj instanceof WP_Post_Type) {
continue;
}
$label = \is_string($obj->labels->name) ? $obj->labels->name : $cpt;
$protected[(int) $pageId] = $label;
}
return $protected;
}
/**
* Enqueue the block editor warning when editing a page that's used as
* the URL base for a custom post type.
*
* Only enqueues when all conditions match:
* - editing a published or draft page (post_type === 'page')
* - the page is assigned to a CPT in the page-for-CPT mapping
* - the matching `_use_slug` option is enabled
* - the page has a non-empty slug (skip new drafts)
*/
public function enqueueBlockEditorAssets(): void
{
$post = get_post();
if (!$post instanceof WP_Post || $post->post_type !== 'page') {
return;
}
if ($post->post_name === '') {
return;
}
$cpt = $this->api->getPostTypeFromPageId($post->ID);
if ($cpt === null) {
return;
}
if (!$this->api->shouldUsePageSlug($cpt)) {
return;
}
$pluginRoot = \dirname(__DIR__, 2);
$assetFile = $pluginRoot . '/build/editor/slug-warning/index.asset.php';
$asset = file_exists($assetFile)
? require $assetFile
: ['dependencies' => [], 'version' => false];
wp_enqueue_script(
'pfcpt-slug-warning',
plugins_url('build/editor/slug-warning/index.js', $pluginRoot . '/plugin.php'),
\is_array($asset['dependencies'] ?? null) ? $asset['dependencies'] : [],
\is_string($asset['version'] ?? null) ? $asset['version'] : false,
true
);
$postTypeObject = get_post_type_object($cpt);
$label = $postTypeObject instanceof WP_Post_Type && \is_string($postTypeObject->labels->name)
? $postTypeObject->labels->name
: $cpt;
wp_add_inline_script(
'pfcpt-slug-warning',
'const pfcptSlugWarning = ' . wp_json_encode([
'postTypeLabel' => $label,
'postTypeName' => $cpt,
]) . ';',
'before'
);
wp_set_script_translations('pfcpt-slug-warning', 'pfcpt', $pluginRoot . '/languages');
}
/**
* Add an indicator to show if a page is set as a post type archive.
*
* @param string[] $postStates
* @return string[]
*/
public function displayPostStates(array $postStates, WP_Post $post): array
{
if ($post->post_type !== 'page') {
return $postStates;
}
$postType = $this->api->getPostTypeFromPageId($post->ID);
if (!$postType) {
return $postStates;
}
$postTypeObject = get_post_type_object($postType);
if (!$postTypeObject) {
return $postStates;
}
$name = $this->api->getOptionName($postType);
$labelName = \is_string($postTypeObject->labels->name) ? $postTypeObject->labels->name : $postType;
/* translators: %s: post type name */
$postStates[$name] = esc_html(\sprintf(__('%s page', 'pfcpt'), $labelName));
return $postStates;
}
/**
* Add archive link to admin bar.
*/
public function addAdminBarArchiveLink(WP_Admin_Bar $adminBar): void
{
$currentScreen = get_current_screen();
if (!$currentScreen || $currentScreen->base !== 'edit') {
return;
}
$postTypeObject = get_post_type_object($currentScreen->post_type);
if (!$postTypeObject) {
return;
}
if (!$postTypeObject->public || !$postTypeObject->show_in_admin_bar) {
return;
}
$archiveUrl = \n5s\PageForCustomPostType\get_page_url_for_custom_post_type($postTypeObject->name);
if (!$archiveUrl) {
return;
}
$adminBar->add_menu([
'id' => 'archive',
'title' => $postTypeObject->labels->view_items,
'href' => $archiveUrl,
'meta' => [
'target' => '_blank',
],
]);
}
/**
* Add submenu link to archive under each post type.
*/
public function addPostTypeSubmenus(): void
{
$pageId = get_option('page_for_posts');
$postTypeObject = get_post_type_object('post');
if ($postTypeObject && $pageId) {
$postType = 'post';
$editPostLink = get_edit_post_link($pageId);
if ($editPostLink) {
$archivesLabel = \is_string($postTypeObject->labels->archives) ? $postTypeObject->labels->archives : $postType;
add_submenu_page(
'edit.php',
$archivesLabel,
$archivesLabel,
'edit_pages',
$editPostLink
);
}
}
foreach ($this->api->getPageIds() as $postType => $pageId) {
$postTypeObject = get_post_type_object($postType);
if (!$postTypeObject) {
continue;
}
$editPostLink = get_edit_post_link($pageId);
if (!$editPostLink) {
continue;
}
$archivesLabel = \is_string($postTypeObject->labels->archives) ? $postTypeObject->labels->archives : $postType;
add_submenu_page(
'edit.php?post_type=' . $postType,
$archivesLabel,
$archivesLabel,
'edit_pages',
$editPostLink
);
}
}
/**
* Add option to Settings > Reading.
*/
public function addReadingSettings(): void
{
$postTypes = $this->api->getPostTypes();
add_settings_section(
'page_for_custom_post_type',
__('Pages for post type', 'pfcpt'),
'__return_false',
'reading'
);
foreach ($postTypes as $postTypeObj) {
$fieldId = $this->api->getOptionName($postTypeObj);
$useSlugFieldId = $this->api->getUseSlugOptionName($postTypeObj);
$value = $this->api->getPageIdFromPostType($postTypeObj->name);
$useSlugValue = $this->api->shouldUsePageSlug($postTypeObj->name);
register_setting('reading', $fieldId, [
'type' => 'integer',
]);
register_setting('reading', $useSlugFieldId, [
'type' => 'boolean',
'default' => false,
]);
$labelName = \is_string($postTypeObj->labels->name) ? $postTypeObj->labels->name : $postTypeObj->name;
add_settings_field(
$fieldId,
$labelName,
$this->renderPageDropdown(...),
'reading',
'page_for_custom_post_type',
[
'name' => $fieldId,
'useSlugName' => $useSlugFieldId,
'postType' => $postTypeObj,
'value' => $value,
'useSlugValue' => $useSlugValue,
'label_for' => $fieldId . '_dropdown',
]
);
}
}
/**
* Render the dropdown for selecting a page.
*
* @param array{name: string, useSlugName: string, postType: WP_Post_Type, value: mixed, useSlugValue: mixed, label_for: string} $args
*/
private function renderPageDropdown(array $args): void
{
$value = is_numeric($args['value']) ? (int) $args['value'] : 0;
$useSlugValue = (bool) $args['useSlugValue'];
$postTypeName = $args['postType']->name;
$defaultLabel = $this->getDefaultLabel($postTypeName);
/** @var array{name?: string, id?: string, selected?: int|string, show_option_none?: string, exclude?: int[], echo?: bool|int} $dropdownArgs */
$dropdownArgs = apply_filters('pfcpt/dropdown_page_args', [
'name' => esc_attr($args['name']),
'id' => esc_attr($args['name'] . '_dropdown'),
'selected' => $value,
'show_option_none' => $defaultLabel ?? __('— Select —', 'pfcpt'),
'exclude' => $this->getExcludedPageIds(),
]);
if (!\is_array($dropdownArgs)) {
return;
}
$dropdownArgs['echo'] = false;
$dropdownId = esc_attr($args['name'] . '_dropdown');
$checkboxWrapperId = esc_attr($args['name'] . '_use_slug_wrapper');
$appendSlug = static function (string $title, \WP_Post $page): string {
$permalink = get_permalink($page->ID);
if ($permalink === false) {
return $title;
}
return $title . ' (' . wp_make_link_relative($permalink) . ')';
};
add_filter('list_pages', $appendSlug, 10, 2);
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_dropdown_pages() returns pre-escaped HTML.
$dropdown = wp_dropdown_pages($dropdownArgs);
remove_filter('list_pages', $appendSlug);
?>
<?= $dropdown; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div id="<?= esc_attr($checkboxWrapperId); ?>" style="margin-top: 0.5em;<?= $value > 0 ? '' : ' display: none;'; ?>">
<label>
<input
type="checkbox"
name="<?= esc_attr($args['useSlugName']); ?>"
value="1"
<?php checked($useSlugValue); ?>
/>
<?php
printf(
/* translators: %s: plural post type name */
esc_html__('Selected page slug replaces default "%s" post type slug', 'pfcpt'),
esc_html(mb_strtolower(\is_string($args['postType']->labels->name) ? $args['postType']->labels->name : $args['postType']->name))
);
?>
</label>
<p class="description">
⚠️
<?php
printf(
/* translators: %s: plural post type name */
esc_html__('Changing this option will alter all single "%s" URLs. This may affect SEO and existing links.', 'pfcpt'),
esc_html(mb_strtolower(\is_string($args['postType']->labels->name) ? $args['postType']->labels->name : $args['postType']->name))
);
?>
</p>
</div>
<script>
(function() {
var dropdown = document.getElementById('<?= esc_js($dropdownId); ?>');
var checkboxWrapper = document.getElementById('<?= esc_js($checkboxWrapperId); ?>');
dropdown.addEventListener('change', function() {
checkboxWrapper.style.display = dropdown.value > 0 ? '' : 'none';
});
})();
</script>
<?php
}
/**
* Get page IDs to exclude from the dropdown.
*
* Excludes the static front page and page for posts.
*
* @return int[]
*/
private function getExcludedPageIds(): array
{
$excluded = [];
$frontPageOption = get_option('page_on_front');
$frontPageId = is_numeric($frontPageOption) ? (int) $frontPageOption : 0;
if ($frontPageId > 0) {
$excluded[] = $frontPageId;
}
$postsPageOption = get_option('page_for_posts');
$postsPageId = is_numeric($postsPageOption) ? (int) $postsPageOption : 0;
if ($postsPageId > 0) {
$excluded[] = $postsPageId;
}
return $excluded;
}
/**
* Get the default label for the dropdown based on original post type args.
*/
private function getDefaultLabel(string $postTypeName): ?string
{
$originalArgs = $this->postType->getOriginalArgs($postTypeName);
if (empty($originalArgs)) {
return null;
}
// Read has_archive directly from args (don't rely on WP_Post_Type defaults)
$hasArchive = $originalArgs['has_archive'] ?? false;
if (!$hasArchive) {
return __('— No archive —', 'pfcpt');
}
global $wp_rewrite;
if (!$wp_rewrite instanceof \WP_Rewrite) {
return null;
}
// Get rewrite slug from args
$rewriteArgs = \is_array($originalArgs['rewrite'] ?? null) ? $originalArgs['rewrite'] : [];
$rewriteSlug = \is_string($rewriteArgs['slug'] ?? null) ? $rewriteArgs['slug'] : $postTypeName;
$archiveSlug = $hasArchive === true ? $rewriteSlug : (\is_string($hasArchive) ? $hasArchive : $postTypeName);
$prefix = $wp_rewrite->root;
if (!empty($rewriteArgs['with_front'])) {
$prefix = substr($wp_rewrite->front, 1);
}
/* translators: %s: archive slug */
return \sprintf(__('— Default (/%s/) —', 'pfcpt'), $prefix . $archiveSlug);
}
}