Make WordPress Core


Ignore:
Timestamp:
07/17/2023 01:16:14 PM (15 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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            }
Note: See TracChangeset for help on using the changeset viewer.