Make WordPress Core


Ignore:
Timestamp:
05/29/2015 12:52:17 PM (10 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/admin/includesPost.php

    r32002 r32647  
    314314        flush_rewrite_rules();
    315315    }
     316
     317    /**
     318     * @ticket 5305
     319     */
     320    public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_year_archives() {
     321        global $wp_rewrite;
     322        $wp_rewrite->init();
     323        $wp_rewrite->set_permalink_structure( '/%postname%/' );
     324        $wp_rewrite->flush_rules();
     325
     326        $p = $this->factory->post->create( array(
     327            'post_name' => '2015',
     328        ) );
     329
     330        $found = get_sample_permalink( $p );
     331        $this->assertEquals( '2015-2', $found[1] );
     332
     333        $wp_rewrite->set_permalink_structure( '' );
     334        flush_rewrite_rules();
     335    }
     336
     337    /**
     338     * @ticket 5305
     339     */
     340    public function test_get_sample_permalink_should_allow_yearlike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
     341        global $wp_rewrite;
     342        $wp_rewrite->init();
     343        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     344        $wp_rewrite->flush_rules();
     345
     346        $p = $this->factory->post->create( array(
     347            'post_name' => '2015',
     348        ) );
     349
     350        $found = get_sample_permalink( $p );
     351        $this->assertEquals( '2015', $found[1] );
     352
     353        $wp_rewrite->set_permalink_structure( '' );
     354        flush_rewrite_rules();
     355    }
     356
     357    /**
     358     * @ticket 5305
     359     */
     360    public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_month_archives() {
     361        global $wp_rewrite;
     362        $wp_rewrite->init();
     363        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     364        $wp_rewrite->flush_rules();
     365
     366        $p = $this->factory->post->create( array(
     367            'post_name' => '11',
     368        ) );
     369
     370        $found = get_sample_permalink( $p );
     371        $this->assertEquals( '11-2', $found[1] );
     372
     373        $wp_rewrite->set_permalink_structure( '' );
     374        flush_rewrite_rules();
     375    }
     376
     377    /**
     378     * @ticket 5305
     379     */
     380    public function test_get_sample_permalink_should_ignore_potential_month_conflicts_for_invalid_monthnum() {
     381        global $wp_rewrite;
     382        $wp_rewrite->init();
     383        $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
     384        $wp_rewrite->flush_rules();
     385
     386        $p = $this->factory->post->create( array(
     387            'post_name' => '13',
     388        ) );
     389
     390        $found = get_sample_permalink( $p );
     391        $this->assertEquals( '13', $found[1] );
     392
     393        $wp_rewrite->set_permalink_structure( '' );
     394        flush_rewrite_rules();
     395    }
     396
     397    /**
     398     * @ticket 5305
     399     */
     400    public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_day_archives() {
     401        global $wp_rewrite;
     402        $wp_rewrite->init();
     403        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
     404        $wp_rewrite->flush_rules();
     405
     406        $p = $this->factory->post->create( array(
     407            'post_name' => '30',
     408        ) );
     409
     410        $found = get_sample_permalink( $p );
     411        $this->assertEquals( '30-2', $found[1] );
     412
     413        $wp_rewrite->set_permalink_structure( '' );
     414        flush_rewrite_rules();
     415    }
     416
     417    /**
     418     * @ticket 5305
     419     */
     420    public function test_get_sample_permalink_should_iterate_slug_suffix_when_a_date_conflict_is_found() {
     421        global $wp_rewrite;
     422        $wp_rewrite->init();
     423        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
     424        $wp_rewrite->flush_rules();
     425
     426        $this->factory->post->create( array(
     427            'post_name' => '30-2',
     428        ) );
     429
     430        $p = $this->factory->post->create( array(
     431            'post_name' => '30',
     432        ) );
     433
     434        $found = get_sample_permalink( $p );
     435        $this->assertEquals( '30-3', $found[1] );
     436
     437        $wp_rewrite->set_permalink_structure( '' );
     438        flush_rewrite_rules();
     439    }
     440
     441    /**
     442     * @ticket 5305
     443     */
     444    public function test_get_sample_permalink_should_ignore_potential_day_conflicts_for_invalid_day() {
     445        global $wp_rewrite;
     446        $wp_rewrite->init();
     447        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
     448        $wp_rewrite->flush_rules();
     449
     450        $p = $this->factory->post->create( array(
     451            'post_name' => '32',
     452        ) );
     453
     454        $found = get_sample_permalink( $p );
     455        $this->assertEquals( '32', $found[1] );
     456
     457        $wp_rewrite->set_permalink_structure( '' );
     458        flush_rewrite_rules();
     459    }
     460
     461    /**
     462     * @ticket 5305
     463     */
     464    public function test_get_sample_permalink_should_allow_daylike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
     465        global $wp_rewrite;
     466        $wp_rewrite->init();
     467        $wp_rewrite->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' );
     468        $wp_rewrite->flush_rules();
     469
     470        $p = $this->factory->post->create( array(
     471            'post_name' => '30',
     472        ) );
     473
     474        $found = get_sample_permalink( $p );
     475        $this->assertEquals( '30', $found[1] );
     476
     477        $wp_rewrite->set_permalink_structure( '' );
     478        flush_rewrite_rules();
     479    }
    316480}
Note: See TracChangeset for help on using the changeset viewer.