Make WordPress Core


Ignore:
Timestamp:
06/26/2023 10:15:04 AM (13 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], [56021].

See #58206.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r56014 r56031  
    10611061                 * minute all present). Set these flags now as we need them for the endpoints.
    10621062                 */
    1063                 if ( strpos( $struct, '%postname%' ) !== false
    1064                         || strpos( $struct, '%post_id%' ) !== false
    1065                         || strpos( $struct, '%pagename%' ) !== false
    1066                         || ( strpos( $struct, '%year%' ) !== false && strpos( $struct, '%monthnum%' ) !== false && strpos( $struct, '%day%' ) !== false && strpos( $struct, '%hour%' ) !== false && strpos( $struct, '%minute%' ) !== false && strpos( $struct, '%second%' ) !== false )
    1067                         ) {
     1063                if ( str_contains( $struct, '%postname%' )
     1064                    || str_contains( $struct, '%post_id%' )
     1065                    || str_contains( $struct, '%pagename%' )
     1066                    || ( str_contains( $struct, '%year%' )
     1067                        && str_contains( $struct, '%monthnum%' )
     1068                        && str_contains( $struct, '%day%' )
     1069                        && str_contains( $struct, '%hour%' )
     1070                        && str_contains( $struct, '%minute%' )
     1071                        && str_contains( $struct, '%second%' ) )
     1072                ) {
    10681073                    $post = true;
    1069                     if ( strpos( $struct, '%pagename%' ) !== false ) {
     1074                    if ( str_contains( $struct, '%pagename%' ) ) {
    10701075                        $page = true;
    10711076                    }
     
    10751080                    // For custom post types, we need to add on endpoints as well.
    10761081                    foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) {
    1077                         if ( strpos( $struct, "%$ptype%" ) !== false ) {
     1082                        if ( str_contains( $struct, "%$ptype%" ) ) {
    10781083                            $post = true;
    10791084
     
    15561561                $match = str_replace( '.+?', '.+', $match );
    15571562
    1558                 if ( strpos( $query, $this->index ) !== false ) {
     1563                if ( str_contains( $query, $this->index ) ) {
    15591564                    $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
    15601565                } else {
Note: See TracChangeset for help on using the changeset viewer.