From b1ebb813bfcc87d465bfcdca02d5afba553cc81b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 24 Jun 2023 09:50:34 +0000 Subject: [PATCH] Code Modernization: Use `str_ends_with()` in a few more places. `str_ends_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) ends with the given substring (needle). WordPress core includes a polyfill for `str_ends_with()` on PHP < 8.0 as of WordPress 5.9. Follow-up to [55990]. See #58220. git-svn-id: https://develop.svn.wordpress.org/trunk@56014 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/plugin-editor.php | 2 +- src/wp-admin/theme-editor.php | 2 +- src/wp-includes/class-wp-rewrite.php | 2 +- src/wp-includes/load.php | 2 +- src/wp-includes/rest-api.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/plugin-editor.php b/src/wp-admin/plugin-editor.php index 78eac87f92..1b0b714c32 100644 --- a/src/wp-admin/plugin-editor.php +++ b/src/wp-admin/plugin-editor.php @@ -162,7 +162,7 @@ if ( ! empty( $posted_content ) ) { $content = file_get_contents( $real_file ); } -if ( '.php' === substr( $real_file, strrpos( $real_file, '.' ) ) ) { +if ( str_ends_with( $real_file, '.php' ) ) { $functions = wp_doc_link_parse( $content ); if ( ! empty( $functions ) ) { diff --git a/src/wp-admin/theme-editor.php b/src/wp-admin/theme-editor.php index 345d00017f..07de6269a7 100644 --- a/src/wp-admin/theme-editor.php +++ b/src/wp-admin/theme-editor.php @@ -161,7 +161,7 @@ if ( ! empty( $posted_content ) ) { $f = fopen( $file, 'r' ); $content = fread( $f, filesize( $file ) ); - if ( '.php' === substr( $file, strrpos( $file, '.' ) ) ) { + if ( str_ends_with( $file, '.php' ) ) { $functions = wp_doc_link_parse( $content ); if ( ! empty( $functions ) ) { diff --git a/src/wp-includes/class-wp-rewrite.php b/src/wp-includes/class-wp-rewrite.php index 35598c3d80..1963f21b0c 100644 --- a/src/wp-includes/class-wp-rewrite.php +++ b/src/wp-includes/class-wp-rewrite.php @@ -1907,7 +1907,7 @@ class WP_Rewrite { unset( $this->feed_structure ); unset( $this->comment_feed_structure ); - $this->use_trailing_slashes = ( '/' === substr( $this->permalink_structure, -1, 1 ) ); + $this->use_trailing_slashes = str_ends_with( $this->permalink_structure, '/' ); // Enable generic rules for pages if permalink structure doesn't begin with a wildcard. if ( preg_match( '/^[^%]*%(?:postname|category|tag|author)%/', $this->permalink_structure ) ) { diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 6e7ab4cbcd..9d20aff3d7 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -877,7 +877,7 @@ function wp_get_active_and_valid_plugins() { foreach ( $active_plugins as $plugin ) { if ( ! validate_file( $plugin ) // $plugin must validate as file. - && '.php' === substr( $plugin, -4 ) // $plugin must end with '.php'. + && str_ends_with( $plugin, '.php' ) // $plugin must end with '.php'. && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist. // Not already included as a network plugin. && ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) ) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index d9dd834c90..70dc9deec6 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -467,7 +467,7 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) ); // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php. // To work around this, we manually add index.php to the URL, avoiding the redirect. - if ( 'index.php' !== substr( $url, 9 ) ) { + if ( ! str_ends_with( $url, 'index.php' ) ) { $url .= 'index.php'; }