Make WordPress Core


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

    r55942 r55988  
    40224022            $type = 'text/x-diff';
    40234023        }
    4024     } elseif ( isset( $args['file'] ) && false !== strpos( basename( $args['file'] ), '.' ) ) {
     4024    } elseif ( isset( $args['file'] ) && str_contains( basename( $args['file'] ), '.' ) ) {
    40254025        $extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) );
    40264026        foreach ( wp_get_mime_types() as $exts => $mime ) {
     
    41584158            )
    41594159        );
    4160     } elseif ( false !== strpos( $type, 'json' ) ) {
     4160    } elseif ( str_contains( $type, 'json' ) ) {
    41614161        $settings['codemirror'] = array_merge(
    41624162            $settings['codemirror'],
     
    41754175            $settings['codemirror']['mode']['json'] = true;
    41764176        }
    4177     } elseif ( false !== strpos( $type, 'jsx' ) ) {
     4177    } elseif ( str_contains( $type, 'jsx' ) ) {
    41784178        $settings['codemirror'] = array_merge(
    41794179            $settings['codemirror'],
     
    42214221            )
    42224222        );
    4223     } elseif ( false !== strpos( $type, 'xml' ) ) {
     4223    } elseif ( str_contains( $type, 'xml' ) ) {
    42244224        $settings['codemirror'] = array_merge(
    42254225            $settings['codemirror'],
Note: See TracChangeset for help on using the changeset viewer.