Changeset 63909
- Timestamp:
- 09/23/2026 11:56:00 PM (7 weeks ago)
- Location:
- trunk
- Files:
-
- 1 added
- 9 edited
-
src/wp-includes/deprecated.php (modified) (1 diff)
-
src/wp-includes/formatting.php (modified) (1 diff)
-
src/wp-includes/post.php (modified) (5 diffs)
-
src/wp-includes/taxonomy.php (modified) (3 diffs)
-
src/wp-includes/theme-templates.php (modified) (1 diff)
-
tests/phpunit/tests/formatting/wpTruncateSlug.php (added)
-
tests/phpunit/tests/post/truncatePostSlug.php (modified) (1 diff)
-
tests/phpunit/tests/term/wpInsertTerm.php (modified) (1 diff)
-
tests/phpunit/tests/term/wpUniqueTermSlug.php (modified) (1 diff)
-
tests/phpunit/tests/term/wpUpdateTerm.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/deprecated.php
r63320 r63909 6533 6533 return $attributes_string; 6534 6534 } 6535 6536 /** 6537 * Truncates a post slug. 6538 * 6539 * @since 3.6.0 6540 * @deprecated 7.2.0 Use wp_truncate_slug() instead. 6541 * @see wp_truncate_slug() 6542 * 6543 * @param string $slug The slug to truncate. 6544 * @param int $length Optional. Max length of the slug. Default 200 (characters). 6545 * @return string The truncated slug. 6546 */ 6547 function _truncate_post_slug( $slug, $length = 200 ) { 6548 _deprecated_function( __FUNCTION__, '7.2.0', 'wp_truncate_slug()' ); 6549 6550 return wp_truncate_slug( $slug, $length ); 6551 } -
trunk/src/wp-includes/formatting.php
r63665 r63909 2402 2402 2403 2403 /** 2404 * Truncates a slug to a given length. 2405 * 2406 * Non-ASCII slugs are stored percent-encoded, so the slug is truncated on a 2407 * character boundary to avoid cutting a percent-encoded sequence in half. 2408 * 2409 * @since 7.2.0 2410 * @access private 2411 * 2412 * @see utf8_uri_encode() 2413 * 2414 * @param string $slug The slug to truncate. 2415 * @param int $length Optional. Max length of the slug. Default 200 (characters). 2416 * @return string The truncated slug. 2417 */ 2418 function wp_truncate_slug( $slug, $length = 200 ) { 2419 if ( strlen( $slug ) > $length ) { 2420 $decoded_slug = urldecode( $slug ); 2421 if ( $decoded_slug === $slug ) { 2422 $slug = substr( $slug, 0, $length ); 2423 } else { 2424 $slug = utf8_uri_encode( $decoded_slug, $length, true ); 2425 } 2426 } 2427 2428 return rtrim( $slug, '-' ); 2429 } 2430 2431 /** 2404 2432 * Ensures a string is a valid SQL 'order by' clause. 2405 2433 * -
trunk/src/wp-includes/post.php
r63758 r63909 5722 5722 $suffix = 2; 5723 5723 do { 5724 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";5724 $alt_post_name = wp_truncate_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5725 5725 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) ); 5726 5726 ++$suffix; … … 5759 5759 $suffix = 2; 5760 5760 do { 5761 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";5761 $alt_post_name = wp_truncate_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5762 5762 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id, $post_parent ) ); 5763 5763 ++$suffix; … … 5815 5815 $suffix = 2; 5816 5816 do { 5817 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";5817 $alt_post_name = wp_truncate_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5818 5818 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id ) ); 5819 5819 ++$suffix; … … 5836 5836 */ 5837 5837 return apply_filters( 'wp_unique_post_slug', $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ); 5838 }5839 5840 /**5841 * Truncates a post slug.5842 *5843 * @since 3.6.05844 * @access private5845 *5846 * @see utf8_uri_encode()5847 *5848 * @param string $slug The slug to truncate.5849 * @param int $length Optional. Max length of the slug. Default 200 (characters).5850 * @return string The truncated slug.5851 */5852 function _truncate_post_slug( $slug, $length = 200 ) {5853 if ( strlen( $slug ) > $length ) {5854 $decoded_slug = urldecode( $slug );5855 if ( $decoded_slug === $slug ) {5856 $slug = substr( $slug, 0, $length );5857 } else {5858 $slug = utf8_uri_encode( $decoded_slug, $length, true );5859 }5860 }5861 5862 return rtrim( $slug, '-' );5863 5838 } 5864 5839 … … 8727 8702 } 8728 8703 add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); 8729 $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed';8704 $post_name = wp_truncate_slug( $post->post_name, 191 ) . '__trashed'; 8730 8705 $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); 8731 8706 clean_post_cache( $post->ID ); -
trunk/src/wp-includes/taxonomy.php
r63758 r63909 3214 3214 * until it finds a number that is truly unique. 3215 3215 * 3216 * Appending a parent slug or a number can push the result past the 200 character 3217 * limit of the `slug` column in the terms table, so the slug is truncated to make 3218 * room for whatever is appended to it. 3219 * 3216 3220 * The only purpose for `$term` is for appending a parent, if one exists. 3217 3221 * 3218 3222 * @since 2.3.0 3223 * @since 7.2.0 The returned slug is truncated to 200 characters when a parent slug 3224 * or a numeric suffix is appended to it. 3219 3225 * 3220 3226 * @global wpdb $wpdb WordPress database abstraction object. … … 3272 3278 if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) { 3273 3279 if ( $parent_suffix ) { 3274 $slug .= $parent_suffix;3280 $slug = wp_truncate_slug( $slug . $parent_suffix, 200 ); 3275 3281 } 3276 3282 … … 3284 3290 $num = 2; 3285 3291 do { 3286 $alt_slug = $slug . "-$num"; 3292 // Reserve room for the suffix so the result still fits the 200 character column. 3293 $numeric_suffix = "-$num"; 3294 $alt_slug = wp_truncate_slug( $slug, 200 - strlen( $numeric_suffix ) ) . $numeric_suffix; 3287 3295 ++$num; 3288 3296 $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); -
trunk/src/wp-includes/theme-templates.php
r61469 r63909 88 88 do { 89 89 $query_args = $check_query_args; 90 $alt_post_name = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";90 $alt_post_name = wp_truncate_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 91 91 $query_args['post_name__in'] = array( $alt_post_name ); 92 92 $query = new WP_Query( $query_args ); -
trunk/tests/phpunit/tests/post/truncatePostSlug.php
r62660 r63909 12 12 * 13 13 * @ticket 56868 14 * @ticket 46010 14 15 * 15 16 * @dataProvider data_truncate_post_slug_should_truncate 17 * 18 * @expectedDeprecated _truncate_post_slug 16 19 * 17 20 * @param string $slug The slug to truncate. -
trunk/tests/phpunit/tests/term/wpInsertTerm.php
r63880 r63909 908 908 } 909 909 910 /** 911 * Non-ASCII slugs are stored percent-encoded, so a short term name can still 912 * overflow the 200 character `slug` column once a parent slug is appended. 913 * 914 * @ticket 46010 915 */ 916 public function test_child_term_with_long_encoded_slug_should_be_inserted() { 917 $taxonomy = 'wptests_tax'; 918 register_taxonomy( $taxonomy, 'post', array( 'hierarchical' => true ) ); 919 920 // This name percent-encodes to a 116 character slug. 921 $name = 'Категория на продукта'; 922 923 $parent = wp_insert_term( $name, $taxonomy ); 924 $this->assertNotWPError( $parent ); 925 926 $child = wp_insert_term( $name, $taxonomy, array( 'parent' => $parent['term_id'] ) ); 927 928 $this->assertNotWPError( $child, 'The child term could not be inserted.' ); 929 $this->assertLessThanOrEqual( 200, strlen( get_term( $child['term_id'] )->slug ) ); 930 } 931 932 /** 933 * @ticket 46010 934 */ 935 public function test_terms_with_long_colliding_slugs_should_remain_unique() { 936 $taxonomy = 'wptests_tax'; 937 register_taxonomy( $taxonomy, 'post' ); 938 939 // 22 characters percent-encode to exactly 198 bytes, so every name below shares a slug. 940 $prefix = str_repeat( 'あ', 22 ); 941 942 $slugs = array(); 943 for ( $i = 1; $i <= 12; $i++ ) { 944 $term = wp_insert_term( $prefix . ' ' . str_repeat( 'か', $i ), $taxonomy ); 945 946 $this->assertNotWPError( $term, "Term $i could not be inserted." ); 947 948 $slug = get_term( $term['term_id'] )->slug; 949 $this->assertLessThanOrEqual( 200, strlen( $slug ), "Term $i has an overlong slug." ); 950 951 $slugs[] = $slug; 952 } 953 954 $this->assertSame( $slugs, array_unique( $slugs ), 'Duplicate slugs were created.' ); 955 } 956 910 957 /** Helpers */ 911 958 -
trunk/tests/phpunit/tests/term/wpUniqueTermSlug.php
r51568 r63909 173 173 $this->assertSame( 'dog-animal-2', $slug ); 174 174 } 175 176 /** 177 * The `slug` column in the terms table holds 200 characters. 178 * 179 * @ticket 46010 180 */ 181 public function test_parent_suffixed_slug_should_be_truncated_to_the_column_length() { 182 // This name percent-encodes to a 116 character slug, so appending it to itself overflows. 183 $name = 'Категория на продукта'; 184 $slug = sanitize_title( $name ); 185 186 $parent = self::factory()->term->create( 187 array( 188 'taxonomy' => 'wptests_tax2', 189 'name' => $name, 190 'slug' => $slug, 191 ) 192 ); 193 194 $child = self::factory()->term->create_and_get( 195 array( 196 'taxonomy' => 'wptests_tax2', 197 'name' => 'Child', 198 'slug' => 'child', 199 'parent' => $parent, 200 ) 201 ); 202 203 $actual = wp_unique_term_slug( $slug, $child ); 204 205 $this->assertLessThanOrEqual( 200, strlen( $actual ), 'The slug does not fit the column.' ); 206 $this->assertSame( 207 0, 208 preg_match( '/%(?![0-9a-fA-F]{2})/', $actual ), 209 'The slug contains a truncated percent-encoded sequence.' 210 ); 211 $this->assertTrue( 212 wp_is_valid_utf8( urldecode( $actual ) ), 213 'The slug does not decode to valid UTF-8.' 214 ); 215 } 216 217 /** 218 * @ticket 46010 219 */ 220 public function test_numeric_suffixed_slug_should_be_truncated_to_the_column_length() { 221 $slug = str_repeat( 'a', 200 ); 222 223 self::factory()->term->create( 224 array( 225 'taxonomy' => 'wptests_tax1', 226 'name' => 'Existing', 227 'slug' => $slug, 228 ) 229 ); 230 231 $term = self::factory()->term->create_and_get( 232 array( 233 'taxonomy' => 'wptests_tax1', 234 'name' => 'New', 235 'slug' => 'new', 236 ) 237 ); 238 239 $this->assertSame( str_repeat( 'a', 198 ) . '-2', wp_unique_term_slug( $slug, $term ) ); 240 } 241 242 /** 243 * A two digit suffix needs one more character of headroom than a one digit suffix. 244 * 245 * @ticket 46010 246 */ 247 public function test_multi_digit_numeric_suffix_should_be_truncated_to_the_column_length() { 248 $slug = str_repeat( 'a', 198 ); 249 250 self::factory()->term->create( 251 array( 252 'taxonomy' => 'wptests_tax1', 253 'name' => 'Existing', 254 'slug' => $slug, 255 ) 256 ); 257 258 // Occupy every one digit suffix, forcing the loop on to "-10". 259 for ( $num = 2; $num <= 9; $num++ ) { 260 self::factory()->term->create( 261 array( 262 'taxonomy' => 'wptests_tax1', 263 'name' => "Existing $num", 264 'slug' => $slug . "-$num", 265 ) 266 ); 267 } 268 269 $term = self::factory()->term->create_and_get( 270 array( 271 'taxonomy' => 'wptests_tax1', 272 'name' => 'New', 273 'slug' => 'new', 274 ) 275 ); 276 277 $actual = wp_unique_term_slug( $slug, $term ); 278 279 $this->assertSame( str_repeat( 'a', 197 ) . '-10', $actual ); 280 $this->assertNull( term_exists( $actual ), 'The returned slug is already in use.' ); 281 } 175 282 } -
trunk/tests/phpunit/tests/term/wpUpdateTerm.php
r56547 r63909 803 803 $this->assertSame( 'invalid_term', $found->get_error_code() ); 804 804 } 805 806 /** 807 * wp_update_term() shares the uniquifier with wp_insert_term(). 808 * 809 * @ticket 46010 810 */ 811 public function test_wp_update_term_child_with_long_encoded_slug_should_be_updated() { 812 $taxonomy = 'wptests_tax'; 813 register_taxonomy( $taxonomy, 'post', array( 'hierarchical' => true ) ); 814 815 // This name percent-encodes to a 116 character slug. 816 $name = 'Категория на продукта'; 817 818 $parent = wp_insert_term( $name, $taxonomy ); 819 $this->assertNotWPError( $parent ); 820 821 $child = self::factory()->term->create( 822 array( 823 'taxonomy' => $taxonomy, 824 'name' => 'Child', 825 'slug' => 'child', 826 'parent' => $parent['term_id'], 827 ) 828 ); 829 830 /* 831 * Clearing the slug field on the Edit Category screen regenerates the slug 832 * from the name, which then collides with the parent and gets suffixed. 833 */ 834 $found = wp_update_term( 835 $child, 836 $taxonomy, 837 array( 838 'name' => $name, 839 'slug' => '', 840 'parent' => $parent['term_id'], 841 ) 842 ); 843 844 $this->assertNotWPError( $found, 'The term could not be updated.' ); 845 846 $updated = get_term( $child ); 847 848 // wp_update_term() ignores the return value of $wpdb->update(), so an 849 // overlong slug leaves the whole row unwritten without reporting an error. 850 $this->assertSame( $name, $updated->name, 'The term name was not updated.' ); 851 $this->assertNotSame( 'child', $updated->slug, 'The term slug was not updated.' ); 852 $this->assertLessThanOrEqual( 200, strlen( $updated->slug ) ); 853 854 _unregister_taxonomy( $taxonomy ); 855 } 805 856 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)