diff --git a/wp-includes/post.php b/wp-includes/post.php index 70d650ea82..50570936c1 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3150,8 +3150,8 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { $suffix = 2; do { - $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; - $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) ); + $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; + $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); $suffix++; } while ( $post_name_check ); $slug = $alt_post_name; @@ -3167,7 +3167,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { $suffix = 2; do { - $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; + $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) ); $suffix++; } while ( $post_name_check ); @@ -3181,7 +3181,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { $suffix = 2; do { - $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; + $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); $suffix++; } while ( $post_name_check ); @@ -3192,6 +3192,29 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); } +/** + * Truncates a post slug. + * + * @since 3.6.0 + * @access private + * @uses utf8_uri_encode() Makes sure UTF-8 characters are properly cut and encoded. + * + * @param string $slug The slug to truncate. + * @param int $length Max length of the slug. + * @return string The truncated slug. + */ +function _truncate_post_slug( $slug, $length = 200 ) { + if ( strlen( $slug ) > $length ) { + $decoded_slug = urldecode( $slug ); + if ( $decoded_slug === $slug ) + $slug = substr( $slug, 0, $length ); + else + $slug = utf8_uri_encode( $decoded_slug, $length ); + } + + return rtrim( $slug, '-' ); +} + /** * Adds tags to a post. *