Make WordPress Core


Ignore:
Timestamp:
05/29/2015 12:52:17 PM (9 years ago)
Author:
boonebgorges
Message:

Disallow post slugs that will result in permalinks that conflict with date archive URLs.

On certain permalink structures, a numeric post slug will result in a post
permalink that conflicts with a date archive URL. For example, with permastruct
/%year%/%monthnum%/%postname%/, a post published in May 2015 with slug
'15' will result in a URL (/2015/05/15/) that conflicts with the archive
for May 15, 2015.

To avoid this problem, wp_unique_post_slug() rejects a requested slug when it
would generate a conflict of this type. Thus, in our example, '15' would
become '15-2'.

Props valendesigns, boonebgorges, Denis-de-Bernardy, loushou.
See #5305.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/wpUniquePostSlug.php

    r32604 r32647  
    165165        $this->assertSame( 'foo', $actual );
    166166    }
     167
     168    /**
     169     * @ticket 5305
     170     */
     171    public function test_slugs_resulting_in_permalinks_that_resemble_year_archives_should_be_suffixed() {
     172        global $wp_rewrite;
     173        $wp_rewrite->init();
     174        $wp_rewrite->set_permalink_structure( '/%postname%/' );
     175        $wp_rewrite->flush_rules();
     176
     177        $p = $this->factory->post->create( array(
     178            'post_type' => 'post',
     179            'post_name' => 'foo',
     180        ) );
     181
     182        $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
     183        $this->assertEquals( '2015-2', $found );
     184
     185        $wp_rewrite->set_permalink_structure( '' );
     186        flush_rewrite_rules();
     187    }
     188
     189    /**
     190     * @ticket 5305
     191     */
     192    public function test_yearlike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_year_archives() {
     193        global $wp_rewrite;
     194        $wp_rewrite->init();
     195        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     196        $wp_rewrite->flush_rules();
     197
     198        $p = $this->factory->post->create( array(
     199            'post_type' => 'post',
     200            'post_name' => 'foo',
     201        ) );
     202
     203        $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
     204        $this->assertEquals( '2015', $found );
     205
     206        $wp_rewrite->set_permalink_structure( '' );
     207        flush_rewrite_rules();
     208    }
     209
     210    /**
     211     * @ticket 5305
     212     */
     213    public function test_slugs_resulting_in_permalinks_that_resemble_month_archives_should_be_suffixed() {
     214        global $wp_rewrite;
     215        $wp_rewrite->init();
     216        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     217        $wp_rewrite->flush_rules();
     218
     219        $p = $this->factory->post->create( array(
     220            'post_type' => 'post',
     221            'post_name' => 'foo',
     222        ) );
     223
     224        $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
     225        $this->assertEquals( '11-2', $found );
     226
     227        $wp_rewrite->set_permalink_structure( '' );
     228        flush_rewrite_rules();
     229    }
     230
     231    /**
     232     * @ticket 5305
     233     */
     234    public function test_monthlike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_month_archives() {
     235        global $wp_rewrite;
     236        $wp_rewrite->init();
     237        $wp_rewrite->set_permalink_structure( '/%year%/foo/%postname%/' );
     238        $wp_rewrite->flush_rules();
     239
     240        $p = $this->factory->post->create( array(
     241            'post_type' => 'post',
     242            'post_name' => 'foo',
     243        ) );
     244
     245        $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
     246        $this->assertEquals( '11', $found );
     247
     248        $wp_rewrite->set_permalink_structure( '' );
     249        flush_rewrite_rules();
     250    }
     251
     252    /**
     253     * @ticket 5305
     254     */
     255    public function test_monthlike_slugs_should_not_be_suffixed_for_invalid_month_numbers() {
     256        global $wp_rewrite;
     257        $wp_rewrite->init();
     258        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     259        $wp_rewrite->flush_rules();
     260
     261        $p = $this->factory->post->create( array(
     262            'post_type' => 'post',
     263            'post_name' => 'foo',
     264        ) );
     265
     266        $found = wp_unique_post_slug( '13', $p, 'publish', 'post', 0 );
     267        $this->assertEquals( '13', $found );
     268
     269        $wp_rewrite->set_permalink_structure( '' );
     270        flush_rewrite_rules();
     271    }
     272
     273    /**
     274     * @ticket 5305
     275     */
     276    public function test_slugs_resulting_in_permalinks_that_resemble_day_archives_should_be_suffixed() {
     277        global $wp_rewrite;
     278        $wp_rewrite->init();
     279        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
     280        $wp_rewrite->flush_rules();
     281
     282        $p = $this->factory->post->create( array(
     283            'post_type' => 'post',
     284            'post_name' => 'foo',
     285        ) );
     286
     287        $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
     288        $this->assertEquals( '30-2', $found );
     289
     290        $wp_rewrite->set_permalink_structure( '' );
     291        flush_rewrite_rules();
     292    }
     293
     294    /**
     295     * @ticket 5305
     296     */
     297    public function test_daylike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_day_archives() {
     298        global $wp_rewrite;
     299        $wp_rewrite->init();
     300        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     301        $wp_rewrite->flush_rules();
     302
     303        $p = $this->factory->post->create( array(
     304            'post_type' => 'post',
     305            'post_name' => 'foo',
     306        ) );
     307
     308        $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
     309        $this->assertEquals( '30', $found );
     310
     311        $wp_rewrite->set_permalink_structure( '' );
     312        flush_rewrite_rules();
     313    }
     314
     315    /**
     316     * @ticket 5305
     317     */
     318    public function test_daylike_slugs_should_not_be_suffixed_for_invalid_day_numbers() {
     319        global $wp_rewrite;
     320        $wp_rewrite->init();
     321        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
     322        $wp_rewrite->flush_rules();
     323
     324        $p = $this->factory->post->create( array(
     325            'post_type' => 'post',
     326            'post_name' => 'foo',
     327        ) );
     328
     329        $found = wp_unique_post_slug( '32', $p, 'publish', 'post', 0 );
     330        $this->assertEquals( '32', $found );
     331
     332        $wp_rewrite->set_permalink_structure( '' );
     333        flush_rewrite_rules();
     334    }
    167335}
Note: See TracChangeset for help on using the changeset viewer.