Make WordPress Core

Changeset 23420


Ignore:
Timestamp:
02/15/2013 02:35:41 PM (13 years ago)
Author:
SergeyBiryukov
Message:

Properly truncate UTF-8 post slugs in wp_unique_post_slug(). fixes #21013.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r23416 r23420  
    31513151                        $suffix = 2;
    31523152                        do {
    3153                                 $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3154                                 $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );
     3153                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     3154                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
    31553155                                $suffix++;
    31563156                        } while ( $post_name_check );
     
    31683168                        $suffix = 2;
    31693169                        do {
    3170                                 $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     3170                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    31713171                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );
    31723172                                $suffix++;
     
    31823182                        $suffix = 2;
    31833183                        do {
    3184                                 $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     3184                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    31853185                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
    31863186                                $suffix++;
     
    31913191
    31923192        return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
     3193}
     3194
     3195/**
     3196 * Truncates a post slug.
     3197 *
     3198 * @since 3.6.0
     3199 * @access private
     3200 * @uses utf8_uri_encode() Makes sure UTF-8 characters are properly cut and encoded.
     3201 *
     3202 * @param string $slug The slug to truncate.
     3203 * @param int $length Max length of the slug.
     3204 * @return string The truncated slug.
     3205 */
     3206function _truncate_post_slug( $slug, $length = 200 ) {
     3207        if ( strlen( $slug ) > $length ) {
     3208                $decoded_slug = urldecode( $slug );
     3209                if ( $decoded_slug === $slug )
     3210                        $slug = substr( $slug, 0, $length );
     3211                else
     3212                        $slug = utf8_uri_encode( $decoded_slug, $length );
     3213        }
     3214
     3215        return rtrim( $slug, '-' );
    31933216}
    31943217
Note: See TracChangeset for help on using the changeset viewer.