WordPress.org

Make WordPress Core

Ticket #5305: 5305.4.diff

File 5305.4.diff, 2.5 KB (added by valendesigns, 5 years ago)

Added a unit test.

  • src/wp-includes/rewrite.php

    diff --git src/wp-includes/rewrite.php src/wp-includes/rewrite.php
    index 984fbbe..d696325 100644
    class WP_Rewrite { 
    895895         * one is used. If none matches, then the default will be used, which is
    896896         * year, month, day.
    897897         *
    898          * Prevents post ID and date permalinks from overlapping. In the case of
    899          * post_id, the date permalink will be prepended with front permalink with
    900          * 'date/' before the actual permalink to form the complete date permalink
    901          * structure.
     898         * Prevents post ID or slug and date permalinks from overlapping. In the case of
     899         * post_id or a postname only structure, the date permalink will be prepended
     900         * with front permalink and 'date/' before the actual permalink to form the
     901         * complete date permalink structure.
    902902         *
    903903         * @since 1.5.0
    904904         * @access public
    class WP_Rewrite { 
    930930                if ( empty($date_endian) )
    931931                        $date_endian = '%year%/%monthnum%/%day%';
    932932
    933                 // Do not allow the date tags and %post_id% to overlap in the permalink
    934                 // structure. If they do, move the date tags to $front/date/.
     933                /*
     934                 * Do not allow the date tags and %post_id% or %postname% to overlap in the
     935                 * permalink structure. If they do, move the date tags to $front/date/.
     936                 */
    935937                $front = $this->front;
    936938                preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
    937939                $tok_index = 1;
    938940                foreach ( (array) $tokens[0] as $token) {
    939                         if ( '%post_id%' == $token && ($tok_index <= 3) ) {
     941                        if ( in_array( $token, array( '%post_id%', '%postname%' ) ) && $tok_index <= 3 ) {
    940942                                $front = $front . 'date/';
    941943                                break;
    942944                        }
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index ae549e0..2c294a2 100644
    class Tests_Post extends WP_UnitTestCase { 
    470470        }
    471471
    472472        /**
     473         * @ticket 5305
     474         */
     475        function test_permalink_with_date_collision() {
     476                global $wp_rewrite;
     477                $wp_rewrite->init();
     478                $wp_rewrite->set_permalink_structure( '/archive/%postname%/' );
     479                $wp_rewrite->flush_rules();
     480
     481                $id = $this->factory->post->create( array(
     482                        'post_author'  => $this->author_id,
     483                        'post_status'  => 'publish',
     484                        'post_content' => rand_str(),
     485                        'post_title'   => '2015',
     486                        'post_date'    => '2015-02-01 01:00:00',
     487                ) );
     488
     489                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     490
     491                $wp_rewrite->set_permalink_structure('');
     492        }
     493
     494        /**
    473495         * @ticket 21013
    474496         */
    475497        function test_wp_unique_post_slug_with_non_latin_slugs() {