Skip to content

Commit 98d2187

Browse files
committed
Taxonomy: Limit term slugs to 200 characters in wp_unique_term_slug().
Prevents the auto-generation of term slugs that exceed database schema's maximum of 200 characters. This prevents database errors inserting the term. This is particularly useful for slugs generated for non-latin alphabets as the slug is stored in it's URL encoded form in the database, for example `Категория на продукта` generates a 116 character slug. * Introduces `wp_truncate_slug()` as a generically named function for truncating the slug of any object * Deprecates `_truncate_post_slug()` in favour of above The test suite for `_truncate_post_slug()` is retained (with the addition of expecting a deprecation notice) in order to ensure that future updates do not result in errors. Props khokansardar, arkaprabhachowdhury, callumbw95, mikejolley, mindctrl, sajib1223, subrataemfluence, swissspidy. Fixes #46010. git-svn-id: https://develop.svn.wordpress.org/trunk@63909 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 66772c8 commit 98d2187

10 files changed

Lines changed: 374 additions & 32 deletions

File tree

‎src/wp-includes/deprecated.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,3 +6532,20 @@ function wp_sanitize_script_attributes( $attributes ) {
65326532
}
65336533
return $attributes_string;
65346534
}
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+
}

‎src/wp-includes/formatting.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,34 @@ function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'displa
24002400
return $title;
24012401
}
24022402

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+
24032431
/**
24042432
* Ensures a string is a valid SQL 'order by' clause.
24052433
*

‎src/wp-includes/post.php‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,7 +5721,7 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p
57215721
) {
57225722
$suffix = 2;
57235723
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";
57255725
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) );
57265726
++$suffix;
57275727
} while ( $post_name_check );
@@ -5758,7 +5758,7 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p
57585758
) {
57595759
$suffix = 2;
57605760
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";
57625762
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id, $post_parent ) );
57635763
++$suffix;
57645764
} while ( $post_name_check );
@@ -5814,7 +5814,7 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p
58145814
) {
58155815
$suffix = 2;
58165816
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";
58185818
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id ) );
58195819
++$suffix;
58205820
} while ( $post_name_check );
@@ -5837,31 +5837,6 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p
58375837
return apply_filters( 'wp_unique_post_slug', $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug );
58385838
}
58395839

5840-
/**
5841-
* Truncates a post slug.
5842-
*
5843-
* @since 3.6.0
5844-
* @access private
5845-
*
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-
}
5864-
58655840
/**
58665841
* Adds tags to a post.
58675842
*
@@ -8726,7 +8701,7 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
87268701
return $post->post_name;
87278702
}
87288703
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';
87308705
$wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
87318706
clean_post_cache( $post->ID );
87328707
return $post_name;

‎src/wp-includes/taxonomy.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,9 +3213,15 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
32133213
* If that still doesn't return a unique slug, then it tries to append a number
32143214
* until it finds a number that is truly unique.
32153215
*
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+
*
32163220
* The only purpose for `$term` is for appending a parent, if one exists.
32173221
*
32183222
* @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.
32193225
*
32203226
* @global wpdb $wpdb WordPress database abstraction object.
32213227
*
@@ -3271,7 +3277,7 @@ function wp_unique_term_slug( $slug, $term ) {
32713277
*/
32723278
if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
32733279
if ( $parent_suffix ) {
3274-
$slug .= $parent_suffix;
3280+
$slug = wp_truncate_slug( $slug . $parent_suffix, 200 );
32753281
}
32763282

32773283
if ( ! empty( $term->term_id ) ) {
@@ -3283,7 +3289,9 @@ function wp_unique_term_slug( $slug, $term ) {
32833289
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
32843290
$num = 2;
32853291
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;
32873295
++$num;
32883296
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
32893297
} while ( $slug_check );

‎src/wp-includes/theme-templates.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_id
8787
$suffix = 2;
8888
do {
8989
$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";
9191
$query_args['post_name__in'] = array( $alt_post_name );
9292
$query = new WP_Query( $query_args );
9393
++$suffix;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* @group formatting
5+
*
6+
* @covers ::wp_truncate_slug
7+
*/
8+
class Tests_Formatting_wpTruncateSlug extends WP_UnitTestCase {
9+
10+
/**
11+
* Tests that wp_truncate_slug() correctly truncates slugs.
12+
*
13+
* @ticket 56868
14+
* @ticket 46010
15+
*
16+
* @dataProvider data_wp_truncate_slug_should_truncate
17+
*
18+
* @param string $slug The slug to truncate.
19+
* @param int $length Max length of the slug.
20+
* @param string $expected The expected truncated slug.
21+
* @param string $message Test feedback message.
22+
*/
23+
public function test_wp_truncate_slug_should_truncate( $slug, $length, $expected, $message ) {
24+
$this->assertSame( $expected, wp_truncate_slug( $slug, $length ), $message );
25+
}
26+
27+
/**
28+
* Data provider for test_wp_truncate_slug_should_truncate().
29+
*
30+
* @return array[]
31+
*/
32+
public function data_wp_truncate_slug_should_truncate() {
33+
return array(
34+
'a slug that is too long' => array(
35+
'slug' => 'truncated slug',
36+
'length' => 9,
37+
'expected' => 'truncated',
38+
'message' => '"truncated slug" should have been truncated to "truncated".',
39+
),
40+
'a slug that is too long and ends with a dash' => array(
41+
'slug' => 'truncated-slug',
42+
'length' => 10,
43+
'expected' => 'truncated',
44+
'message' => '"truncated-slug" should have been truncated to "truncated".',
45+
),
46+
47+
// URL-encoded characters.
48+
'URL-encoded characters and "length" includes the first URL-encoded character' => array(
49+
'slug' => 'myslug%2F',
50+
'length' => 7,
51+
'expected' => 'myslug',
52+
'message' => '"myslug%2F" should have been truncated to "myslug".',
53+
),
54+
'URL-encoded characters and "length" includes the second URL-encoded character' => array(
55+
'slug' => 'myslug%2F',
56+
'length' => 8,
57+
'expected' => 'myslug',
58+
'message' => '"myslug%2F" should have been truncated to "myslug".',
59+
),
60+
'URL-encoded characters and "length" includes the third URL-encoded character' => array(
61+
'slug' => 'myslug%2F',
62+
'length' => 9,
63+
'expected' => 'myslug%2F',
64+
'message' => '"myslug%2F" should have been truncated to "myslug%2F".',
65+
),
66+
67+
// URL-encoded accent characters.
68+
'URL-encoded accent characters and "length" includes the first URL-encoded character' => array(
69+
'slug' => 'myslug%C4%85',
70+
'length' => 7,
71+
'expected' => 'myslug',
72+
'message' => '"myslug%C4%85" should have been truncated to "myslug".',
73+
),
74+
'URL-encoded accent characters and "length" includes the second URL-encoded character' => array(
75+
'slug' => 'myslug%C4%85',
76+
'length' => 8,
77+
'expected' => 'myslug',
78+
'message' => '"myslug%C4%85" should have been truncated to "myslug".',
79+
),
80+
'URL-encoded accent characters and "length" includes the third URL-encoded character' => array(
81+
'slug' => 'myslug%C4%85',
82+
'length' => 9,
83+
'expected' => 'myslug',
84+
'message' => '"myslug%C4%85" should have been truncated to "myslug".',
85+
),
86+
'URL-encoded accent characters and "length" includes the fourth URL-encoded character' => array(
87+
'slug' => 'myslug%C4%85',
88+
'length' => 10,
89+
'expected' => 'myslug',
90+
'message' => '"myslug%C4%85" should have been truncated to "myslug".',
91+
),
92+
'URL-encoded accent characters and "length" includes the fifth URL-encoded character' => array(
93+
'slug' => 'myslug%C4%85',
94+
'length' => 11,
95+
'expected' => 'myslug',
96+
'message' => '"myslug%C4%85" should have been truncated to "myslug".',
97+
),
98+
'URL-encoded accent characters and "length" includes the sixth URL-encoded character' => array(
99+
'slug' => 'myslug%C4%85',
100+
'length' => 12,
101+
'expected' => 'myslug%C4%85',
102+
'message' => '"myslug%C4%85" should have been truncated to "myslug%C4%85".',
103+
),
104+
);
105+
}
106+
}

‎tests/phpunit/tests/post/truncatePostSlug.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ class Tests_Post_TruncatePostSlug extends WP_UnitTestCase {
1111
* Tests that _truncate_post_slug() correctly truncates slugs.
1212
*
1313
* @ticket 56868
14+
* @ticket 46010
1415
*
1516
* @dataProvider data_truncate_post_slug_should_truncate
1617
*
18+
* @expectedDeprecated _truncate_post_slug
19+
*
1720
* @param string $slug The slug to truncate.
1821
* @param int $length Max length of the slug.
1922
* @param string $expected The expected truncated slug.

‎tests/phpunit/tests/term/wpInsertTerm.php‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,53 @@ public function test_wp_insert_term_with_empty_name_after_db_sanitization() {
907907
$this->assertSame( 'invalid_term_name', $term->get_error_code() );
908908
}
909909

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+
910957
/** Helpers */
911958

912959
public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {

0 commit comments

Comments
 (0)