Make WordPress Core


Ignore:
Timestamp:
05/02/2023 03:43:03 PM (17 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/l10n.php

    r55351 r55703  
    10851085    $src = $wp_scripts->registered[ $handle ]->src;
    10861086
    1087     if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
     1087    if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
    10881088        $src = $wp_scripts->base_url . $src;
    10891089    }
     
    10991099    // If the host is the same or it's a relative URL.
    11001100    if (
    1101         ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
     1101        ( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
    11021102        ( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
    11031103    ) {
     
    11161116        $relative = implode( '/', $relative );
    11171117    } elseif (
    1118         ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
     1118        ( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
    11191119        ( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
    11201120    ) {
     
    11351135        if ( ! isset( $site_url['path'] ) ) {
    11361136            $relative = trim( $src_url['path'], '/' );
    1137         } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
     1137        } elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
    11381138            // Make the src relative to the WP root.
    11391139            $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
     
    13741374        foreach ( $lang_files as $lang_file ) {
    13751375            $lang_file = basename( $lang_file, '.mo' );
    1376             if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
    1377                 0 !== strpos( $lang_file, 'admin-' ) ) {
     1376            if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) &&
     1377                ! str_starts_with( $lang_file, 'admin-' ) ) {
    13781378                $languages[] = $lang_file;
    13791379            }
Note: See TracChangeset for help on using the changeset viewer.