Repository navigation
Expand file tree
/
Copy pathview-config.php
More file actions
756 lines (708 loc) · 18.6 KB
/
Copy pathview-config.php
File metadata and controls
756 lines (708 loc) · 18.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
<?php
/**
* Entity view configuration API.
*
* Builds the default view configuration for an entity and exposes it through
* the dynamic `get_entity_view_config_{$kind}_{$name}` filter so core and third
* parties can provide the configuration for a specific entity. The dynamic
* portions of the hook name are lowercased, e.g.
* `get_entity_view_config_posttype_page` for the `page` post type.
*
* @package WordPress
* @since 7.1.0
*/
/**
* Builds the name of the dynamic filter that provides the view configuration
* for an entity.
*
* The entity kind and name are embedded in the hook name lowercased, so the
* hook follows the WordPress convention of lowercase hook names regardless of
* how the entity identifiers are spelled: the `postType`/`page` entity maps to
* the `get_entity_view_config_posttype_page` hook.
*
* @since 7.1.0
*
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @return string The filter name.
*/
function wp_get_entity_view_config_hook_name( $kind, $name ) {
return strtolower( "get_entity_view_config_{$kind}_{$name}" );
}
/**
* Builds the default `form` configuration for post types that don't provide their own.
*
* It is a sensible default for `post`, `page`, and custom post types alike rather
* than being tailored per type. Post types that need a different shape can replace
* it entirely with a dedicated `form` through their own filter callback.
*
* It is intentionally NOT gated by `supports`. The registered fields are the
* single source of truth for what applies: each field is registered for a post
* type based on its `supports` (and related flags such as `theme_supports`), and
* the editor drops any form field whose definition is absent or whose `isVisible`
* returns `false`.
*
* @since 7.1.0
*
* @return array The default form configuration.
*/
function _wp_get_default_posttype_form() {
return array(
'layout' => array( 'type' => 'panel' ),
'fields' => array(
array(
'id' => 'featured_media',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
array(
'id' => 'post-content-info',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
array(
'id' => 'excerpt',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'top',
),
),
array(
'id' => 'status',
'label' => __( 'Status' ),
'children' => array(
array(
'id' => 'status',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
'scheduled_date',
'password',
'sticky',
),
),
'date',
'slug',
'author',
'template',
array(
'id' => 'discussion',
'label' => __( 'Discussion' ),
'children' => array(
array(
'id' => 'comment_status',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
'ping_status',
),
),
'parent',
'format',
'revisions',
),
);
}
/**
* Returns the view configuration for the given entity.
*
* Builds the default configuration shared by all entities and then exposes it
* through the dynamic `get_entity_view_config_{$kind}_{$name}` filter — with the
* dynamic portions lowercased, see wp_get_entity_view_config_hook_name()
* — so that core and third parties can provide the configuration for a
* specific entity.
*
* @since 7.1.0
*
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @return array {
* The view configuration for the entity.
*
* @type array $default_view Default view configuration.
* @type array $default_layouts Default layouts configuration.
* @type array $view_list List of available views.
* @type array $form Form configuration.
* }
*/
function wp_get_entity_view_config( $kind, $name ) {
$default_view = array(
'type' => 'table',
'filters' => array(),
'sort' => array(
'field' => 'title',
'direction' => 'asc',
),
'perPage' => 20,
'fields' => array( 'author', 'status' ),
'titleField' => 'title',
);
$default_layouts = array(
'table' => array(),
'grid' => array(),
'list' => array(),
);
$all_items_title = __( 'All items' );
if ( 'postType' === $kind ) {
$post_type_object = get_post_type_object( $name );
if ( $post_type_object && ! empty( $post_type_object->labels->all_items ) ) {
$all_items_title = $post_type_object->labels->all_items;
}
}
$view_list = array(
array(
'title' => $all_items_title,
'slug' => 'all',
),
);
$config = array(
'default_view' => $default_view,
'default_layouts' => $default_layouts,
'view_list' => $view_list,
'form' => 'postType' === $kind ? _wp_get_default_posttype_form() : array(),
);
$data = new WP_View_Config_Data( $config );
return $data->apply_filters( $kind, $name );
}
/**
* Provides the view configuration for the `page` post type.
*
* @since 7.1.0
*
* @param WP_View_Config_Data $data The view configuration container for the entity.
* @return WP_View_Config_Data The updated view configuration container.
*/
function _wp_get_entity_view_config_posttype_page( $data ) {
$default_layouts = array(
'table' => array(
'layout' => array(
'styles' => array(
'author' => array(
'align' => 'start',
),
),
),
),
'grid' => array(),
'list' => array(),
);
$default_view = array(
'type' => 'list',
'filters' => array(),
'perPage' => 20,
'sort' => array(
'field' => 'title',
'direction' => 'asc',
),
'showLevels' => true,
'titleField' => 'title',
'mediaField' => 'featured_media',
'fields' => array( 'author', 'status' ),
);
$view_list = array(
array(
'title' => __( 'Published' ),
'slug' => 'published',
'view' => array(
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'publish',
'isLocked' => true,
),
),
),
),
array(
'title' => __( 'Scheduled' ),
'slug' => 'future',
'view' => array(
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'future',
'isLocked' => true,
),
),
),
),
array(
'title' => __( 'Drafts' ),
'slug' => 'drafts',
'view' => array(
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'draft',
'isLocked' => true,
),
),
),
),
array(
'title' => __( 'Pending' ),
'slug' => 'pending',
'view' => array(
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'pending',
'isLocked' => true,
),
),
),
),
array(
'title' => __( 'Private' ),
'slug' => 'private',
'view' => array(
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'private',
'isLocked' => true,
),
),
),
),
array(
'title' => __( 'Trash' ),
'slug' => 'trash',
'view' => array(
'type' => 'table',
'layout' => $default_layouts['table']['layout'],
'filters' => array(
array(
'field' => 'status',
'operator' => 'isAny',
'value' => 'trash',
'isLocked' => true,
),
),
),
),
);
$data->set(
array(
'default_view' => $default_view,
'default_layouts' => $default_layouts,
),
1
);
// Append the status views, thereby preserving the base "all items" view,
// so its post-type-specific title is kept.
$data->merge( array( 'view_list' => $view_list ), 1 );
return $data;
}
/**
* Provides the view configuration for the `wp_block` post type.
*
* @since 7.1.0
*
* @param WP_View_Config_Data $data The view configuration container for the entity.
* @return WP_View_Config_Data The updated view configuration container.
*/
function _wp_get_entity_view_config_posttype_wp_block( $data ) {
$default_layouts = array(
'table' => array(
'layout' => array(
'styles' => array(
'author' => array(
'width' => '1%',
),
),
),
),
'grid' => array(
'layout' => array(
'badgeFields' => array( 'sync-status' ),
),
),
);
$default_view = array(
'type' => 'grid',
'perPage' => 20,
'titleField' => 'title',
'mediaField' => 'preview',
'fields' => array( 'sync-status' ),
'filters' => array(),
'layout' => $default_layouts['grid']['layout'],
);
$view_list = array(
array(
'title' => __( 'All patterns' ),
'slug' => 'all-patterns',
),
array(
'title' => __( 'My patterns' ),
'slug' => 'my-patterns',
),
);
// Gather categories from the block pattern categories registry.
$registry = WP_Block_Pattern_Categories_Registry::get_instance();
$categories = array();
foreach ( $registry->get_all_registered() as $category ) {
$categories[ $category['name'] ] = $category['label'];
}
// Ensure "Uncategorized" is always included for patterns
// that have no category assigned.
$categories['uncategorized'] ??= __( 'Uncategorized' );
// Also gather user-created pattern categories (wp_pattern_category taxonomy).
$user_terms = get_terms(
array(
'taxonomy' => 'wp_pattern_category',
'hide_empty' => false,
)
);
if ( ! is_wp_error( $user_terms ) ) {
foreach ( $user_terms as $term ) {
$categories[ $term->slug ] = $term->name;
}
}
// Sort categories alphabetically by label.
asort( $categories, SORT_NATURAL | SORT_FLAG_CASE );
foreach ( $categories as $category_name => $label ) {
$view_list[] = array(
'title' => $label,
'slug' => $category_name,
);
}
$form = array(
'layout' => array( 'type' => 'panel' ),
'fields' => array(
array(
'id' => 'excerpt',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'top',
),
),
array(
'id' => 'post-content-info',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
'sync-status',
'revisions',
),
);
$data->set(
array(
'default_view' => $default_view,
'default_layouts' => $default_layouts,
'view_list' => $view_list,
'form' => $form,
),
1
);
return $data;
}
/**
* Provides the view configuration for the `wp_template_part` post type.
*
* @since 7.1.0
*
* @param WP_View_Config_Data $data The view configuration container for the entity.
* @return WP_View_Config_Data The updated view configuration container.
*/
function _wp_get_entity_view_config_posttype_wp_template_part( $data ) {
$default_layouts = array(
'table' => array(
'layout' => array(
'styles' => array(
'author' => array(
'width' => '1%',
),
),
),
),
'grid' => array(
'layout' => array(),
),
);
$default_view = array(
'type' => 'grid',
'perPage' => 20,
'titleField' => 'title',
'mediaField' => 'preview',
'fields' => array( 'author' ),
'filters' => array(),
'layout' => $default_layouts['grid']['layout'],
);
$view_list = array(
array(
'title' => __( 'All template parts' ),
'slug' => 'all-parts',
),
);
$areas = get_allowed_block_template_part_areas();
// Ensure default areas appear in a consistent order.
$preferred_order = array( 'header', 'footer', 'sidebar', 'navigation-overlay', 'uncategorized' );
$ordered_areas = array();
$remaining_areas = array();
foreach ( $areas as $area ) {
$position = array_search( $area['area'], $preferred_order, true );
if ( false !== $position ) {
$ordered_areas[ $position ] = $area;
} else {
$remaining_areas[] = $area;
}
}
ksort( $ordered_areas );
$areas = array_merge( array_values( $ordered_areas ), $remaining_areas );
foreach ( $areas as $area ) {
$view_list[] = array(
'title' => $area['label'],
'slug' => $area['area'],
'view' => array(
'filters' => array(
array(
'field' => 'area',
'operator' => 'is',
'value' => $area['area'],
'isLocked' => true,
),
),
),
);
}
$form = array(
'layout' => array( 'type' => 'panel' ),
'fields' => array(
array(
'id' => 'last_edited_date',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'none',
),
),
'revisions',
),
);
$data->set(
array(
'default_view' => $default_view,
'default_layouts' => $default_layouts,
'view_list' => $view_list,
'form' => $form,
),
1
);
return $data;
}
/**
* Provides the view configuration for the `wp_template` post type.
*
* @since 7.1.0
*
* @param WP_View_Config_Data $data The view configuration container for the entity.
* @return WP_View_Config_Data The updated view configuration container.
*/
function _wp_get_entity_view_config_posttype_wp_template( $data ) {
$default_view = array(
'type' => 'grid',
'perPage' => 20,
'sort' => array(
'field' => 'title',
'direction' => 'asc',
),
'titleField' => 'title',
'descriptionField' => 'description',
'mediaField' => 'preview',
'fields' => array( 'author', 'active', 'slug', 'theme' ),
'filters' => array(),
'showMedia' => true,
);
$default_layouts = array(
'table' => array( 'showMedia' => false ),
'grid' => array( 'showMedia' => true ),
'list' => array( 'showMedia' => false ),
);
$view_list = array(
array(
'title' => __( 'All templates' ),
'slug' => 'all',
),
);
$templates = get_block_templates( array(), 'wp_template' );
// Collect unique authors, tracking whether they come from a registered
// source (theme, plugin, site) so we can sort those before user ones.
$seen_authors = array();
$registered_authors = array();
$user_authors = array();
foreach ( $templates as $template ) {
/*
* Determine the original source of the template ('theme', 'plugin',
* 'site', or 'user').
*/
$original_source = 'user';
if ( 'wp_template' === $template->type || 'wp_template_part' === $template->type ) {
if ( $template->has_theme_file &&
( 'theme' === $template->origin || (
empty( $template->origin ) && in_array(
$template->source,
array(
'theme',
'custom',
),
true
) )
)
) {
/*
* Added by theme.
* Template originally provided by a theme, but customized by a user.
* Templates originally didn't have the 'origin' field so identify
* older customized templates by checking for no origin and a 'theme'
* or 'custom' source.
*/
$original_source = 'theme';
} elseif ( 'plugin' === $template->origin ) {
// Added by plugin.
$original_source = 'plugin';
} elseif ( empty( $template->has_theme_file ) && 'custom' === $template->source && empty( $template->author ) ) {
/*
* Added by site.
* Template was created from scratch, but has no author. Author support
* was only added to templates in WordPress 5.9. Fallback to showing the
* site logo and title.
*/
$original_source = 'site';
}
}
// Determine a human readable text for the author of the template.
$author_text = '';
switch ( $original_source ) {
case 'theme':
$theme_name = wp_get_theme( $template->theme )->get( 'Name' );
$author_text = empty( $theme_name ) ? $template->theme : $theme_name;
break;
case 'plugin':
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_name = '';
if ( isset( $template->plugin ) ) {
$plugins = wp_get_active_and_valid_plugins();
foreach ( $plugins as $plugin_file ) {
$plugin_basename = plugin_basename( $plugin_file );
list( $plugin_slug, ) = explode( '/', $plugin_basename );
if ( $plugin_slug === $template->plugin ) {
$plugin_data = get_plugin_data( $plugin_file );
if ( ! empty( $plugin_data['Name'] ) ) {
$plugin_name = $plugin_data['Name'];
}
break;
}
}
}
/*
* Fall back to the theme name if the plugin is not defined. That's needed to keep backwards
* compatibility with templates that were registered before the plugin attribute was added.
*/
if ( '' === $plugin_name ) {
$plugins = get_plugins();
$plugin_basename = plugin_basename( sanitize_text_field( $template->theme . '.php' ) );
if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
$plugin_name = $plugins[ $plugin_basename ]['Name'];
} else {
$plugin_name = $template->plugin ?? $template->theme;
}
}
$author_text = $plugin_name;
break;
case 'site':
$author_text = get_bloginfo( 'name' );
break;
case 'user':
$author = get_user_by( 'id', $template->author );
if ( ! $author ) {
$author_text = __( 'Unknown author' );
} else {
$author_text = $author->get( 'display_name' );
}
break;
}
if ( ! empty( $author_text ) && ! isset( $seen_authors[ $author_text ] ) ) {
$seen_authors[ $author_text ] = true;
$entry = array(
'title' => $author_text,
'slug' => $author_text,
'view' => array(
'filters' => array(
array(
'field' => 'author',
'operator' => 'is',
'value' => $author_text,
'isLocked' => true,
),
),
),
);
if ( 'user' === $original_source ) {
$user_authors[] = $entry;
} else {
$registered_authors[] = $entry;
}
}
}
$form = array(
'layout' => array( 'type' => 'panel' ),
'fields' => array(
array(
'id' => 'description',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'top',
),
),
array(
'id' => 'description_readonly',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
array(
'id' => 'last_edited_date',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'none',
),
),
'revisions',
// The following fields are only meaningful in the `home`/`index`
// template summary. They edit other entities (`root/site` and the
// posts page); the editor merges those records into the form data
// under a namespace and controls when the fields are shown.
'posts_page_title',
'posts_per_page',
'default_comment_status',
),
);
$data->set(
array(
'default_view' => $default_view,
'default_layouts' => $default_layouts,
'view_list' => array_merge( $view_list, $registered_authors, $user_authors ),
'form' => $form,
),
1
);
return $data;
}