Make WordPress Core


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

Props spacedmonkey.
See #58220.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.