Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:34:56 PM (19 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.

File:
1 edited

Legend:

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

    r55875 r55988  
    273273            $curl = str_replace( $static_characters, $static_replacements, $curl );
    274274
    275             if ( false !== strpos( $curl, "'" ) ) {
     275            if ( str_contains( $curl, "'" ) ) {
    276276                $curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
    277277                $curl = wptexturize_primes( $curl, "'", $prime, $open_sq_flag, $closing_single_quote );
     
    279279                $curl = str_replace( $open_sq_flag, $opening_single_quote, $curl );
    280280            }
    281             if ( false !== strpos( $curl, '"' ) ) {
     281            if ( str_contains( $curl, '"' ) ) {
    282282                $curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
    283283                $curl = wptexturize_primes( $curl, '"', $double_prime, $open_q_flag, $closing_quote );
    284284                $curl = str_replace( $open_q_flag, $opening_quote, $curl );
    285285            }
    286             if ( false !== strpos( $curl, '-' ) ) {
     286            if ( str_contains( $curl, '-' ) ) {
    287287                $curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
    288288            }
     
    327327
    328328    foreach ( $sentences as $key => &$sentence ) {
    329         if ( false === strpos( $sentence, $needle ) ) {
     329        if ( ! str_contains( $sentence, $needle ) ) {
    330330            continue;
    331331        } elseif ( 0 !== $key && 0 === substr_count( $sentence, $close_quote ) ) {
     
    363363            $sentence = preg_replace( $quote_pattern, $close_quote, $sentence );
    364364        }
    365         if ( '"' === $needle && false !== strpos( $sentence, '"' ) ) {
     365        if ( '"' === $needle && str_contains( $sentence, '"' ) ) {
    366366            $sentence = str_replace( '"', $close_quote, $sentence );
    367367        }
     
    454454     * Replace pre tags with placeholders and bring them back after autop.
    455455     */
    456     if ( strpos( $text, '<pre' ) !== false ) {
     456    if ( str_contains( $text, '<pre' ) ) {
    457457        $text_parts = explode( '</pre>', $text );
    458458        $last_part  = array_pop( $text_parts );
     
    499499
    500500    // Collapse line breaks before and after <option> elements so they don't get autop'd.
    501     if ( strpos( $text, '<option' ) !== false ) {
     501    if ( str_contains( $text, '<option' ) ) {
    502502        $text = preg_replace( '|\s*<option|', '<option', $text );
    503503        $text = preg_replace( '|</option>\s*|', '</option>', $text );
     
    508508     * so they don't get autop'd.
    509509     */
    510     if ( strpos( $text, '</object>' ) !== false ) {
     510    if ( str_contains( $text, '</object>' ) ) {
    511511        $text = preg_replace( '|(<object[^>]*>)\s*|', '$1', $text );
    512512        $text = preg_replace( '|\s*</object>|', '</object>', $text );
     
    518518     * before and after <source> and <track> elements.
    519519     */
    520     if ( strpos( $text, '<source' ) !== false || strpos( $text, '<track' ) !== false ) {
     520    if ( str_contains( $text, '<source' ) || str_contains( $text, '<track' ) ) {
    521521        $text = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $text );
    522522        $text = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $text );
     
    525525
    526526    // Collapse line breaks before and after <figcaption> elements.
    527     if ( strpos( $text, '<figcaption' ) !== false ) {
     527    if ( str_contains( $text, '<figcaption' ) ) {
    528528        $text = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $text );
    529529        $text = preg_replace( '|</figcaption>\s*|', '</figcaption>', $text );
     
    594594
    595595    // Restore newlines in all elements.
    596     if ( false !== strpos( $text, '<!-- wpnl -->' ) ) {
     596    if ( str_contains( $text, '<!-- wpnl -->' ) ) {
    597597        $text = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $text );
    598598    }
     
    764764        // Loop through delimiters (elements) only.
    765765        for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
    766             if ( false !== strpos( $textarr[ $i ], $needle ) ) {
     766            if ( str_contains( $textarr[ $i ], $needle ) ) {
    767767                $textarr[ $i ] = str_replace( $needle, $replace, $textarr[ $i ] );
    768768                $changed       = true;
     
    776776        for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
    777777            foreach ( $needles as $needle ) {
    778                 if ( false !== strpos( $textarr[ $i ], $needle ) ) {
     778                if ( str_contains( $textarr[ $i ], $needle ) ) {
    779779                    $textarr[ $i ] = strtr( $textarr[ $i ], $replace_pairs );
    780780                    $changed       = true;
     
    20562056    $filename = trim( $filename, '.-_' );
    20572057
    2058     if ( false === strpos( $filename, '.' ) ) {
     2058    if ( ! str_contains( $filename, '.' ) ) {
    20592059        $mime_types = wp_get_mime_types();
    20602060        $filetype   = wp_check_filetype( 'test.' . $filename, $mime_types );
     
    44764476    }
    44774477
    4478     if ( ( false !== strpos( $url, '[' ) ) || ( false !== strpos( $url, ']' ) ) ) {
     4478    if ( ( str_contains( $url, '[' ) ) || ( str_contains( $url, ']' ) ) ) {
    44794479
    44804480        $parsed = wp_parse_url( $url );
     
    51445144 */
    51455145function wp_pre_kses_less_than_callback( $matches ) {
    5146     if ( false === strpos( $matches[0], '>' ) ) {
     5146    if ( ! str_contains( $matches[0], '>' ) ) {
    51475147        return esc_html( $matches[0] );
    51485148    }
     
    59335933    foreach ( $emoji as $emojum ) {
    59345934        $emoji_char = html_entity_decode( $emojum );
    5935         if ( false !== strpos( $content, $emoji_char ) ) {
     5935        if ( str_contains( $content, $emoji_char ) ) {
    59365936            $content = preg_replace( "/$emoji_char/", $emojum, $content );
    59375937        }
     
    59505950 */
    59515951function wp_staticize_emoji( $text ) {
    5952     if ( false === strpos( $text, '&#x' ) ) {
     5952    if ( ! str_contains( $text, '&#x' ) ) {
    59535953        if ( ( function_exists( 'mb_check_encoding' ) && mb_check_encoding( $text, 'ASCII' ) ) || ! preg_match( '/[^\x00-\x7F]/', $text ) ) {
    59545954            // The text doesn't contain anything that might be emoji, so we can return early.
     
    59695969    $possible_emoji = array();
    59705970    foreach ( $emoji as $emojum ) {
    5971         if ( false !== strpos( $text, $emojum ) ) {
     5971        if ( str_contains( $text, $emojum ) ) {
    59725972            $possible_emoji[ $emojum ] = html_entity_decode( $emojum );
    59735973        }
     
    60076007
    60086008        // If it's not a tag and not in ignore block.
    6009         if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] && false !== strpos( $content, '&#x' ) ) {
     6009        if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] && str_contains( $content, '&#x' ) ) {
    60106010            foreach ( $possible_emoji as $emojum => $emoji_char ) {
    6011                 if ( false === strpos( $content, $emojum ) ) {
     6011                if ( ! str_contains( $content, $emojum ) ) {
    60126012                    continue;
    60136013                }
Note: See TracChangeset for help on using the changeset viewer.