From 0023f179d2e4d7d94ac2e21967a21de10c56a099 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 14 Oct 2022 15:53:33 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@54518 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index ae1e714ea8..1d6b8a7e68 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -104,17 +104,20 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f ); } - if ( count( array_filter( $arg_group['args'], 'is_array' ) ) !== count( $arg_group['args'] ) ) { - _doing_it_wrong( - __FUNCTION__, - sprintf( - /* translators: 1: $args, 2: The REST API route being registered. */ - __( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ), - '$args', - '' . $clean_namespace . '/' . trim( $route, '/' ) . '' - ), - '6.1.0' - ); + foreach ( $arg_group['args'] as $arg ) { + if ( ! is_array( $arg ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: $args, 2: The REST API route being registered. */ + __( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ), + '$args', + '' . $clean_namespace . '/' . trim( $route, '/' ) . '' + ), + '6.1.0' + ); + break; // Leave the foreach loop once a non-array argument was found. + } } }