Make WordPress Core


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

    r55673 r55703  
    1818function remove_block_asset_path_prefix( $asset_handle_or_path ) {
    1919        $path_prefix = 'file:';
    20         if ( 0 !== strpos( $asset_handle_or_path, $path_prefix ) ) {
     20        if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
    2121                return $asset_handle_or_path;
    2222        }
    … …  
    2525                strlen( $path_prefix )
    2626        );
    27         if ( strpos( $path, './' ) === 0 ) {
     27        if ( str_starts_with( $path, './' ) ) {
    2828                $path = substr( $path, 2 );
    2929        }
    … …  
    4545 */
    4646function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
    47         if ( 0 === strpos( $block_name, 'core/' ) ) {
     47        if ( str_starts_with( $block_name, 'core/' ) ) {
    4848                $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
    49                 if ( 0 === strpos( $field_name, 'editor' ) ) {
     49                if ( str_starts_with( $field_name, 'editor' ) ) {
    5050                        $asset_handle .= '-editor';
    5151                }
    52                 if ( 0 === strpos( $field_name, 'view' ) ) {
     52                if ( str_starts_with( $field_name, 'view' ) ) {
    5353                        $asset_handle .= '-view';
    5454                }
    … …  
    138138        $script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
    139139
    140         $is_core_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
    141         $is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
     140        $is_core_block  = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
     141        $is_theme_block = str_starts_with( $script_path_norm, $theme_path_norm );
    142142
    143143        $script_uri = plugins_url( $script_path, $metadata['file'] );
    … …  
    192192        }
    193193
    194         $is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
     194        $is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
    195195        // Skip registering individual styles for each core block when a bundled version provided.
    196196        if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
    … …  
    360360
    361361        // Add `style` and `editor_style` for core blocks if missing.
    362         if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) {
     362        if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) {
    363363                $block_name = str_replace( 'core/', '', $metadata['name'] );
    364364
    … …  
    699699 */
    700700function strip_core_block_namespace( $block_name = null ) {
    701         if ( is_string( $block_name ) && 0 === strpos( $block_name, 'core/' ) ) {
     701        if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
    702702                return substr( $block_name, 5 );
    703703        }
Note: See TracChangeset for help on using the changeset viewer.