Make WordPress Core

Ticket #13459: 13459.4.diff

File 13459.4.diff, 7.8 KB (added by ericlewis, 10 years ago)
  • src/wp-includes/post.php

     
    35863586                if ( 'nav_menu_item' == $post_type )
    35873587                        return $slug;
    35883588
    3589                 /*
    3590                  * Page slugs must be unique within their own trees. Pages are in a separate
    3591                  * namespace than posts so page slugs are allowed to overlap post slugs.
    3592                  */
    3593                 $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";
    3594                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
     3589                // Ensure the entire URL path is unique, respective of potential collisions.
     3590                $post_types_with_possible_collisions = array( $post_type, 'attachment' );
     3591                // Posts and pages can share the same permalink namespace.
     3592                if ( $post_type === 'page' && '/%postname%/' === $wp_rewrite->permalink_structure && $post_parent === 0 ) {
     3593                        $post_types_with_possible_collisions[] = 'post';
     3594                }
     3595                $post_types_with_possible_collisions = implode( "', '", $post_types_with_possible_collisions );
     3596                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '$post_types_with_possible_collisions' ) AND ID != %d AND post_parent = %d LIMIT 1";
     3597                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
    35953598
    35963599                /**
    35973600                 * Filter whether the post slug would make a bad hierarchical post slug.
     
    36073610                        $suffix = 2;
    36083611                        do {
    36093612                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3610                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
     3613                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );
    36113614                                $suffix++;
    36123615                        } while ( $post_name_check );
    36133616                        $slug = $alt_post_name;
    36143617                }
    36153618        } else {
    3616                 // Post slugs must be unique across all posts.
    3617                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
    3618                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
     3619                // Ensure the entire URL path is unique, respective of potential collisions.
     3620                $post_types_with_possible_collisions = array( $post_type );
     3621                // Posts and pages can share the same permalink namespace.
     3622                if ( '/%postname%/' === $wp_rewrite->permalink_structure && 'post' === $post_type ) {
     3623                        array_push( $post_types_with_possible_collisions, 'page', 'attachment' );
     3624                }
     3625                $post_types_with_possible_collisions = implode( "', '", $post_types_with_possible_collisions );
     3626                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '$post_types_with_possible_collisions' ) AND ID != %d LIMIT 1";
    36193627
     3628                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
     3629
    36203630                // Prevent new post slugs that could result in URLs that conflict with date archives.
    36213631                $post = get_post( $post_ID );
    36223632                $conflicts_with_date_archive = false;
     
    36523662                        $suffix = 2;
    36533663                        do {
    36543664                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3655                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
     3665                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
    36563666                                $suffix++;
    36573667                        } while ( $post_name_check );
    36583668                        $slug = $alt_post_name;
  • tests/phpunit/tests/post/wpUniquePostSlug.php

     
    302302                $found = wp_unique_post_slug( '32', $p, 'publish', 'post', 0 );
    303303                $this->assertEquals( '32', $found );
    304304        }
    305 }
     305
     306        /**
     307         * @ticket 13459
     308         */
     309        public function test_post_slugs_should_not_collide_with_page_slugs_with_the_same_permalink_structure() {
     310                $this->set_permalink_structure( '/%postname%/' );
     311                $page_args = array(
     312                        'post_type' => 'page',
     313                        'post_name' => 'green',
     314                        'post_status' => 'publish',
     315                );
     316                $page = $this->factory->post->create( $page_args );
     317                $this->assertEquals( 'green', get_post( $page )->post_name );
     318
     319                $post_args = array(
     320                        'post_type' => 'post',
     321                        'post_name' => 'green',
     322                        'post_status' => 'publish',
     323                );
     324                $post = $this->factory->post->create( $post_args );
     325
     326                $this->assertEquals( 'green-2', get_post( $post )->post_name );
     327        }
     328
     329        /**
     330         * @ticket 13459
     331         */
     332        public function test_page_slugs_should_not_collide_with_post_slugs_with_the_same_permalink_structure() {
     333                $this->set_permalink_structure( '/%postname%/' );
     334                $post_args = array(
     335                        'post_type' => 'post',
     336                        'post_name' => 'red'
     337                );
     338                $post = $this->factory->post->create( $post_args );
     339                $this->assertEquals( 'red', get_post( $post )->post_name );
     340
     341                $page_args = array(
     342                        'post_type' => 'page',
     343                        'post_name' => 'red'
     344                );
     345                $page = $this->factory->post->create( $page_args );
     346                $this->assertEquals( 'red-2', get_post( $page )->post_name );
     347        }
     348
     349        /**
     350         * @ticket 13459
     351         */
     352        public function test_post_slugs_should_not_conflict_with_post_slugs_with_postname_permalink_structure() {
     353                $this->set_permalink_structure( '/%postname%/' );
     354                $post_args = array(
     355                        'post_type' => 'post',
     356                        'post_name' => 'blue'
     357                );
     358                $post = $this->factory->post->create( $post_args );
     359                $this->assertEquals( 'blue', get_post( $post )->post_name );
     360
     361                $post_args = array(
     362                        'post_type' => 'post',
     363                        'post_name' => 'blue'
     364                );
     365                $post = $this->factory->post->create( $post_args );
     366                $this->assertEquals( 'blue-2', get_post( $post )->post_name );
     367        }
     368
     369        /**
     370         * @ticket 13459
     371         */
     372        public function test_page_slugs_should_not_conflict_with_page_slugs_with_postname_permalink_structure() {
     373                $this->set_permalink_structure( '/%postname%/' );
     374                $page_args = array(
     375                        'post_type' => 'page',
     376                        'post_name' => 'orange'
     377                );
     378                $page = $this->factory->post->create( $page_args );
     379                $this->assertEquals( 'orange', get_post( $page )->post_name );
     380
     381                $page_args = array(
     382                        'post_type' => 'page',
     383                        'post_name' => 'orange'
     384                );
     385                $page = $this->factory->post->create( $page_args );
     386                $this->assertEquals( 'orange-2', get_post( $page )->post_name );
     387        }
     388
     389        /**
     390         * @ticket 13459
     391         */
     392        public function test_post_slugs_should_not_collide_with_attachments_when_permalink_structure_is_postname() {
     393                $this->set_permalink_structure( '/%postname%/' );
     394                $args = array(
     395                        'post_type' => 'post',
     396                        'post_name' => 'some-slug',
     397                        'post_status' => 'publish',
     398                );
     399                $post = self::factory()->post->create( $args );
     400
     401                $args = array(
     402                        'post_mime_type' => 'image/jpeg',
     403                        'post_type' => 'attachment',
     404                        'post_name' => 'some-slug'
     405                );
     406                $attachment = self::factory()->attachment->create_object( 'image.jpg', 0, $args );
     407
     408                $this->assertEquals( 'some-slug', get_post( $post )->post_name );
     409                $this->assertEquals( 'some-slug-2', get_post( $attachment )->post_name );
     410        }
     411
     412        /**
     413         * @ticket 13459
     414         */
     415        public function test_post_slugs_should_collide_with_attachments_when_their_permalink_structures_differ() {
     416                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     417
     418                $args = array(
     419                        'post_mime_type' => 'image/jpeg',
     420                        'post_type' => 'attachment',
     421                        'post_name' => 'some-slug'
     422                );
     423                $attachment = self::factory()->attachment->create_object( 'image.jpg', 0, $args );
     424
     425                $args = array(
     426                        'post_type' => 'post',
     427                        'post_name' => 'some-slug',
     428                        'post_status' => 'publish',
     429                );
     430                $post = self::factory()->post->create( $args );
     431
     432                $this->assertEquals( 'some-slug', get_post( $post )->post_name );
     433                $this->assertEquals( 'some-slug', get_post( $attachment )->post_name );
     434        }
     435}
     436 No newline at end of file