WordPress.org

Make WordPress Core

Ticket #21013: 21013.5.patch

File 21013.5.patch, 2.6 KB (added by SergeyBiryukov, 6 years ago)
  • wp-includes/post.php

     
    28502850                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
    28512851                        $suffix = 2;
    28522852                        do {
    2853                                 $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    2854                                 $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );
     2853                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     2854                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
    28552855                                $suffix++;
    28562856                        } while ( $post_name_check );
    28572857                        $slug = $alt_post_name;
     
    28652865                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 ) ) {
    28662866                        $suffix = 2;
    28672867                        do {
    2868                                 $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     2868                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    28692869                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );
    28702870                                $suffix++;
    28712871                        } while ( $post_name_check );
     
    28792879                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
    28802880                        $suffix = 2;
    28812881                        do {
    2882                                 $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     2882                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    28832883                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
    28842884                                $suffix++;
    28852885                        } while ( $post_name_check );
     
    28912891}
    28922892
    28932893/**
     2894 * Truncate post slug with UTF-8 characters.
     2895 *
     2896 * @since 3.5.0
     2897 * @access private
     2898 *
     2899 * @param string $slug The slug to truncate.
     2900 * @param int $length Max length of the slug.
     2901 * @return string The truncated slug.
     2902 */
     2903function _truncate_post_slug( $slug, $length = 200 ) {
     2904        while ( strlen( $slug ) > $length ) {
     2905                $last_char_length = ( preg_match( '/(%[a-fA-F0-9]{2}){2}$/', $slug ) ) ? 6 : 1;
     2906                $slug = substr( $slug, 0, -$last_char_length );
     2907        }
     2908
     2909        return $slug;
     2910}
     2911
     2912/**
    28942913 * Adds tags to a post.
    28952914 *
    28962915 * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true.