Code Modernization: Replace usage of strpos() with str_starts_with().

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

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

This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.

While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.

Follow-up to [52039], [52040], [52326].

Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.

git-svn-id: https://develop.svn.wordpress.org/trunk@55703 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-05-02 15:43:03 +00:00
parent 73dbbe5372
commit 4cb3e1582a
54 changed files with 105 additions and 105 deletions
+1 -1
View File
@@ -165,7 +165,7 @@ function resolve_block_template( $template_type, $template_hierarchy, $fallback_
// Is the active theme a child theme, and is the PHP fallback template part of it?
if (
strpos( $fallback_template, $theme_base_path ) === 0 &&
str_starts_with( $fallback_template, $theme_base_path ) &&
strpos( $fallback_template, $parent_theme_base_path ) === false
) {
$fallback_template_slug = substr(
+10 -10
View File
@@ -17,14 +17,14 @@
*/
function remove_block_asset_path_prefix( $asset_handle_or_path ) {
$path_prefix = 'file:';
if ( 0 !== strpos( $asset_handle_or_path, $path_prefix ) ) {
if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
return $asset_handle_or_path;
}
$path = substr(
$asset_handle_or_path,
strlen( $path_prefix )
);
if ( strpos( $path, './' ) === 0 ) {
if ( str_starts_with( $path, './' ) ) {
$path = substr( $path, 2 );
}
return $path;
@@ -44,12 +44,12 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
* @return string Generated asset name for the block's field.
*/
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
if ( 0 === strpos( $block_name, 'core/' ) ) {
if ( str_starts_with( $block_name, 'core/' ) ) {
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
if ( 0 === strpos( $field_name, 'editor' ) ) {
if ( str_starts_with( $field_name, 'editor' ) ) {
$asset_handle .= '-editor';
}
if ( 0 === strpos( $field_name, 'view' ) ) {
if ( str_starts_with( $field_name, 'view' ) ) {
$asset_handle .= '-view';
}
if ( $index > 0 ) {
@@ -137,8 +137,8 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
$is_theme_block = str_starts_with( $script_path_norm, $theme_path_norm );
$script_uri = plugins_url( $script_path, $metadata['file'] );
if ( $is_core_block ) {
@@ -191,7 +191,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
}
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
// Skip registering individual styles for each core block when a bundled version provided.
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
return false;
@@ -359,7 +359,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$metadata = apply_filters( 'block_type_metadata', $metadata );
// Add `style` and `editor_style` for core blocks if missing.
if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) {
if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) {
$block_name = str_replace( 'core/', '', $metadata['name'] );
if ( ! isset( $metadata['style'] ) ) {
@@ -698,7 +698,7 @@ function serialize_block_attributes( $block_attributes ) {
* @return string Block name to use for serialization.
*/
function strip_core_block_namespace( $block_name = null ) {
if ( is_string( $block_name ) && 0 === strpos( $block_name, 'core/' ) ) {
if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
return substr( $block_name, 5 );
}
+1 -1
View File
@@ -125,7 +125,7 @@ function _walk_bookmarks( $bookmarks, $args = '' ) {
$output .= $parsed_args['link_before'];
if ( null != $bookmark->link_image && $parsed_args['show_images'] ) {
if ( strpos( $bookmark->link_image, 'http' ) === 0 ) {
if ( str_starts_with( $bookmark->link_image, 'http' ) ) {
$output .= "<img src=\"$bookmark->link_image\" $alt $title />";
} else { // If it's a relative path.
$output .= '<img src="' . get_option( 'siteurl' ) . "$bookmark->link_image\" $alt $title />";
+1 -1
View File
@@ -22,7 +22,7 @@ require ABSPATH . WPINC . '/SimplePie/Author.php';
* @since 3.5.0
*/
function wp_simplepie_autoload( $class ) {
if ( 0 !== strpos( $class, 'SimplePie_' ) )
if ( ! str_starts_with( $class, 'SimplePie_' ) )
return;
$file = ABSPATH . WPINC . '/' . str_replace( '_', '/', $class ) . '.php';
@@ -1981,7 +1981,7 @@ final class WP_Customize_Manager {
&&
$parsed_allowed_url['host'] === $parsed_original_url['host']
&&
0 === strpos( $parsed_original_url['path'], $parsed_allowed_url['path'] )
str_starts_with( $parsed_original_url['path'], $parsed_allowed_url['path'] )
);
if ( $is_allowed ) {
break;
@@ -2059,7 +2059,7 @@ final class WP_Customize_Widgets {
* @return bool Whether the option capture is ignored.
*/
protected function is_option_capture_ignored( $option_name ) {
return ( 0 === strpos( $option_name, '_transient_' ) );
return ( str_starts_with( $option_name, '_transient_' ) );
}
/**
+1 -1
View File
@@ -4757,7 +4757,7 @@ class WP_Query {
$content = str_replace( '<!-- /wp:nextpage -->', '', $content );
// Ignore nextpage at the beginning of the content.
if ( 0 === strpos( $content, '<!--nextpage-->' ) ) {
if ( str_starts_with( $content, '<!--nextpage-->' ) ) {
$content = substr( $content, 15 );
}
@@ -314,7 +314,7 @@ When seeking help with this issue, you may be asked for some of the following in
return $plugins[ "{$extension['slug']}/{$extension['slug']}.php" ];
} else {
foreach ( $plugins as $file => $plugin_data ) {
if ( 0 === strpos( $file, "{$extension['slug']}/" ) || $file === $extension['slug'] ) {
if ( str_starts_with( $file, "{$extension['slug']}/" ) || $file === $extension['slug'] ) {
return $plugin_data;
}
}
+3 -3
View File
@@ -362,7 +362,7 @@ class WP_Recovery_Mode {
$error_file = wp_normalize_path( $error['file'] );
$wp_plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
if ( 0 === strpos( $error_file, $wp_plugin_dir ) ) {
if ( str_starts_with( $error_file, $wp_plugin_dir ) ) {
$path = str_replace( $wp_plugin_dir . '/', '', $error_file );
$parts = explode( '/', $path );
@@ -379,7 +379,7 @@ class WP_Recovery_Mode {
foreach ( $wp_theme_directories as $theme_directory ) {
$theme_directory = wp_normalize_path( $theme_directory );
if ( 0 === strpos( $error_file, $theme_directory ) ) {
if ( str_starts_with( $error_file, $theme_directory ) ) {
$path = str_replace( $theme_directory . '/', '', $error_file );
$parts = explode( '/', $path );
@@ -413,7 +413,7 @@ class WP_Recovery_Mode {
$network_plugins = wp_get_active_network_plugins();
foreach ( $network_plugins as $plugin ) {
if ( 0 === strpos( $plugin, $extension['slug'] . '/' ) ) {
if ( str_starts_with( $plugin, $extension['slug'] . '/' ) ) {
return true;
}
}
+3 -3
View File
@@ -375,7 +375,7 @@ class WP_Scripts extends WP_Dependencies {
return true;
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
@@ -702,12 +702,12 @@ JS;
return true;
}
if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
if ( str_starts_with( $src, '/' . WPINC . '/js/l10n' ) ) {
return false;
}
foreach ( (array) $this->default_dirs as $test ) {
if ( 0 === strpos( $src, $test ) ) {
if ( str_starts_with( $src, $test ) ) {
return true;
}
}
+2 -2
View File
@@ -393,7 +393,7 @@ class WP_Styles extends WP_Dependencies {
* @return string Style's fully-qualified URL.
*/
public function _css_href( $src, $ver, $handle ) {
if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
@@ -427,7 +427,7 @@ class WP_Styles extends WP_Dependencies {
}
foreach ( (array) $this->default_dirs as $test ) {
if ( 0 === strpos( $src, $test ) ) {
if ( str_starts_with( $src, $test ) ) {
return true;
}
}
+1 -1
View File
@@ -1768,7 +1768,7 @@ final class WP_Theme implements ArrayAccess {
* @param WP_Theme[] $themes Array of theme objects to sort (passed by reference).
*/
public static function sort_by_name( &$themes ) {
if ( 0 === strpos( get_user_locale(), 'en_' ) ) {
if ( str_starts_with( get_user_locale(), 'en_' ) ) {
uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
} else {
foreach ( $themes as $key => $theme ) {
+1 -1
View File
@@ -227,7 +227,7 @@ class WP {
} else {
foreach ( (array) $rewrite as $match => $query ) {
// If the requested file is the anchor of the match, prepend it to the path info.
if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
if ( ! empty( $requested_file ) && str_starts_with( $match, $requested_file ) && $requested_file != $requested_path ) {
$request_match = $requested_file . '/' . $requested_path;
}
+1 -1
View File
@@ -2816,7 +2816,7 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) {
// Do not search for a pingback server on our own uploads.
$uploads_dir = wp_get_upload_dir();
if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) {
if ( str_starts_with( $url, $uploads_dir['baseurl'] ) ) {
return false;
}
+1 -1
View File
@@ -50,7 +50,7 @@ function wp_get_extension_error_description( $error ) {
$core_errors = array();
foreach ( $constants as $constant => $value ) {
if ( 0 === strpos( $constant, 'E_' ) ) {
if ( str_starts_with( $constant, 'E_' ) ) {
$core_errors[ $value ] = $constant;
}
}
+9 -9
View File
@@ -2378,7 +2378,7 @@ function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false
$uploads['error'] = $tested_paths[ $path ];
} else {
if ( ! wp_mkdir_p( $path ) ) {
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) {
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
} else {
$error_path = wp_basename( $uploads['basedir'] ) . $uploads['subdir'];
@@ -2413,7 +2413,7 @@ function _wp_upload_dir( $time = null ) {
if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) {
$dir = WP_CONTENT_DIR . '/uploads';
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
} elseif ( ! str_starts_with( $upload_path, ABSPATH ) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH.
$dir = path_join( ABSPATH, $upload_path );
} else {
@@ -2575,7 +2575,7 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null )
$file_type = wp_check_filetype( $filename );
$mime_type = $file_type['type'];
$is_image = ( ! empty( $mime_type ) && 0 === strpos( $mime_type, 'image/' ) );
$is_image = ( ! empty( $mime_type ) && str_starts_with( $mime_type, 'image/' ) );
$upload_dir = wp_get_upload_dir();
$lc_filename = null;
@@ -2913,7 +2913,7 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
$new_file = $upload['path'] . "/$filename";
if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
if ( 0 === strpos( $upload['basedir'], ABSPATH ) ) {
if ( str_starts_with( $upload['basedir'], ABSPATH ) ) {
$error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
} else {
$error_path = wp_basename( $upload['basedir'] ) . $upload['subdir'];
@@ -3082,7 +3082,7 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
$real_mime = false;
// Validate image types.
if ( $type && 0 === strpos( $type, 'image/' ) ) {
if ( $type && str_starts_with( $type, 'image/' ) ) {
// Attempt to figure out what type of image it actually is.
$real_mime = wp_get_image_mime( $file );
@@ -3153,7 +3153,7 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
$type = false;
$ext = false;
}
} elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) {
} elseif ( str_starts_with( $real_mime, 'video/' ) || str_starts_with( $real_mime, 'audio/' ) ) {
/*
* For these types, only the major type must match the real value.
* This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip,
@@ -3311,7 +3311,7 @@ function wp_get_image_mime( $file ) {
$magic = bin2hex( $magic );
if (
// RIFF.
( 0 === strpos( $magic, '52494646' ) ) &&
( str_starts_with( $magic, '52494646' ) ) &&
// WEBP.
( 16 === strpos( $magic, '57454250' ) )
) {
@@ -7183,7 +7183,7 @@ function wp_auth_check_load() {
function wp_auth_check_html() {
$login_url = wp_login_url();
$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
$same_domain = ( strpos( $login_url, $current_domain ) === 0 );
$same_domain = str_starts_with( $login_url, $current_domain );
/**
* Filters whether the authentication check originated at the same domain.
@@ -7431,7 +7431,7 @@ function wp_delete_file_from_directory( $file, $directory ) {
$real_directory = wp_normalize_path( $real_directory );
}
if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
if ( false === $real_file || false === $real_directory || ! str_starts_with( $real_file, trailingslashit( $real_directory ) ) ) {
return false;
}
+1 -1
View File
@@ -4860,7 +4860,7 @@ function wp_admin_css_uri( $file = 'wp-admin' ) {
*/
function wp_admin_css( $file = 'wp-admin', $force_echo = false ) {
// For backward compatibility.
$handle = 0 === strpos( $file, 'css/' ) ? substr( $file, 4 ) : $file;
$handle = str_starts_with( $file, 'css/' ) ? substr( $file, 4 ) : $file;
if ( wp_styles()->query( $handle ) ) {
if ( $force_echo || did_action( 'wp_print_styles' ) ) {
+1 -1
View File
@@ -1260,7 +1260,7 @@ function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowe
* Note: the attribute name should only contain `A-Za-z0-9_-` chars,
* double hyphens `--` are not accepted by WordPress.
*/
if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] )
if ( str_starts_with( $name_low, 'data-' ) && ! empty( $allowed_attr['data-*'] )
&& preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match )
) {
/*
+6 -6
View File
@@ -1084,7 +1084,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
$src = $wp_scripts->registered[ $handle ]->src;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
$src = $wp_scripts->base_url . $src;
}
@@ -1098,7 +1098,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
// If the host is the same or it's a relative URL.
if (
( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
) {
// Make the src relative the specific plugin or theme.
@@ -1115,7 +1115,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
$relative = implode( '/', $relative );
} elseif (
( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
) {
// Make the src relative the specific plugin.
@@ -1134,7 +1134,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {
$relative = trim( $src_url['path'], '/' );
} elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
} elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
// Make the src relative to the WP root.
$relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
$relative = trim( $relative, '/' );
@@ -1373,8 +1373,8 @@ function get_available_languages( $dir = null ) {
if ( $lang_files ) {
foreach ( $lang_files as $lang_file ) {
$lang_file = basename( $lang_file, '.mo' );
if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
0 !== strpos( $lang_file, 'admin-' ) ) {
if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) &&
! str_starts_with( $lang_file, 'admin-' ) ) {
$languages[] = $lang_file;
}
}
+1 -1
View File
@@ -3636,7 +3636,7 @@ function plugins_url( $path = '', $plugin = '' ) {
$plugin = wp_normalize_path( $plugin );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
if ( ! empty( $plugin ) && 0 === strpos( $plugin, $mu_plugin_dir ) ) {
if ( ! empty( $plugin ) && str_starts_with( $plugin, $mu_plugin_dir ) ) {
$url = WPMU_PLUGIN_URL;
} else {
$url = WP_PLUGIN_URL;
+2 -2
View File
@@ -3730,7 +3730,7 @@ function get_taxonomies_for_attachments( $output = 'names' ) {
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
foreach ( $taxonomy->object_type as $object_type ) {
if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
if ( 'attachment' === $object_type || str_starts_with( $object_type, 'attachment:' ) ) {
if ( 'names' === $output ) {
$taxonomies[] = $taxonomy->name;
} else {
@@ -5120,7 +5120,7 @@ function attachment_url_to_postid( $url ) {
$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
}
if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
if ( str_starts_with( $path, $dir['baseurl'] . '/' ) ) {
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
}
+3 -3
View File
@@ -1187,7 +1187,7 @@ if ( ! function_exists( 'auth_redirect' ) ) :
// If https is required and request is http, redirect.
if ( $secure && ! is_ssl() && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit;
} else {
@@ -1218,7 +1218,7 @@ if ( ! function_exists( 'auth_redirect' ) ) :
// If the user wants ssl but the session is not ssl, redirect.
if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit;
} else {
@@ -1281,7 +1281,7 @@ if ( ! function_exists( 'check_admin_referer' ) ) :
*/
do_action( 'check_admin_referer', $action, $result );
if ( ! $result && ! ( -1 === $action && strpos( $referer, $adminurl ) === 0 ) ) {
if ( ! $result && ! ( -1 === $action && str_starts_with( $referer, $adminurl ) ) ) {
wp_nonce_ays( $action );
die();
}
+1 -1
View File
@@ -770,7 +770,7 @@ function plugin_basename( $file ) {
arsort( $wp_plugin_paths );
foreach ( $wp_plugin_paths as $dir => $realdir ) {
if ( strpos( $file, $realdir ) === 0 ) {
if ( str_starts_with( $file, $realdir ) ) {
$file = $dir . substr( $file, strlen( $realdir ) );
}
}
+1 -1
View File
@@ -887,7 +887,7 @@ function post_password_required( $post = null ) {
$hasher = new PasswordHash( 8, true );
$hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
if ( 0 !== strpos( $hash, '$P$B' ) ) {
if ( ! str_starts_with( $hash, '$P$B' ) ) {
$required = true;
} else {
$required = ! $hasher->CheckPassword( $post->post_password, $hash );
+4 -4
View File
@@ -723,7 +723,7 @@ function get_attached_file( $attachment_id, $unfiltered = false ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
// If the file is relative, prepend upload dir.
if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
if ( $file && ! str_starts_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
$uploads = wp_get_upload_dir();
if ( false === $uploads['error'] ) {
$file = $uploads['basedir'] . "/$file";
@@ -795,7 +795,7 @@ function _wp_relative_upload_path( $path ) {
$new_path = $path;
$uploads = wp_get_upload_dir();
if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
if ( str_starts_with( $new_path, $uploads['basedir'] ) ) {
$new_path = str_replace( $uploads['basedir'], '', $new_path );
$new_path = ltrim( $new_path, '/' );
}
@@ -6483,7 +6483,7 @@ function wp_get_attachment_url( $attachment_id = 0 ) {
$uploads = wp_get_upload_dir();
if ( $uploads && false === $uploads['error'] ) {
// Check that the upload base exists in the file location.
if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
if ( str_starts_with( $file, $uploads['basedir'] ) ) {
// Replace file location with url location.
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) {
@@ -6612,7 +6612,7 @@ function wp_attachment_is( $type, $post = null ) {
return false;
}
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) {
return true;
}
+2 -2
View File
@@ -939,12 +939,12 @@ function rest_is_field_included( $field, $fields ) {
foreach ( $fields as $accepted_field ) {
// Check to see if $field is the parent of any item in $fields.
// A field "parent" should be accepted if "parent.child" is accepted.
if ( strpos( $accepted_field, "$field." ) === 0 ) {
if ( str_starts_with( $accepted_field, "$field." ) ) {
return true;
}
// Conversely, if "parent" is accepted, all "parent.child" fields
// should also be accepted.
if ( strpos( $field, "$accepted_field." ) === 0 ) {
if ( str_starts_with( $field, "$accepted_field." ) ) {
return true;
}
}
@@ -1031,7 +1031,7 @@ class WP_REST_Request implements ArrayAccess {
}
$api_root = rest_url();
if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
if ( get_option( 'permalink_structure' ) && str_starts_with( $url, $api_root ) ) {
// Pretty permalinks on, and URL is under the API root.
$api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
$route = parse_url( $api_url_part, PHP_URL_PATH );
@@ -648,7 +648,7 @@ class WP_REST_Server {
// Convert $rel URIs to their compact versions if they exist.
foreach ( $curies as $curie ) {
$href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) );
if ( strpos( $rel, $href_prefix ) !== 0 ) {
if ( ! str_starts_with( $rel, $href_prefix ) ) {
continue;
}
@@ -1044,7 +1044,7 @@ class WP_REST_Server {
$with_namespace = array();
foreach ( $this->get_namespaces() as $namespace ) {
if ( 0 === strpos( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
if ( str_starts_with( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
$with_namespace[] = $this->get_routes( $namespace );
}
}
@@ -1827,7 +1827,7 @@ class WP_REST_Server {
);
foreach ( $server as $key => $value ) {
if ( strpos( $key, 'HTTP_' ) === 0 ) {
if ( str_starts_with( $key, 'HTTP_' ) ) {
$headers[ substr( $key, 5 ) ] = $value;
} elseif ( 'REDIRECT_HTTP_AUTHORIZATION' === $key && empty( $server['HTTP_AUTHORIZATION'] ) ) {
/*
+1 -1
View File
@@ -600,7 +600,7 @@ function url_to_postid( $url ) {
// If the requesting file is the anchor of the match,
// prepend it to the path info.
if ( ! empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) {
if ( ! empty( $url ) && ( $url != $request ) && str_starts_with( $match, $url ) ) {
$request_match = $url . '/' . $request;
}
+1 -1
View File
@@ -184,7 +184,7 @@ function wp_get_script_polyfill( $scripts, $tests ) {
$src = $scripts->registered[ $handle ]->src;
$ver = $scripts->registered[ $handle ]->ver;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && 0 === strpos( $src, $scripts->content_url ) ) ) {
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) {
$src = $scripts->base_url . $src;
}
+2 -2
View File
@@ -2262,7 +2262,7 @@ function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
$terms_from_remaining_taxonomies = get_terms( $args );
// Array keys should be preserved for values of $fields that use term_id for keys.
if ( ! empty( $args['fields'] ) && 0 === strpos( $args['fields'], 'id=>' ) ) {
if ( ! empty( $args['fields'] ) && str_starts_with( $args['fields'], 'id=>' ) ) {
$terms = $terms + $terms_from_remaining_taxonomies;
} else {
$terms = array_merge( $terms, $terms_from_remaining_taxonomies );
@@ -3506,7 +3506,7 @@ function wp_update_term_count_now( $terms, $taxonomy ) {
} else {
$object_types = (array) $taxonomy->object_type;
foreach ( $object_types as &$object_type ) {
if ( 0 === strpos( $object_type, 'attachment:' ) ) {
if ( str_starts_with( $object_type, 'attachment:' ) ) {
list( $object_type ) = explode( ':', $object_type );
}
}
+4 -4
View File
@@ -457,7 +457,7 @@ function search_theme_directories( $force = false ) {
* to use in get_theme_root().
*/
foreach ( $wp_theme_directories as $theme_root ) {
if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) {
$relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
} else {
$relative_theme_roots[ $theme_root ] = $theme_root;
@@ -634,11 +634,11 @@ function get_theme_root_uri( $stylesheet_or_template = '', $theme_root = '' ) {
if ( $stylesheet_or_template && $theme_root ) {
if ( in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
// Absolute path. Make an educated guess. YMMV -- but note the filter below.
if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) {
$theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
} elseif ( 0 === strpos( $theme_root, ABSPATH ) ) {
} elseif ( str_starts_with( $theme_root, ABSPATH ) ) {
$theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
} elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) ) {
} elseif ( str_starts_with( $theme_root, WP_PLUGIN_DIR ) || str_starts_with( $theme_root, WPMU_PLUGIN_DIR ) ) {
$theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
} else {
$theme_root_uri = $theme_root;
+1 -1
View File
@@ -989,7 +989,7 @@ function get_blogs_of_user( $user_id, $all = false ) {
if ( 'capabilities' !== substr( $key, -12 ) ) {
continue;
}
if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
if ( $wpdb->base_prefix && ! str_starts_with( $key, $wpdb->base_prefix ) ) {
continue;
}
$site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
@@ -223,7 +223,7 @@ class WP_Widget_Block extends WP_Widget {
* @return bool Updated `is_wide` value.
*/
public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
if ( strpos( $widget_id, 'block-' ) === 0 ) {
if ( str_starts_with( $widget_id, 'block-' ) ) {
return false;
}