Make WordPress Core


Ignore:
Timestamp:
09/05/2026 01:39:01 AM (2 months ago)
Author:
westonruter
Message:

Taxonomy: Fix wp_get_object_terms() when requesting a count.

Requesting a count passed the numeric string get_terms() returns straight to array_merge(), which warns and yields null on PHP 7.4 and throws a TypeError on PHP 8.0 and later. The count is now cast before it is merged, and summed once every taxonomy has been queried, so the function returns the numeric string its documentation has described since r49947. An empty object or taxonomy list returns '0' for the same reason, rather than the empty array that contradicted the documented type.

A taxonomy registered with an args array is queried by a separate recursive call, and those results were merged with array_merge() unconditionally. For the id=> values of fields the term IDs are integer array keys, which array_merge() renumbers, so such a taxonomy came back keyed from zero. The recursive branch now uses the union operator for those, as the merge for the remaining taxonomies has done since r41809.

The functions get_terms(), wp_get_object_terms(), wp_count_terms(), wp_get_post_categories(), wp_get_post_tags() and wp_get_post_terms() each gain a conditional return type that resolves the result from the fields value, falling back to what that function's own default answers with, as was done for WP_Term_Query::query() in r63358.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/7278.
Follow-up to r38667, r40513, r41809, r49947, r63358.

Props marian1, westonruter, swissspidy.
See #65817, #66049.
Fixes #61936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r62751 r63488  
    66 */
    77class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
    8         private $taxonomy = 'wptests_tax';
     8        /**
     9         * Name of the taxonomy registered for each test.
     10         */
     11        private string $taxonomy = 'wptests_tax';
    912
    1013        /**
    … …  
    1215         *
    1316         * Used in the `test_taxonomies_passed_to_wp_get_object_terms_filter_should_be_quoted()` method.
    14          *
    15          * @var array
    16          */
    17         private $taxonomies;
     17         * This is a SQL fragment of taxonomy names from which terms were retrieved.
     18         */
     19        private ?string $taxonomies = null;
    1820
    1921        public function set_up() {
    … …  
    2628         */
    2729        public function tear_down() {
    28                 unset( $this->taxonomies );
     30                $this->taxonomies = null;
    2931
    3032                parent::tear_down();
    … …  
    10131015                $this->assertSameSets( $expected, $actual );
    10141016        }
     1017
     1018        /**
     1019         * @ticket 61936
     1020         */
     1021        public function test_should_return_numeric_string_for_fields_count() {
     1022                $fields = array( 'fields' => 'count' );
     1023
     1024                $taxonomies = array( 'wptests_tax', 'wptests_tax_2' );
     1025
     1026                register_taxonomy( $taxonomies[0], 'post' );
     1027                register_taxonomy( $taxonomies[1], 'post' );
     1028
     1029                $term_1_id = self::factory()->term->create( array( 'taxonomy' => $taxonomies[0] ) );
     1030                $term_2_id = self::factory()->term->create( array( 'taxonomy' => $taxonomies[1] ) );
     1031
     1032                $post_ids = self::factory()->post->create_many( 2 );
     1033                wp_set_object_terms( $post_ids[0], $term_1_id, $taxonomies[0] );
     1034                wp_set_object_terms( $post_ids[1], $term_1_id, $taxonomies[0] );
     1035                wp_set_object_terms( $post_ids[1], $term_2_id, $taxonomies[1] );
     1036
     1037                $terms_count = wp_get_object_terms( 0, $taxonomies[0], $fields );
     1038                $this->assertSame( '0', $terms_count, 'Incorrect term count with empty object_ids.' );
     1039
     1040                $terms_count = wp_get_object_terms( $post_ids[0], '', $fields );
     1041                $this->assertSame( '0', $terms_count, 'Incorrect term count with empty taxonomies.' );
     1042
     1043                $terms_count = wp_get_object_terms( $post_ids[0], $taxonomies[0], $fields );
     1044                $this->assertSame( '1', $terms_count, 'Incorrect term count with single object_id and single taxonomy.' );
     1045
     1046                $terms_count = wp_get_object_terms( $post_ids[0], $taxonomies, $fields );
     1047                $this->assertSame( '1', $terms_count, 'Incorrect term count with single object_id and multiple taxonomies.' );
     1048
     1049                $terms_count = wp_get_object_terms( $post_ids[1], $taxonomies, $fields );
     1050                $this->assertSame( '2', $terms_count, 'Incorrect term count with single object_id and multiple taxonomies.' );
     1051
     1052                $terms_count = wp_get_object_terms( $post_ids, $taxonomies, $fields );
     1053                $this->assertSame( '3', $terms_count, 'Incorrect term count with multiple object_ids and multiple taxonomies.' );
     1054        }
     1055
     1056        /**
     1057         * Ensures that a count can be requested with the `$args` parameter in query string form.
     1058         *
     1059         * An empty object ID or taxonomy list short-circuits before `$args` would otherwise be
     1060         * parsed, so the query string has to be parsed up front for the `fields` value to be read.
     1061         *
     1062         * @ticket 61936
     1063         */
     1064        public function test_should_return_numeric_string_for_fields_count_passed_as_query_string() {
     1065                $post_id = self::factory()->post->create();
     1066                $this->assertIsInt( $post_id, 'The post was not created.' );
     1067
     1068                $term_id = self::factory()->term->create( array( 'taxonomy' => $this->taxonomy ) );
     1069                $this->assertIsInt( $term_id, 'The term was not created.' );
     1070
     1071                wp_set_object_terms( $post_id, $term_id, $this->taxonomy );
     1072
     1073                $terms_count = wp_get_object_terms( 0, $this->taxonomy, 'fields=count' );
     1074                $this->assertSame( '0', $terms_count, 'Incorrect term count with empty object_ids.' );
     1075
     1076                $terms_count = wp_get_object_terms( $post_id, '', 'fields=count' );
     1077                $this->assertSame( '0', $terms_count, 'Incorrect term count with empty taxonomies.' );
     1078
     1079                $terms_count = wp_get_object_terms( $post_id, $this->taxonomy, 'fields=count' );
     1080                $this->assertSame( '1', $terms_count, 'Incorrect term count.' );
     1081        }
     1082
     1083        /**
     1084         * Ensures that a count is summed across a taxonomy registered with an 'args' array.
     1085         *
     1086         * Such a taxonomy is queried by a recursive call rather than alongside the others,
     1087         * and that call returns the count as a numeric string rather than as an array.
     1088         *
     1089         * @ticket 61936
     1090         */
     1091        public function test_should_return_numeric_string_for_fields_count_with_taxonomy_registered_with_args() {
     1092                $taxonomy1 = 'wptests_tax';
     1093                $taxonomy2 = 'wptests_tax_2';
     1094
     1095                // Any non-empty 'args' array sends the taxonomy down the recursive path.
     1096                register_taxonomy( $taxonomy1, 'post', array( 'args' => array( 0 ) ) );
     1097                register_taxonomy( $taxonomy2, 'post' );
     1098
     1099                $post_id = self::factory()->post->create();
     1100                $this->assertIsInt( $post_id, 'The post was not created.' );
     1101
     1102                $term_1_id = self::factory()->term->create( array( 'taxonomy' => $taxonomy1 ) );
     1103                $this->assertIsInt( $term_1_id, 'The term in the first taxonomy was not created.' );
     1104
     1105                $term_2_id = self::factory()->term->create( array( 'taxonomy' => $taxonomy2 ) );
     1106                $this->assertIsInt( $term_2_id, 'The term in the second taxonomy was not created.' );
     1107
     1108                wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
     1109                wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
     1110
     1111                $terms_count = wp_get_object_terms( $post_id, $taxonomy1, array( 'fields' => 'count' ) );
     1112                $this->assertSame( '1', $terms_count, 'Incorrect term count with the single taxonomy registered with args.' );
     1113
     1114                $terms_count = wp_get_object_terms( $post_id, array( $taxonomy1, $taxonomy2 ), array( 'fields' => 'count' ) );
     1115                $this->assertSame( '2', $terms_count, 'Incorrect term count across both taxonomies.' );
     1116        }
     1117
     1118        /**
     1119         * Verifies that the `id=>` values of the `fields` argument key the results by term ID.
     1120         *
     1121         * These are the values for which the results are merged with `+` rather than with
     1122         * array_merge(), so that the term ID keys survive.
     1123         *
     1124         * @ticket 61936
     1125         */
     1126        public function test_should_return_terms_keyed_by_term_id_for_id_fields() {
     1127                register_taxonomy( 'wptests_tax_hierarchical', 'post', array( 'hierarchical' => true ) );
     1128
     1129                $parent_id = self::factory()->term->create(
     1130                        array(
     1131                                'taxonomy' => 'wptests_tax_hierarchical',
     1132                                'name'     => 'Parent',
     1133                                'slug'     => 'parent',
     1134                        )
     1135                );
     1136                $this->assertIsInt( $parent_id, 'The parent term was not created.' );
     1137
     1138                $child_id = self::factory()->term->create(
     1139                        array(
     1140                                'taxonomy' => 'wptests_tax_hierarchical',
     1141                                'name'     => 'Child',
     1142                                'slug'     => 'child',
     1143                                'parent'   => $parent_id,
     1144                        )
     1145                );
     1146                $this->assertIsInt( $child_id, 'The child term was not created.' );
     1147
     1148                $post_id = self::factory()->post->create();
     1149                $this->assertIsInt( $post_id, 'The post was not created.' );
     1150
     1151                wp_set_object_terms( $post_id, array( $parent_id, $child_id ), 'wptests_tax_hierarchical' );
     1152
     1153                $expected = array(
     1154                        'id=>parent' => array(
     1155                                $parent_id => 0,
     1156                                $child_id  => $parent_id,
     1157                        ),
     1158                        'id=>name'   => array(
     1159                                $parent_id => 'Parent',
     1160                                $child_id  => 'Child',
     1161                        ),
     1162                        'id=>slug'   => array(
     1163                                $parent_id => 'parent',
     1164                                $child_id  => 'child',
     1165                        ),
     1166                );
     1167
     1168                foreach ( $expected as $fields => $expected_terms ) {
     1169                        $actual = wp_get_object_terms(
     1170                                $post_id,
     1171                                'wptests_tax_hierarchical',
     1172                                array(
     1173                                        'fields'  => $fields,
     1174                                        'orderby' => 'term_id',
     1175                                        'order'   => 'ASC',
     1176                                )
     1177                        );
     1178
     1179                        $this->assertSame( $expected_terms, $actual, "Incorrect terms for the '$fields' fields value." );
     1180                }
     1181        }
     1182
     1183        /**
     1184         * Ensures the term ID keys survive for a taxonomy registered with an 'args' array.
     1185         *
     1186         * Such a taxonomy is queried by a recursive call, and those results were merged with
     1187         * array_merge(), which renumbers the integer keys the `id=>` values rely on.
     1188         *
     1189         * @ticket 61936
     1190         */
     1191        public function test_should_return_terms_keyed_by_term_id_for_id_fields_with_taxonomy_registered_with_args() {
     1192                $taxonomy1 = 'wptests_tax';
     1193                $taxonomy2 = 'wptests_tax_2';
     1194
     1195                // Any non-empty 'args' array sends the taxonomy down the recursive path.
     1196                register_taxonomy( $taxonomy1, 'post', array( 'args' => array( 0 ) ) );
     1197                register_taxonomy( $taxonomy2, 'post' );
     1198
     1199                $post_id = self::factory()->post->create();
     1200                $this->assertIsInt( $post_id, 'The post was not created.' );
     1201
     1202                $term_1_id = self::factory()->term->create(
     1203                        array(
     1204                                'taxonomy' => $taxonomy1,
     1205                                'name'     => 'Alpha',
     1206                        )
     1207                );
     1208                $this->assertIsInt( $term_1_id, 'The term in the first taxonomy was not created.' );
     1209
     1210                $term_2_id = self::factory()->term->create(
     1211                        array(
     1212                                'taxonomy' => $taxonomy2,
     1213                                'name'     => 'Beta',
     1214                        )
     1215                );
     1216                $this->assertIsInt( $term_2_id, 'The term in the second taxonomy was not created.' );
     1217
     1218                wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
     1219                wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
     1220
     1221                $actual = wp_get_object_terms(
     1222                        $post_id,
     1223                        array( $taxonomy1, $taxonomy2 ),
     1224                        array( 'fields' => 'id=>name' )
     1225                );
     1226
     1227                $expected = array(
     1228                        $term_1_id => 'Alpha',
     1229                        $term_2_id => 'Beta',
     1230                );
     1231
     1232                $this->assertSame( $expected, $actual, 'Terms are not keyed by term ID.' );
     1233        }
    10151234}
Note: See TracChangeset for help on using the changeset viewer.