summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAaron Jorbin <jorbin@git.wordpress.org>2024-01-30 17:16:33 +0000
committerAaron Jorbin <jorbin@git.wordpress.org>2024-01-30 17:16:33 +0000
commit7bd333d762d9a9e44b0dd1877c31641cc46d40ca (patch)
tree711d3a2ba380bc9355b9b3000c989f75ace4aa2c
parent6e49b5da7e06a60331e1a20b170d90780b84a622 (diff)
downloadwordpress-7bd333d762d9a9e44b0dd1877c31641cc46d40ca.tar.gz
wordpress-7bd333d762d9a9e44b0dd1877c31641cc46d40ca.zip
General: Backport polyfills for `str_ends_with()` and `str_starts_with()` .
Merges [52040], [56016], and [56015] to 4.1 branch. Props ocean90, SergeyBiryukov, desrosj, joemcgill, jorbin, mukesh27. git-svn-id: https://develop.svn.wordpress.org/branches/4.1@57438 602fd350-edb4-49c9-b593-d223f7449a82
-rw-r--r--src/wp-includes/compat.php46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php
index e657aea5a7..62316ff1e3 100644
--- a/src/wp-includes/compat.php
+++ b/src/wp-includes/compat.php
@@ -247,3 +247,49 @@ endif;
if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
define( 'JSON_PRETTY_PRINT', 128 );
}
+
+if ( ! function_exists( 'str_starts_with' ) ) {
+ /**
+ * Polyfill for `str_starts_with()` function added in PHP 8.0.
+ *
+ * Performs a case-sensitive check indicating if
+ * the haystack begins with needle.
+ *
+ * @since 5.9.0
+ *
+ * @param string $haystack The string to search in.
+ * @param string $needle The substring to search for in the `$haystack`.
+ * @return bool True if `$haystack` starts with `$needle`, otherwise false.
+ */
+ function str_starts_with( $haystack, $needle ) {
+ if ( '' === $needle ) {
+ return true;
+ }
+
+ return 0 === strpos( $haystack, $needle );
+ }
+}
+
+if ( ! function_exists( 'str_ends_with' ) ) {
+ /**
+ * Polyfill for `str_ends_with()` function added in PHP 8.0.
+ *
+ * Performs a case-sensitive check indicating if
+ * the haystack ends with needle.
+ *
+ * @since 5.9.0
+ *
+ * @param string $haystack The string to search in.
+ * @param string $needle The substring to search for in the `$haystack`.
+ * @return bool True if `$haystack` ends with `$needle`, otherwise false.
+ */
+ function str_ends_with( $haystack, $needle ) {
+ if ( '' === $haystack ) {
+ return '' === $needle;
+ }
+
+ $len = strlen( $needle );
+
+ return substr( $haystack, -$len, $len ) === $needle;
+ }
+}