Skip to content

Commit 9cee648

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@57452 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a074589 commit 9cee648

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/wp-includes/functions.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,57 @@
55
* @package WordPress
66
*/
77

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+
13+
if ( ! 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+
35+
if ( ! 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+
}
58+
859
require( ABSPATH . WPINC . '/option.php' );
960

1061
/**

0 commit comments

Comments
 (0)