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.

Location:
trunk/src/wp-content/themes
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyseventeen/inc/icon-functions.php

    r51617 r55988  
    131131    if ( 'social' === $args->theme_location ) {
    132132        foreach ( $social_icons as $attr => $value ) {
    133             if ( false !== strpos( $item_output, $attr ) ) {
     133            if ( str_contains( $item_output, $attr ) ) {
    134134                $item_output = str_replace( $args->link_after, '</span>' . twentyseventeen_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
    135135            }
  • trunk/src/wp-content/themes/twentyseventeen/template-parts/post/content-audio.php

    r53308 r55988  
    4747
    4848        // Only get audio from the content if a playlist isn't present.
    49     if ( false === strpos( $content, 'wp-playlist-script' ) ) {
     49    if ( ! str_contains( $content, 'wp-playlist-script' ) ) {
    5050        $audio = get_media_embedded_in_content( $content, array( 'audio' ) );
    5151    }
  • trunk/src/wp-content/themes/twentyseventeen/template-parts/post/content-video.php

    r53308 r55988  
    4747
    4848        // Only get video from the content if a playlist isn't present.
    49     if ( false === strpos( $content, 'wp-playlist-script' ) ) {
     49    if ( ! str_contains( $content, 'wp-playlist-script' ) ) {
    5050        $video = get_media_embedded_in_content( $content, array( 'video', 'object', 'embed', 'iframe' ) );
    5151    }
  • trunk/src/wp-content/themes/twentytwenty/comments.php

    r55420 r55988  
    8181
    8282                // If we're only showing the "Next" link, add a class indicating so.
    83                 if ( false === strpos( $comment_pagination, 'prev page-numbers' ) ) {
     83                if ( ! str_contains( $comment_pagination, 'prev page-numbers' ) ) {
    8484                    $pagination_classes = ' only-next';
    8585                }
  • trunk/src/wp-content/themes/twentytwenty/functions.php

    r55929 r55988  
    317317
    318318            // Add a style attribute with the height, or append the height to the style attribute if the style attribute already exists.
    319             if ( strpos( $html, ' style=' ) === false ) {
     319            if ( ! str_contains( $html, ' style=' ) ) {
    320320                $search[]  = '/(src=)/';
    321321                $replace[] = "style=\"height: {$logo_height}px;\" src=";
  • trunk/src/wp-content/themes/twentytwenty/template-parts/pagination.php

    r51322 r55988  
    3838
    3939// If we're not outputting the previous page link, prepend a placeholder with `visibility: hidden` to take its place.
    40 if ( strpos( $posts_pagination, 'prev page-numbers' ) === false ) {
     40if ( ! str_contains( $posts_pagination, 'prev page-numbers' ) ) {
    4141    $posts_pagination = str_replace( '<div class="nav-links">', '<div class="nav-links"><span class="prev page-numbers placeholder" aria-hidden="true">' . $prev_text . '</span>', $posts_pagination );
    4242}
    4343
    4444// If we're not outputting the next page link, append a placeholder with `visibility: hidden` to take its place.
    45 if ( strpos( $posts_pagination, 'next page-numbers' ) === false ) {
     45if ( ! str_contains( $posts_pagination, 'next page-numbers' ) ) {
    4646    $posts_pagination = str_replace( '</div>', '<span class="next page-numbers placeholder" aria-hidden="true">' . $next_text . '</span></div>', $posts_pagination );
    4747}
  • trunk/src/wp-content/themes/twentytwentyone/inc/template-functions.php

    r55095 r55988  
    446446    }
    447447
    448     if ( isset( $attr['class'] ) && false !== strpos( $attr['class'], 'custom-logo' ) ) {
     448    if ( isset( $attr['class'] ) && str_contains( $attr['class'], 'custom-logo' ) ) {
    449449        return $attr;
    450450    }
Note: See TracChangeset for help on using the changeset viewer.