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/src/wp-includes/taxonomy.php

    r63426 r63488  
    13101310 * @since 4.8.0 Introduced 'suppress_filter' parameter.
    13111311 *
    1312  * @param array|string $args       Optional. Array or string of arguments. See WP_Term_Query::__construct()
     1312 * @param array|string $args       Optional. Array or string of arguments. See {@see WP_Term_Query::__construct()}
    13131313 *                                 for information on accepted arguments. Default empty array.
    13141314 * @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format.
    … …  
    13161316 *                                 function parameter will be parsed as a taxonomy or array of taxonomies.
    13171317 *                                 Default empty.
    1318  * @return WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string,
    1319  *                                                  or WP_Error if any of the taxonomies do not exist.
    1320  *                                                  See the function description for more information.
     1318 * @return WP_Term[]|int[]|string[]|int|string|WP_Error Array of terms, a count thereof as a numeric string,
     1319 *                                                      the integer 0 when the queried parent term is not in
     1320 *                                                      the taxonomy hierarchy, or WP_Error if any of the
     1321 *                                                      taxonomies do not exist. See the function description
     1322 *                                                      for more information.
     1323 *
     1324 * @phpstan-return (
     1325 *     $args is array{ fields: 'count', ... }
     1326 *         ? 0|numeric-string|WP_Error
     1327 *         : ( $args is array{ fields: 'ids'|'tt_ids', ... }
     1328 *             ? int[]|WP_Error
     1329 *             : ( $args is array{ fields: 'id=>parent', ... }
     1330 *                 ? array<int, int>|WP_Error
     1331 *                 : ( $args is array{ fields: 'names'|'slugs', ... }
     1332 *                     ? string[]|WP_Error
     1333 *                     : ( $args is array{ fields: 'id=>name'|'id=>slug', ... }
     1334 *                         ? array<int, string>|WP_Error
     1335 *                         : ( $deprecated is ''
     1336 *                             ? ( $args is array
     1337 *                                 ? WP_Term[]|WP_Error
     1338 *                                 : WP_Term[]|int[]|string[]|int|string|WP_Error )
     1339 *                             : WP_Term[]|int[]|string[]|int|string|WP_Error ) ) ) ) )
     1340 * )
    13211341 */
    13221342function get_terms( $args = array(), $deprecated = '' ) {
    … …  
    19581978 * @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
    19591979 *
    1960  * @param array|string $args       Optional. Array or string of arguments. See WP_Term_Query::__construct()
     1980 * @param array|string $args       Optional. Array or string of arguments. See {@see WP_Term_Query::__construct()}
    19611981 *                                 for information on accepted arguments. Default empty array.
    19621982 * @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format.
    … …  
    19641984 *                                 function parameter will be parsed as a taxonomy or array of taxonomies.
    19651985 *                                 Default empty.
    1966  * @return string|WP_Error Numeric string containing the number of terms in that
    1967  *                         taxonomy or WP_Error if the taxonomy does not exist.
     1986 * @return string|int|WP_Error Numeric string containing the number of terms in that taxonomy,
     1987 *                             the integer 0 when the queried parent term is not in the taxonomy
     1988 *                             hierarchy, or WP_Error if the taxonomy does not exist.
     1989 * @phpstan-return numeric-string|0|WP_Error
    19681990 */
    19691991function wp_count_terms( $args = array(), $deprecated = '' ) {
    … …  
    19932015        }
    19942016
    1995         $args['fields'] = 'count';
     2017        $args = array_merge(
     2018                $args,
     2019                array( 'fields' => 'count' )
     2020        );
    19962021
    19972022        return get_terms( $args );
    … …  
    22902315 * @since 6.3.0 Passing `update_term_meta_cache` argument value false by default resulting in get_terms() to not
    22912316 *              prime the term meta cache.
     2317 * @since 7.2.0 A count is returned as a numeric string when `$fields` is 'count'. Previously requesting
     2318 *              a count resulted in an error.
    22922319 *
    22932320 * @param int|int[]       $object_ids The ID(s) of the object(s) to retrieve.
    22942321 * @param string|string[] $taxonomies The taxonomy names to retrieve terms from.
    2295  * @param array|string    $args       See WP_Term_Query::__construct() for supported arguments.
     2322 * @param array|string    $args       See {@see WP_Term_Query::__construct()} for supported arguments.
    22962323 * @return WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string,
    22972324 *                                                  or WP_Error if any of the taxonomies do not exist.
    2298  *                                                  See WP_Term_Query::get_terms() for more information.
     2325 *                                                  See {@see WP_Term_Query::get_terms()} for more information.
     2326 *
     2327 * @phpstan-return (
     2328 *     $args is array{ fields: 'count', ... }
     2329 *         ? numeric-string|WP_Error
     2330 *         : ( $args is array{ fields: 'ids'|'tt_ids', ... }
     2331 *             ? int[]|WP_Error
     2332 *             : ( $args is array{ fields: 'id=>parent', ... }
     2333 *                 ? array<int, int>|WP_Error
     2334 *                 : ( $args is array{ fields: 'names'|'slugs', ... }
     2335 *                     ? string[]|WP_Error
     2336 *                     : ( $args is array{ fields: 'id=>name'|'id=>slug', ... }
     2337 *                         ? array<int, string>|WP_Error
     2338 *                         : ( $args is array
     2339 *                             ? WP_Term[]|WP_Error
     2340 *                             : WP_Term[]|int[]|string[]|string|WP_Error ) ) ) ) )
     2341 * )
    22992342 */
    23002343function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
     2344        $args = wp_parse_args( $args );
     2345
    23012346        if ( empty( $object_ids ) || empty( $taxonomies ) ) {
     2347                if ( isset( $args['fields'] ) && 'count' === $args['fields'] ) {
     2348                        return '0';
     2349                }
     2350
    23022351                return array();
    23032352        }
    … …  
    23392388         * When one or more queried taxonomies is registered with an 'args' array,
    23402389         * those params override the `$args` passed to this function.
     2390         *
     2391         * That array is for query modifiers such as 'orderby'. Overriding 'fields'
     2392         * there does not work, because callers depend on the shape they asked for:
     2393         * get_the_taxonomies() reads `$term->name` off the result, and
     2394         * wp_set_object_terms() needs the 'tt_ids' it requested. A taxonomy that
     2395         * overrides it also returns a different shape than the taxonomies queried
     2396         * below, leaving the two merged into one another here.
    23412397         */
    23422398        $terms = array();
    … …  
    23462402                        if ( isset( $t->args ) && is_array( $t->args ) && array_merge( $args, $t->args ) != $args ) {
    23472403                                unset( $taxonomies[ $index ] );
    2348                                 $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
     2404
     2405                                // Cast because a count is returned as a numeric string. The counts are summed below.
     2406                                $terms_from_taxonomy = (array) wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) );
     2407
     2408                                // Array keys should be preserved for values of $fields that use term_id for keys.
     2409                                if ( ! empty( $args['fields'] ) && str_starts_with( $args['fields'], 'id=>' ) ) {
     2410                                        $terms = $terms + $terms_from_taxonomy;
     2411                                } else {
     2412                                        $terms = array_merge( $terms, $terms_from_taxonomy );
     2413                                }
    23492414                        }
    23502415                }
    … …  
    23612426        // Taxonomies registered without an 'args' param are handled here.
    23622427        if ( ! empty( $taxonomies ) ) {
    2363                 $terms_from_remaining_taxonomies = get_terms( $args );
     2428                // Cast because a count is returned as a numeric string. The counts are summed below.
     2429                $terms_from_remaining_taxonomies = (array) get_terms( $args );
    23642430
    23652431                // Array keys should be preserved for values of $fields that use term_id for keys.
    … …  
    23712437        }
    23722438
     2439        if ( isset( $args['fields'] ) && 'count' === $args['fields'] ) {
     2440                $terms = (string) array_sum( $terms );
     2441        }
     2442
    23732443        /**
    23742444         * Filters the terms for a given object or objects.
    … …  
    23802450         * @param string[]                        $taxonomies Array of taxonomy names from which terms were retrieved.
    23812451         * @param array                           $args       Array of arguments for retrieving terms for the given
    2382          *                                                    object(s). See wp_get_object_terms() for details.
     2452         *                                                    object(s). See {@see wp_get_object_terms()} for details.
    23832453         */
    23842454        $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args );
    … …  
    23992469         * @param string                          $taxonomies SQL fragment of taxonomy names from which terms were retrieved.
    24002470         * @param array                           $args       Array of arguments for retrieving terms for the given
    2401          *                                                    object(s). See wp_get_object_terms() for details.
     2471         *                                                    object(s). See {@see wp_get_object_terms()} for details.
    24022472         */
    24032473        return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
Note: See TracChangeset for help on using the changeset viewer.