From 61c8b525b05a755b8689233b92379e29113d9e21 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Tue, 30 Jan 2024 18:01:22 +0000 Subject: [PATCH] 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.6 branch. Props ocean90, SergeyBiryukov, desrosj, joemcgill, jorbin, mukesh27. Built from https://develop.svn.wordpress.org/branches/4.6@57450 git-svn-id: http://core.svn.wordpress.org/branches/4.6@56951 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 56 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 35068ed5b6..5a4b9b0673 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -5,6 +5,58 @@ * @package WordPress */ +/* + These next two functions are in this file due to compat.php having `__autoload` + in it and the PHP linter auto failing on it. +*/ + +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; + } +} + + require( ABSPATH . WPINC . '/option.php' ); /** @@ -2357,7 +2409,7 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { } else { if ( $type !== $real_mime ) { /* - * Everything else including image/* and application/*: + * Everything else including image/* and application/*: * If the real content type doesn't match the file extension, assume it's dangerous. */ $type = $ext = false; @@ -2366,7 +2418,7 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { } } - // The mime type must be allowed + // The mime type must be allowed if ( $type ) { $allowed = get_allowed_mime_types();