Database: Replace `str_ends_with()` usage in `wpdb::prepare()`.

This avoids a fatal error if the file is included directly outside of WordPress core, e.g. by HyperDB.

While WordPress core does include a polyfill function, it is not directly loaded in the `wpdb` class.

This commit replaces the `str_ends_with()` calls with `substr_compare()` for now.

Follow-up to [55151].

Props Otto42.
See #52506.
Built from https://develop.svn.wordpress.org/trunk@55157


git-svn-id: http://core.svn.wordpress.org/trunk@54690 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-01-28 13:48:20 +00:00
parent ac0cb7a37b
commit d9de5c33a0
2 changed files with 7 additions and 3 deletions

View File

@ -1561,7 +1561,9 @@ class wpdb {
$format = substr( $placeholder, 1, -1 );
$type = substr( $placeholder, -1 );
if ( 'f' === $type && true === $this->allow_unsafe_unquoted_parameters && str_ends_with( $split_query[ $key - 1 ], '%' ) ) {
if ( 'f' === $type && true === $this->allow_unsafe_unquoted_parameters
&& 0 === substr_compare( $split_query[ $key - 1 ], '%', -1, 1 )
) {
/*
* Before WP 6.2 the "force floats to be locale-unaware" RegEx didn't
@ -1620,7 +1622,9 @@ class wpdb {
* First, "numbered or formatted string placeholders (eg, %1$s, %5s)".
* Second, if "%s" has a "%" before it, even if it's unrelated (e.g. "LIKE '%%%s%%'").
*/
if ( true !== $this->allow_unsafe_unquoted_parameters || ( '' === $format && ! str_ends_with( $split_query[ $key - 1 ], '%' ) ) ) {
if ( true !== $this->allow_unsafe_unquoted_parameters
|| ( '' === $format && 0 !== substr_compare( $split_query[ $key - 1 ], '%', -1, 1 ) )
) {
$placeholder = "'%" . $format . "s'";
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55156';
$wp_version = '6.2-alpha-55157';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.