WordPress.org

Make WordPress Core

Ticket #9726: 9726.4.diff

File 9726.4.diff, 4.8 KB (added by ryan, 9 years ago)
  • wp-includes/post.php

     
    13941394        if ( !empty($ID) ) {
    13951395                $update = true;
    13961396                $previous_status = get_post_field('post_status', $ID);
     1397                $previous_slug = get_post_field('post_name', $ID);
    13971398        } else {
    13981399                $previous_status = 'new';
    13991400        }
     
    15131514        if ( !isset($post_password) || 'private' == $post_status )
    15141515                $post_password = '';
    15151516
    1516         $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
     1517        // If updating a published post, make slug unique only if it has changed.
     1518        // This prevents slugs for already published posts from being changed due to new uniqueness rules.
     1519        if ( !isset($previous_slug) || ('publish' != $previous_status) || ('publish' != $post_status) || ($previous_slug != $post_name))
     1520                $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    15171521
    15181522        // expected_slashed (everything!)
    15191523        $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );
     
    17361740 * @param integer $post_parent
    17371741 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
    17381742 */
    1739 function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
     1743function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type = '', $post_parent = '') {
    17401744        global $wpdb, $wp_rewrite;
    1741         if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
    1742                 $hierarchical_post_types = apply_filters('hierarchical_post_types', array('page', 'attachment'));
    1743                 if ( in_array($post_type, $hierarchical_post_types) ) {
    1744                         $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode("', '", $wpdb->escape($hierarchical_post_types)) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
    1745                         $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID, $post_parent));
    1746                 } else {
    1747                         $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1";
    1748                         $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID, $post_parent));
    1749                 }
    17501745
    1751                 if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
    1752                         $suffix = 2;
    1753                         do {
    1754                                 $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
    1755                                 $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
    1756                                 $suffix++;
    1757                         } while ($post_name_check);
    1758                         $slug = $alt_post_name;
    1759                 }
     1746        $original_slug = $slug;
     1747
     1748        if ( in_array( $post_status, array( 'draft', 'pending' ) ) )
     1749                return apply_filters('unique_post_slug', $slug, $original_slug, $post_ID, $post_status, $post_type, $post_parent);
     1750       
     1751        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type != 'revision' AND post_status != 'draft' AND ID != %d LIMIT 1";
     1752        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));
     1753        if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
     1754                $suffix = 2;
     1755                do {
     1756                        $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
     1757                        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
     1758                        $suffix++;
     1759                } while ($post_name_check);
     1760                $slug = $alt_post_name;
    17601761        }
    1761         return $slug;
     1762
     1763        return apply_filters('unique_post_slug', $slug, $original_slug, $post_ID, $post_status, $post_type, $post_parent);
    17621764}
    17631765
    17641766/**
     
    24522454        else
    24532455                $post_name = sanitize_title($post_name);
    24542456
    2455         // expected_slashed ($post_name)
    2456         $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d LIMIT 1", $post_name, $post_ID ) );
     2457        $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    24572458
    2458         if ( $post_name_check ) {
    2459                 $suffix = 2;
    2460                 while ( $post_name_check ) {
    2461                         $alt_post_name = $post_name . "-$suffix";
    2462                         // expected_slashed ($alt_post_name, $post_name)
    2463                         $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d LIMIT 1", $alt_post_name, $post_ID ) );
    2464                         $suffix++;
    2465                 }
    2466                 $post_name = $alt_post_name;
    2467         }
    2468 
    24692459        if ( empty($post_date) )
    24702460                $post_date = current_time('mysql');
    24712461        if ( empty($post_date_gmt) )
     
    37763766
    37773767                add_filter('the_preview', '_set_preview');
    37783768        }
    3779 }
     3769}
     3770 No newline at end of file