WordPress.org

Make WordPress Core

Ticket #6595: no-duplicate-permalinks-on-edit.diff

File no-duplicate-permalinks-on-edit.diff, 4.1 KB (added by nbachiyski, 5 years ago)
  • wp-includes/post.php

     
    14101410 
    14111411        // Create a valid post name.  Drafts and pending posts are allowed to have an empty 
    14121412        // post name. 
    1413         if ( empty($post_name) ) { 
     1413        if ( !isset($post_name) || empty($post_name) ) { 
    14141414                if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) 
    14151415                        $post_name = sanitize_title($post_title); 
     1416                else 
     1417                        $post_name = ''; 
    14161418        } else { 
    14171419                $post_name = sanitize_title($post_name); 
    14181420        } 
     
    14841486        if ( !isset($post_password) || 'private' == $post_status ) 
    14851487                $post_password = ''; 
    14861488 
    1487         if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) { 
    1488                 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $post_name, $post_type, $post_ID, $post_parent)); 
     1489        $post_name = wp_unique_slug($post_ID, $post_status, $post_name, $post_type, $post_parent); 
    14891490 
    1490                 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) { 
    1491                         $suffix = 2; 
    1492                         do { 
    1493                                 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix"; 
    1494                                 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent)); 
    1495                                 $suffix++; 
    1496                         } while ($post_name_check); 
    1497                         $post_name = $alt_post_name; 
    1498                 } 
    1499         } 
    1500  
    15011491        // expected_slashed (everything!) 
    15021492        $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' ) ); 
    15031493        $data = apply_filters('wp_insert_post_data', $data, $postarr); 
     
    17051695        return wp_publish_post($post_id); 
    17061696} 
    17071697 
     1698 
    17081699/** 
     1700 * Given the desired slug and some post details computes a unique slug for the post. 
     1701 *  
     1702 * @param integer $post_ID 
     1703 * @param string $post_status no uniqueness checks are made if the post is still draft or pending 
     1704 * @param string $post_name the desired slug 
     1705 * @param string $post_type 
     1706 * @param integer $post_parent 
     1707 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)  
     1708 */ 
     1709function wp_unique_slug($post_ID, $post_status, $post_name, $post_type, $post_parent) { 
     1710        global $wpdb, $wp_rewrite; 
     1711        if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) { 
     1712                $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $post_name, $post_type, $post_ID, $post_parent)); 
     1713 
     1714                if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) { 
     1715                        $suffix = 2; 
     1716                        do { 
     1717                                $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix"; 
     1718                                $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent)); 
     1719                                $suffix++; 
     1720                        } while ($post_name_check); 
     1721                        $post_name = $alt_post_name; 
     1722                } 
     1723        } 
     1724        return $post_name; 
     1725} 
     1726 
     1727/** 
    17091728 * Adds tags to a post. 
    17101729 * 
    17111730 * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true. 
     
    37013720 
    37023721                add_filter('the_preview', '_set_preview'); 
    37033722        } 
    3704 } 
     3723} 
     3724 No newline at end of file 
  • wp-admin/includes/post.php

     
    923923        if (!is_null($name)) { 
    924924                $post->post_name = sanitize_title($name? $name : $title, $post->ID); 
    925925        } 
     926         
     927        $post->post_name = wp_unique_slug($post->ID, $post->post_status, $post->post_name, $post->post_type, $post->post_parent);        
    926928 
    927929        $post->filter = 'sample';  
    928930