From 9101a4abffe20b430d558ec57973872a404fb321 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Wed, 23 Oct 2024 13:06:37 +0200 Subject: [PATCH] Fixes the warning if null is given to prepare_items_query() See https://core.trac.wordpress.org/ticket/62287 --- .../class-wp-rest-posts-controller.php | 24 ++++++++++--------- .../class-wp-rest-revisions-controller.php | 8 ++++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index c4db4871df644..1cf06d45b2819 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1181,17 +1181,19 @@ public function delete_item( $request ) { protected function prepare_items_query( $prepared_args = array(), $request = null ) { $query_args = array(); - foreach ( $prepared_args as $key => $value ) { - /** - * Filters the query_vars used in get_items() for the constructed query. - * - * The dynamic portion of the hook name, `$key`, refers to the query_var key. - * - * @since 4.7.0 - * - * @param string $value The query_var value. - */ - $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + if ( is_array( $prepared_args ) ) { + foreach ( $prepared_args as $key => $value ) { + /** + * Filters the query_vars used in get_items() for the constructed query. + * + * The dynamic portion of the hook name, `$key`, refers to the query_var key. + * + * @since 4.7.0 + * + * @param string $value The query_var value. + */ + $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + } } if ( 'post' !== $this->post_type || ! isset( $query_args['ignore_sticky_posts'] ) ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index fb5fa29231a4f..a25d565428f90 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -532,9 +532,11 @@ public function delete_item( $request ) { protected function prepare_items_query( $prepared_args = array(), $request = null ) { $query_args = array(); - foreach ( $prepared_args as $key => $value ) { - /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ - $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + if ( is_array( $prepared_args ) ) { + foreach ( $prepared_args as $key => $value ) { + /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ + $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + } } // Map to proper WP_Query orderby param.