Ticket #13459: 13459-w-test.diff
| File 13459-w-test.diff, 5.8 KB (added by , 11 years ago) |
|---|
-
tests/phpunit/tests/post.php
1068 1068 $this->assertEquals( $post->$field, $value ); 1069 1069 } 1070 1070 } 1071 1072 function test_unique_slug_page_then_post() { 1073 global $wp_rewrite; 1074 $wp_rewrite->set_permalink_structure( '/%postname%/' ); 1075 $page_args = array( 1076 'post_type' => 'page', 1077 'post_name' => 'green', 1078 'post_status' => 'publish', 1079 ); 1080 $page = $this->factory->post->create( $page_args ); 1081 $this->assertEquals( 'green', get_post( $page )->post_name ); 1082 1083 $post_args = array( 1084 'post_type' => 'post', 1085 'post_name' => 'green', 1086 'post_status' => 'publish', 1087 ); 1088 $post = $this->factory->post->create( $post_args ); 1089 1090 $this->assertEquals( 'green-2', get_post( $post )->post_name ); 1091 $wp_rewrite->flush_rules(); 1092 } 1093 1094 function test_unique_slug_post_then_page() { 1095 global $wp_rewrite; 1096 $wp_rewrite->set_permalink_structure( '/%postname%/' ); 1097 $post_args = array( 1098 'post_type' => 'post', 1099 'post_name' => 'red' 1100 ); 1101 $post = $this->factory->post->create( $post_args ); 1102 $this->assertEquals( 'red', get_post( $post )->post_name ); 1103 1104 $page_args = array( 1105 'post_type' => 'page', 1106 'post_name' => 'red' 1107 ); 1108 $page = $this->factory->post->create( $page_args ); 1109 $this->assertEquals( 'red-2', get_post( $page )->post_name ); 1110 $wp_rewrite->flush_rules(); 1111 } 1112 1113 function test_unique_slug_post_then_post() { 1114 global $wp_rewrite; 1115 $wp_rewrite->set_permalink_structure( '/%postname%/' ); 1116 $post_args = array( 1117 'post_type' => 'post', 1118 'post_name' => 'blue' 1119 ); 1120 $post = $this->factory->post->create( $post_args ); 1121 $this->assertEquals( 'blue', get_post( $post )->post_name ); 1122 1123 $post_args = array( 1124 'post_type' => 'post', 1125 'post_name' => 'blue' 1126 ); 1127 $post = $this->factory->post->create( $post_args ); 1128 $this->assertEquals( 'blue-2', get_post( $post )->post_name ); 1129 $wp_rewrite->flush_rules(); 1130 } 1131 1132 function test_unique_slug_page_then_page() { 1133 global $wp_rewrite; 1134 $wp_rewrite->set_permalink_structure( '/%postname%/' ); 1135 $page_args = array( 1136 'post_type' => 'page', 1137 'post_name' => 'orange' 1138 ); 1139 $page = $this->factory->post->create( $page_args ); 1140 $this->assertEquals( 'orange', get_post( $page )->post_name ); 1141 1142 $page_args = array( 1143 'post_type' => 'page', 1144 'post_name' => 'orange' 1145 ); 1146 $page = $this->factory->post->create( $page_args ); 1147 $this->assertEquals( 'orange-2', get_post( $page )->post_name ); 1148 $wp_rewrite->flush_rules(); 1149 } 1071 1150 } -
src/wp-includes/post.php
3678 3678 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 3679 3679 */ 3680 3680 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { 3681 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) 3681 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) { 3682 3682 return $slug; 3683 } 3683 3684 3684 3685 global $wpdb, $wp_rewrite; 3685 3686 3686 3687 $original_slug = $slug; 3687 3688 3688 3689 $feeds = $wp_rewrite->feeds; 3689 if ( ! is_array( $feeds ) ) 3690 if ( ! is_array( $feeds ) ) { 3690 3691 $feeds = array(); 3692 } 3691 3693 3692 3694 if ( 'attachment' == $post_type ) { 3693 3695 // Attachment slugs must be unique across all types. … … 3712 3714 $slug = $alt_post_name; 3713 3715 } 3714 3716 } elseif ( is_post_type_hierarchical( $post_type ) ) { 3715 if ( 'nav_menu_item' == $post_type ) 3717 if ( 'nav_menu_item' == $post_type ) { 3716 3718 return $slug; 3719 } 3717 3720 3718 /* 3719 * Page slugs must be unique within their own trees. Pages are in a separate 3720 * namespace than posts so page slugs are allowed to overlap post slugs. 3721 */ 3722 $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"; 3723 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 3721 if( '/%postname%/' == $wp_rewrite->permalink_structure && $post_parent === 0 ) { 3722 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d AND post_parent = %d LIMIT 1"; 3723 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) ); 3724 } else { 3725 $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"; 3726 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 3727 } 3724 3728 3725 3729 /** 3726 3730 * Filter whether the post slug would make a bad hierarchical post slug. … … 3746 3750 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 3747 3751 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); 3748 3752 3753 if ( ! $post_name_check && '/%postname%/' == $wp_rewrite->permalink_structure ) { 3754 $post_name_check = get_page_by_path( $slug ); 3755 } 3756 3749 3757 /** 3750 3758 * Filter whether the post slug would be bad as a flat slug. 3751 3759 * … … 3760 3768 do { 3761 3769 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 3762 3770 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); 3771 3772 if ( ! $post_name_check && '/%postname%/' == $wp_rewrite->permalink_structure ) { 3773 $post_name_check = get_page_by_path( $alt_post_name ); 3774 } 3775 3763 3776 $suffix++; 3764 3777 } while ( $post_name_check ); 3765 3778 $slug = $alt_post_name;