Ticket #18962: 18962.2.diff
File 18962.2.diff, 5.7 KB (added by , 10 years ago) |
---|
-
src/wp-includes/post.php
3691 3691 do { 3692 3692 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 3693 3693 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); 3694 3694 $suffix++; 3695 3695 } while ( $post_name_check ); 3696 3696 $slug = $alt_post_name; 3697 3697 } 3698 3698 } elseif ( is_post_type_hierarchical( $post_type ) ) { 3699 3699 if ( 'nav_menu_item' == $post_type ) 3700 3700 return $slug; 3701 3701 3702 3702 /* 3703 3703 * Page slugs must be unique within their own trees. Pages are in a separate 3704 3704 * namespace than posts so page slugs are allowed to overlap post slugs. 3705 3705 */ 3706 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %sAND ID != %d AND post_parent = %d LIMIT 1";3706 $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"; 3707 3707 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 3708 3708 3709 3709 /** 3710 3710 * Filter whether the post slug would make a bad hierarchical post slug. 3711 3711 * 3712 3712 * @since 3.1.0 3713 3713 * 3714 3714 * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. 3715 3715 * @param string $slug The post slug. 3716 3716 * @param string $post_type Post type. 3717 3717 * @param int $post_parent Post parent ID. 3718 3718 */ 3719 3719 if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { 3720 3720 $suffix = 2; 3721 3721 do { 3722 3722 $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, $post_parent ) );3723 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); 3724 3724 $suffix++; 3725 3725 } while ( $post_name_check ); 3726 3726 $slug = $alt_post_name; 3727 3727 } 3728 3728 } else { 3729 3729 // Post slugs must be unique across all posts. 3730 3730 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 3731 3731 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); 3732 3732 3733 3733 /** 3734 3734 * Filter whether the post slug would be bad as a flat slug. 3735 3735 * 3736 3736 * @since 3.1.0 3737 3737 * 3738 3738 * @param bool $bad_slug Whether the post slug would be bad as a flat slug. -
tests/phpunit/tests/post.php
1006 1006 $one = $this->factory->post->create( $args ); 1007 1007 $args['post_type'] = 'post-type-2'; 1008 1008 $two = $this->factory->post->create( $args ); 1009 1009 1010 1010 $this->assertEquals( 'some-slug', get_post( $one )->post_name ); 1011 1011 $this->assertEquals( 'some-slug', get_post( $two )->post_name ); 1012 1012 1013 1013 $this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-1', 0 ) ); 1014 1014 $this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-2', 0 ) ); 1015 1015 1016 1016 _unregister_post_type( 'post-type-1' ); 1017 1017 _unregister_post_type( 'post-type-2' ); 1018 1018 } 1019 1019 1020 1020 /** 1021 * @ticket 30339 1022 */ 1023 function test_wp_unique_post_slug_with_hierarchy() { 1024 register_post_type( 'post-type-1', array( 'hierarchical' => true ) ); 1025 1026 $args = array( 1027 'post_type' => 'post-type-1', 1028 'post_name' => 'some-slug', 1029 'post_status' => 'publish', 1030 ); 1031 $one = $this->factory->post->create( $args ); 1032 $args['post_name'] = 'some-slug-2'; 1033 $two = $this->factory->post->create( $args ); 1034 1035 $this->assertEquals( 'some-slug', get_post( $one )->post_name ); 1036 $this->assertEquals( 'some-slug-2', get_post( $two )->post_name ); 1037 1038 $this->assertEquals( 'some-slug-3', wp_unique_post_slug( 'some-slug', 0, 'publish', 'post-type-1', 0 ) ); 1039 1040 _unregister_post_type( 'post-type-1' ); 1041 } 1042 1043 /** 1044 * @ticket 18962 1045 */ 1046 function test_wp_unique_post_slug_with_hierarchy_and_attachments() { 1047 register_post_type( 'post-type-1', array( 'hierarchical' => true ) ); 1048 1049 $args = array( 1050 'post_type' => 'post-type-1', 1051 'post_name' => 'some-slug', 1052 'post_status' => 'publish', 1053 ); 1054 $one = $this->factory->post->create( $args ); 1055 1056 $args = array( 1057 'post_mime_type' => 'image/jpeg', 1058 'post_type' => 'attachment', 1059 'post_name' => 'image' 1060 ); 1061 $attachment = $this->factory->attachment->create_object( 'image.jpg', $one, $args ); 1062 1063 $args = array( 1064 'post_type' => 'post-type-1', 1065 'post_name' => 'image', 1066 'post_status' => 'publish', 1067 'post_parent' => $one 1068 ); 1069 $two = $this->factory->post->create( $args ); 1070 1071 $this->assertEquals( 'some-slug', get_post( $one )->post_name ); 1072 $this->assertEquals( 'image', get_post( $attachment )->post_name ); 1073 $this->assertEquals( 'image-2', get_post( $two )->post_name ); 1074 1075 // 'image' can be a child of image-2 1076 $this->assertEquals( 'image', wp_unique_post_slug( 'image', 0, 'publish', 'post-type-1', $two ) ); 1077 1078 _unregister_post_type( 'post-type-1' ); 1079 } 1080 1081 /** 1021 1082 * @ticket 21212 1022 1083 */ 1023 1084 function test_utf8mb3_post_saves_with_emoji() { 1024 1085 global $wpdb; 1025 1086 $_wpdb = new wpdb_exposed_methods_for_testing(); 1026 1087 1027 1088 if ( 'utf8' !== $_wpdb->get_col_charset( $wpdb->posts, 'post_title' ) ) { 1028 1089 $this->markTestSkipped( 'This test is only useful with the utf8 character set' ); 1029 1090 } 1030 1091 1031 1092 require_once( ABSPATH . '/wp-admin/includes/post.php' ); 1032 1093 1033 1094 $post_id = $this->factory->post->create(); 1034 1095 1035 1096 $data = array(