Make WordPress Core

Ticket #13459: 13459.3.diff

File 13459.3.diff, 6.2 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++;
     3667
    36573668                        } while ( $post_name_check );
    36583669                        $slug = $alt_post_name;
    36593670                }
  • tests/phpunit/tests/post.php

     
    12531253                $this->assertEquals(get_date_from_gmt($post['post_date_gmt']), $out->post_date);
    12541254                $this->assertEquals($post['post_date_gmt'], $out->post_date_gmt);
    12551255        }
     1256
     1257        function test_unique_slug_page_then_post() {
     1258                global $wp_rewrite;
     1259                $wp_rewrite->set_permalink_structure( '/%postname%/' );
     1260                $page_args = array(
     1261                        'post_type' => 'page',
     1262                        'post_name' => 'green',
     1263                        'post_status' => 'publish',
     1264                );
     1265                $page = $this->factory->post->create( $page_args );
     1266                $this->assertEquals( 'green', get_post( $page )->post_name );
     1267
     1268                $post_args = array(
     1269                        'post_type' => 'post',
     1270                        'post_name' => 'green',
     1271                        'post_status' => 'publish',
     1272                );
     1273                $post = $this->factory->post->create( $post_args );
     1274
     1275                $this->assertEquals( 'green-2', get_post( $post )->post_name );
     1276                $wp_rewrite->flush_rules();
     1277        }
     1278
     1279        function test_unique_slug_post_then_page() {
     1280                global $wp_rewrite;
     1281                $wp_rewrite->set_permalink_structure( '/%postname%/' );
     1282                $post_args = array(
     1283                        'post_type' => 'post',
     1284                        'post_name' => 'red'
     1285                );
     1286                $post = $this->factory->post->create( $post_args );
     1287                $this->assertEquals( 'red', get_post( $post )->post_name );
     1288
     1289                $page_args = array(
     1290                        'post_type' => 'page',
     1291                        'post_name' => 'red'
     1292                );
     1293                $page = $this->factory->post->create( $page_args );
     1294                $this->assertEquals( 'red-2', get_post( $page )->post_name );
     1295                $wp_rewrite->flush_rules();
     1296        }
     1297
     1298        function test_unique_slug_post_then_post() {
     1299                global $wp_rewrite;
     1300                $wp_rewrite->set_permalink_structure( '/%postname%/' );
     1301                $post_args = array(
     1302                        'post_type' => 'post',
     1303                        'post_name' => 'blue'
     1304                );
     1305                $post = $this->factory->post->create( $post_args );
     1306                $this->assertEquals( 'blue', get_post( $post )->post_name );
     1307
     1308                $post_args = array(
     1309                        'post_type' => 'post',
     1310                        'post_name' => 'blue'
     1311                );
     1312                $post = $this->factory->post->create( $post_args );
     1313                $this->assertEquals( 'blue-2', get_post( $post )->post_name );
     1314                $wp_rewrite->flush_rules();
     1315        }
     1316
     1317        function test_unique_slug_page_then_page() {
     1318                global $wp_rewrite;
     1319                $wp_rewrite->set_permalink_structure( '/%postname%/' );
     1320                $page_args = array(
     1321                        'post_type' => 'page',
     1322                        'post_name' => 'orange'
     1323                );
     1324                $page = $this->factory->post->create( $page_args );
     1325                $this->assertEquals( 'orange', get_post( $page )->post_name );
     1326
     1327                $page_args = array(
     1328                        'post_type' => 'page',
     1329                        'post_name' => 'orange'
     1330                );
     1331                $page = $this->factory->post->create( $page_args );
     1332                $this->assertEquals( 'orange-2', get_post( $page )->post_name );
     1333                $wp_rewrite->flush_rules();
     1334        }
    12561335}