Make WordPress Core

Changeset 55053


Ignore:
Timestamp:
01/11/2023 03:21:18 PM (2 years ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/kses.php

    r54933 r55053  
    16871687 */
    16881688function 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
Note: See TracChangeset for help on using the changeset viewer.