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.


git-svn-id: https://develop.svn.wordpress.org/trunk@55053 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2023-01-11 15:21:18 +00:00
parent c3aed4a3b1
commit 0aa20cc050

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 {