Make WordPress Core

Ticket #13459: 13459.2.diff

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

     
    35833583                        $slug = $alt_post_name;
    35843584                }
    35853585        } elseif ( is_post_type_hierarchical( $post_type ) ) {
     3586                // Nav menu items don't need unique post slugs because they don't have rewrites.
    35863587                if ( 'nav_menu_item' == $post_type )
    35873588                        return $slug;
    35883589
    35893590                /*
    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.
     3591                 * Ensure the entire URL path is unique, respective of potentially conflicting
     3592                 * post type paths.
    35923593                 */
    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 ) );
    3595 
     3594                if( '/%postname%/' === $wp_rewrite->permalink_structure && $post_parent === 0 ) {
     3595                        // If posts share the same permalink structure with pages, also check posts for uniqueness.
     3596                        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'post', 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
     3597                        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
     3598                } else {
     3599                        $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";
     3600                        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
     3601                }
    35963602                /**
    35973603                 * Filter whether the post slug would make a bad hierarchical post slug.
    35983604                 *
     
    36133619                        $slug = $alt_post_name;
    36143620                }
    36153621        } 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 ) );
     3622                // Post slugs of a non-hierarchical post-type must be unique to one another.
     3623                if ( '/%postname%/' == $wp_rewrite->permalink_structure && 'post' === $post_type ) {
     3624                        // Avoid collisions between post and page slugs.
     3625                        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'page' ) AND ID != %d LIMIT 1";
     3626                        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
     3627                } else {
     3628                        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
     3629                        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
     3630                }
    36193631
    36203632                // Prevent new post slugs that could result in URLs that conflict with date archives.
    36213633                $post = get_post( $post_ID );
  • 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}