From de505f0eac437806173c3d93f8e976b1c5e628f7 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Jan 2023 15:23:13 +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. Built from https://develop.svn.wordpress.org/trunk@55053 git-svn-id: http://core.svn.wordpress.org/trunk@54586 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/kses.php | 11 ++++++++++- wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/wp-includes/kses.php b/wp-includes/kses.php index 7928f6d57a..157be6be11 100644 --- a/wp-includes/kses.php +++ b/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 { diff --git a/wp-includes/version.php b/wp-includes/version.php index c1b67be861..df05c2f376 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.