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/class-wp.php

    r55703 r55988  
    275275
    276276            // If req_uri is empty or if it is a request for ourself, unset error.
    277             if ( empty( $requested_path ) || $requested_file == $self || strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
     277            if ( empty( $requested_path ) || $requested_file == $self || str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' ) ) {
    278278                unset( $error, $_GET['error'] );
    279279
    280                 if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
     280                if ( isset( $perma_query_vars ) && str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' ) ) {
    281281                    unset( $perma_query_vars );
    282282                }
     
    456456            // We're showing a feed, so WP is indeed the only thing that last changed.
    457457            if ( ! empty( $this->query_vars['withcomments'] )
    458                 || false !== strpos( $this->query_vars['feed'], 'comments-' )
     458                || str_contains( $this->query_vars['feed'], 'comments-' )
    459459                || ( empty( $this->query_vars['withoutcomments'] )
    460460                    && ( ! empty( $this->query_vars['p'] )
     
    721721                if ( $post && ! empty( $this->query_vars['page'] ) ) {
    722722                    // Check if content is actually intended to be paged.
    723                     if ( false !== strpos( $post->post_content, $next ) ) {
     723                    if ( str_contains( $post->post_content, $next ) ) {
    724724                        $page          = trim( $this->query_vars['page'], '/' );
    725725                        $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
Note: See TracChangeset for help on using the changeset viewer.