Make WordPress Core

Ticket #18962: 18962.diff

File 18962.diff, 2.8 KB (added by nacin, 10 years ago)

Patch by mboynes, I just tweaked it a little and added tests.

  • src/wp-includes/post.php

     
    36773677        if ( ! is_array( $feeds ) )
    36783678                $feeds = array();
    36793679
    3680         $hierarchical_post_types = get_post_types( array('hierarchical' => true) );
    36813680        if ( 'attachment' == $post_type ) {
    36823681                // Attachment slugs must be unique across all types.
    36833682                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
     
    37003699                        } while ( $post_name_check );
    37013700                        $slug = $alt_post_name;
    37023701                }
    3703         } elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
     3702        } elseif ( is_post_type_hierarchical( $post_type ) ) {
    37043703                if ( 'nav_menu_item' == $post_type )
    37053704                        return $slug;
    37063705
     
    37083707                 * Page slugs must be unique within their own trees. Pages are in a separate
    37093708                 * namespace than posts so page slugs are allowed to overlap post slugs.
    37103709                 */
    3711                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
    3712                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
     3710                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1";
     3711                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
    37133712
    37143713                /**
    37153714                 * Filter whether the post slug would make a bad hierarchical post slug.
  • tests/phpunit/tests/post.php

     
    991991                _unregister_taxonomy( $tax );
    992992        }
    993993
     994        /**
     995         * @ticket 18962
     996         */
     997        function test_wp_unique_post_slug_with_multiple_hierarchies() {
     998                register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
     999                register_post_type( 'post-type-2', array( 'hierarchical' => true ) );
     1000
     1001                $args = array(
     1002                        'post_type' => 'post-type-1',
     1003                        'post_name' => 'some-slug',
     1004                        'post_status' => 'publish',
     1005                );
     1006                $one = $this->factory->post->create( $args );
     1007                $args['post_type'] = 'post-type-2';
     1008                $two = $this->factory->post->create( $args );
     1009
     1010                $this->assertEquals( 'some-slug', get_post( $one )->post_name );
     1011                $this->assertEquals( 'some-slug', get_post( $two )->post_name );
     1012
     1013                $this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-1', 0 ) );
     1014                $this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-2', 0 ) );
     1015
     1016                _unregister_post_type( 'post-type-1' );
     1017                _unregister_post_type( 'post-type-2' );
     1018        }
    9941019}