Make WordPress Core

Changeset 63909


Ignore:
Timestamp:
09/23/2026 11:56:00 PM (7 weeks ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r63320 r63909  
    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 */
     6547function _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  
    24022402
    24032403/**
     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 */
     2418function 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/**
    24042432 * Ensures a string is a valid SQL 'order by' clause.
    24052433 *
  • trunk/src/wp-includes/post.php

    r63758 r63909  
    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;
    … …  
    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;
    … …  
    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;
    … …  
    58365836         */
    58375837        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.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, '-' );
    58635838}
    58645839
    … …  
    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 );
  • trunk/src/wp-includes/taxonomy.php

    r63758 r63909  
    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.
    … …  
    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
    … …  
    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 ) );
  • trunk/src/wp-includes/theme-templates.php

    r61469 r63909  
    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 );
  • trunk/tests/phpunit/tests/post/truncatePostSlug.php

    r62660 r63909  
    1212         *
    1313         * @ticket 56868
     14         * @ticket 46010
    1415         *
    1516         * @dataProvider data_truncate_post_slug_should_truncate
     17         *
     18         * @expectedDeprecated _truncate_post_slug
    1619         *
    1720         * @param string $slug     The slug to truncate.
  • trunk/tests/phpunit/tests/term/wpInsertTerm.php

    r63880 r63909  
    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
  • trunk/tests/phpunit/tests/term/wpUniqueTermSlug.php

    r51568 r63909  
    173173                $this->assertSame( 'dog-animal-2', $slug );
    174174        }
     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        }
    175282}
  • trunk/tests/phpunit/tests/term/wpUpdateTerm.php

    r56547 r63909  
    803803                $this->assertSame( 'invalid_term', $found->get_error_code() );
    804804        }
     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        }
    805856}
Note: See TracChangeset for help on using the changeset viewer.