WordPress.org

Make WordPress Core

Changeset 11071


Ignore:
Timestamp:
04/23/09 20:21:18 (5 years ago)
Author:
ryan
Message:

Add wp_unique_post_slug(). Apply it when getting sample permalinks. Props nbachiyski. fixes #6595

Location:
trunk
Files:
2 edited

Legend:

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

    r10957 r11071  
    927927        $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID); 
    928928    } 
     929 
     930    $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);   
    929931 
    930932    // If the user wants to set a new name -- override the current one 
  • trunk/wp-includes/post.php

    r10904 r11071  
    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); 
     
    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  
    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     } 
     1489    $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); 
    15001490 
    15011491    // expected_slashed (everything!) 
     
    17041694 
    17051695    return wp_publish_post($post_id); 
     1696} 
     1697 
     1698 
     1699/** 
     1700 * Given the desired slug and some post details computes a unique slug for the post. 
     1701 * 
     1702 * @param string $slug the desired slug (post_name) 
     1703 * @param integer $post_ID 
     1704 * @param string $post_status no uniqueness checks are made if the post is still draft or pending 
     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_post_slug($slug, $post_ID, $post_status, $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", $slug, $post_type, $post_ID, $post_parent)); 
     1713 
     1714        if ($post_name_check || in_array($slug, $wp_rewrite->feeds) ) { 
     1715            $suffix = 2; 
     1716            do { 
     1717                $alt_post_name = substr($slug, 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            $slug = $alt_post_name; 
     1722        } 
     1723    } 
     1724    return $slug; 
    17061725} 
    17071726 
Note: See TracChangeset for help on using the changeset viewer.