Formatting: Improve performance of `esc_url()`.

This changeset indirectly improves performance of the commonly used `esc_url()` function by optimizing the low-level function `wp_kses_bad_protocol()` for the by far most common scenarios, which are URLs using either the `http` or `https` protocol.

For this common scenario, the changeset now avoids the `do while` loop. While for a single call to the `esc_url()` function the performance wins are negligible, given that `esc_url()` is often called many times in one page load, they can add up, making this a worthwhile improvement.

Props mukesh27, schlessera, markjaquith, azaozz, spacedmonkey.
Fixes #22951.

Built from https://develop.svn.wordpress.org/trunk@55053


git-svn-id: http://core.svn.wordpress.org/trunk@54586 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-01-11 15:23:13 +00:00
parent bc8ed77b19
commit de505f0eac
2 changed files with 11 additions and 2 deletions

View File

@ -1686,7 +1686,16 @@ function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) {
* @return string Filtered content.
*/
function wp_kses_bad_protocol( $content, $allowed_protocols ) {
$content = wp_kses_no_null( $content );
$content = wp_kses_no_null( $content );
// Short-circuit if the string starts with `https://` or `http://`. Most common cases.
if (
( str_starts_with( $content, 'https://' ) && in_array( 'https', $allowed_protocols, true ) ) ||
( str_starts_with( $content, 'http://' ) && in_array( 'http', $allowed_protocols, true ) )
) {
return $content;
}
$iterations = 0;
do {

View File

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