From d36aa352455d8df717f74182594b8fa81feec827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 23 Jun 2026 10:15:40 +0200 Subject: [PATCH 1/8] Code Modernization: Use array_find() and array_find_key() in various places. Replace `foreach` loops that return the first matching element (or its key) with the `array_find()` and `array_find_key()` functions. `array_find()` and `array_find_key()` were introduced in PHP 8.4; polyfills have been available in core since WordPress 6.8.0 (see `wp-includes/compat.php`), so this is safe on all supported PHP versions. Affected functions: * `wp_ext2type()` * `rest_find_matching_pattern_property_schema()` * `wp_get_sidebar()` Props soeren. See #XXXXX. Co-Authored-By: Claude Opus 4.8 --- src/wp-includes/functions.php | 7 +------ src/wp-includes/rest-api.php | 9 ++++----- src/wp-includes/widgets.php | 7 +++---- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 355d9f8a1ec37..31e530c4ce8f9 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3015,12 +3015,7 @@ function wp_ext2type( $ext ) { $ext = strtolower( $ext ); $ext2type = wp_get_ext_types(); - foreach ( $ext2type as $type => $exts ) { - if ( in_array( $ext, $exts, true ) ) { - return $type; - } - } - return null; + return array_find_key( $ext2type, fn( $exts ) => in_array( $ext, $exts, true ) ); } /** diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index a4c22e8f1cca1..44049a44a8b0c 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1852,11 +1852,10 @@ function rest_validate_json_schema_pattern( $pattern, $value ) { */ function rest_find_matching_pattern_property_schema( $property, $args ) { if ( isset( $args['patternProperties'] ) ) { - foreach ( $args['patternProperties'] as $pattern => $child_schema ) { - if ( rest_validate_json_schema_pattern( $pattern, $property ) ) { - return $child_schema; - } - } + return array_find( + $args['patternProperties'], + fn( $child_schema, $pattern ) => rest_validate_json_schema_pattern( $pattern, $property ) + ); } return null; diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index 7a3cb5673a4fa..6d424f8b367e3 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1066,10 +1066,9 @@ function wp_get_sidebars_widgets( $deprecated = true ) { function wp_get_sidebar( $id ) { global $wp_registered_sidebars; - foreach ( (array) $wp_registered_sidebars as $sidebar ) { - if ( $sidebar['id'] === $id ) { - return $sidebar; - } + $sidebar = array_find( (array) $wp_registered_sidebars, fn( $sidebar ) => $sidebar['id'] === $id ); + if ( null !== $sidebar ) { + return $sidebar; } if ( 'wp_inactive_widgets' === $id ) { From d72b86ecf1d3f7154d193952758e904b660c7f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 23 Jun 2026 10:22:44 +0200 Subject: [PATCH 2/8] Improve --- src/wp-includes/rest-api.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 44049a44a8b0c..f01d7dd6a6259 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1851,14 +1851,10 @@ function rest_validate_json_schema_pattern( $pattern, $value ) { * @return array|null The schema of matching pattern property, or null if no patterns match. */ function rest_find_matching_pattern_property_schema( $property, $args ) { - if ( isset( $args['patternProperties'] ) ) { - return array_find( - $args['patternProperties'], - fn( $child_schema, $pattern ) => rest_validate_json_schema_pattern( $pattern, $property ) - ); - } - - return null; + return array_find( + $args['patternProperties'] ?? array(), + fn( $child_schema, $pattern ) => rest_validate_json_schema_pattern( $pattern, $property ) + ); } /** From 8795b5e128fcdc2d847828f9d0d756616963ccd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 23 Jun 2026 10:35:58 +0200 Subject: [PATCH 3/8] Update src/wp-includes/widgets.php Co-authored-by: Christoph Daum --- src/wp-includes/widgets.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index 6d424f8b367e3..cf05cee6ff218 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1066,7 +1066,10 @@ function wp_get_sidebars_widgets( $deprecated = true ) { function wp_get_sidebar( $id ) { global $wp_registered_sidebars; - $sidebar = array_find( (array) $wp_registered_sidebars, fn( $sidebar ) => $sidebar['id'] === $id ); + $sidebar = array_find( + (array) $wp_registered_sidebars, + fn( $sidebar ) => $sidebar['id'] === $id + ); if ( null !== $sidebar ) { return $sidebar; } From f0706b429114d33ee9a676cc66e4ecf773f26b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 23 Jun 2026 10:36:08 +0200 Subject: [PATCH 4/8] Update src/wp-includes/functions.php Co-authored-by: Christoph Daum --- src/wp-includes/functions.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 31e530c4ce8f9..51d4fb02d6d8e 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3015,7 +3015,10 @@ function wp_ext2type( $ext ) { $ext = strtolower( $ext ); $ext2type = wp_get_ext_types(); - return array_find_key( $ext2type, fn( $exts ) => in_array( $ext, $exts, true ) ); + return array_find_key( + $ext2type, + fn( $exts ) => in_array( $ext, $exts, true ) + ); } /** From 22fa189134c4292e11ed97b0c33fb5fdc32fdd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 23 Jun 2026 10:48:58 +0200 Subject: [PATCH 5/8] Revert --- src/wp-includes/widgets.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index cf05cee6ff218..7a3cb5673a4fa 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1066,12 +1066,10 @@ function wp_get_sidebars_widgets( $deprecated = true ) { function wp_get_sidebar( $id ) { global $wp_registered_sidebars; - $sidebar = array_find( - (array) $wp_registered_sidebars, - fn( $sidebar ) => $sidebar['id'] === $id - ); - if ( null !== $sidebar ) { - return $sidebar; + foreach ( (array) $wp_registered_sidebars as $sidebar ) { + if ( $sidebar['id'] === $id ) { + return $sidebar; + } } if ( 'wp_inactive_widgets' === $id ) { From 8a747271902f67311ca38b2928659704bad35a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Thu, 9 Jul 2026 12:25:19 +0200 Subject: [PATCH 6/8] Add another array_find_key() --- src/wp-includes/class-wp-view-config-data.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-view-config-data.php b/src/wp-includes/class-wp-view-config-data.php index ea075891c4845..1e7a7f1ca1dc1 100644 --- a/src/wp-includes/class-wp-view-config-data.php +++ b/src/wp-includes/class-wp-view-config-data.php @@ -259,13 +259,10 @@ public function update_view_list_items( array $items, int $version ) { // The patch key is the identity. unset( $value['slug'] ); - $index = null; - foreach ( $view_list as $i => $item ) { - if ( is_array( $item ) && isset( $item['slug'] ) && $item['slug'] === $slug ) { - $index = $i; - break; - } - } + $index = array_find_key( + $view_list, + fn( $item ) => is_array( $item ) && isset( $item['slug'] ) && $item['slug'] === $slug + ); if ( null === $index ) { $view_list[] = array_merge( array( 'slug' => $slug ), $value ); From fc76c13f6cf70967c9ad2a56adf9d619f88d5f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Thu, 30 Jul 2026 20:15:05 +0200 Subject: [PATCH 7/8] Use `array_find()` --- .../class-wp-interactivity-api.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index 62bfc29cfc03f..cdf13b13119e8 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -1387,15 +1387,12 @@ private function merge_style_property( string $style_attribute_value, string $st */ private function data_wp_text_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) { if ( 'enter' === $mode ) { - $entries = $this->get_directive_entries( $p, 'text' ); - $valid_entry = null; + $entries = $this->get_directive_entries( $p, 'text' ); // Get the first valid `data-wp-text` entry without suffix or unique ID. - foreach ( $entries as $entry ) { - if ( null === $entry['suffix'] && null === $entry['unique_id'] && ! empty( $entry['value'] ) ) { - $valid_entry = $entry; - break; - } - } + $valid_entry = array_find( + $entries, + fn( $entry ) => null === $entry['suffix'] && null === $entry['unique_id'] && ! empty( $entry['value'] ) + ); if ( null === $valid_entry ) { return; } From 89c3d3ba427943c37bbb30630667d4837f88a414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Thu, 30 Jul 2026 20:51:27 +0200 Subject: [PATCH 8/8] Revert --- src/wp-includes/rest-api.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index a62857ec1330b..d54cee18c5b39 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1868,10 +1868,15 @@ function rest_validate_json_schema_pattern( $pattern, $value ) { * @return array|null The schema of matching pattern property, or null if no patterns match. */ function rest_find_matching_pattern_property_schema( $property, $args ) { - return array_find( - $args['patternProperties'] ?? array(), - fn( $child_schema, $pattern ) => rest_validate_json_schema_pattern( $pattern, $property ) - ); + if ( isset( $args['patternProperties'] ) ) { + foreach ( $args['patternProperties'] as $pattern => $child_schema ) { + if ( rest_validate_json_schema_pattern( $pattern, $property ) ) { + return $child_schema; + } + } + } + + return null; } /**