Make WordPress Core


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

    r55711 r55988  
    148148 */
    149149function has_shortcode( $content, $tag ) {
    150     if ( false === strpos( $content, '[' ) ) {
     150    if ( ! str_contains( $content, '[' ) ) {
    151151        return false;
    152152    }
     
    206206    global $shortcode_tags;
    207207
    208     if ( false === strpos( $content, '[' ) ) {
     208    if ( ! str_contains( $content, '[' ) ) {
    209209        return $content;
    210210    }
     
    413413        }
    414414
    415         $noopen  = false === strpos( $element, '[' );
    416         $noclose = false === strpos( $element, ']' );
     415        $noopen  = ! str_contains( $element, '[' );
     416        $noclose = ! str_contains( $element, ']' );
    417417        if ( $noopen || $noclose ) {
    418418            // This element does not contain shortcodes.
     
    560560        // Reject any unclosed HTML elements.
    561561        foreach ( $atts as &$value ) {
    562             if ( false !== strpos( $value, '<' ) ) {
     562            if ( str_contains( $value, '<' ) ) {
    563563                if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
    564564                    $value = '';
     
    635635    global $shortcode_tags;
    636636
    637     if ( false === strpos( $content, '[' ) ) {
     637    if ( ! str_contains( $content, '[' ) ) {
    638638        return $content;
    639639    }
Note: See TracChangeset for help on using the changeset viewer.