From 84e9601e5a2966c0aa80020bbf0c043dd8b6bfbb Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 22 Jun 2023 14:57:24 +0000 Subject: [PATCH] Code Modernization: Replace usage of `substr()` with `str_starts_with()` and `str_ends_with()`. `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. This commit uses `str_starts_with()` and `str_ends_with()` in core files where appropriate: * `$needle === substr( $string, 0, $length )`, where `$length` is the length of `$needle`, is replaced with `str_starts_with( $haystack, $needle )`. * `$needle === substr( $string, $offset )`, where `$offset` is negative and the absolute value of `$offset` is the length of `$needle`, is replaced with `str_ends_with( $haystack, $needle )`. This aims to make the code more readable and consistent, as well as better aligned with modern development practices. Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988]. Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov. Fixes #58220. Built from https://develop.svn.wordpress.org/trunk@55990 git-svn-id: http://core.svn.wordpress.org/trunk@55502 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 2 +- wp-admin/includes/class-core-upgrader.php | 2 +- wp-admin/includes/class-language-pack-upgrader.php | 4 ++-- wp-admin/includes/class-wp-screen.php | 8 ++++---- .../includes/class-wp-site-health-auto-updates.php | 2 +- wp-admin/includes/deprecated.php | 2 +- wp-admin/includes/file.php | 14 +++++++------- wp-admin/includes/media.php | 6 +++--- wp-admin/includes/misc.php | 4 ++-- wp-admin/includes/plugin.php | 10 +++++----- wp-admin/includes/update-core.php | 4 ++-- wp-admin/setup-config.php | 2 +- wp-admin/widgets-form.php | 4 ++-- wp-includes/bookmark-template.php | 2 +- wp-includes/class-wp-customize-manager.php | 2 +- wp-includes/class-wp-customize-nav-menus.php | 2 +- wp-includes/class-wp-embed.php | 2 +- wp-includes/class-wp-http-cookie.php | 4 ++-- wp-includes/class-wp-http-encoding.php | 2 +- wp-includes/class-wp-network.php | 2 +- wp-includes/class-wpdb.php | 4 ++-- wp-includes/comment-template.php | 2 +- wp-includes/deprecated.php | 2 +- wp-includes/formatting.php | 10 +++++----- wp-includes/functions.php | 10 +++++----- wp-includes/http.php | 4 ++-- wp-includes/kses.php | 4 ++-- wp-includes/l10n.php | 4 ++-- wp-includes/load.php | 2 +- wp-includes/media.php | 2 +- wp-includes/ms-blogs.php | 4 ++-- wp-includes/ms-deprecated.php | 2 +- wp-includes/ms-load.php | 4 ++-- wp-includes/ms-settings.php | 4 ++-- wp-includes/pluggable.php | 4 ++-- wp-includes/pomo/po.php | 4 ++-- wp-includes/post.php | 4 ++-- wp-includes/shortcodes.php | 2 +- wp-includes/user.php | 2 +- wp-includes/vars.php | 2 +- wp-includes/version.php | 2 +- wp-includes/widgets.php | 8 ++++---- 42 files changed, 83 insertions(+), 83 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index ad48c2f3b4..9e9315dede 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -3323,7 +3323,7 @@ function wp_ajax_send_attachment_to_editor() { remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' ); - if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { + if ( str_starts_with( $post->post_mime_type, 'image' ) ) { $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none'; $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : ''; diff --git a/wp-admin/includes/class-core-upgrader.php b/wp-admin/includes/class-core-upgrader.php index 138f904e22..dc8fb4b806 100644 --- a/wp-admin/includes/class-core-upgrader.php +++ b/wp-admin/includes/class-core-upgrader.php @@ -405,7 +405,7 @@ class Core_Upgrader extends WP_Upgrader { foreach ( $checksums as $file => $checksum ) { // Skip files which get updated. - if ( 'wp-content' === substr( $file, 0, 10 ) ) { + if ( str_starts_with( $file, 'wp-content' ) ) { continue; } if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum ) { diff --git a/wp-admin/includes/class-language-pack-upgrader.php b/wp-admin/includes/class-language-pack-upgrader.php index 8f99dd35e8..0309570031 100644 --- a/wp-admin/includes/class-language-pack-upgrader.php +++ b/wp-admin/includes/class-language-pack-upgrader.php @@ -336,9 +336,9 @@ class Language_Pack_Upgrader extends WP_Upgrader { $po = false; $mo = false; foreach ( (array) $files as $file => $filedata ) { - if ( '.po' === substr( $file, -3 ) ) { + if ( str_ends_with( $file, '.po' ) ) { $po = true; - } elseif ( '.mo' === substr( $file, -3 ) ) { + } elseif ( str_ends_with( $file, '.mo' ) ) { $mo = true; } } diff --git a/wp-admin/includes/class-wp-screen.php b/wp-admin/includes/class-wp-screen.php index 1901bc8f25..739a182ded 100644 --- a/wp-admin/includes/class-wp-screen.php +++ b/wp-admin/includes/class-wp-screen.php @@ -230,7 +230,7 @@ final class WP_Screen { $post_type = $id; $id = 'post'; // Changes later. Ends up being $base. } else { - if ( '.php' === substr( $id, -4 ) ) { + if ( str_ends_with( $id, '.php' ) ) { $id = substr( $id, 0, -4 ); } @@ -241,16 +241,16 @@ final class WP_Screen { } if ( ! $post_type && $hook_name ) { - if ( '-network' === substr( $id, -8 ) ) { + if ( str_ends_with( $id, '-network' ) ) { $id = substr( $id, 0, -8 ); $in_admin = 'network'; - } elseif ( '-user' === substr( $id, -5 ) ) { + } elseif ( str_ends_with( $id, '-user' ) ) { $id = substr( $id, 0, -5 ); $in_admin = 'user'; } $id = sanitize_key( $id ); - if ( 'edit-comments' !== $id && 'edit-tags' !== $id && 'edit-' === substr( $id, 0, 5 ) ) { + if ( 'edit-comments' !== $id && 'edit-tags' !== $id && str_starts_with( $id, 'edit-' ) ) { $maybe = substr( $id, 5 ); if ( taxonomy_exists( $maybe ) ) { $id = 'edit-tags'; diff --git a/wp-admin/includes/class-wp-site-health-auto-updates.php b/wp-admin/includes/class-wp-site-health-auto-updates.php index 15b382ceb8..d1655eb1d8 100644 --- a/wp-admin/includes/class-wp-site-health-auto-updates.php +++ b/wp-admin/includes/class-wp-site-health-auto-updates.php @@ -358,7 +358,7 @@ class WP_Site_Health_Auto_Updates { $unwritable_files = array(); foreach ( array_keys( $checksums ) as $file ) { - if ( 'wp-content' === substr( $file, 0, 10 ) ) { + if ( str_starts_with( $file, 'wp-content' ) ) { continue; } if ( ! file_exists( ABSPATH . $file ) ) { diff --git a/wp-admin/includes/deprecated.php b/wp-admin/includes/deprecated.php index 63aa7f7fa0..91d47446f5 100644 --- a/wp-admin/includes/deprecated.php +++ b/wp-admin/includes/deprecated.php @@ -1365,7 +1365,7 @@ function wp_dashboard_plugins_output( $rss, $args = array() ) { // Is this random plugin's slug already installed? If so, try again. reset( $plugin_slugs ); foreach ( $plugin_slugs as $plugin_slug ) { - if ( $slug === substr( $plugin_slug, 0, strlen( $slug ) ) ) { + if ( str_starts_with( $plugin_slug, $slug ) ) { unset( $items[$item_key] ); continue 2; } diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 562fece43e..3ead3d98cd 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -1253,7 +1253,7 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) { $signature_url = false; - if ( is_string( $url_path ) && ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) ) { + if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) { $signature_url = str_replace( $url_path, $url_path . '.sig', $url ); } @@ -1646,7 +1646,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); } - if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory. + if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. continue; } @@ -1659,7 +1659,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { $dirname = dirname( $info['name'] ); - if ( '/' === substr( $info['name'], -1 ) ) { + if ( str_ends_with( $info['name'], '/' ) ) { // Directory. $needed_dirs[] = $to . untrailingslashit( $info['name'] ); } elseif ( '.' !== $dirname ) { @@ -1726,11 +1726,11 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); } - if ( '/' === substr( $info['name'], -1 ) ) { // Directory. + if ( str_ends_with( $info['name'], '/' ) ) { // Directory. continue; } - if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files. + if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. continue; } @@ -1800,7 +1800,7 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { // Determine any children directories needed (From within the archive). foreach ( $archive_files as $file ) { - if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory. + if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. continue; } @@ -1866,7 +1866,7 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { continue; } - if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files. + if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. continue; } diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 8313c0235d..a8e5f18a70 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -1354,7 +1354,7 @@ function media_post_single_attachment_fields_to_edit( $form_fields, $post ) { function image_media_send_to_editor( $html, $attachment_id, $attachment ) { $post = get_post( $attachment_id ); - if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { + if ( str_starts_with( $post->post_mime_type, 'image' ) ) { $url = $attachment['url']; $align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none'; $size = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; @@ -1465,7 +1465,7 @@ function get_attachment_fields_to_edit( $post, $errors = null ) { $form_fields = array_merge_recursive( $form_fields, (array) $errors ); // This was formerly in image_attachment_fields_to_edit(). - if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { + if ( str_starts_with( $post->post_mime_type, 'image' ) ) { $alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); if ( empty( $alt ) ) { @@ -3220,7 +3220,7 @@ function edit_form_image_editor( $post ) { ?>
- post_mime_type, 0, 5 ) ) : ?> + post_mime_type, 'image' ) ) : ?>


diff --git a/wp-admin/includes/misc.php b/wp-admin/includes/misc.php index 7922e8c270..209e644978 100644 --- a/wp-admin/includes/misc.php +++ b/wp-admin/includes/misc.php @@ -81,7 +81,7 @@ function extract_from_markers( $filename, $marker ) { } if ( $state ) { - if ( '#' === substr( $markerline, 0, 1 ) ) { + if ( str_starts_with( $markerline, '#' ) ) { continue; } @@ -750,7 +750,7 @@ function set_screen_options() { default: $screen_option = false; - if ( '_page' === substr( $option, -5 ) || 'layout_columns' === $option ) { + if ( str_ends_with( $option, '_page' ) || 'layout_columns' === $option ) { /** * Filters a screen option value before it is set. * diff --git a/wp-admin/includes/plugin.php b/wp-admin/includes/plugin.php index 49b574ec4b..1ab53717e7 100644 --- a/wp-admin/includes/plugin.php +++ b/wp-admin/includes/plugin.php @@ -294,7 +294,7 @@ function get_plugins( $plugin_folder = '' ) { if ( $plugins_dir ) { while ( ( $file = readdir( $plugins_dir ) ) !== false ) { - if ( '.' === substr( $file, 0, 1 ) ) { + if ( str_starts_with( $file, '.' ) ) { continue; } @@ -303,11 +303,11 @@ function get_plugins( $plugin_folder = '' ) { if ( $plugins_subdir ) { while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) { - if ( '.' === substr( $subfile, 0, 1 ) ) { + if ( str_starts_with( $subfile, '.' ) ) { continue; } - if ( '.php' === substr( $subfile, -4 ) ) { + if ( str_ends_with( $subfile, '.php' ) ) { $plugin_files[] = "$file/$subfile"; } } @@ -315,7 +315,7 @@ function get_plugins( $plugin_folder = '' ) { closedir( $plugins_subdir ); } } else { - if ( '.php' === substr( $file, -4 ) ) { + if ( str_ends_with( $file, '.php' ) ) { $plugin_files[] = $file; } } @@ -371,7 +371,7 @@ function get_mu_plugins() { $plugins_dir = @opendir( WPMU_PLUGIN_DIR ); if ( $plugins_dir ) { while ( ( $file = readdir( $plugins_dir ) ) !== false ) { - if ( '.php' === substr( $file, -4 ) ) { + if ( str_ends_with( $file, '.php' ) ) { $plugin_files[] = $file; } } diff --git a/wp-admin/includes/update-core.php b/wp-admin/includes/update-core.php index 506836ed5b..268e0956e4 100644 --- a/wp-admin/includes/update-core.php +++ b/wp-admin/includes/update-core.php @@ -1242,7 +1242,7 @@ function update_core( $from, $to ) { if ( is_array( $checksums ) ) { foreach ( $checksums as $file => $checksum ) { - if ( 'wp-content' === substr( $file, 0, 10 ) ) { + if ( str_starts_with( $file, 'wp-content' ) ) { continue; } @@ -1349,7 +1349,7 @@ function update_core( $from, $to ) { if ( isset( $checksums ) && is_array( $checksums ) ) { foreach ( $checksums as $file => $checksum ) { - if ( 'wp-content' === substr( $file, 0, 10 ) ) { + if ( str_starts_with( $file, 'wp-content' ) ) { continue; } diff --git a/wp-admin/setup-config.php b/wp-admin/setup-config.php index 93e32e0b5e..5d2ae4b895 100644 --- a/wp-admin/setup-config.php +++ b/wp-admin/setup-config.php @@ -363,7 +363,7 @@ switch ( $step ) { $key = 0; foreach ( $config_file as $line_num => $line ) { - if ( '$table_prefix =' === substr( $line, 0, 15 ) ) { + if ( str_starts_with( $line, '$table_prefix =' ) ) { $config_file[ $line_num ] = '$table_prefix = \'' . addcslashes( $prefix, "\\'" ) . "';\r\n"; continue; } diff --git a/wp-admin/widgets-form.php b/wp-admin/widgets-form.php index 4b544b6342..b77f23dbc3 100644 --- a/wp-admin/widgets-form.php +++ b/wp-admin/widgets-form.php @@ -301,7 +301,7 @@ if ( isset( $_GET['editwidget'] ) && $_GET['editwidget'] ) { $sbvalue ) { echo "\t\t"; - if ( 'wp_inactive_widgets' === $sbname || 'orphaned_widgets' === substr( $sbname, 0, 16 ) ) { + if ( 'wp_inactive_widgets' === $sbname || str_starts_with( $sbname, 'orphaned_widgets' ) ) { echo ' '; } else { if ( ! isset( $sidebars_widgets[ $sbname ] ) || ! is_array( $sidebars_widgets[ $sbname ] ) ) { @@ -450,7 +450,7 @@ do_action( 'widgets_admin_page' ); $theme_sidebars = array(); foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) { - if ( str_contains( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) { + if ( str_contains( $registered_sidebar['class'], 'inactive-sidebar' ) || str_starts_with( $sidebar, 'orphaned_widgets' ) ) { $wrap_class = 'widgets-holder-wrap'; if ( ! empty( $registered_sidebar['class'] ) ) { $wrap_class .= ' ' . $registered_sidebar['class']; diff --git a/wp-includes/bookmark-template.php b/wp-includes/bookmark-template.php index 0573329daa..d9bca2bbd7 100644 --- a/wp-includes/bookmark-template.php +++ b/wp-includes/bookmark-template.php @@ -83,7 +83,7 @@ function _walk_bookmarks( $bookmarks, $args = '' ) { $title = $desc; if ( $parsed_args['show_updated'] ) { - if ( '00' !== substr( $bookmark->link_updated_f, 0, 2 ) ) { + if ( ! str_starts_with( $bookmark->link_updated_f, '00' ) ) { $title .= ' ('; $title .= sprintf( /* translators: %s: Date and time of last update. */ diff --git a/wp-includes/class-wp-customize-manager.php b/wp-includes/class-wp-customize-manager.php index ce85de9bd4..e8b8c7f199 100644 --- a/wp-includes/class-wp-customize-manager.php +++ b/wp-includes/class-wp-customize-manager.php @@ -6076,7 +6076,7 @@ final class WP_Customize_Manager { __( 'This video file is too large to use as a header video. Try a shorter video or optimize the compression settings and re-upload a file that is less than 8MB. Or, upload your video to YouTube and link it with the option below.' ) ); } - if ( '.mp4' !== substr( $video, -4 ) && '.mov' !== substr( $video, -4 ) ) { // Check for .mp4 or .mov format, which (assuming h.264 encoding) are the only cross-browser-supported formats. + if ( ! str_ends_with( $video, '.mp4' ) && ! str_ends_with( $video, '.mov' ) ) { // Check for .mp4 or .mov format, which (assuming h.264 encoding) are the only cross-browser-supported formats. $validity->add( 'invalid_file_type', sprintf( diff --git a/wp-includes/class-wp-customize-nav-menus.php b/wp-includes/class-wp-customize-nav-menus.php index 3be8c06105..bbda8b51b3 100644 --- a/wp-includes/class-wp-customize-nav-menus.php +++ b/wp-includes/class-wp-customize-nav-menus.php @@ -1472,7 +1472,7 @@ final class WP_Customize_Nav_Menus { ( ! empty( $args['container'] ) || - ( isset( $args['items_wrap'] ) && '<' === substr( $args['items_wrap'], 0, 1 ) ) + ( isset( $args['items_wrap'] ) && str_starts_with( $args['items_wrap'], '<' ) ) ) ); $args['can_partial_refresh'] = $can_partial_refresh; diff --git a/wp-includes/class-wp-embed.php b/wp-includes/class-wp-embed.php index 55515e2254..daa4d30790 100644 --- a/wp-includes/class-wp-embed.php +++ b/wp-includes/class-wp-embed.php @@ -388,7 +388,7 @@ class WP_Embed { } foreach ( $post_metas as $post_meta_key ) { - if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) { + if ( str_starts_with( $post_meta_key, '_oembed_' ) ) { delete_post_meta( $post_id, $post_meta_key ); } } diff --git a/wp-includes/class-wp-http-cookie.php b/wp-includes/class-wp-http-cookie.php index 00e8ec0370..e7ea4bc96d 100644 --- a/wp-includes/class-wp-http-cookie.php +++ b/wp-includes/class-wp-http-cookie.php @@ -115,7 +115,7 @@ class WP_Http_Cookie { $this->domain = $parsed_url['host']; } $this->path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '/'; - if ( '/' !== substr( $this->path, -1 ) ) { + if ( ! str_ends_with( $this->path, '/' ) ) { $this->path = dirname( $this->path ) . '/'; } @@ -202,7 +202,7 @@ class WP_Http_Cookie { } // Host - very basic check that the request URL ends with the domain restriction (minus leading dot). - $domain = ( '.' === substr( $domain, 0, 1 ) ) ? substr( $domain, 1 ) : $domain; + $domain = ( str_starts_with( $domain, '.' ) ) ? substr( $domain, 1 ) : $domain; if ( substr( $url['host'], -strlen( $domain ) ) !== $domain ) { return false; } diff --git a/wp-includes/class-wp-http-encoding.php b/wp-includes/class-wp-http-encoding.php index 255b2114ea..f2f82e3801 100644 --- a/wp-includes/class-wp-http-encoding.php +++ b/wp-includes/class-wp-http-encoding.php @@ -104,7 +104,7 @@ class WP_Http_Encoding { public static function compatible_gzinflate( $gz_data ) { // Compressed data might contain a full header, if so strip it for gzinflate(). - if ( "\x1f\x8b\x08" === substr( $gz_data, 0, 3 ) ) { + if ( str_starts_with( $gz_data, "\x1f\x8b\x08" ) ) { $i = 10; $flg = ord( substr( $gz_data, 3, 1 ) ); if ( $flg > 0 ) { diff --git a/wp-includes/class-wp-network.php b/wp-includes/class-wp-network.php index fd92c5ba70..b1d46b7ce0 100644 --- a/wp-includes/class-wp-network.php +++ b/wp-includes/class-wp-network.php @@ -306,7 +306,7 @@ class WP_Network { } $this->cookie_domain = $this->domain; - if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) { + if ( str_starts_with( $this->cookie_domain, 'www.' ) ) { $this->cookie_domain = substr( $this->cookie_domain, 4 ); } } diff --git a/wp-includes/class-wpdb.php b/wp-includes/class-wpdb.php index 6ea7403bf1..0f145e7f8b 100644 --- a/wp-includes/class-wpdb.php +++ b/wp-includes/class-wpdb.php @@ -1564,7 +1564,7 @@ class wpdb { $type = substr( $placeholder, -1 ); if ( 'f' === $type && true === $this->allow_unsafe_unquoted_parameters - && '%' === substr( $split_query[ $key - 1 ], -1, 1 ) + && str_ends_with( $split_query[ $key - 1 ], '%' ) ) { /* @@ -1625,7 +1625,7 @@ class wpdb { * Second, if "%s" has a "%" before it, even if it's unrelated (e.g. "LIKE '%%%s%%'"). */ if ( true !== $this->allow_unsafe_unquoted_parameters - || ( '' === $format && '%' !== substr( $split_query[ $key - 1 ], -1, 1 ) ) + || ( '' === $format && ! str_ends_with( $split_query[ $key - 1 ], '%' ) ) ) { $placeholder = "'%" . $format . "s'"; } diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 3027cc606b..59b2361d84 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -425,7 +425,7 @@ function get_comment_author_url_link( $link_text = '', $before = '', $after = '' $display = str_replace( 'http://www.', '', $display ); $display = str_replace( 'http://', '', $display ); - if ( '/' === substr( $display, -1 ) ) { + if ( str_ends_with( $display, '/' ) ) { $display = substr( $display, 0, -1 ); } diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 35c0d3094f..cac1126dbe 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -1042,7 +1042,7 @@ function get_links_list($order = 'name') { // Handle link category sorting. $direction = 'ASC'; - if ( '_' === substr($order,0,1) ) { + if ( str_starts_with( $order, '_' ) ) { $direction = 'DESC'; $order = substr($order,1); } diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 0782af0d2a..dce33c19b2 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -242,7 +242,7 @@ function wptexturize( $text, $reset = false ) { // Only call _wptexturize_pushpop_element if $curl is a delimiter. $first = $curl[0]; if ( '<' === $first ) { - if ( '' ), '', $content ); while ( ( $newstring = wp_kses( $content, $allowed_html, $allowed_protocols ) ) != $content ) { $content = $newstring; diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 9467a6b19c..8444bf2c82 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -1181,7 +1181,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) { } // Translations are always based on the unminified filename. - if ( substr( $relative, -7 ) === '.min.js' ) { + if ( str_ends_with( $relative, '.min.js' ) ) { $relative = substr( $relative, 0, -7 ) . '.js'; } @@ -1454,7 +1454,7 @@ function wp_get_installed_translations( $type ) { if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) { continue; } - if ( substr( $file, -3 ) !== '.po' ) { + if ( ! str_ends_with( $file, '.po' ) ) { continue; } if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) { diff --git a/wp-includes/load.php b/wp-includes/load.php index 33a21af6ba..6e7ab4cbcd 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -835,7 +835,7 @@ function wp_get_mu_plugins() { return $mu_plugins; } while ( ( $plugin = readdir( $dh ) ) !== false ) { - if ( '.php' === substr( $plugin, -4 ) ) { + if ( str_ends_with( $plugin, '.php' ) ) { $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin; } } diff --git a/wp-includes/media.php b/wp-includes/media.php index bf803f5773..0cddaca4f5 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1324,7 +1324,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain * (which is to say, when they share the domain name of the current request). */ - if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) { + if ( is_ssl() && ! str_starts_with( $image_baseurl, 'https' ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) { $image_baseurl = set_url_scheme( $image_baseurl, 'https' ); } diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index 035b93e9da..eddb99c605 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -137,7 +137,7 @@ function get_blog_details( $fields = null, $get_all = true ) { if ( false !== $blog ) { return $blog; } - if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { + if ( str_starts_with( $fields['domain'], 'www.' ) ) { $nowww = substr( $fields['domain'], 4 ); $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) ); } else { @@ -155,7 +155,7 @@ function get_blog_details( $fields = null, $get_all = true ) { if ( false !== $blog ) { return $blog; } - if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { + if ( str_starts_with( $fields['domain'], 'www.' ) ) { $nowww = substr( $fields['domain'], 4 ); $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); } else { diff --git a/wp-includes/ms-deprecated.php b/wp-includes/ms-deprecated.php index 8ef357857c..c6c7f544c0 100644 --- a/wp-includes/ms-deprecated.php +++ b/wp-includes/ms-deprecated.php @@ -295,7 +295,7 @@ function wpmu_admin_do_redirect( $url = '' ) { if ( isset( $_GET['redirect'] ) && isset( $_POST['redirect'] ) && $_GET['redirect'] !== $_POST['redirect'] ) { wp_die( __( 'A variable mismatch has been detected.' ), __( 'Sorry, you are not allowed to view this item.' ), 400 ); } elseif ( isset( $_GET['redirect'] ) ) { - if ( 's_' === substr( $_GET['redirect'], 0, 2 ) ) + if ( str_starts_with( $_GET['redirect'], 's_' ) ) $url .= '&action=blogs&s='. esc_html( substr( $_GET['redirect'], 2 ) ); } elseif ( isset( $_POST['redirect'] ) ) { $url = wpmu_admin_redirect_add_updated_param( $_POST['redirect'] ); diff --git a/wp-includes/ms-load.php b/wp-includes/ms-load.php index 70c98b2c5e..7601a6a279 100644 --- a/wp-includes/ms-load.php +++ b/wp-includes/ms-load.php @@ -46,7 +46,7 @@ function wp_get_active_network_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. ) { $plugins[] = WP_PLUGIN_DIR . '/' . $plugin; @@ -229,7 +229,7 @@ function get_site_by_path( $domain, $path, $segments = null ) { // Either www or non-www is supported, not both. If a www domain is requested, // query for both to provide the proper redirect. $domains = array( $domain ); - if ( 'www.' === substr( $domain, 0, 4 ) ) { + if ( str_starts_with( $domain, 'www.' ) ) { $domains[] = substr( $domain, 4 ); } diff --git a/wp-includes/ms-settings.php b/wp-includes/ms-settings.php index 6824895880..27dc1337f5 100644 --- a/wp-includes/ms-settings.php +++ b/wp-includes/ms-settings.php @@ -55,10 +55,10 @@ ms_subdomain_constants(); if ( ! isset( $current_site ) || ! isset( $current_blog ) ) { $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) ); - if ( ':80' === substr( $domain, -3 ) ) { + if ( str_ends_with( $domain, ':80' ) ) { $domain = substr( $domain, 0, -3 ); $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 ); - } elseif ( ':443' === substr( $domain, -4 ) ) { + } elseif ( str_ends_with( $domain, ':443' ) ) { $domain = substr( $domain, 0, -4 ); $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 ); } diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index d8549d0abe..73d7d20748 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -376,7 +376,7 @@ if ( ! function_exists( 'wp_mail' ) ) : $from_email = 'wordpress@'; if ( null !== $sitename ) { - if ( 'www.' === substr( $sitename, 0, 4 ) ) { + if ( str_starts_with( $sitename, 'www.' ) ) { $sitename = substr( $sitename, 4 ); } @@ -1557,7 +1557,7 @@ if ( ! function_exists( 'wp_validate_redirect' ) ) : function wp_validate_redirect( $location, $fallback_url = '' ) { $location = wp_sanitize_redirect( trim( $location, " \t\n\r\0\x08\x0B" ) ); // Browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'. - if ( '//' === substr( $location, 0, 2 ) ) { + if ( str_starts_with( $location, '//' ) ) { $location = 'http:' . $location; } diff --git a/wp-includes/pomo/po.php b/wp-includes/pomo/po.php index a443765707..89d827c255 100644 --- a/wp-includes/pomo/po.php +++ b/wp-includes/pomo/po.php @@ -505,10 +505,10 @@ if ( ! class_exists( 'PO', false ) ) : * @return string */ public static function trim_quotes( $s ) { - if ( '"' === substr( $s, 0, 1 ) ) { + if ( str_starts_with( $s, '"' ) ) { $s = substr( $s, 1 ); } - if ( '"' === substr( $s, -1, 1 ) ) { + if ( str_ends_with( $s, '"' ) ) { $s = substr( $s, 0, -1 ); } return $s; diff --git a/wp-includes/post.php b/wp-includes/post.php index 2b39d90c18..0ae4c3c152 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -6762,7 +6762,7 @@ function wp_mime_type_icon( $mime = 0 ) { if ( $dh ) { while ( false !== $file = readdir( $dh ) ) { $file = wp_basename( $file ); - if ( '.' === substr( $file, 0, 1 ) ) { + if ( str_starts_with( $file, '.' ) ) { continue; } @@ -7813,7 +7813,7 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) { $post = get_post( $post ); - if ( '__trashed' === substr( $post->post_name, -9 ) ) { + if ( str_ends_with( $post->post_name, '__trashed' ) ) { return $post->post_name; } add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php index d4e874fd74..79c780eca1 100644 --- a/wp-includes/shortcodes.php +++ b/wp-includes/shortcodes.php @@ -423,7 +423,7 @@ function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) { continue; } - if ( $ignore_html || '