Skip to content

Commit 0aa20cc

Browse files
committed
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
1 parent c3aed4a commit 0aa20cc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/wp-includes/kses.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,16 @@ function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) {
16861686
* @return string Filtered content.
16871687
*/
16881688
function wp_kses_bad_protocol( $content, $allowed_protocols ) {
1689-
$content = wp_kses_no_null( $content );
1689+
$content = wp_kses_no_null( $content );
1690+
1691+
// Short-circuit if the string starts with `https://` or `http://`. Most common cases.
1692+
if (
1693+
( str_starts_with( $content, 'https://' ) && in_array( 'https', $allowed_protocols, true ) ) ||
1694+
( str_starts_with( $content, 'http://' ) && in_array( 'http', $allowed_protocols, true ) )
1695+
) {
1696+
return $content;
1697+
}
1698+
16901699
$iterations = 0;
16911700

16921701
do {

0 commit comments

Comments
 (0)