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
This commit is contained in:
Sergey Biryukov
2023-06-22 14:57:24 +00:00
parent 372d9c07e5
commit 84e9601e5a
42 changed files with 83 additions and 83 deletions

View File

@@ -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'] : '';

View File

@@ -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 ) {

View File

@@ -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;
}
}

View File

@@ -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';

View File

@@ -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 ) ) {

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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 ) {
?>
</div>
<div class="wp_attachment_details edit-form-section">
<?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
<?php if ( str_starts_with( $post->post_mime_type, 'image' ) ) : ?>
<p class="attachment-alt-text">
<label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
<textarea class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" aria-describedby="alt-text-description"><?php echo esc_attr( $alt_text ); ?></textarea>

View File

@@ -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.
*

View File

@@ -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;
}
}

View File

@@ -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;
}