From 0aa20cc0502f9d46cc1fe99abd3ba8bdfb4fc5d1 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Jan 2023 15:21:18 +0000 Subject: [PATCH] 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 --- src/wp-includes/kses.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index 7928f6d57a..157be6be11 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -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 {