Make WordPress Core

Changeset 54518


Ignore:
Timestamp:
10/14/2022 03:53:33 PM (4 years ago)
Author:
SergeyBiryukov
Message:

REST API: Simplify the check for an array of arrays in register_rest_route().

The previous implementation of checking whether the args parameter is an array of arrays used array_filter(), which would always loop the full array, even though we are only interested in finding one (the first) non-array to display a _doing_it_wrong() message.

This commit aims to improve readability and performance of this check by using a foreach loop instead, leaving it as soon as the first non-array argument is found.

Follow-up to [54339].

Props TobiasBg, audrasjb, costdev, SergeyBiryukov.
Fixes #56804.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r54346 r54518  
    105105                }
    106106
    107                 if ( count( array_filter( $arg_group['args'], 'is_array' ) ) !== count( $arg_group['args'] ) ) {
    108                         _doing_it_wrong(
    109                                 __FUNCTION__,
    110                                 sprintf(
    111                                         /* translators: 1: $args, 2: The REST API route being registered. */
    112                                         __( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ),
    113                                         '<code>$args</code>',
    114                                         '<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>'
    115                                 ),
    116                                 '6.1.0'
    117                         );
     107                foreach ( $arg_group['args'] as $arg ) {
     108                        if ( ! is_array( $arg ) ) {
     109                                _doing_it_wrong(
     110                                        __FUNCTION__,
     111                                        sprintf(
     112                                                /* translators: 1: $args, 2: The REST API route being registered. */
     113                                                __( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ),
     114                                                '<code>$args</code>',
     115                                                '<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>'
     116                                        ),
     117                                        '6.1.0'
     118                                );
     119                                break; // Leave the foreach loop once a non-array argument was found.
     120                        }
    118121                }
    119122        }
Note: See TracChangeset for help on using the changeset viewer.