Skip to content

Commit c478159

Browse files
oandregalclaude
andcommitted
Entity Fields: Validate the ids of collection fields before using them.
A `field.php` that returned a non-string `id`, such as an array, caused a fatal error when the id was used as a key in get_collection_fields(). Other non-string ids were silently coerced, and a folder setting the id of an earlier one silently replaced it. Such fields are now reported with _doing_it_wrong() and skipped, along with a `field.php` that does not return an array. The rest of the collection is read, and register_collection() returns false when a field was skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4316614 commit c478159

10 files changed

Lines changed: 239 additions & 26 deletions

File tree

‎src/wp-includes/class-wp-entity-fields-registry.php‎

Lines changed: 108 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,17 @@ public function unregister( $kind, $name, $ids = null ) {
383383
* register().
384384
*
385385
* Like register(), it only runs on the `wp_entity_fields_api_init` action. An
386-
* invalid configuration is reported with _doing_it_wrong() and skipped.
386+
* invalid configuration is reported with _doing_it_wrong() and skipped. So
387+
* is an invalid `field.php`, see get_collection_fields(): the rest of the
388+
* fields of the collection are registered.
387389
*
388390
* @since 7.2.0
389391
*
390392
* @param string $directory The directory of the collection.
391393
* @return bool Whether the collection is valid and all of its fields are
392394
* registered. True as well when it has no fields; false when
393-
* called outside the `wp_entity_fields_api_init` action.
395+
* a `field.php` is skipped, or when called outside the
396+
* `wp_entity_fields_api_init` action.
394397
*/
395398
public function register_collection( $directory ) {
396399
if ( ! $this->doing_entity_fields_api_init( __METHOD__ ) ) {
@@ -403,11 +406,11 @@ public function register_collection( $directory ) {
403406
}
404407

405408
if ( empty( $collection['fields'] ) ) {
406-
return true;
409+
return ! $collection['skipped'];
407410
}
408411
$registered = $this->register( $collection['origin'], $collection['kind'], $collection['name'], $collection['fields'], $collection['module'] );
409412

410-
return count( $registered ) === count( $collection['fields'] );
413+
return ! $collection['skipped'] && count( $registered ) === count( $collection['fields'] );
411414
}
412415

413416
/**
@@ -423,27 +426,13 @@ public function register_collection( $directory ) {
423426
* the alphabetical order of their folders. The
424427
* id of a field is the name of its folder,
425428
* unless its `field.php` sets an `id`, and it
426-
* comes first in the field.
429+
* comes first in the field. A `field.php`
430+
* that does not return an array with a
431+
* non-empty string id, or whose id an earlier
432+
* folder took, is reported and skipped.
427433
*/
428434
public function get_collection_fields( $directory ) {
429-
$files = glob( $directory . '/*/field.php' );
430-
if ( empty( $files ) ) {
431-
return array();
432-
}
433-
sort( $files, SORT_STRING );
434-
435-
$fields = array();
436-
foreach ( $files as $file ) {
437-
$field = self::require_file( $file );
438-
if ( ! is_array( $field ) ) {
439-
continue;
440-
}
441-
// The id comes first, like in the fields registered directly.
442-
$field = array_merge( array( 'id' => basename( dirname( $file ) ) ), $field );
443-
$fields[ $field['id'] ] = $field;
444-
}
445-
446-
return $fields;
435+
return $this->read_collection_fields( __METHOD__, $directory )['fields'];
447436
}
448437

449438
/**
@@ -808,7 +797,9 @@ protected function skip_invalid_ids( $method, $kind, $name, $ids ) {
808797
* @param string $method The calling method, for the notice.
809798
* @param string $directory The directory of the collection.
810799
* @return array|null The configuration of the collection, with the `module`
811-
* key set and its `fields` as a list, or null when the
800+
* key set, its valid `fields` as a list, and `skipped`
801+
* telling whether a `field.php` was skipped, see
802+
* read_collection_fields(); or null when the
812803
* configuration is invalid.
813804
*/
814805
protected function get_collection( $method, $directory ) {
@@ -839,15 +830,106 @@ protected function get_collection( $method, $directory ) {
839830
return null;
840831
}
841832

833+
$read = $this->read_collection_fields( $method, $directory );
834+
842835
return array_merge(
843836
$config,
844837
array(
845-
'module' => $config['module'] ?? null,
846-
'fields' => array_values( $this->get_collection_fields( $directory ) ),
838+
'module' => $config['module'] ?? null,
839+
'fields' => array_values( $read['fields'] ),
840+
'skipped' => $read['skipped'],
847841
)
848842
);
849843
}
850844

845+
/**
846+
* Reads the fields of a field collection, see get_collection_fields().
847+
*
848+
* The id of a field is used as a key, so it is checked before: a
849+
* `field.php` that does not return an array with a non-empty string id
850+
* is reported with _doing_it_wrong() and skipped, and so is one whose
851+
* id an earlier folder of the collection took. The rest of the fields
852+
* are read.
853+
*
854+
* @since 7.2.0
855+
*
856+
* @param string $method The calling method, for the notice.
857+
* @param string $directory The directory of the collection.
858+
* @return array {
859+
* The fields of the collection.
860+
*
861+
* @type array<string, array> $fields The valid fields, keyed by id,
862+
* in the alphabetical order of
863+
* their folders.
864+
* @type bool $skipped Whether a `field.php` was skipped.
865+
* }
866+
*/
867+
protected function read_collection_fields( $method, $directory ) {
868+
$files = glob( $directory . '/*/field.php' );
869+
if ( empty( $files ) ) {
870+
return array(
871+
'fields' => array(),
872+
'skipped' => false,
873+
);
874+
}
875+
sort( $files, SORT_STRING );
876+
877+
$fields = array();
878+
$invalid = array();
879+
$duplicated = array();
880+
foreach ( $files as $file ) {
881+
$folder = basename( dirname( $file ) );
882+
$field = self::require_file( $file );
883+
if ( ! is_array( $field ) ) {
884+
$invalid[] = $folder;
885+
continue;
886+
}
887+
888+
// The id comes first, like in the fields registered directly.
889+
$field = array_merge( array( 'id' => $folder ), $field );
890+
$id = $field['id'];
891+
if ( ! is_string( $id ) || '' === $id ) {
892+
$invalid[] = $folder;
893+
} elseif ( isset( $fields[ $id ] ) ) {
894+
$duplicated[] = $folder;
895+
} else {
896+
$fields[ $id ] = $field;
897+
}
898+
}
899+
900+
$collection = basename( $directory );
901+
if ( $invalid ) {
902+
_doing_it_wrong(
903+
$method,
904+
sprintf(
905+
/* translators: 1: The name of a field collection. 2: Comma-separated list of folder names. */
906+
__( 'The `field.php` of a field must return an array, with a non-empty string `id` if it sets one. These fields of the field collection "%1$s" are skipped: %2$s.' ),
907+
$collection,
908+
implode( ', ', $invalid )
909+
),
910+
'7.2.0'
911+
);
912+
}
913+
914+
if ( $duplicated ) {
915+
_doing_it_wrong(
916+
$method,
917+
sprintf(
918+
/* translators: 1: The name of a field collection. 2: Comma-separated list of folder names. */
919+
__( '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.' ),
920+
$collection,
921+
implode( ', ', $duplicated )
922+
),
923+
'7.2.0'
924+
);
925+
}
926+
927+
return array(
928+
'fields' => $fields,
929+
'skipped' => $invalid || $duplicated,
930+
);
931+
}
932+
851933
/**
852934
* Requires a file without sharing the variables of the caller: this is
853935
* about variable scope.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A field of books whose id is an array.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'id' => array( 'array_id' ),
10+
'type' => 'text',
11+
'label' => 'Array id',
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A field of books whose id is empty.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'id' => '',
10+
'type' => 'text',
11+
'label' => 'Empty id',
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A collection for books with invalid fields along a valid one.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'origin' => 'fixture',
10+
'kind' => 'postType',
11+
'name' => 'test_book',
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A field of books whose id is a number.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'id' => 42,
10+
'type' => 'text',
11+
'label' => 'Int id',
12+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* A field of books that does not return an array.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return 'not_array';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A field of books whose id is null.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'id' => null,
10+
'type' => 'text',
11+
'label' => 'Null id',
12+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* A valid field of books.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'type' => 'text',
10+
'label' => 'Valid',
11+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* A field of books taking the id of an earlier folder.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
return array(
9+
'id' => 'valid',
10+
'type' => 'text',
11+
'label' => 'Valid again',
12+
);

‎tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,46 @@ public function test_an_invalid_collection_is_skipped() {
193193
);
194194
}
195195

196+
/**
197+
* A `field.php` that does not return an array with a non-empty string
198+
* id, or whose id an earlier folder took, is reported and skipped
199+
* before its id is used as a key: an array id does not cause a fatal
200+
* error, and a later folder does not replace an earlier one.
201+
*
202+
* @ticket 66229
203+
*/
204+
public function test_invalid_collection_fields_are_skipped() {
205+
$this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::get_collection_fields' );
206+
$reported = &$this->record_notices( 'WP_Entity_Fields_Registry::get_collection_fields' );
207+
$fields = wp_get_entity_field_collection_fields( DIR_TESTDATA . '/entity-fields/collections/invalid_fields/book' );
208+
209+
$this->assertSame( array( 'valid' ), array_keys( $fields ), 'Only the valid field is read.' );
210+
$this->assertSame( 'Valid', $fields['valid']['label'], 'A later folder with the same id does not replace the earlier one.' );
211+
$this->assertCount( 2, $reported, 'The invalid fields and the duplicated ones are reported once each.' );
212+
$this->assertStringContainsString( 'array_id, empty_id, int_id, not_array, null_id', $reported[0], 'The invalid fields are named by folder.' );
213+
$this->assertStringContainsString( 'valid_again', $reported[1], 'The duplicated fields are named by folder.' );
214+
}
215+
216+
/**
217+
* A collection with invalid fields registers its valid ones, and returns
218+
* false since not all of its fields are registered.
219+
*
220+
* @ticket 66229
221+
*/
222+
public function test_a_collection_with_invalid_fields_registers_the_valid_ones() {
223+
$this->register_post_types( array( 'test_book' => array( 'title' ) ) );
224+
225+
$this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::register_collection' );
226+
$reported = &$this->record_notices( 'WP_Entity_Fields_Registry::register_collection' );
227+
$results = $this->register_fixture_collections( 'invalid_fields', array( 'book' ) );
228+
$fields = wp_get_registered_entity_fields( 'postType', 'test_book' );
229+
230+
$this->assertSame( array( 'book' => false ), $results, 'A collection that skips a field returns false.' );
231+
$this->assertSame( array( 'valid' ), array_column( $fields, 'id' ) );
232+
$this->assertSame( 'Valid', $fields[0]['label'] );
233+
$this->assertCount( 2, $reported, 'The notices name the method that registers the collection.' );
234+
}
235+
196236
/**
197237
* A collection is registered on the action, like fields are: elsewhere
198238
* it is refused and the registry is left untouched.

0 commit comments

Comments
 (0)