Make WordPress Core

Changeset 56245


Ignore:
Timestamp:
07/17/2023 01:16:14 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use str_contains() in a few more places.

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 [55988], [55990], [56014], [56021], [56031], [56032], [56065], [56241].

See #58206.

Location:
trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r56174 r56245  
    38543854        );
    38553855
    3856         if ( strpos( $parsed, 'class="wp-embedded-content' ) ) {
     3856        if ( str_contains( $parsed, 'class="wp-embedded-content' ) ) {
    38573857                if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
    38583858                        $script_src = includes_url( 'js/wp-embed.js' );
  • trunk/src/wp-admin/includes/theme.php

    r56059 r56245  
    11681168        if ( ! empty( $redirect ) ) {
    11691169                $functions_path = '';
    1170                 if ( strpos( STYLESHEETPATH, $extension ) ) {
     1170                if ( str_contains( STYLESHEETPATH, $extension ) ) {
    11711171                        $functions_path = STYLESHEETPATH . '/functions.php';
    1172                 } elseif ( strpos( TEMPLATEPATH, $extension ) ) {
     1172                } elseif ( str_contains( TEMPLATEPATH, $extension ) ) {
    11731173                        $functions_path = TEMPLATEPATH . '/functions.php';
    11741174                }
  • trunk/src/wp-includes/canonical.php

    r56177 r56245  
    680680
    681681        // Strip multiple slashes out of the URL.
    682         if ( strpos( $redirect['path'], '//' ) > -1 ) {
     682        if ( str_contains( $redirect['path'], '//' ) ) {
    683683                $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
    684684        }
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r56180 r56245  
    48874887                } else {
    48884888                        foreach ( (array) $blogs as $blog ) {
    4889                                 if ( strpos( $blog['url'], $_SERVER['HTTP_HOST'] ) ) {
     4889                                if ( str_contains( $blog['url'], $_SERVER['HTTP_HOST'] ) ) {
    48904890                                        return array( $blog );
    48914891                                }
  • trunk/src/wp-includes/link-template.php

    r56191 r56245  
    44394439                $user = get_user_by( 'id', absint( $id_or_email ) );
    44404440        } elseif ( is_string( $id_or_email ) ) {
    4441                 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
     4441                if ( str_contains( $id_or_email, '@md5.gravatar.com' ) ) {
    44424442                        // MD5 hash.
    44434443                        list( $email_hash ) = explode( '@', $id_or_email );
  • trunk/src/wp-includes/load.php

    r56238 r56245  
    7676
    7777        // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests.
    78         if ( isset( $_SERVER['SCRIPT_FILENAME'] )
    79                 && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) === strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 )
    80         ) {
     78        if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && str_ends_with( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) ) {
    8179                $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
    8280        }
    8381
    8482        // Fix for Dreamhost and other PHP as CGI hosts.
    85         if ( isset( $_SERVER['SCRIPT_NAME'] ) && ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) ) {
     83        if ( isset( $_SERVER['SCRIPT_NAME'] ) && str_contains( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) {
    8684                unset( $_SERVER['PATH_INFO'] );
    8785        }
     
    938936
    939937        while ( ( $plugin = readdir( $dh ) ) !== false ) {
    940                 if ( '.php' === substr( $plugin, -4 ) ) {
     938                if ( str_ends_with( $plugin, '.php' ) ) {
    941939                        $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
    942940                }
     
    982980        foreach ( $active_plugins as $plugin ) {
    983981                if ( ! validate_file( $plugin )                     // $plugin must validate as file.
    984                         && '.php' === substr( $plugin, -4 )             // $plugin must end with '.php'.
     982                        && str_ends_with( $plugin, '.php' )             // $plugin must end with '.php'.
    985983                        && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
    986984                        // Not already included as a network plugin.
     
    16181616        $bytes = (int) $value;
    16191617
    1620         if ( false !== strpos( $value, 'g' ) ) {
     1618        if ( str_contains( $value, 'g' ) ) {
    16211619                $bytes *= GB_IN_BYTES;
    1622         } elseif ( false !== strpos( $value, 'm' ) ) {
     1620        } elseif ( str_contains( $value, 'm' ) ) {
    16231621                $bytes *= MB_IN_BYTES;
    1624         } elseif ( false !== strpos( $value, 'k' ) ) {
     1622        } elseif ( str_contains( $value, 'k' ) ) {
    16251623                $bytes *= KB_IN_BYTES;
    16261624        }
     
    19091907        if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
    19101908                foreach ( $accepted as $type ) {
    1911                         if ( false !== strpos( $_SERVER['HTTP_ACCEPT'], $type ) ) {
     1909                        if ( str_contains( $_SERVER['HTTP_ACCEPT'], $type ) ) {
    19121910                                return true;
    19131911                        }
  • trunk/src/wp-includes/media.php

    r56214 r56245  
    13241324                        'file'   => $image_basename,
    13251325                );
    1326         } elseif ( strpos( $image_src, $image_meta['file'] ) ) {
     1326        } elseif ( str_contains( $image_src, $image_meta['file'] ) ) {
    13271327                return false;
    13281328        }
  • trunk/src/wp-includes/pluggable.php

    r56192 r56245  
    12381238                nocache_headers();
    12391239
    1240                 $redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     1240                if ( str_contains( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) {
     1241                        $redirect = wp_get_referer();
     1242                } else {
     1243                        $redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     1244                }
    12411245
    12421246                $login_url = wp_login_url( $redirect, true );
  • trunk/src/wp-login.php

    r56157 r56245  
    13631363                        } elseif ( isset( $_GET['registration'] ) && 'disabled' === $_GET['registration'] ) {
    13641364                                $errors->add( 'registerdisabled', __( '<strong>Error:</strong> User registration is currently not allowed.' ) );
    1365                         } elseif ( strpos( $redirect_to, 'about.php?updated' ) ) {
     1365                        } elseif ( str_contains( $redirect_to, 'about.php?updated' ) ) {
    13661366                                $errors->add( 'updated', __( '<strong>You have successfully updated WordPress!</strong> Please log back in to see what&#8217;s new.' ), 'message' );
    13671367                        } elseif ( WP_Recovery_Mode_Link_Service::LOGIN_ACTION_ENTERED === $action ) {
Note: See TracChangeset for help on using the changeset viewer.