Make WordPress Core

Changeset 57452


Ignore:
Timestamp:
01/30/2024 06:03:42 PM (6 months ago)
Author:
jorbin
Message:

General: Backport polyfills for str_ends_with() and str_starts_with().

Uses src/wp-includes/functions.php becouse commiting to src/wp-includes/compat.php fails due to the presence of autoload.

Merges [52040], [56016], and [56015] to 4.7 branch.

Props ocean90, SergeyBiryukov, desrosj, joemcgill, jorbin, mukesh27.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/functions.php

    r54566 r57452  
    55 * @package WordPress
    66 */
     7
     8/*
     9    These next two functions are in this file due to compat.php having `__autoload`
     10    in it and the PHP linter auto failing on it.
     11*/
     12
     13if ( ! function_exists( 'str_starts_with' ) ) {
     14    /**
     15     * Polyfill for `str_starts_with()` function added in PHP 8.0.
     16     *
     17     * Performs a case-sensitive check indicating if
     18     * the haystack begins with needle.
     19     *
     20     * @since 5.9.0
     21     *
     22     * @param string $haystack The string to search in.
     23     * @param string $needle   The substring to search for in the `$haystack`.
     24     * @return bool True if `$haystack` starts with `$needle`, otherwise false.
     25     */
     26    function str_starts_with( $haystack, $needle ) {
     27        if ( '' === $needle ) {
     28            return true;
     29        }
     30
     31        return 0 === strpos( $haystack, $needle );
     32    }
     33}
     34
     35if ( ! function_exists( 'str_ends_with' ) ) {
     36    /**
     37     * Polyfill for `str_ends_with()` function added in PHP 8.0.
     38     *
     39     * Performs a case-sensitive check indicating if
     40     * the haystack ends with needle.
     41     *
     42     * @since 5.9.0
     43     *
     44     * @param string $haystack The string to search in.
     45     * @param string $needle   The substring to search for in the `$haystack`.
     46     * @return bool True if `$haystack` ends with `$needle`, otherwise false.
     47     */
     48    function str_ends_with( $haystack, $needle ) {
     49        if ( '' === $haystack ) {
     50            return '' === $needle;
     51        }
     52
     53        $len = strlen( $needle );
     54
     55        return substr( $haystack, -$len, $len ) === $needle;
     56    }
     57}
    758
    859require( ABSPATH . WPINC . '/option.php' );
Note: See TracChangeset for help on using the changeset viewer.