WordPress.org

Make WordPress Core

Changeset 30629


Ignore:
Timestamp:
11/30/2014 04:48:54 AM (3 years ago)
Author:
johnbillion
Message:

Check attachments as well as the current post type when checking for duplicate post slugs. This avoids creating a page with a slug which conflicts with an existing attachment (the inverse is already handled).

Updates the existing test for pages which should take priority over attachments in cases where an existing clash exists.

Fixes #18962
Props boonebgorges

Location:
trunk
Files:
2 edited

Legend:

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

    r30545 r30629  
    37043704         * namespace than posts so page slugs are allowed to overlap post slugs.
    37053705         */
    3706         $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";
     3706        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
    37073707        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
    37083708
  • trunk/tests/phpunit/tests/post.php

    r30529 r30629  
    505505     */
    506506    function test_get_page_by_path_priority() {
     507        global $wpdb;
     508
    507509        $attachment = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) );
    508510        $page       = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
    509511        $other_att  = $this->factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) );
     512
     513        $wpdb->update( $wpdb->posts, array( 'post_name' => 'some-page' ), array( 'ID' => $page->ID ) );
     514        clean_post_cache( $page->ID );
     515
     516        $page = get_post( $page->ID );
    510517
    511518        $this->assertEquals( 'some-page', $attachment->post_name );
     
    989996
    990997    /**
     998     * @ticket 18962
     999     */
     1000    function test_wp_unique_post_slug_with_hierarchy_and_attachments() {
     1001        register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
     1002
     1003        $args = array(
     1004            'post_type' => 'post-type-1',
     1005            'post_name' => 'some-slug',
     1006            'post_status' => 'publish',
     1007        );
     1008        $one = $this->factory->post->create( $args );
     1009
     1010        $args = array(
     1011            'post_mime_type' => 'image/jpeg',
     1012            'post_type' => 'attachment',
     1013            'post_name' => 'image'
     1014        );
     1015        $attachment = $this->factory->attachment->create_object( 'image.jpg', $one, $args );
     1016
     1017        $args = array(
     1018            'post_type' => 'post-type-1',
     1019            'post_name' => 'image',
     1020            'post_status' => 'publish',
     1021            'post_parent' => $one
     1022        );
     1023        $two = $this->factory->post->create( $args );
     1024
     1025        $this->assertEquals( 'some-slug', get_post( $one )->post_name );
     1026        $this->assertEquals( 'image', get_post( $attachment )->post_name );
     1027        $this->assertEquals( 'image-2', get_post( $two )->post_name );
     1028
     1029        // 'image' can be a child of image-2
     1030        $this->assertEquals( 'image', wp_unique_post_slug( 'image', 0, 'publish', 'post-type-1', $two ) );
     1031
     1032        _unregister_post_type( 'post-type-1' );
     1033    }
     1034
     1035    /**
    9911036     * @ticket 21212
    9921037     */
Note: See TracChangeset for help on using the changeset viewer.