From 971fe5e550b9363c02be4a61d4e8ef0200a44621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 7 Jul 2026 09:25:43 +0200 Subject: [PATCH 1/2] Use modern PHP string functions instead of strpos/substr Replace legacy `strpos()` and `substr()` checks with `str_contains()`, `str_starts_with()` and `str_ends_with()` for better readability and consistency across the codebase. --- lib/class-wp-icons-registry-gutenberg.php | 4 ++-- lib/experimental/knowledge/knowledge.php | 6 +++--- lib/experimental/rest-api-overrides.php | 2 +- packages/block-library/src/rss/index.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/class-wp-icons-registry-gutenberg.php b/lib/class-wp-icons-registry-gutenberg.php index 1ba92121c43be8..37fb1d88c7b2d5 100644 --- a/lib/class-wp-icons-registry-gutenberg.php +++ b/lib/class-wp-icons-registry-gutenberg.php @@ -34,7 +34,7 @@ public function register( $icon_name, $icon_properties ) { return false; } - if ( false === strpos( $icon_name, '/' ) ) { + if ( !str_contains( $icon_name, '/' ) ) { _doing_it_wrong( __METHOD__, __( 'Icon name must be namespaced in the form "collection/icon-name".', 'gutenberg' ), @@ -289,7 +289,7 @@ function gutenberg_override_wp_icons_registry() { // the `core/` namespace onto the Gutenberg registry so they are not lost. if ( null !== $original_registry ) { foreach ( $original_registry->get_registered_icons() as $icon ) { - if ( strpos( $icon['name'], 'core/' ) === 0 ) { + if ( str_starts_with($icon['name'], 'core/') ) { continue; } $icon_properties = array( 'label' => $icon['label'] ); diff --git a/lib/experimental/knowledge/knowledge.php b/lib/experimental/knowledge/knowledge.php index 687a34b3d2f3c9..e43892f6eccf0a 100644 --- a/lib/experimental/knowledge/knowledge.php +++ b/lib/experimental/knowledge/knowledge.php @@ -179,7 +179,7 @@ function wp_knowledge_ensure_default_type_term( int $post_id ): void { return; } - if ( 0 === strpos( $post->post_name, 'guideline-' ) ) { + if ( str_starts_with($post->post_name, 'guideline-') ) { $term_id = wp_knowledge_get_or_create_type_term( 'guideline' ); if ( null !== $term_id ) { wp_set_object_terms( $post_id, $term_id, 'wp_knowledge_type' ); @@ -337,7 +337,7 @@ function wp_knowledge_maybe_map_term_label( array $data, string $taxonomy ): arr * @return string|null Scope key, or null if not a registry scope. */ function wp_guideline_scope_from_slug( string $slug ): ?string { - if ( 0 !== strpos( $slug, 'guideline-' ) || 0 === strpos( $slug, 'guideline-block-' ) ) { + if ( !str_starts_with($slug, 'guideline-') || str_starts_with($slug, 'guideline-block-') ) { return null; } @@ -383,7 +383,7 @@ function wp_knowledge_guard_guideline_row( $prepared_post, $request ) { // phpcs } } - if ( 0 !== strpos( (string) $slug, 'guideline-' ) ) { + if ( !str_starts_with((string) $slug, 'guideline-') ) { return $prepared_post; } diff --git a/lib/experimental/rest-api-overrides.php b/lib/experimental/rest-api-overrides.php index 75cd408e353f0a..c337270a633173 100644 --- a/lib/experimental/rest-api-overrides.php +++ b/lib/experimental/rest-api-overrides.php @@ -32,7 +32,7 @@ function gutenberg_boot_allow_auto_draft_in_rest_api() { foreach ( $post_types as $post_type ) { $supports_editor = post_type_supports( $post_type->name, 'editor' ); $supports_title = post_type_supports( $post_type->name, 'title' ); - $is_wp_core_system = strpos( $post_type->name, 'wp_' ) === 0; + $is_wp_core_system = str_starts_with($post_type->name, 'wp_'); $is_attachment = 'attachment' === $post_type->name; // Only add auto-draft to content post types: diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index 5ab990bbcbcee9..de5407998f75fd 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -97,7 +97,7 @@ function render_block_core_rss( $attributes ) { $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); // Change existing [...] to […]. - if ( '[...]' === substr( $excerpt, -5 ) ) { + if ( str_ends_with($excerpt, '[...]') ) { $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; } From 0e374794ab2ddd0c1117c2894e46080be2a1b6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 7 Jul 2026 09:30:18 +0200 Subject: [PATCH 2/2] Format code --- lib/class-wp-icons-registry-gutenberg.php | 4 ++-- lib/experimental/knowledge/knowledge.php | 6 +++--- lib/experimental/rest-api-overrides.php | 2 +- packages/block-library/src/rss/index.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/class-wp-icons-registry-gutenberg.php b/lib/class-wp-icons-registry-gutenberg.php index 37fb1d88c7b2d5..d7ddc477922c8e 100644 --- a/lib/class-wp-icons-registry-gutenberg.php +++ b/lib/class-wp-icons-registry-gutenberg.php @@ -34,7 +34,7 @@ public function register( $icon_name, $icon_properties ) { return false; } - if ( !str_contains( $icon_name, '/' ) ) { + if ( ! str_contains( $icon_name, '/' ) ) { _doing_it_wrong( __METHOD__, __( 'Icon name must be namespaced in the form "collection/icon-name".', 'gutenberg' ), @@ -289,7 +289,7 @@ function gutenberg_override_wp_icons_registry() { // the `core/` namespace onto the Gutenberg registry so they are not lost. if ( null !== $original_registry ) { foreach ( $original_registry->get_registered_icons() as $icon ) { - if ( str_starts_with($icon['name'], 'core/') ) { + if ( str_starts_with( $icon['name'], 'core/' ) ) { continue; } $icon_properties = array( 'label' => $icon['label'] ); diff --git a/lib/experimental/knowledge/knowledge.php b/lib/experimental/knowledge/knowledge.php index e43892f6eccf0a..3e1507ae81790a 100644 --- a/lib/experimental/knowledge/knowledge.php +++ b/lib/experimental/knowledge/knowledge.php @@ -179,7 +179,7 @@ function wp_knowledge_ensure_default_type_term( int $post_id ): void { return; } - if ( str_starts_with($post->post_name, 'guideline-') ) { + if ( str_starts_with( $post->post_name, 'guideline-' ) ) { $term_id = wp_knowledge_get_or_create_type_term( 'guideline' ); if ( null !== $term_id ) { wp_set_object_terms( $post_id, $term_id, 'wp_knowledge_type' ); @@ -337,7 +337,7 @@ function wp_knowledge_maybe_map_term_label( array $data, string $taxonomy ): arr * @return string|null Scope key, or null if not a registry scope. */ function wp_guideline_scope_from_slug( string $slug ): ?string { - if ( !str_starts_with($slug, 'guideline-') || str_starts_with($slug, 'guideline-block-') ) { + if ( ! str_starts_with( $slug, 'guideline-' ) || str_starts_with( $slug, 'guideline-block-' ) ) { return null; } @@ -383,7 +383,7 @@ function wp_knowledge_guard_guideline_row( $prepared_post, $request ) { // phpcs } } - if ( !str_starts_with((string) $slug, 'guideline-') ) { + if ( ! str_starts_with( (string) $slug, 'guideline-' ) ) { return $prepared_post; } diff --git a/lib/experimental/rest-api-overrides.php b/lib/experimental/rest-api-overrides.php index c337270a633173..bddab7fe88cebf 100644 --- a/lib/experimental/rest-api-overrides.php +++ b/lib/experimental/rest-api-overrides.php @@ -32,7 +32,7 @@ function gutenberg_boot_allow_auto_draft_in_rest_api() { foreach ( $post_types as $post_type ) { $supports_editor = post_type_supports( $post_type->name, 'editor' ); $supports_title = post_type_supports( $post_type->name, 'title' ); - $is_wp_core_system = str_starts_with($post_type->name, 'wp_'); + $is_wp_core_system = str_starts_with( $post_type->name, 'wp_' ); $is_attachment = 'attachment' === $post_type->name; // Only add auto-draft to content post types: diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index de5407998f75fd..c33bafaf762c6c 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -97,7 +97,7 @@ function render_block_core_rss( $attributes ) { $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); // Change existing [...] to […]. - if ( str_ends_with($excerpt, '[...]') ) { + if ( str_ends_with( $excerpt, '[...]' ) ) { $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; }