-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin: Fix fatal error for older WP/PHP versions (#40538)
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Polyfills for functions missing from older WP versions. | ||
* | ||
* This file should be removed when WordPress 5.9.0 becomes the lowest | ||
* supported version by this plugin. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'str_contains' ) ) { | ||
/** | ||
* Polyfill for `str_contains()` function added in PHP 8.0. | ||
* | ||
* Performs a case-sensitive check indicating if needle is | ||
* contained in haystack. | ||
* | ||
* @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 `$needle` is in `$haystack`, otherwise false. | ||
*/ | ||
function str_contains( $haystack, $needle ) { | ||
return ( '' === $needle || false !== strpos( $haystack, $needle ) ); | ||
} | ||
} | ||
|
||
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 && '' !== $needle ) { | ||
return false; | ||
} | ||
$len = strlen( $needle ); | ||
return 0 === substr_compare( $haystack, $needle, -$len, $len ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters