WordPress.org

Make WordPress Core

Ticket #9726: 9726.5.diff

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

     
    17371737 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
    17381738 */
    17391739function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
     1740        if ( in_array( $post_status, array( 'draft', 'pending' ) ) )
     1741                return $slug;
     1742       
    17401743        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));
     1744        $hierarchical_post_types = apply_filters('hierarchical_post_types', array('page'));
     1745        if ( 'attachment' == $post_type ) {
     1746                // Attachment slugs must be unique across all types.
     1747                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
     1748                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));
     1749               
     1750                if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
     1751                        $suffix = 2;
     1752                        do {
     1753                                $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
     1754                                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
     1755                                $suffix++;
     1756                        } while ($post_name_check);
     1757                        $slug = $alt_post_name;
    17491758                }
    1750 
     1759        } elseif ( in_array($post_type, $hierarchical_post_types) ) {
     1760                // Page slugs must be unique within their own trees.  Pages are in a
     1761                // separate namespace than posts so page slugs are allowed to overlap post slugs.
     1762                $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";
     1763                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID, $post_parent));
     1764               
    17511765                if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
    17521766                        $suffix = 2;
    17531767                        do {
    17541768                                $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));
     1769                                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID, $post_parent));
    17561770                                $suffix++;
    17571771                        } while ($post_name_check);
    17581772                        $slug = $alt_post_name;
    17591773                }
     1774        } else {
     1775                // Post slugs must be unique across all posts.
     1776                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
     1777                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID));
     1778               
     1779                if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
     1780                        $suffix = 2;
     1781                        do {
     1782                                $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
     1783                                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID));
     1784                                $suffix++;
     1785                        } while ($post_name_check);
     1786                        $slug = $alt_post_name;
     1787                }
    17601788        }
     1789
    17611790        return $slug;
    17621791}
    17631792
     
    24532482                $post_name = sanitize_title($post_name);
    24542483
    24552484        // 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 ) );
     2485        $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    24572486
    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 
    24692487        if ( empty($post_date) )
    24702488                $post_date = current_time('mysql');
    24712489        if ( empty($post_date_gmt) )