From 77a972838c495dca96164e9ebfa24780be439e4e Mon Sep 17 00:00:00 2001 From: desrosj Date: Thu, 6 Jan 2022 17:10:00 +0000 Subject: [PATCH] Formatting: Correctly encode ASCII characters in post slugs. Props zieladam, whyisjake, xknown, peterwilsoncc, desrosj, iandunn. Built from https://develop.svn.wordpress.org/trunk@52457 git-svn-id: http://core.svn.wordpress.org/trunk@52049 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 17 +++++++++++------ wp-includes/post.php | 2 +- wp-includes/version.php | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 905b07b598..dac06eb571 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -1138,12 +1138,14 @@ function wp_check_invalid_utf8( $string, $strip = false ) { * Encode the Unicode values to be used in the URI. * * @since 1.5.0 + * @since 5.8.3 Added the `encode_ascii_characters` parameter. * - * @param string $utf8_string - * @param int $length Max length of the string + * @param string $utf8_string String to encode. + * @param int $length Max length of the string + * @param bool $encode_ascii_characters Whether to encode ascii characters such as < " ' * @return string String with Unicode encoded for URI. */ -function utf8_uri_encode( $utf8_string, $length = 0 ) { +function utf8_uri_encode( $utf8_string, $length = 0, $encode_ascii_characters = false ) { $unicode = ''; $values = array(); $num_octets = 1; @@ -1158,11 +1160,14 @@ function utf8_uri_encode( $utf8_string, $length = 0 ) { $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { - if ( $length && ( $unicode_length >= $length ) ) { + $char = chr( $value ); + $encoded_char = $encode_ascii_characters ? rawurlencode( $char ) : $char; + $encoded_char_length = strlen( $encoded_char ); + if ( $length && ( $unicode_length + $encoded_char_length ) > $length ) { break; } - $unicode .= chr( $value ); - $unicode_length++; + $unicode .= $encoded_char; + $unicode_length += $encoded_char_length; } else { if ( count( $values ) == 0 ) { if ( $value < 224 ) { diff --git a/wp-includes/post.php b/wp-includes/post.php index 5cc7f66244..582e17a863 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5142,7 +5142,7 @@ function _truncate_post_slug( $slug, $length = 200 ) { if ( $decoded_slug === $slug ) { $slug = substr( $slug, 0, $length ); } else { - $slug = utf8_uri_encode( $decoded_slug, $length ); + $slug = utf8_uri_encode( $decoded_slug, $length, true ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 8db4c50615..d0d01ad390 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-52456'; +$wp_version = '6.0-alpha-52457'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.