Ticket #20419: 20419.4.diff
File 20419.4.diff, 18.2 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/ajax-actions.php
diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php index 63a6cda..b1e4b1b 100644
function wp_ajax_inline_save() { 1661 1661 // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published. 1662 1662 if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ) ) ) { 1663 1663 $post['post_status'] = 'publish'; 1664 $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] ); 1664 $data['post_name'] = wp_unique_post_slug( $data['post_name'], (object) array( 1665 'ID' => $post['ID'], 1666 'post_status' => $post['post_status'], 1667 'post_type' => $post['post_type'], 1668 'post_parent' => $post['post_parent'], 1669 ) ); 1665 1670 } 1666 1671 1667 1672 // Update the post. -
src/wp-admin/includes/post.php
diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php index 2f1f178..e7c2af1 100644
function get_sample_permalink($id, $title = null, $name = null) { 1226 1226 if ( !is_null($name) ) 1227 1227 $post->post_name = sanitize_title($name ? $name : $title, $post->ID); 1228 1228 1229 $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent); 1229 $post->post_name = wp_unique_post_slug( $post->post_name, (object) array( 1230 'ID' => $post->ID, 1231 'post_status' => $post->post_status, 1232 'post_type' => $post->post_type, 1233 'post_parent' => $post->post_parent, 1234 ) ); 1230 1235 1231 1236 $post->filter = 'sample'; 1232 1237 -
src/wp-includes/post.php
diff --git src/wp-includes/post.php src/wp-includes/post.php index 1920e84..93e973b 100644
function wp_insert_post( $postarr, $wp_error = false ) { 3288 3288 $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); 3289 3289 } 3290 3290 3291 $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); 3291 $post_name = wp_unique_post_slug( $post_name, (object) array( 3292 'ID' => $post_ID, 3293 'post_type' => $post_type, 3294 'post_status' => $post_status, 3295 'post_title' => $post_title, 3296 'post_name' => $post_name, 3297 'post_parent' => $post_parent, 3298 ) ); 3292 3299 3293 3300 // Don't unslash. 3294 3301 $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; … … function wp_insert_post( $postarr, $wp_error = false ) { 3370 3377 } 3371 3378 3372 3379 if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { 3373 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); 3380 // Mock a WP_Post object for wp_unique_post_slug(). 3381 $_post_data_for_slug = new WP_Post( (object) array( 3382 'ID' => $post_ID, 3383 'post_status' => $data['post_status'], 3384 'post_type' => $post_type, 3385 'post_parent' => $post_parent, 3386 ) ); 3387 $_slug = sanitize_title( $data['post_title'], $post_ID ); 3388 $data['post_name'] = wp_unique_post_slug( $_slug, $_post_data_for_slug ); 3389 3374 3390 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 3375 3391 clean_post_cache( $post_ID ); 3376 3392 } … … function check_and_publish_future_post( $post_id ) { 3680 3696 * Computes a unique slug for the post, when given the desired slug and some post details. 3681 3697 * 3682 3698 * @since 2.8.0 3699 * @since 4.6.0 Second parameter should now be a post object or ID. `$post_status`, `$post_type`, and `$post_parent` 3700 * parameters are deprecated. 3683 3701 * 3684 3702 * @global wpdb $wpdb WordPress database abstraction object. 3685 3703 * @global WP_Rewrite $wp_rewrite 3686 3704 * 3687 * @param string $slug The desired slug (post_name). 3688 * @param int $post_ID Post ID. 3689 * @param string $post_status No uniqueness checks are made if the post is still draft or pending. 3690 * @param string $post_type Post type. 3691 * @param int $post_parent Post parent ID. 3705 * @param string $slug The desired slug (post_name). 3706 * @param obj $post Post object or ID. 3692 3707 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 3693 3708 */ 3694 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { 3695 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) 3709 function wp_unique_post_slug( $slug, $post, $deprecated_post_status = null, $deprecated_post_type = null, $deprecated_post_parent = null ) { 3710 if ( $deprecated_post_status || $deprecated_post_type || $deprecated_post_parent ) { 3711 _deprecated_argument( __FUNCTION__, '4.6', __( 'wp_unique_post_slug() accepts two parameters: the desired post slug, and a WP_Post object or ID.' ) ); 3712 } 3713 3714 $_post = get_post( $post ); 3715 if ( ! $_post ) { 3716 /* 3717 * This is a brand new post, so we mock it. The value of post_status, 3718 * post_type, and post_parent don't matter. They're only present to prevent 3719 * notices when checking for deprecated values. 3720 */ 3721 $_post = new WP_Post( (object) array( 3722 'post_status' => 'publish', 3723 'post_type' => 'post', 3724 'post_parent' => 0, 3725 ) ); 3726 } 3727 3728 $post = $_post; 3729 unset( $_post ); 3730 3731 // Support for deprecated parameters. 3732 if ( $deprecated_post_status ) { 3733 $post->post_status = $deprecated_post_status; 3734 } 3735 3736 if ( $deprecated_post_type ) { 3737 $post->post_type = $deprecated_post_type; 3738 } 3739 3740 if ( $deprecated_post_parent ) { 3741 $post->post_parent = $deprecated_post_parent; 3742 } 3743 3744 if ( in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post->post_status && 'revision' == $post->post_type ) ) { 3696 3745 return $slug; 3746 } 3697 3747 3698 3748 global $wpdb, $wp_rewrite; 3699 3749 … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3703 3753 if ( ! is_array( $feeds ) ) 3704 3754 $feeds = array(); 3705 3755 3706 if ( 'attachment' == $post_type ) {3756 if ( 'attachment' === $post->post_type ) { 3707 3757 // Attachment slugs must be unique across all types. 3708 3758 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; 3709 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post _ID ) );3759 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->ID ) ); 3710 3760 3711 3761 /** 3712 3762 * Filter whether the post slug would make a bad attachment slug. … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3720 3770 $suffix = 2; 3721 3771 do { 3722 3772 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 3723 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post _ID ) );3773 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->ID ) ); 3724 3774 $suffix++; 3725 3775 } while ( $post_name_check ); 3726 3776 $slug = $alt_post_name; 3727 3777 } 3728 } elseif ( is_post_type_hierarchical( $post _type ) ) {3729 if ( 'nav_menu_item' == $post_type )3778 } elseif ( is_post_type_hierarchical( $post->post_type ) ) { 3779 if ( 'nav_menu_item' === $post->post_type ) { 3730 3780 return $slug; 3781 } 3731 3782 3732 3783 /* 3733 3784 * Page slugs must be unique within their own trees. Pages are in a separate 3734 3785 * namespace than posts so page slugs are allowed to overlap post slugs. 3735 3786 */ 3736 3787 $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"; 3737 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post _type, $post_ID, $post_parent ) );3788 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->post_type, $post->ID, $post->post_parent ) ); 3738 3789 3739 3790 /** 3740 3791 * Filter whether the post slug would make a bad hierarchical post slug. … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3746 3797 * @param string $post_type Post type. 3747 3798 * @param int $post_parent Post parent ID. 3748 3799 */ 3749 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post _type, $post_parent ) ) {3800 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post->post_type, $post->post_parent ) ) { 3750 3801 $suffix = 2; 3751 3802 do { 3752 3803 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 3753 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post _type, $post_ID, $post_parent ) );3804 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->post_type, $post->ID, $post->post_parent ) ); 3754 3805 $suffix++; 3755 3806 } while ( $post_name_check ); 3756 3807 $slug = $alt_post_name; … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3758 3809 } else { 3759 3810 // Post slugs must be unique across all posts. 3760 3811 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 3761 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post _type, $post_ID ) );3812 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->post_type, $post->ID ) ); 3762 3813 3763 3814 // Prevent new post slugs that could result in URLs that conflict with date archives. 3764 $ post = get_post( $post_ID );3815 $_post = get_post( $post->ID ); 3765 3816 $conflicts_with_date_archive = false; 3766 if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { 3817 $slug_num = intval( $slug ); 3818 if ( 'post' === $post->post_type && ( ! $_post || $_post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num ) { 3767 3819 $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); 3768 3820 $postname_index = array_search( '%postname%', $permastructs ); 3769 3770 3821 /* 3771 3822 * Potential date clashes are as follows: 3772 3823 * … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3791 3842 * @param string $slug The post slug. 3792 3843 * @param string $post_type Post type. 3793 3844 */ 3794 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post _type ) ) {3845 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post->post_type ) ) { 3795 3846 $suffix = 2; 3796 3847 do { 3797 3848 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 3798 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post _type, $post_ID ) );3849 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->post_type, $post->ID ) ); 3799 3850 $suffix++; 3800 3851 } while ( $post_name_check ); 3801 3852 $slug = $alt_post_name; … … function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 3814 3865 * @param int $post_parent Post parent ID 3815 3866 * @param string $original_slug The original post slug. 3816 3867 */ 3817 return apply_filters( 'wp_unique_post_slug', $slug, $post _ID, $post_status, $post_type, $post_parent, $original_slug );3868 return apply_filters( 'wp_unique_post_slug', $slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent, $original_slug ); 3818 3869 } 3819 3870 3820 3871 /** -
tests/phpunit/tests/post/wpUniquePostSlug.php
diff --git tests/phpunit/tests/post/wpUniquePostSlug.php tests/phpunit/tests/post/wpUniquePostSlug.php index 62f1ad6..88dfdcd 100644
class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 41 41 42 42 /** 43 43 * @ticket 18962 44 * @expectedDeprecated wp_unique_post_slug 44 45 */ 45 46 public function test_with_multiple_hierarchies() { 46 47 register_post_type( 'post-type-1', array( 'hierarchical' => true ) ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 67 68 68 69 /** 69 70 * @ticket 30339 71 * @expectedDeprecated wp_unique_post_slug 70 72 */ 71 73 public function test_with_hierarchy() { 72 74 register_post_type( 'post-type-1', array( 'hierarchical' => true ) ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 90 92 91 93 /** 92 94 * @ticket 18962 95 * @expectedDeprecated wp_unique_post_slug 93 96 */ 94 97 function test_wp_unique_post_slug_with_hierarchy_and_attachments() { 95 98 register_post_type( 'post-type-1', array( 'hierarchical' => true ) ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 104 107 $args = array( 105 108 'post_mime_type' => 'image/jpeg', 106 109 'post_type' => 'attachment', 107 'post_name' => 'image' 110 'post_name' => 'image', 111 'post_status' => 'inherit', 108 112 ); 109 113 $attachment = self::factory()->attachment->create_object( 'image.jpg', $one, $args ); 110 114 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 137 141 138 142 $p2 = self::factory()->post->create( array( 139 143 'post_type' => 'post', 144 'post_status' => $status, 145 'post_type' => 'post', 146 'post_parent' => 0, 140 147 ) ); 141 148 142 $actual = wp_unique_post_slug( 'foo', $p2 , $status, 'post', 0);149 $actual = wp_unique_post_slug( 'foo', $p2 ); 143 150 144 151 $this->assertSame( 'foo', $actual ); 145 152 } … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 160 167 161 168 $p2 = self::factory()->post->create( array( 162 169 'post_type' => 'post', 170 'post_status' => 'inherit', 171 'post_type' => 'revision', 172 'post_parent' => 0, 163 173 ) ); 164 174 165 $actual = wp_unique_post_slug( 'foo', $p2 , 'inherit', 'revision', 0);175 $actual = wp_unique_post_slug( 'foo', $p2 ); 166 176 167 177 $this->assertSame( 'foo', $actual ); 168 178 } … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 176 186 $p = self::factory()->post->create( array( 177 187 'post_type' => 'post', 178 188 'post_name' => 'foo', 189 'post_status' => 'publish', 190 'post_type' => 'post', 191 'post_parent' => 0, 179 192 ) ); 180 193 181 $found = wp_unique_post_slug( '2015', $p , 'publish', 'post', 0);194 $found = wp_unique_post_slug( '2015', $p ); 182 195 $this->assertEquals( '2015-2', $found ); 183 196 } 184 197 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 192 205 'post_type' => 'post', 193 206 'post_name' => 'foo', 194 207 'post_status' => 'publish', 208 'post_type' => 'post', 209 'post_parent' => 0, 195 210 ) ); 196 211 197 $found = wp_unique_post_slug( '2015', $p , 'publish', 'post', 0);212 $found = wp_unique_post_slug( '2015', $p ); 198 213 $this->assertEquals( '2015-2', $found ); 199 214 } 200 215 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 207 222 $p = self::factory()->post->create( array( 208 223 'post_type' => 'post', 209 224 'post_name' => 'foo', 225 'post_status' => 'publish', 226 'post_type' => 'post', 227 'post_parent' => 0, 210 228 ) ); 211 229 212 $found = wp_unique_post_slug( '2015', $p , 'publish', 'post', 0);230 $found = wp_unique_post_slug( '2015', $p ); 213 231 $this->assertEquals( '2015', $found ); 214 232 } 215 233 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 222 240 $p = self::factory()->post->create( array( 223 241 'post_type' => 'post', 224 242 'post_name' => 'foo', 243 'post_status' => 'publish', 244 'post_type' => 'post', 245 'post_parent' => 0, 225 246 ) ); 226 247 227 $found = wp_unique_post_slug( '11', $p , 'publish', 'post', 0);248 $found = wp_unique_post_slug( '11', $p ); 228 249 $this->assertEquals( '11-2', $found ); 229 250 } 230 251 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 237 258 $p = self::factory()->post->create( array( 238 259 'post_type' => 'post', 239 260 'post_name' => 'foo', 261 'post_status' => 'publish', 262 'post_type' => 'post', 263 'post_parent' => 0, 240 264 ) ); 241 265 242 $found = wp_unique_post_slug( '11', $p , 'publish', 'post', 0);266 $found = wp_unique_post_slug( '11', $p ); 243 267 $this->assertEquals( '11', $found ); 244 268 } 245 269 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 252 276 $p = self::factory()->post->create( array( 253 277 'post_type' => 'post', 254 278 'post_name' => 'foo', 279 'post_status' => 'publish', 280 'post_type' => 'post', 281 'post_parent' => 0, 255 282 ) ); 256 283 257 $found = wp_unique_post_slug( '13', $p , 'publish', 'post', 0);284 $found = wp_unique_post_slug( '13', $p ); 258 285 $this->assertEquals( '13', $found ); 259 286 } 260 287 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 267 294 $p = self::factory()->post->create( array( 268 295 'post_type' => 'post', 269 296 'post_name' => 'foo', 297 'post_status' => 'publish', 298 'post_type' => 'post', 299 'post_parent' => 0, 270 300 ) ); 271 301 272 $found = wp_unique_post_slug( '30', $p , 'publish', 'post', 0);302 $found = wp_unique_post_slug( '30', $p ); 273 303 $this->assertEquals( '30-2', $found ); 274 304 } 275 305 … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 282 312 $p = self::factory()->post->create( array( 283 313 'post_type' => 'post', 284 314 'post_name' => 'foo', 315 'post_status' => 'publish', 316 'post_type' => 'post', 317 'post_parent' => 0, 285 318 ) ); 286 319 287 $found = wp_unique_post_slug( '30', $p , 'publish', 'post', 0);320 $found = wp_unique_post_slug( '30', $p ); 288 321 $this->assertEquals( '30', $found ); 289 322 } 290 323 291 324 /** 292 325 * @ticket 5305 326 * @expectedDeprecated wp_unique_post_slug 293 327 */ 294 328 public function test_daylike_slugs_should_not_be_suffixed_for_invalid_day_numbers() { 295 329 $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 305 339 306 340 /** 307 341 * @ticket 34971 342 * @expectedDeprecated wp_unique_post_slug 308 343 */ 309 344 public function test_embed_slug_should_be_suffixed_for_posts() { 310 345 $this->set_permalink_structure( '/%postname%/' ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 320 355 321 356 /** 322 357 * @ticket 34971 358 * @expectedDeprecated wp_unique_post_slug 323 359 */ 324 360 public function test_embed_slug_should_be_suffixed_for_pages() { 325 361 $this->set_permalink_structure( '/%postname%/' ); … … class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 335 371 336 372 /** 337 373 * @ticket 34971 374 * @expectedDeprecated wp_unique_post_slug 338 375 */ 339 376 public function test_embed_slug_should_be_suffixed_for_attachments() { 340 377 $this->set_permalink_structure( '/%postname%/' );