Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:55:47 PM (15 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Replace usage of substr() with str_starts_with() and str_ends_with().

str_starts_with() and str_ends_with() were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle).

WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.

This commit uses str_starts_with() and str_ends_with() in core files where appropriate:

  • $needle === substr( $string, 0, $length ), where $length is the length of $needle, is replaced with str_starts_with( $haystack, $needle ).
  • $needle === substr( $string, $offset ), where $offset is negative and the absolute value of $offset is the length of $needle, is replaced with str_ends_with( $haystack, $needle ).

This aims to make the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].

Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.

File:
1 edited

Legend:

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

    r55414 r55990  
    909909    if ( is_array( $sidebars_widgets ) ) {
    910910        foreach ( $sidebars_widgets as $sidebar => $widgets ) {
    911             if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
     911            if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) ) {
    912912                continue;
    913913            }
     
    13831383
    13841384    foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
    1385         if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
     1385        if ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) {
    13861386            $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets );
    13871387            unset( $existing_sidebars_widgets[ $sidebar ] );
     
    14911491        // Remove orphaned widgets, we're only interested in previously active sidebars.
    14921492        foreach ( $old_sidebars_widgets as $sidebar => $widgets ) {
    1493             if ( 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
     1493            if ( str_starts_with( $sidebar, 'orphaned_widgets' ) ) {
    14941494                unset( $old_sidebars_widgets[ $sidebar ] );
    14951495            }
     
    16281628
    16291629            // Change existing [...] to [&hellip;].
    1630             if ( '[...]' === substr( $summary, -5 ) ) {
     1630            if ( str_ends_with( $summary, '[...]' ) ) {
    16311631                $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
    16321632            }
Note: See TracChangeset for help on using the changeset viewer.