From 59f1c9c205e98284d9a927f38061ab47263c5d0b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 17 Jul 2023 13:18:27 +0000 Subject: [PATCH] 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 --- wp-admin/includes/ajax-actions.php | 2 +- wp-admin/includes/theme.php | 4 ++-- wp-includes/canonical.php | 2 +- wp-includes/class-wp-xmlrpc-server.php | 2 +- wp-includes/link-template.php | 2 +- wp-includes/load.php | 18 ++++++++---------- wp-includes/media.php | 2 +- wp-includes/pluggable.php | 6 +++++- wp-includes/version.php | 2 +- wp-login.php | 2 +- 10 files changed, 22 insertions(+), 20 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 1cbcdff3dc..3c5dab31b5 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -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 { diff --git a/wp-admin/includes/theme.php b/wp-admin/includes/theme.php index 82b9715631..d98cb5de33 100644 --- a/wp-admin/includes/theme.php +++ b/wp-admin/includes/theme.php @@ -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'; } diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 79832d3f29..606973b31d 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -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'] ); } diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index c6cf0fece2..d1aec36932 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -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 ); } } diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 5a09674622..c0437f6de9 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -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 { diff --git a/wp-includes/load.php b/wp-includes/load.php index ae4ec69a84..c03368901e 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -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; } } diff --git a/wp-includes/media.php b/wp-includes/media.php index b706cd9897..f4d6a5a67d 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -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; } diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index cd3cad1b7b..ea422fce2f 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 3adfdddbfd..2e0f48d59f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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. diff --git a/wp-login.php b/wp-login.php index f0de91a102..8810f5d0d8 100644 --- a/wp-login.php +++ b/wp-login.php @@ -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', __( 'Error: User registration is currently not allowed.' ) ); - } elseif ( strpos( $redirect_to, 'about.php?updated' ) ) { + } elseif ( str_contains( $redirect_to, 'about.php?updated' ) ) { $errors->add( 'updated', __( 'You have successfully updated WordPress! Please log back in to see what’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' );