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/class-wp-query.php

    r55887 r55988  
    10161016        }
    10171017
    1018         if ( false !== strpos( $qv['feed'], 'comments-' ) ) {
     1018        if ( str_contains( $qv['feed'], 'comments-' ) ) {
    10191019            $qv['feed']         = str_replace( 'comments-', '', $qv['feed'] );
    10201020            $qv['withcomments'] = 1;
     
    11861186                }
    11871187
    1188                 if ( strpos( $term, '+' ) !== false ) {
     1188                if ( str_contains( $term, '+' ) ) {
    11891189                    $terms = preg_split( '/[+]+/', $term );
    11901190                    foreach ( $terms as $term ) {
     
    12991299
    13001300        if ( '' !== $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) {
    1301             if ( strpos( $q['tag'], ',' ) !== false ) {
     1301            if ( str_contains( $q['tag'], ',' ) ) {
    13021302                $tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] );
    13031303                foreach ( (array) $tags as $tag ) {
     
    23552355
    23562356        if ( '' !== $q['author_name'] ) {
    2357             if ( strpos( $q['author_name'], '/' ) !== false ) {
     2357            if ( str_contains( $q['author_name'], '/' ) ) {
    23582358                $q['author_name'] = explode( '/', $q['author_name'] );
    23592359                if ( $q['author_name'][ count( $q['author_name'] ) - 1 ] ) {
     
    47554755
    47564756        $content = $post->post_content;
    4757         if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
     4757        if ( str_contains( $content, '<!--nextpage-->' ) ) {
    47584758            $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
    47594759            $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
Note: See TracChangeset for help on using the changeset viewer.