Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:34:56 PM (18 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/pluggable.php

    r55703 r55988  
    280280                // Iterate through the raw headers.
    281281                foreach ( (array) $tempheaders as $header ) {
    282                     if ( strpos( $header, ':' ) === false ) {
     282                    if ( ! str_contains( $header, ':' ) ) {
    283283                        if ( false !== stripos( $header, 'boundary=' ) ) {
    284284                            $parts    = preg_split( '/boundary=/i', trim( $header ) );
     
    316316                            break;
    317317                        case 'content-type':
    318                             if ( strpos( $content, ';' ) !== false ) {
     318                            if ( str_contains( $content, ';' ) ) {
    319319                                list( $type, $charset_content ) = explode( ';', $content );
    320320                                $content_type                   = trim( $type );
     
    11871187
    11881188        // If https is required and request is http, redirect.
    1189         if ( $secure && ! is_ssl() && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
     1189        if ( $secure && ! is_ssl() && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
    11901190            if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
    11911191                wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
     
    12181218
    12191219            // If the user wants ssl but the session is not ssl, redirect.
    1220             if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
     1220            if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
    12211221                if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
    12221222                    wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
Note: See TracChangeset for help on using the changeset viewer.