Make WordPress Core

Changeset 11467


Ignore:
Timestamp:
05/27/2009 04:21:53 PM (16 years ago)
Author:
ryan
Message:

wp_unique_post_slug() fixes. Props Denis-de-Bernardy. fixes #9726

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r11461 r11467  
    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));
    1749         }
    1750 
     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       
    17511750        if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
    17521751            $suffix = 2;
    17531752            do {
    17541753                $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));
     1754                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
    17561755                $suffix++;
    17571756            } while ($post_name_check);
    17581757            $slug = $alt_post_name;
    17591758        }
    1760     }
     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       
     1765        if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
     1766            $suffix = 2;
     1767            do {
     1768                $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
     1769                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID, $post_parent));
     1770                $suffix++;
     1771            } while ($post_name_check);
     1772            $slug = $alt_post_name;
     1773        }
     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        }
     1788    }
     1789
    17611790    return $slug;
    17621791}
     
    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 ) );
    2457 
    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     }
     2485    $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    24682486
    24692487    if ( empty($post_date) )
Note: See TracChangeset for help on using the changeset viewer.