Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:34:56 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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

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

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

This commit replaces false !== strpos( ... ) with str_contains() in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].

Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.

File:
1 edited

Legend:

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

    r55968 r55988  
    709709                                        return false;
    710710                                }
    711                         } elseif ( false === strpos( $data, '"' ) ) {
     711                        } elseif ( ! str_contains( $data, '"' ) ) {
    712712                                return false;
    713713                        }
    … …  
    11591159        }
    11601160
    1161         if ( strpos( $uri, '?' ) !== false ) {
     1161        if ( str_contains( $uri, '?' ) ) {
    11621162                list( $base, $query ) = explode( '?', $uri, 2 );
    11631163                $base                .= '?';
    1164         } elseif ( $protocol || strpos( $uri, '=' ) === false ) {
     1164        } elseif ( $protocol || ! str_contains( $uri, '=' ) ) {
    11651165                $base  = $uri . '?';
    11661166                $query = '';
    … …  
    20552055
    20562056        // Do not allow path traversals.
    2057         if ( false !== strpos( $target, '../' ) || false !== strpos( $target, '..' . DIRECTORY_SEPARATOR ) ) {
     2057        if ( str_contains( $target, '../' ) || str_contains( $target, '..' . DIRECTORY_SEPARATOR ) ) {
    20582058                return false;
    20592059        }
    … …  
    26422642
    26432643                // The (resized) image files would have name and extension, and will be in the uploads dir.
    2644                 if ( $name && $ext && @is_dir( $dir ) && false !== strpos( $dir, $upload_dir['basedir'] ) ) {
     2644                if ( $name && $ext && @is_dir( $dir ) && str_contains( $dir, $upload_dir['basedir'] ) ) {
    26452645                        /**
    26462646                         * Filters the file list used for calculating a unique filename for a newly added file.
    … …  
    46574657                $mce_init['rtl_ui']         = true;
    46584658
    4659                 if ( ! empty( $mce_init['plugins'] ) && strpos( $mce_init['plugins'], 'directionality' ) === false ) {
     4659                if ( ! empty( $mce_init['plugins'] ) && ! str_contains( $mce_init['plugins'], 'directionality' ) ) {
    46604660                        $mce_init['plugins'] .= ',directionality';
    46614661                }
    … …  
    59695969        if ( empty( $loaded_mods )
    59705970                && function_exists( 'phpinfo' )
    5971                 && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' )
     5971                && ! str_contains( ini_get( 'disable_functions' ), 'phpinfo' )
    59725972        ) {
    59735973                ob_start();
    … …  
    59755975                $phpinfo = ob_get_clean();
    59765976
    5977                 if ( false !== strpos( $phpinfo, $mod ) ) {
     5977                if ( str_contains( $phpinfo, $mod ) ) {
    59785978                        return true;
    59795979                }
    … …  
    60506050
    60516051        // `../` which does not occur at the end of the path is not allowed:
    6052         if ( false !== strpos( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) {
     6052        if ( str_contains( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) {
    60536053                return 1;
    60546054        }
    … …  
    61056105
    61066106                // The request is for the admin.
    6107                 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
     6107                if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
    61086108                        $path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] );
    61096109
    … …  
    61146114
    61156115                } else {
    6116                         if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
     6116                        if ( str_contains( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
    61176117                                // Request is hitting a file inside ABSPATH.
    61186118                                $directory = str_replace( ABSPATH, '', $script_filename_dir );
    61196119                                // Strip off the subdirectory, and any file/query params.
    61206120                                $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] );
    6121                         } elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
     6121                        } elseif ( str_contains( $abspath_fix, $script_filename_dir ) ) {
    61226122                                // Request is hitting a file above ABSPATH.
    61236123                                $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
    … …  
    71057105        $ua = $_SERVER['HTTP_USER_AGENT'];
    71067106
    7107         if ( strpos( $ua, 'iPhone' ) !== false
    7108                 || strpos( $ua, 'iPad' ) !== false
    7109                 || strpos( $ua, 'iPod' ) !== false ) {
     7107        if ( str_contains( $ua, 'iPhone' )
     7108                || str_contains( $ua, 'iPad' )
     7109                || str_contains( $ua, 'iPod' ) ) {
    71107110                        return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' );
    71117111        }
    … …  
    85118511
    85128512        if (
    8513                 strpos( $path, '/' ) === false &&
    8514                 strpos( $path, '\\' ) === false
     8513                ! str_contains( $path, '/' ) &&
     8514                ! str_contains( $path, '\\' )
    85158515        ) {
    85168516                unset( $directory_cache[ $path ] );
Note: See TracChangeset for help on using the changeset viewer.