Make WordPress Core

Changeset 56031


Ignore:
Timestamp:
06/26/2023 10:15:04 AM (3 years 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.

Location:
trunk/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r55990 r56031  
    33203320
    33213321        $url = empty( $attachment['url'] ) ? '' : $attachment['url'];
    3322         $rel = ( strpos( $url, 'attachment_id' ) || get_attachment_link( $id ) == $url );
     3322        $rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url );
    33233323
    33243324        remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );
  • trunk/src/wp-admin/includes/media.php

    r55990 r56031  
    832832                        $rel = '';
    833833
    834                         if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
     834                        if ( str_contains( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) === $attachment['url'] ) {
    835835                                $rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
    836836                        }
     
    13601360                $size  = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
    13611361                $alt   = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
    1362                 $rel   = ( strpos( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url );
     1362                $rel   = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url );
    13631363
    13641364                return get_image_send_to_editor( $attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt );
  • trunk/src/wp-includes/class-wp-editor.php

    r55988 r56031  
    572572                                                // Force urlencoding of commas.
    573573                                                foreach ( $editor_styles as $key => $url ) {
    574                                                         if ( strpos( $url, ',' ) !== false ) {
     574                                                        if ( str_contains( $url, ',' ) ) {
    575575                                                                $editor_styles[ $key ] = str_replace( ',', '%2C', $url );
    576576                                                        }
  • 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 {
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r55988 r56031  
    69916991
    69926992                foreach ( $p as $para ) {
    6993                         if ( strpos( $para, $pagelinkedto ) !== false ) { // It exists, but is it a link?
     6993                        if ( str_contains( $para, $pagelinkedto ) ) { // It exists, but is it a link?
    69946994                                preg_match( '|<a[^>]+?' . $preg_target . '[^>]*>([^>]+?)</a>|', $para, $context );
    69956995
  • trunk/src/wp-includes/formatting.php

    r56021 r56031  
    10201020
    10211021        // Don't bother if there are no entities - saves a lot of processing.
    1022         if ( strpos( $text, '&' ) === false ) {
     1022        if ( ! str_contains( $text, '&' ) ) {
    10231023                return $text;
    10241024        }
     
    24752475        }
    24762476
    2477         if ( strpos( $content, '&' ) !== false ) {
     2477        if ( str_contains( $content, '&' ) ) {
    24782478                $content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content );
    24792479        }
     
    25262526        );
    25272527
    2528         if ( strpos( $content, '&#1' ) !== false ) {
     2528        if ( str_contains( $content, '&#1' ) ) {
    25292529                $content = strtr( $content, $wp_htmltranswinuni );
    25302530        }
     
    55825582        $filtered = wp_check_invalid_utf8( $str );
    55835583
    5584         if ( strpos( $filtered, '<' ) !== false ) {
     5584        if ( str_contains( $filtered, '<' ) ) {
    55855585                $filtered = wp_pre_kses_less_than( $filtered );
    55865586                // This will strip extra whitespace for us.
     
    60676067
    60686068        foreach ( $headers as $header ) {
    6069                 if ( strpos( $header, ':' ) === false ) {
     6069                if ( ! str_contains( $header, ':' ) ) {
    60706070                        continue;
    60716071                }
     
    60796079
    60806080                if ( 'content-type' === strtolower( $name ) ) {
    6081                         if ( strpos( $content, ';' ) !== false ) {
     6081                        if ( str_contains( $content, ';' ) ) {
    60826082                                list( $type, $charset ) = explode( ';', $content );
    60836083                                $content_type           = trim( $type );
  • trunk/src/wp-includes/functions.php

    r55990 r56031  
    61056105
    61066106                // The request is for the admin.
    6107                 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
     6107                if ( str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
    61086108                        $path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] );
    61096109
  • trunk/src/wp-includes/general-template.php

    r55988 r56031  
    908908
    909909        $url = true;
    910         if ( strpos( $show, 'url' ) === false &&
    911                 strpos( $show, 'directory' ) === false &&
    912                 strpos( $show, 'home' ) === false ) {
     910
     911        if ( ! str_contains( $show, 'url' )
     912                && ! str_contains( $show, 'directory' )
     913                && ! str_contains( $show, 'home' )
     914        ) {
    913915                $url = false;
    914916        }
     
    37393741                                $wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && (int) $match[1] >= 534 );
    37403742                        } elseif ( $is_IE ) {
    3741                                 $wp_rich_edit = ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' ) !== false );
     3743                                $wp_rich_edit = str_contains( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' );
    37423744                        } elseif ( $is_gecko || $is_chrome || $is_edge || ( $is_opera && ! wp_is_mobile() ) ) {
    37433745                                $wp_rich_edit = true;
  • trunk/src/wp-includes/load.php

    r56014 r56031  
    7474
    7575        // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests.
    76         if ( isset( $_SERVER['SCRIPT_FILENAME'] )
    77                 && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) === strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 )
    78         ) {
     76        if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && str_ends_with( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) ) {
    7977                $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
    8078        }
    8179
    8280        // Fix for Dreamhost and other PHP as CGI hosts.
    83         if ( isset( $_SERVER['SCRIPT_NAME'] ) && ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) ) {
     81        if ( isset( $_SERVER['SCRIPT_NAME'] ) && str_contains( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) {
    8482                unset( $_SERVER['PATH_INFO'] );
    8583        }
  • trunk/src/wp-includes/media.php

    r55990 r56031  
    40604060         * See #29602.
    40614061         */
    4062         if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
    4063                 strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
    4064 
     4062        if ( wp_is_mobile()
     4063                && str_contains( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' )
     4064                && str_contains( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' )
     4065        ) {
    40654066                $defaults['multi_selection'] = false;
    40664067        }
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r55703 r56031  
    15391539
    15401540                        // For non-variable routes, generate links.
    1541                         if ( strpos( $route, '{' ) === false ) {
     1541                        if ( ! str_contains( $route, '{' ) ) {
    15421542                                $data['_links'] = array(
    15431543                                        'self' => array(
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r56019 r56031  
    11121112                        $value = trim( $value );
    11131113
    1114                         if ( strpos( $value, ';' ) === false ) {
     1114                        if ( ! str_contains( $value, ';' ) ) {
    11151115                                continue;
    11161116                        }
     
    11221122
    11231123                        foreach ( $attr_parts as $part ) {
    1124                                 if ( strpos( $part, '=' ) === false ) {
     1124                                if ( ! str_contains( $part, '=' ) ) {
    11251125                                        continue;
    11261126                                }
Note: See TracChangeset for help on using the changeset viewer.