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/src/wp-includes/post.php

    r32621 r32647  
    37913791        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
    37923792
     3793        // Prevent post slugs that could result in URLs that conflict with date archives.
     3794        $conflicts_with_date_archive = false;
     3795        if ( 'post' === $post_type && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) {
     3796            $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
     3797            $postname_index = array_search( '%postname%', $permastructs );
     3798
     3799            /*
     3800             * Potential date clashes are as follows:
     3801             *
     3802             * - Any integer in the first permastruct position could be a year.
     3803             * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
     3804             * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
     3805             */
     3806            if ( 0 === $postname_index ||
     3807                ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) ||
     3808                ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
     3809            ) {
     3810                $conflicts_with_date_archive = true;
     3811            }
     3812        }
     3813
    37933814        /**
    37943815         * Filter whether the post slug would be bad as a flat slug.
     
    38003821         * @param string $post_type Post type.
    38013822         */
    3802         if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
     3823        if ( $post_name_check || in_array( $slug, $feeds ) || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
    38033824            $suffix = 2;
    38043825            do {
Note: See TracChangeset for help on using the changeset viewer.