Make WordPress Core


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

    r55412 r55988  
    6666
    6767if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
    68     if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Lynx' ) !== false ) {
     68    if ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Lynx' ) ) {
    6969        $is_lynx = true;
    70     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Edg' ) !== false ) {
     70    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Edg' ) ) {
    7171        $is_edge = true;
    7272    } elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chrome' ) !== false ) {
     
    9090    } elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'safari' ) !== false ) {
    9191        $is_safari = true;
    92     } elseif ( ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false || strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident' ) !== false ) && strpos( $_SERVER['HTTP_USER_AGENT'], 'Win' ) !== false ) {
     92    } elseif ( ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Trident' ) ) && str_contains( $_SERVER['HTTP_USER_AGENT'], 'Win' ) ) {
    9393        $is_winIE = true;
    94     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false && strpos( $_SERVER['HTTP_USER_AGENT'], 'Mac' ) !== false ) {
     94    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) && str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mac' ) ) {
    9595        $is_macIE = true;
    96     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Gecko' ) !== false ) {
     96    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Gecko' ) ) {
    9797        $is_gecko = true;
    98     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera' ) !== false ) {
     98    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera' ) ) {
    9999        $is_opera = true;
    100     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Nav' ) !== false && strpos( $_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.' ) !== false ) {
     100    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Nav' ) && str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.' ) ) {
    101101        $is_NS4 = true;
    102102    }
     
    116116 * @global bool $is_apache
    117117 */
    118 $is_apache = ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false );
     118$is_apache = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) );
    119119
    120120/**
     
    123123 * @global bool $is_nginx
    124124 */
    125 $is_nginx = ( strpos( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) !== false );
     125$is_nginx = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) );
    126126
    127127/**
     
    130130 * @global bool $is_IIS
    131131 */
    132 $is_IIS = ! $is_apache && ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) !== false || strpos( $_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer' ) !== false );
     132$is_IIS = ! $is_apache && ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer' ) );
    133133
    134134/**
     
    149149    if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
    150150        $is_mobile = false;
    151     } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) !== false // Many mobile devices (all iPhone, iPad, etc.)
    152         || strpos( $_SERVER['HTTP_USER_AGENT'], 'Android' ) !== false
    153         || strpos( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) !== false
    154         || strpos( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) !== false
    155         || strpos( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) !== false
    156         || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) !== false
    157         || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) !== false ) {
     151    } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.)
     152        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
     153        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' )
     154        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' )
     155        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' )
     156        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' )
     157        || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) {
    158158            $is_mobile = true;
    159159    } else {
Note: See TracChangeset for help on using the changeset viewer.