Ticket #13459: 13459.2.diff
| File 13459.2.diff, 5.6 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/post.php
3583 3583 $slug = $alt_post_name; 3584 3584 } 3585 3585 } elseif ( is_post_type_hierarchical( $post_type ) ) { 3586 // Nav menu items don't need unique post slugs because they don't have rewrites. 3586 3587 if ( 'nav_menu_item' == $post_type ) 3587 3588 return $slug; 3588 3589 3589 3590 /* 3590 * Page slugs must be unique within their own trees. Pages are in a separate3591 * 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. 3592 3593 */ 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 } 3596 3602 /** 3597 3603 * Filter whether the post slug would make a bad hierarchical post slug. 3598 3604 * … … 3613 3619 $slug = $alt_post_name; 3614 3620 } 3615 3621 } 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 } 3619 3631 3620 3632 // Prevent new post slugs that could result in URLs that conflict with date archives. 3621 3633 $post = get_post( $post_ID ); -
tests/phpunit/tests/post.php
1253 1253 $this->assertEquals(get_date_from_gmt($post['post_date_gmt']), $out->post_date); 1254 1254 $this->assertEquals($post['post_date_gmt'], $out->post_date_gmt); 1255 1255 } 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 } 1256 1335 }