Repository navigation
Expand file tree
/
Copy pathclass-wp-entity-fields-registry.php
More file actions
1067 lines (996 loc) · 35.8 KB
/
Copy pathclass-wp-entity-fields-registry.php
File metadata and controls
1067 lines (996 loc) · 35.8 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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Entity Fields API: WP_Entity_Fields_Registry class
*
* @package WordPress
* @subpackage Entity Fields API
* @since 7.2.0
*/
/**
* Holds the fields registered for entities.
*
* The registry maps an entity, identified by its kind and name, to its
* registered field definitions keyed by id, in registration order, each
* with the origins that registered and updated it, and to the script modules
* registered for it, each with the ids of the fields it applies to.
* register() adds fields, update() changes registered ones, and both
* validate the definitions before storing them: a field that cannot be
* stored is reported and skipped, and the rest of the call is stored.
* register_collection() adds the fields a folder declares, see it for the
* format.
*
* Fields are registered on the `wp_entity_fields_api_init` action, and only
* there, with wp_register_entity_fields(), wp_update_entity_fields(), and
* wp_unregister_entity_fields(): register(), update(), and unregister() refuse
* to run while the action is not firing.
*
* The registry is filled lazily: the first time its fields are read, it
* fires the `wp_entity_fields_api_init` action, on which the default fields of every
* post type and the fields of plugins are registered. A read before `init`
* has completed is refused, see initialize().
*
* Once the action has fired the registry does not change: every reader of
* a request, the REST controller as well as the import map of the editor
* script, sees the same fields.
*
* @since 7.2.0
*
* @access private
*/
class WP_Entity_Fields_Registry {
/**
* Registered fields, as kind => name => array of field id => definition.
*
* Kept nested rather than under a joined `{$kind}/{$name}` key: both parts
* may contain any character, so no separator keeps two entities apart.
*
* @since 7.2.0
* @var array<string, array<string, array<string, array>>>
*/
protected $fields = array();
/**
* Registered script modules, as kind => name => array of module id =>
* list of the ids of the fields the module applies to, in registration
* order.
*
* @since 7.2.0
* @var array<string, array<string, array<string, string[]>>>
*/
protected $field_modules = array();
/**
* Whether the `wp_entity_fields_api_init` action has fired since the registry
* was created.
*
* @since 7.2.0
* @var bool
*/
protected $initialized = false;
/**
* The singleton instance.
*
* @since 7.2.0
* @var WP_Entity_Fields_Registry|null
*/
protected static $instance = null;
/**
* Returns the singleton instance.
*
* @since 7.2.0
*
* @return WP_Entity_Fields_Registry The registry.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Registers fields for an entity.
*
* The fields must be new: a field with the id of a registered field is
* reported and skipped, and so is every definition after the first of an
* id that appears more than once in the call. The rest of the fields of
* the call are registered: a field another plugin got to first does not
* cost a plugin the others. A definition that is not an array with a
* non-empty string `id` is reported and skipped the same way. To change
* a registered field, see update(); to replace it, unregister it first.
* The script module, if any, applies to every field the call registers.
* A field whose `type` DataViews does not provide is reported and
* registered anyway.
*
* The origin is stored as the `origin` property of each field, as
* `registeredBy`, replacing any `origin` the definition sets.
*
* @since 7.2.0
*
* @param string $origin Who registers the fields: `core`, or
* the slug of the plugin or theme.
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @param array[] $fields The list of field definitions, each with an `id`.
* @param string|null $script_module The id of the script module providing
* the JavaScript parts of the fields, if any.
* @return string[] The ids of the fields registered, in the order of the
* call. Empty when none is, when called outside the
* `wp_entity_fields_api_init` action, or when an argument is
* invalid.
*/
public function register( $origin, $kind, $name, $fields, $script_module = null ) {
if ( ! $this->validate_arguments( __METHOD__, $origin, $kind, $name, $fields, $script_module ) ) {
return array();
}
$fields = $this->skip_invalid_definitions( __METHOD__, $kind, $name, $fields );
if ( ! $fields ) {
return array();
}
$new_fields = array();
$duplicated = array();
$registered_ids = array();
foreach ( $fields as $field ) {
$id = $field['id'];
if ( isset( $this->fields[ $kind ][ $name ][ $id ] ) ) {
$registered_ids[ $id ] = $id;
} elseif ( isset( $new_fields[ $id ] ) ) {
$duplicated[ $id ] = $id;
} else {
$new_fields[ $id ] = $field;
}
}
if ( $duplicated ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of field ids. */
__( 'A field can only be registered once. These fields of %1$s "%2$s" appear more than once in the same call: %3$s. Only their first definition is registered.' ),
$kind,
$name,
implode( ', ', $duplicated )
),
'7.2.0'
);
}
if ( $registered_ids ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of field ids. 4: update() */
__( 'A field can only be registered once. These fields of %1$s "%2$s" are skipped because they are already registered: %3$s. Use %4$s to change a registered field, or unregister it first to replace it.' ),
$kind,
$name,
implode( ', ', $registered_ids ),
'<code>update()</code>'
),
'7.2.0'
);
}
if ( ! $new_fields ) {
return array();
}
$this->report_unknown_types( __METHOD__, $kind, $name, $new_fields );
foreach ( $new_fields as $field ) {
$this->fields[ $kind ][ $name ][ $field['id'] ] = array_merge(
$field,
array(
'origin' => array(
'registeredBy' => $origin,
'updatedBy' => array(),
),
)
);
}
$ids = array_column( $new_fields, 'id' );
$this->add_field_module( $kind, $name, $ids, $script_module );
return $ids;
}
/**
* Updates registered fields of an entity.
*
* Each definition is merged into the registered field with its id,
* property by property; the field keeps its position. The fields must be
* registered: a field that is not is reported and skipped, and so is a
* definition that is not an array with a non-empty string `id`; the rest
* of the fields of the call are updated. The script module, if any,
* applies to every field the call updates, on top of the modules the
* fields have. A field whose `type` DataViews does not provide is
* reported and updated anyway.
*
* The origin is appended to the `updatedBy` list of the `origin` property
* of each field, once; the rest of the `origin` property cannot be
* updated, and any `origin` the definition sets is ignored.
*
* Like register(), it only runs on the `wp_entity_fields_api_init` action.
*
* @since 7.2.0
*
* @param string $origin Who updates the fields: `core`, or
* the slug of the plugin or theme.
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @param array[] $fields The list of partial field definitions,
* each with the `id` of a registered field.
* @param string|null $script_module The id of the script module providing
* the JavaScript parts of the fields, if any.
* @return string[] The ids of the fields updated, in the order of the
* call. Empty when none is, when called outside the
* `wp_entity_fields_api_init` action, or when an argument is
* invalid.
*/
public function update( $origin, $kind, $name, $fields, $script_module = null ) {
if ( ! $this->validate_arguments( __METHOD__, $origin, $kind, $name, $fields, $script_module ) ) {
return array();
}
$fields = $this->skip_invalid_definitions( __METHOD__, $kind, $name, $fields );
if ( ! $fields ) {
return array();
}
$this->report_unknown_types( __METHOD__, $kind, $name, $fields );
$ids = array();
$unregistered_ids = array();
foreach ( $fields as $field ) {
$id = $field['id'];
if ( ! isset( $this->fields[ $kind ][ $name ][ $id ] ) ) {
$unregistered_ids[ $id ] = $id;
continue;
}
$registered = $this->fields[ $kind ][ $name ][ $id ];
$field_origin = $registered['origin'];
if ( ! in_array( $origin, $field_origin['updatedBy'], true ) ) {
$field_origin['updatedBy'][] = $origin;
}
$this->fields[ $kind ][ $name ][ $id ] = array_merge( $registered, $field, array( 'origin' => $field_origin ) );
if ( ! in_array( $id, $ids, true ) ) {
$ids[] = $id;
}
}
if ( $unregistered_ids ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of field ids. 4: register() */
__( 'Only registered fields can be updated. These fields of %1$s "%2$s" are skipped because they are not registered: %3$s. Use %4$s to add a field.' ),
$kind,
$name,
implode( ', ', $unregistered_ids ),
'<code>register()</code>'
),
'7.2.0'
);
}
if ( $ids ) {
$this->add_field_module( $kind, $name, $ids, $script_module );
}
return $ids;
}
/**
* Unregisters fields of an entity.
*
* Unregistering a field removes its registered definition and
* drops it from the script modules that applied to it (a module left with
* no field is forgotten). Unregistering every field forgets the entity:
* its registered fields and script modules.
*
* Like register(), it only runs on the `wp_entity_fields_api_init` action, and an
* invalid entity is reported and unregisters nothing. An id that is not a
* non-empty string is reported and skipped, like a definition register()
* cannot store.
*
* @since 7.2.0
*
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @param string[]|null $ids The ids of the fields to unregister. Default
* null, every field of the entity.
* @return array[] The list of the definitions unregistered, in
* registration order, as get_registered() returns them.
* Empty when none of the fields is registered, when
* called outside the `wp_entity_fields_api_init` action, or when
* the entity is invalid.
*/
public function unregister( $kind, $name, $ids = null ) {
if ( ! $this->doing_entity_fields_api_init( __METHOD__ ) || ! $this->validate_entity( __METHOD__, $kind, $name ) ) {
return array();
}
if ( null === $ids ) {
$unregistered = array_values( $this->fields[ $kind ][ $name ] ?? array() );
unset( $this->fields[ $kind ][ $name ], $this->field_modules[ $kind ][ $name ] );
$this->forget_empty_entity( $kind, $name );
return $unregistered;
}
$ids = $this->skip_invalid_ids( __METHOD__, $kind, $name, (array) $ids );
if ( ! $ids ) {
return array();
}
$unregistered = array_intersect_key( $this->fields[ $kind ][ $name ] ?? array(), array_flip( $ids ) );
if ( empty( $unregistered ) ) {
return array();
}
// A script module only ever applies to registered fields: register()
// and update() add it for fields they store, and fields leave both
// lists together here. So the fields removed are all the registry
// loses, and the modules are not returned: the ones that applied to
// them are listed by get_registered_field_modules(), read beforehand.
$ids = array_keys( $unregistered );
foreach ( $ids as $id ) {
unset( $this->fields[ $kind ][ $name ][ $id ] );
}
foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $module_ids ) {
$remaining = array_values( array_diff( $module_ids, $ids ) );
if ( empty( $remaining ) ) {
unset( $this->field_modules[ $kind ][ $name ][ $module ] );
} else {
$this->field_modules[ $kind ][ $name ][ $module ] = $remaining;
}
}
$this->forget_empty_entity( $kind, $name );
return array_values( $unregistered );
}
/**
* Registers the fields of a field collection.
*
* A collection is a folder whose `index.php` returns its configuration, with
* a folder per field whose `field.php` returns the serializable part of the
* field (see get_collection_fields()). Each file is required without
* sharing the variables of the caller, see require_file(). The JavaScript
* parts of the fields, if any, ship in the script module of the
* collection. The configuration is an array with these keys:
*
* - `origin` (required): who registers the fields: `core`, or the slug of
* the plugin or theme. See register().
* - `kind` (required): the entity kind, e.g. `postType`.
* - `name` (required): the entity name, e.g. `wp_template`.
* - `module` (optional): the id of the script module providing the
* JavaScript parts of the fields. Every field of the collection is
* registered with it.
*
* The collection registers its fields on the entity, after the fields
* registered on it before, in the alphabetical order of their folders.
* register() skips a field already registered for the entity, and
* registers the rest of the collection. Like register(), it does not check
* that the entity exists: the `/wp/v2/entity-fields` route only serves the fields
* of the entities the REST API exposes.
*
* Fields that apply to several entities, such as the defaults every post
* type derives from its supports, are registered in code: a
* `wp_entity_fields_api_init` callback can read their definitions from files with
* get_collection_fields() and register them on each entity with
* register().
*
* Like register(), it only runs on the `wp_entity_fields_api_init` action. An
* invalid configuration is reported with _doing_it_wrong() and skipped. So
* is an invalid `field.php`, see get_collection_fields(): the rest of the
* fields of the collection are registered.
*
* @since 7.2.0
*
* @param string $directory The directory of the collection.
* @return bool Whether the collection is valid and all of its fields are
* registered. True as well when it has no fields; false when
* a `field.php` is skipped, or when called outside the
* `wp_entity_fields_api_init` action.
*/
public function register_collection( $directory ) {
if ( ! $this->doing_entity_fields_api_init( __METHOD__ ) ) {
return false;
}
$collection = $this->get_collection( __METHOD__, $directory );
if ( null === $collection ) {
return false;
}
if ( empty( $collection['fields'] ) ) {
return ! $collection['skipped'];
}
$registered = $this->register( $collection['origin'], $collection['kind'], $collection['name'], $collection['fields'], $collection['module'] );
return ! $collection['skipped'] && count( $registered ) === count( $collection['fields'] );
}
/**
* Returns the fields of a field collection.
*
* A collection holds a folder per field, whose `field.php` returns the
* serializable part of the field, see register_collection().
*
* @since 7.2.0
*
* @param string $directory The directory of the collection.
* @return array<string, array> The fields of the collection, keyed by id, in
* the alphabetical order of their folders. The
* id of a field is the name of its folder,
* unless its `field.php` sets an `id`, and it
* comes first in the field. A `field.php`
* that does not return an array with a
* non-empty string id, or whose id an earlier
* folder took, is reported and skipped.
*/
public function get_collection_fields( $directory ) {
return $this->read_collection_fields( __METHOD__, $directory )['fields'];
}
/**
* Returns the fields registered for an entity.
*
* @since 7.2.0
*
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @return array[] The list of field definitions, in registration order.
* Empty when the entity is invalid.
*/
public function get_registered( $kind, $name ) {
if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) {
return array();
}
$this->initialize();
return array_values( $this->fields[ $kind ][ $name ] ?? array() );
}
/**
* Returns the fields registered for every entity.
*
* @since 7.2.0
*
* @return array<string, array<string, array[]>> The lists of field
* definitions, keyed by
* kind, then by name.
*/
public function get_all_registered() {
$this->initialize();
return array_map(
static function ( $entities ) {
return array_map( 'array_values', $entities );
},
$this->fields
);
}
/**
* Returns the script modules registered for an entity.
*
* @since 7.2.0
*
* @param string $kind The entity kind (e.g. `postType`).
* @param string $name The entity name (e.g. `page`).
* @return array[] {
* The script modules, in registration order. Empty when the entity
* is invalid.
*
* @type array ...$0 {
* @type string $id The id of the script module.
* @type string[] $fields The ids of the fields the module applies
* to, in registration order.
* }
* }
*/
public function get_registered_field_modules( $kind, $name ) {
if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) {
return array();
}
$this->initialize();
$modules = array();
foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $field_ids ) {
$modules[] = array(
// A numeric module id is an integer as an array key.
'id' => (string) $module,
'fields' => $field_ids,
);
}
return $modules;
}
/**
* Returns the ids of the script modules registered for every entity.
*
* @since 7.2.0
*
* @return array<string, array<string, string[]>> The lists of module ids,
* keyed by kind, then by
* name.
*/
public function get_all_registered_field_modules() {
$this->initialize();
/*
* Keeps the module ids and drops the field ids they apply to, e.g.:
*
* array(
* 'postType' => array(
* 'page' => array( 'my-plugin/fields', '123' ),
* 'post' => array( 'my-plugin/fields' ),
* ),
* )
*/
return array_map(
static function ( $entities ) {
return array_map(
static function ( $modules ) {
// A numeric module id is an integer as an array key.
return array_map( 'strval', array_keys( $modules ) );
},
$entities
);
},
$this->field_modules
);
}
/**
* Fires the `wp_entity_fields_api_init` action the first time the registry
* is read.
*
* The read is refused until `init` has completed. The registry holds the
* fields of post types, and post types are not known until then: core
* registers its own on `init` at priority 0, plugins theirs at the default
* priority, and supports are added and removed on `init` too. The default
* fields of a post type derive from its supports, and the action fires
* once, so a read before or during `init` would fix the defaults from the
* post types registered so far for the rest of the request.
*
* did_action() is true from the first `init` callback on, hence the
* doing_action() check for the reads made while it runs.
*
* @since 7.2.0
*/
protected function initialize() {
if ( $this->initialized ) {
return;
}
if ( ! did_action( 'init' ) || doing_action( 'init' ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: init, 2: wp_entity_fields_api_init */
__( 'The registered fields cannot be read until the %1$s action has completed: the post types and supports they derive from are registered on %1$s. Read them once %1$s has completed, or hook %2$s.' ),
'<code>init</code>',
'<code>wp_entity_fields_api_init</code>'
),
'7.2.0'
);
return;
}
// Set before firing, so a callback reading the registry to inspect
// the fields it updates does not fire the action again.
$this->initialized = true;
/**
* Fires the first time the registered fields are read, after `init`.
*
* Register or adjust fields here, with wp_register_entity_fields(),
* wp_update_entity_fields(), and wp_unregister_entity_fields(): it is the only
* time they run.
*
* The first read happens wherever the fields are needed: while
* handling a REST request as well as on `admin_footer` of the pages
* that load the editor script. A callback should register fields and
* nothing else; script modules, styles, and their enqueue hooks
* belong on `init`.
*
* @since 7.2.0
*/
do_action( 'wp_entity_fields_api_init' );
}
/**
* Checks that the `wp_entity_fields_api_init` action is firing, as register(),
* update(), and unregister() require.
*
* Outside the action a registration would either come before the
* defaults, which could then not be registered, or after the fields have been read
* and the import map of the editor script built from them. Refusing
* keeps the registry immutable once read.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @return bool Whether the action is firing.
*/
protected function doing_entity_fields_api_init( $method ) {
if ( doing_action( 'wp_entity_fields_api_init' ) ) {
return true;
}
_doing_it_wrong(
$method,
sprintf(
/* translators: %s: wp_entity_fields_api_init */
__( 'Fields can only be registered, updated, and unregistered on the %s action.' ),
'<code>wp_entity_fields_api_init</code>'
),
'7.2.0'
);
return false;
}
/**
* Reports the fields whose `type` is not one the REST schema of the
* fields lists, with _doing_it_wrong(). Those are the types DataViews
* provides a control for: a misspelt type is otherwise silent, the field
* renders without a control. A missing `type` is fine.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param string $kind The entity kind.
* @param string $name The entity name.
* @param array[] $fields The field definitions.
*/
protected function report_unknown_types( $method, $kind, $name, $fields ) {
$type_schema = ( new WP_REST_Entity_Fields_Controller() )->get_item_schema()['properties']['fields']['items']['properties']['type'];
$unknown = array();
foreach ( $fields as $field ) {
if ( ! isset( $field['type'] ) ) {
continue;
}
$valid = rest_validate_value_from_schema( $field['type'], $type_schema, 'type' );
if ( is_wp_error( $valid ) ) {
$unknown[] = sprintf( '%s (%s)', $field['id'], $valid->get_error_message() );
}
}
if ( ! $unknown ) {
return;
}
_doing_it_wrong(
$method,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of field ids, each with the error of its type. */
__( 'These fields of %1$s "%2$s" have a type DataViews does not provide, so they render without a control: %3$s' ),
$kind,
$name,
implode( ', ', $unknown )
),
'7.2.0'
);
}
/**
* Validates the arguments of register() and update().
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param mixed $origin The origin.
* @param mixed $kind The entity kind.
* @param mixed $name The entity name.
* @param mixed $fields The field definitions.
* @param mixed $script_module The script module id, if any.
* @return bool Whether the call may proceed: the `wp_entity_fields_api_init` action
* is firing and the arguments are valid.
*/
protected function validate_arguments( $method, $origin, $kind, $name, $fields, $script_module ) {
if ( ! $this->doing_entity_fields_api_init( $method ) ) {
return false;
}
if ( ! is_string( $origin ) || '' === $origin ) {
_doing_it_wrong(
$method,
__( 'The origin must be a non-empty string.' ),
'7.2.0'
);
return false;
}
if ( ! $this->validate_entity( $method, $kind, $name ) ) {
return false;
}
if ( ! is_array( $fields ) || empty( $fields ) || ! array_is_list( $fields ) ) {
_doing_it_wrong(
$method,
__( 'The fields must be a list of non-empty field definitions.' ),
'7.2.0'
);
return false;
}
if ( null !== $script_module && ( ! is_string( $script_module ) || '' === $script_module ) ) {
_doing_it_wrong(
$method,
__( 'The script module must be the id of a script module.' ),
'7.2.0'
);
return false;
}
return true;
}
/**
* Checks that the entity kind and name are non-empty strings, reporting
* them with _doing_it_wrong() otherwise.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param mixed $kind The entity kind.
* @param mixed $name The entity name.
* @return bool Whether both are valid.
*/
protected function validate_entity( $method, $kind, $name ) {
foreach ( array( $kind, $name ) as $argument ) {
if ( ! is_string( $argument ) || '' === $argument ) {
_doing_it_wrong(
$method,
__( 'The entity kind and the entity name must be non-empty strings.' ),
'7.2.0'
);
return false;
}
}
return true;
}
/**
* Drops the definitions that are not an array with a non-empty string
* `id`, reporting their positions in the call with _doing_it_wrong():
* like a duplicated field, an invalid definition does not cost the call
* its other fields.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param string $kind The entity kind.
* @param string $name The entity name.
* @param array[] $fields The list of field definitions.
* @return array[] The valid definitions, in the order of the call.
*/
protected function skip_invalid_definitions( $method, $kind, $name, $fields ) {
$valid = array();
$invalid = array();
foreach ( $fields as $position => $field ) {
if ( is_array( $field ) && isset( $field['id'] ) && is_string( $field['id'] ) && '' !== $field['id'] ) {
$valid[] = $field;
} else {
$invalid[] = '#' . ( $position + 1 );
}
}
if ( $invalid ) {
_doing_it_wrong(
$method,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of positions, e.g. #2, #4. 4: id */
__( 'Every field definition must be an array with a non-empty string %4$s. These definitions of %1$s "%2$s" are skipped: %3$s. The rest of the fields of the call are stored.' ),
$kind,
$name,
implode( ', ', $invalid ),
'<code>id</code>'
),
'7.2.0'
);
}
return $valid;
}
/**
* Drops the ids that are not a non-empty string, reporting their
* positions in the call with _doing_it_wrong(): like an invalid
* definition, an invalid id does not cost the call its other ids.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param string $kind The entity kind.
* @param string $name The entity name.
* @param array $ids The ids of the fields.
* @return string[] The valid ids, in the order of the call.
*/
protected function skip_invalid_ids( $method, $kind, $name, $ids ) {
$valid = array();
$invalid = array();
foreach ( array_values( $ids ) as $position => $id ) {
if ( is_string( $id ) && '' !== $id ) {
$valid[] = $id;
} else {
$invalid[] = '#' . ( $position + 1 );
}
}
if ( $invalid ) {
_doing_it_wrong(
$method,
sprintf(
/* translators: 1: Entity kind, e.g. postType. 2: Entity name, e.g. page. 3: Comma-separated list of positions, e.g. #2, #4. */
__( 'Every field id must be a non-empty string. These ids of %1$s "%2$s" are skipped: %3$s. The rest of the fields of the call are unregistered.' ),
$kind,
$name,
implode( ', ', $invalid )
),
'7.2.0'
);
}
return $valid;
}
/**
* Reads and validates a field collection, see register_collection() for
* its format.
*
* An invalid configuration is reported with _doing_it_wrong(): the
* collection is skipped.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param string $directory The directory of the collection.
* @return array|null The configuration of the collection, with the `module`
* key set, its valid `fields` as a list, and `skipped`
* telling whether a `field.php` was skipped, see
* read_collection_fields(); or null when the
* configuration is invalid.
*/
protected function get_collection( $method, $directory ) {
$slug = basename( $directory );
$file = $directory . '/index.php';
$config = is_file( $file ) ? self::require_file( $file ) : null;
$error = null;
if ( ! is_array( $config ) ) {
$error = sprintf(
/* translators: %s: index.php */
__( 'The %s of a field collection must return an array.' ),
'<code>index.php</code>'
);
} elseif ( ! isset( $config['origin'] ) || ! is_string( $config['origin'] ) || '' === $config['origin'] ) {
$error = sprintf(
/* translators: %s: origin */
__( 'The %s of a field collection must be a non-empty string.' ),
'<code>origin</code>'
);
} elseif ( ! isset( $config['kind'] ) || ! is_string( $config['kind'] ) || '' === $config['kind'] ) {
$error = sprintf(
/* translators: %s: kind */
__( 'The %s of a field collection must be a non-empty string.' ),
'<code>kind</code>'
);
} elseif ( ! isset( $config['name'] ) || ! is_string( $config['name'] ) || '' === $config['name'] ) {
$error = sprintf(
/* translators: %s: name */
__( 'The %s of a field collection must be a non-empty string.' ),
'<code>name</code>'
);
} elseif ( isset( $config['module'] ) && ( ! is_string( $config['module'] ) || '' === $config['module'] ) ) {
$error = sprintf(
/* translators: %s: module */
__( 'The %s of a field collection must be the id of a script module.' ),
'<code>module</code>'
);
}
if ( null !== $error ) {
_doing_it_wrong(
$method,
/* translators: 1: The name of a field collection. 2: The error. */
sprintf( __( 'The field collection "%1$s" is skipped. %2$s' ), $slug, $error ),
'7.2.0'
);
return null;
}
$read = $this->read_collection_fields( $method, $directory );
return array_merge(
$config,
array(
'module' => $config['module'] ?? null,
'fields' => array_values( $read['fields'] ),
'skipped' => $read['skipped'],
)
);
}
/**
* Reads the fields of a field collection, see get_collection_fields().
*
* The id of a field is used as a key, so it is checked before: a
* `field.php` that does not return an array with a non-empty string id
* is reported with _doing_it_wrong() and skipped, and so is one whose
* id an earlier folder of the collection took. The rest of the fields
* are read.
*
* @since 7.2.0
*
* @param string $method The calling method, for the notice.
* @param string $directory The directory of the collection.
* @return array {
* The fields of the collection.
*
* @type array<string, array> $fields The valid fields, keyed by id,
* in the alphabetical order of
* their folders.
* @type bool $skipped Whether a `field.php` was skipped.
* }
*/
protected function read_collection_fields( $method, $directory ) {
$files = glob( $directory . '/*/field.php' );
if ( empty( $files ) ) {
return array(
'fields' => array(),
'skipped' => false,
);
}
sort( $files, SORT_STRING );
$fields = array();
$invalid = array();
$duplicated = array();
foreach ( $files as $file ) {
$folder = basename( dirname( $file ) );
$field = self::require_file( $file );
if ( ! is_array( $field ) ) {
$invalid[] = $folder;
continue;
}
// The id comes first, like in the fields registered directly.
$field = array_merge( array( 'id' => $folder ), $field );
$id = $field['id'];
if ( ! is_string( $id ) || '' === $id ) {
$invalid[] = $folder;
} elseif ( isset( $fields[ $id ] ) ) {
$duplicated[] = $folder;
} else {
$fields[ $id ] = $field;
}
}
$collection = basename( $directory );
if ( $invalid ) {
_doing_it_wrong(
$method,
sprintf(
/* translators: 1: The name of a field collection. 2: Comma-separated list of folder names. 3: field.php 4: id */
__( 'The %3$s of a field must return an array, with a non-empty string %4$s if it sets one. These fields of the field collection "%1$s" are skipped: %2$s.' ),
$collection,
implode( ', ', $invalid ),
'<code>field.php</code>',
'<code>id</code>'
),
'7.2.0'
);
}
if ( $duplicated ) {
_doing_it_wrong(
$method,
sprintf(
/* translators: 1: The name of a field collection. 2: Comma-separated list of folder names. */
__( 'A field can only be defined once in a field collection. These fields of the field collection "%1$s" are skipped because an earlier folder defines their id: %2$s.' ),
$collection,
implode( ', ', $duplicated )
),
'7.2.0'
);
}
return array(
'fields' => $fields,