Make WordPress Core

Changeset 56021


Ignore:
Timestamp:
06/25/2023 10:48:28 AM (5 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].

Props spacedmonkey.
See #58220.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

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

    r56005 r56021  
    804804    $result = '';
    805805
    806     if ( false !== strpos( $text, '<!--' ) && false !== strpos( $text, '--->' ) ) {
     806    if ( str_contains( $text, '<!--' ) && str_contains( $text, '--->' ) ) {
    807807        $text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
    808808    }
  • trunk/src/wp-includes/formatting.php

    r55990 r56021  
    44644464     * starting with /, # or ?, or a PHP file).
    44654465     */
    4466     if ( strpos( $url, ':' ) === false && ! in_array( $url[0], array( '/', '#', '?' ), true ) &&
    4467         ! preg_match( '/^[a-z0-9-]+?\.php/i', $url ) ) {
     4466    if ( ! str_contains( $url, ':' ) && ! in_array( $url[0], array( '/', '#', '?' ), true ) &&
     4467        ! preg_match( '/^[a-z0-9-]+?\.php/i', $url )
     4468    ) {
    44684469        $url = 'http://' . $url;
    44694470    }
  • trunk/src/wp-includes/link-template.php

    r56019 r56021  
    223223
    224224        $category = '';
    225         if ( strpos( $permalink, '%category%' ) !== false ) {
     225        if ( str_contains( $permalink, '%category%' ) ) {
    226226            $cats = get_the_category( $post->ID );
    227227            if ( $cats ) {
     
    261261
    262262        $author = '';
    263         if ( strpos( $permalink, '%author%' ) !== false ) {
     263        if ( str_contains( $permalink, '%author%' ) ) {
    264264            $authordata = get_userdata( $post->post_author );
    265265            $author     = $authordata->user_nicename;
     
    503503        }
    504504
    505         if ( strpos( $parentlink, '?' ) === false ) {
     505        if ( ! str_contains( $parentlink, '?' ) ) {
    506506            $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' );
    507507        }
Note: See TracChangeset for help on using the changeset viewer.