From 32e94b4de108fb090ebc678b48113b8954f7f188 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 24 Jun 2023 16:50:24 +0000 Subject: [PATCH] Code Modernization: Use `str_starts_with()` and `str_ends_with()` in a few more places. `str_starts_with()` and `str_ends_with()` were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle). WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9. Follow-up to [55990], [56014]. See #58220. Built from https://develop.svn.wordpress.org/trunk@56019 git-svn-id: http://core.svn.wordpress.org/trunk@55531 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-query.php | 2 +- wp-includes/deprecated.php | 2 +- wp-includes/link-template.php | 2 +- wp-includes/ms-functions.php | 5 ++--- .../endpoints/class-wp-rest-attachments-controller.php | 2 +- wp-includes/version.php | 2 +- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 8afad87ec3..bdfce5e52c 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -1461,7 +1461,7 @@ class WP_Query { foreach ( $q['search_terms'] as $term ) { // If there is an $exclusion_prefix, terms prefixed with it should be excluded. - $exclude = $exclusion_prefix && ( substr( $term, 0, 1 ) === $exclusion_prefix ); + $exclude = $exclusion_prefix && str_starts_with( $term, $exclusion_prefix ); if ( $exclude ) { $like_op = 'NOT LIKE'; $andor_op = 'AND'; diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index cac1126dbe..750260ab56 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -947,7 +947,7 @@ function get_links($category = -1, $before = '', $after = '
', $between = ' _deprecated_function( __FUNCTION__, '2.1.0', 'get_bookmarks()' ); $order = 'ASC'; - if ( substr($orderby, 0, 1) === '_' ) { + if ( str_starts_with($orderby, '_') ) { $order = 'DESC'; $orderby = substr($orderby, 1); } diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 16c9da2d3b..2fe4483a7a 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -3891,7 +3891,7 @@ function set_url_scheme( $url, $scheme = null ) { } $url = trim( $url ); - if ( substr( $url, 0, 2 ) === '//' ) { + if ( str_starts_with( $url, '//' ) ) { $url = 'http:' . $url; } diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index bf62d0c127..3a4dd3115d 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -401,13 +401,12 @@ function is_email_address_unsafe( $user_email ) { continue; } - if ( $email_domain == $banned_domain ) { + if ( $email_domain === $banned_domain ) { $is_email_address_unsafe = true; break; } - $dotted_domain = ".$banned_domain"; - if ( substr( $normalized_email, -strlen( $dotted_domain ) ) === $dotted_domain ) { + if ( str_ends_with( $normalized_email, ".$banned_domain" ) ) { $is_email_address_unsafe = true; break; } diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 98c0a1bb64..d1f91b1e20 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -1137,7 +1137,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $filename = trim( $attributes['filename'] ); // Unquote quoted filename, but after trimming. - if ( substr( $filename, 0, 1 ) === '"' && substr( $filename, -1, 1 ) === '"' ) { + if ( str_starts_with( $filename, '"' ) && str_ends_with( $filename, '"' ) ) { $filename = substr( $filename, 1, -1 ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index b11ed29d98..04ceb76707 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-56018'; +$wp_version = '6.3-alpha-56019'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.