Code Modernization: Use str_contains() in a few more places.

`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).

WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.

This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [55988], [55990], [56014], [56021], [56031], [56032], [56065], [56241].

See #58206.
Built from https://develop.svn.wordpress.org/trunk@56245


git-svn-id: http://core.svn.wordpress.org/trunk@55757 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-07-17 13:18:27 +00:00
parent 54f218ae18
commit 59f1c9c205
10 changed files with 22 additions and 20 deletions

View File

@ -3853,7 +3853,7 @@ function wp_ajax_parse_embed() {
'attr' => $wp_embed->last_attr,
);
if ( strpos( $parsed, 'class="wp-embedded-content' ) ) {
if ( str_contains( $parsed, 'class="wp-embedded-content' ) ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
$script_src = includes_url( 'js/wp-embed.js' );
} else {

View File

@ -1167,9 +1167,9 @@ function resume_theme( $theme, $redirect = '' ) {
*/
if ( ! empty( $redirect ) ) {
$functions_path = '';
if ( strpos( STYLESHEETPATH, $extension ) ) {
if ( str_contains( STYLESHEETPATH, $extension ) ) {
$functions_path = STYLESHEETPATH . '/functions.php';
} elseif ( strpos( TEMPLATEPATH, $extension ) ) {
} elseif ( str_contains( TEMPLATEPATH, $extension ) ) {
$functions_path = TEMPLATEPATH . '/functions.php';
}

View File

@ -679,7 +679,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
}
// Strip multiple slashes out of the URL.
if ( strpos( $redirect['path'], '//' ) > -1 ) {
if ( str_contains( $redirect['path'], '//' ) ) {
$redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
}

View File

@ -4886,7 +4886,7 @@ class wp_xmlrpc_server extends IXR_Server {
return $blogs;
} else {
foreach ( (array) $blogs as $blog ) {
if ( strpos( $blog['url'], $_SERVER['HTTP_HOST'] ) ) {
if ( str_contains( $blog['url'], $_SERVER['HTTP_HOST'] ) ) {
return array( $blog );
}
}

View File

@ -4438,7 +4438,7 @@ function get_avatar_data( $id_or_email, $args = null ) {
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id', absint( $id_or_email ) );
} elseif ( is_string( $id_or_email ) ) {
if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
if ( str_contains( $id_or_email, '@md5.gravatar.com' ) ) {
// MD5 hash.
list( $email_hash ) = explode( '@', $id_or_email );
} else {

View File

@ -75,14 +75,12 @@ function wp_fix_server_vars() {
}
// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests.
if ( isset( $_SERVER['SCRIPT_FILENAME'] )
&& ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) === strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 )
) {
if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && str_ends_with( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) ) {
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
}
// Fix for Dreamhost and other PHP as CGI hosts.
if ( isset( $_SERVER['SCRIPT_NAME'] ) && ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) ) {
if ( isset( $_SERVER['SCRIPT_NAME'] ) && str_contains( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) {
unset( $_SERVER['PATH_INFO'] );
}
@ -937,7 +935,7 @@ function wp_get_mu_plugins() {
}
while ( ( $plugin = readdir( $dh ) ) !== false ) {
if ( '.php' === substr( $plugin, -4 ) ) {
if ( str_ends_with( $plugin, '.php' ) ) {
$mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
}
}
@ -981,7 +979,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 ) )
@ -1617,11 +1615,11 @@ function wp_convert_hr_to_bytes( $value ) {
$value = strtolower( trim( $value ) );
$bytes = (int) $value;
if ( false !== strpos( $value, 'g' ) ) {
if ( str_contains( $value, 'g' ) ) {
$bytes *= GB_IN_BYTES;
} elseif ( false !== strpos( $value, 'm' ) ) {
} elseif ( str_contains( $value, 'm' ) ) {
$bytes *= MB_IN_BYTES;
} elseif ( false !== strpos( $value, 'k' ) ) {
} elseif ( str_contains( $value, 'k' ) ) {
$bytes *= KB_IN_BYTES;
}
@ -1908,7 +1906,7 @@ function wp_is_xml_request() {
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
foreach ( $accepted as $type ) {
if ( false !== strpos( $_SERVER['HTTP_ACCEPT'], $type ) ) {
if ( str_contains( $_SERVER['HTTP_ACCEPT'], $type ) ) {
return true;
}
}

View File

@ -1323,7 +1323,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
'height' => $image_meta['height'],
'file' => $image_basename,
);
} elseif ( strpos( $image_src, $image_meta['file'] ) ) {
} elseif ( str_contains( $image_src, $image_meta['file'] ) ) {
return false;
}

View File

@ -1237,7 +1237,11 @@ if ( ! function_exists( 'auth_redirect' ) ) :
// The cookie is no good, so force login.
nocache_headers();
$redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
if ( str_contains( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) {
$redirect = wp_get_referer();
} else {
$redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
}
$login_url = wp_login_url( $redirect, true );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-beta4-56244';
$wp_version = '6.3-beta4-56245';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -1362,7 +1362,7 @@ switch ( $action ) {
$errors->add( 'loggedout', __( 'You are now logged out.' ), 'message' );
} elseif ( isset( $_GET['registration'] ) && 'disabled' === $_GET['registration'] ) {
$errors->add( 'registerdisabled', __( '<strong>Error:</strong> User registration is currently not allowed.' ) );
} elseif ( strpos( $redirect_to, 'about.php?updated' ) ) {
} elseif ( str_contains( $redirect_to, 'about.php?updated' ) ) {
$errors->add( 'updated', __( '<strong>You have successfully updated WordPress!</strong> Please log back in to see what&#8217;s new.' ), 'message' );
} elseif ( WP_Recovery_Mode_Link_Service::LOGIN_ACTION_ENTERED === $action ) {
$errors->add( 'enter_recovery_mode', __( 'Recovery Mode Initialized. Please log in to continue.' ), 'message' );