Make WordPress Core


Ignore:
Timestamp:
05/02/2023 03:43:03 PM (18 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r55702 r55703  
    23792379        } else {
    23802380            if ( ! wp_mkdir_p( $path ) ) {
    2381                 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
     2381                if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) {
    23822382                    $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
    23832383                } else {
     
    24142414    if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) {
    24152415        $dir = WP_CONTENT_DIR . '/uploads';
    2416     } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
     2416    } elseif ( ! str_starts_with( $upload_path, ABSPATH ) ) {
    24172417        // $dir is absolute, $upload_path is (maybe) relative to ABSPATH.
    24182418        $dir = path_join( ABSPATH, $upload_path );
     
    25762576        $mime_type = $file_type['type'];
    25772577
    2578         $is_image    = ( ! empty( $mime_type ) && 0 === strpos( $mime_type, 'image/' ) );
     2578        $is_image    = ( ! empty( $mime_type ) && str_starts_with( $mime_type, 'image/' ) );
    25792579        $upload_dir  = wp_get_upload_dir();
    25802580        $lc_filename = null;
     
    29142914    $new_file = $upload['path'] . "/$filename";
    29152915    if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
    2916         if ( 0 === strpos( $upload['basedir'], ABSPATH ) ) {
     2916        if ( str_starts_with( $upload['basedir'], ABSPATH ) ) {
    29172917            $error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
    29182918        } else {
     
    30833083
    30843084    // Validate image types.
    3085     if ( $type && 0 === strpos( $type, 'image/' ) ) {
     3085    if ( $type && str_starts_with( $type, 'image/' ) ) {
    30863086
    30873087        // Attempt to figure out what type of image it actually is.
     
    31543154                $ext  = false;
    31553155            }
    3156         } elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) {
     3156        } elseif ( str_starts_with( $real_mime, 'video/' ) || str_starts_with( $real_mime, 'audio/' ) ) {
    31573157            /*
    31583158             * For these types, only the major type must match the real value.
     
    33123312        if (
    33133313            // RIFF.
    3314             ( 0 === strpos( $magic, '52494646' ) ) &&
     3314            ( str_starts_with( $magic, '52494646' ) ) &&
    33153315            // WEBP.
    33163316            ( 16 === strpos( $magic, '57454250' ) )
     
    71847184    $login_url      = wp_login_url();
    71857185    $current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
    7186     $same_domain    = ( strpos( $login_url, $current_domain ) === 0 );
     7186    $same_domain    = str_starts_with( $login_url, $current_domain );
    71877187
    71887188    /**
     
    74327432    }
    74337433
    7434     if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
     7434    if ( false === $real_file || false === $real_directory || ! str_starts_with( $real_file, trailingslashit( $real_directory ) ) ) {
    74357435        return false;
    74367436    }
Note: See TracChangeset for help on using the changeset viewer.