Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:34:56 PM (12 months 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/class-wp-http.php

    r55827 r55988  
    722722         */
    723723        for ( $i = count( $headers ) - 1; $i >= 0; $i-- ) {
    724             if ( ! empty( $headers[ $i ] ) && false === strpos( $headers[ $i ], ':' ) ) {
     724            if ( ! empty( $headers[ $i ] ) && ! str_contains( $headers[ $i ], ':' ) ) {
    725725                $headers = array_splice( $headers, $i );
    726726                break;
     
    735735            }
    736736
    737             if ( false === strpos( $tempheader, ':' ) ) {
     737            if ( ! str_contains( $tempheader, ':' ) ) {
    738738                $stack   = explode( ' ', $tempheader, 3 );
    739739                $stack[] = '';
     
    906906            $accessible_hosts = preg_split( '|,\s*|', WP_ACCESSIBLE_HOSTS );
    907907
    908             if ( false !== strpos( WP_ACCESSIBLE_HOSTS, '*' ) ) {
     908            if ( str_contains( WP_ACCESSIBLE_HOSTS, '*' ) ) {
    909909                $wildcard_regex = array();
    910910                foreach ( $accessible_hosts as $host ) {
     
    10971097        }
    10981098
    1099         if ( false !== strpos( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) ) {
     1099        if ( str_contains( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) ) {
    11001100            return 6;
    11011101        }
Note: See TracChangeset for help on using the changeset viewer.