WordPress.org

Make WordPress Core

Ticket #20419: 20419.4.diff

File 20419.4.diff, 18.2 KB (added by boonebgorges, 3 years ago)
  • src/wp-admin/includes/ajax-actions.php

    diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php
    index 63a6cda..b1e4b1b 100644
    function wp_ajax_inline_save() { 
    16611661        // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published.
    16621662        if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ) ) ) {
    16631663                $post['post_status'] = 'publish';
    1664                 $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] );
     1664                $data['post_name'] = wp_unique_post_slug( $data['post_name'], (object) array(
     1665                        'ID' => $post['ID'],
     1666                        'post_status' => $post['post_status'],
     1667                        'post_type' => $post['post_type'],
     1668                        'post_parent' => $post['post_parent'],
     1669                ) );
    16651670        }
    16661671
    16671672        // Update the post.
  • src/wp-admin/includes/post.php

    diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php
    index 2f1f178..e7c2af1 100644
    function get_sample_permalink($id, $title = null, $name = null) { 
    12261226        if ( !is_null($name) )
    12271227                $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
    12281228
    1229         $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
     1229        $post->post_name = wp_unique_post_slug( $post->post_name, (object) array(
     1230                'ID' => $post->ID,
     1231                'post_status' => $post->post_status,
     1232                'post_type' => $post->post_type,
     1233                'post_parent' => $post->post_parent,
     1234        ) );
    12301235
    12311236        $post->filter = 'sample';
    12321237
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 1920e84..93e973b 100644
    function wp_insert_post( $postarr, $wp_error = false ) { 
    32883288                $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
    32893289        }
    32903290
    3291         $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
     3291        $post_name = wp_unique_post_slug( $post_name, (object) array(
     3292                'ID'          => $post_ID,
     3293                'post_type'   => $post_type,
     3294                'post_status' => $post_status,
     3295                'post_title'  => $post_title,
     3296                'post_name'   => $post_name,
     3297                'post_parent' => $post_parent,
     3298        ) );
    32923299
    32933300        // Don't unslash.
    32943301        $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
    function wp_insert_post( $postarr, $wp_error = false ) { 
    33703377        }
    33713378
    33723379        if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3373                 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent );
     3380                // Mock a WP_Post object for wp_unique_post_slug().
     3381                $_post_data_for_slug = new WP_Post( (object) array(
     3382                        'ID'          => $post_ID,
     3383                        'post_status' => $data['post_status'],
     3384                        'post_type'   => $post_type,
     3385                        'post_parent' => $post_parent,
     3386                ) );
     3387                $_slug = sanitize_title( $data['post_title'], $post_ID );
     3388                $data['post_name'] = wp_unique_post_slug( $_slug, $_post_data_for_slug );
     3389
    33743390                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    33753391                clean_post_cache( $post_ID );
    33763392        }
    function check_and_publish_future_post( $post_id ) { 
    36803696 * Computes a unique slug for the post, when given the desired slug and some post details.
    36813697 *
    36823698 * @since 2.8.0
     3699 * @since 4.6.0 Second parameter should now be a post object or ID. `$post_status`, `$post_type`, and `$post_parent`
     3700 *              parameters are deprecated.
    36833701 *
    36843702 * @global wpdb       $wpdb WordPress database abstraction object.
    36853703 * @global WP_Rewrite $wp_rewrite
    36863704 *
    3687  * @param string $slug        The desired slug (post_name).
    3688  * @param int    $post_ID     Post ID.
    3689  * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
    3690  * @param string $post_type   Post type.
    3691  * @param int    $post_parent Post parent ID.
     3705 * @param string  $slug The desired slug (post_name).
     3706 * @param obj     $post Post object or ID.
    36923707 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
    36933708 */
    3694 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
    3695         if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
     3709function wp_unique_post_slug( $slug, $post, $deprecated_post_status = null, $deprecated_post_type = null, $deprecated_post_parent = null ) {
     3710        if ( $deprecated_post_status || $deprecated_post_type || $deprecated_post_parent ) {
     3711                _deprecated_argument( __FUNCTION__, '4.6', __( 'wp_unique_post_slug() accepts two parameters: the desired post slug, and a WP_Post object or ID.' ) );
     3712        }
     3713
     3714        $_post = get_post( $post );
     3715        if ( ! $_post ) {
     3716                /*
     3717                 * This is a brand new post, so we mock it. The value of post_status,
     3718                 * post_type, and post_parent don't matter. They're only present to prevent
     3719                 * notices when checking for deprecated values.
     3720                 */
     3721                $_post = new WP_Post( (object) array(
     3722                        'post_status' => 'publish',
     3723                        'post_type'   => 'post',
     3724                        'post_parent' => 0,
     3725                ) );
     3726        }
     3727
     3728        $post = $_post;
     3729        unset( $_post );
     3730
     3731        // Support for deprecated parameters.
     3732        if ( $deprecated_post_status ) {
     3733                $post->post_status = $deprecated_post_status;
     3734        }
     3735
     3736        if ( $deprecated_post_type ) {
     3737                $post->post_type = $deprecated_post_type;
     3738        }
     3739
     3740        if ( $deprecated_post_parent ) {
     3741                $post->post_parent = $deprecated_post_parent;
     3742        }
     3743
     3744        if ( in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post->post_status && 'revision' == $post->post_type ) ) {
    36963745                return $slug;
     3746        }
    36973747
    36983748        global $wpdb, $wp_rewrite;
    36993749
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37033753        if ( ! is_array( $feeds ) )
    37043754                $feeds = array();
    37053755
    3706         if ( 'attachment' == $post_type ) {
     3756        if ( 'attachment' === $post->post_type ) {
    37073757                // Attachment slugs must be unique across all types.
    37083758                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
    3709                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
     3759                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->ID ) );
    37103760
    37113761                /**
    37123762                 * Filter whether the post slug would make a bad attachment slug.
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37203770                        $suffix = 2;
    37213771                        do {
    37223772                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3723                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
     3773                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->ID ) );
    37243774                                $suffix++;
    37253775                        } while ( $post_name_check );
    37263776                        $slug = $alt_post_name;
    37273777                }
    3728         } elseif ( is_post_type_hierarchical( $post_type ) ) {
    3729                 if ( 'nav_menu_item' == $post_type )
     3778        } elseif ( is_post_type_hierarchical( $post->post_type ) ) {
     3779                if ( 'nav_menu_item' === $post->post_type ) {
    37303780                        return $slug;
     3781                }
    37313782
    37323783                /*
    37333784                 * Page slugs must be unique within their own trees. Pages are in a separate
    37343785                 * namespace than posts so page slugs are allowed to overlap post slugs.
    37353786                 */
    37363787                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
    3737                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
     3788                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->post_type, $post->ID, $post->post_parent ) );
    37383789
    37393790                /**
    37403791                 * Filter whether the post slug would make a bad hierarchical post slug.
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37463797                 * @param string $post_type   Post type.
    37473798                 * @param int    $post_parent Post parent ID.
    37483799                 */
    3749                 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
     3800                if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post->post_type, $post->post_parent ) ) {
    37503801                        $suffix = 2;
    37513802                        do {
    37523803                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3753                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
     3804                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->post_type, $post->ID, $post->post_parent ) );
    37543805                                $suffix++;
    37553806                        } while ( $post_name_check );
    37563807                        $slug = $alt_post_name;
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37583809        } else {
    37593810                // Post slugs must be unique across all posts.
    37603811                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
    3761                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
     3812                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post->post_type, $post->ID ) );
    37623813
    37633814                // Prevent new post slugs that could result in URLs that conflict with date archives.
    3764                 $post = get_post( $post_ID );
     3815                $_post = get_post( $post->ID );
    37653816                $conflicts_with_date_archive = false;
    3766                 if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) {
     3817                $slug_num = intval( $slug );
     3818                if ( 'post' === $post->post_type && ( ! $_post || $_post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num ) {
    37673819                        $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
    37683820                        $postname_index = array_search( '%postname%', $permastructs );
    3769 
    37703821                        /*
    37713822                         * Potential date clashes are as follows:
    37723823                         *
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    37913842                 * @param string $slug      The post slug.
    37923843                 * @param string $post_type Post type.
    37933844                 */
    3794                 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
     3845                if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post->post_type ) ) {
    37953846                        $suffix = 2;
    37963847                        do {
    37973848                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    3798                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
     3849                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post->post_type, $post->ID ) );
    37993850                                $suffix++;
    38003851                        } while ( $post_name_check );
    38013852                        $slug = $alt_post_name;
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    38143865         * @param int    $post_parent   Post parent ID
    38153866         * @param string $original_slug The original post slug.
    38163867         */
    3817         return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
     3868        return apply_filters( 'wp_unique_post_slug', $slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent, $original_slug );
    38183869}
    38193870
    38203871/**
  • tests/phpunit/tests/post/wpUniquePostSlug.php

    diff --git tests/phpunit/tests/post/wpUniquePostSlug.php tests/phpunit/tests/post/wpUniquePostSlug.php
    index 62f1ad6..88dfdcd 100644
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    4141
    4242        /**
    4343         * @ticket 18962
     44         * @expectedDeprecated wp_unique_post_slug
    4445         */
    4546        public function test_with_multiple_hierarchies() {
    4647                register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    6768
    6869        /**
    6970         * @ticket 30339
     71         * @expectedDeprecated wp_unique_post_slug
    7072         */
    7173        public function test_with_hierarchy() {
    7274                register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    9092
    9193        /**
    9294         * @ticket 18962
     95         * @expectedDeprecated wp_unique_post_slug
    9396         */
    9497        function test_wp_unique_post_slug_with_hierarchy_and_attachments() {
    9598                register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    104107                $args = array(
    105108                        'post_mime_type' => 'image/jpeg',
    106109                        'post_type' => 'attachment',
    107                         'post_name' => 'image'
     110                        'post_name' => 'image',
     111                        'post_status' => 'inherit',
    108112                );
    109113                $attachment = self::factory()->attachment->create_object( 'image.jpg', $one, $args );
    110114
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    137141
    138142                $p2 = self::factory()->post->create( array(
    139143                        'post_type' => 'post',
     144                        'post_status' => $status,
     145                        'post_type' => 'post',
     146                        'post_parent' => 0,
    140147                ) );
    141148
    142                 $actual = wp_unique_post_slug( 'foo', $p2, $status, 'post', 0 );
     149                $actual = wp_unique_post_slug( 'foo', $p2 );
    143150
    144151                $this->assertSame( 'foo', $actual );
    145152        }
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    160167
    161168                $p2 = self::factory()->post->create( array(
    162169                        'post_type' => 'post',
     170                        'post_status' => 'inherit',
     171                        'post_type' => 'revision',
     172                        'post_parent' => 0,
    163173                ) );
    164174
    165                 $actual = wp_unique_post_slug( 'foo', $p2, 'inherit', 'revision', 0 );
     175                $actual = wp_unique_post_slug( 'foo', $p2 );
    166176
    167177                $this->assertSame( 'foo', $actual );
    168178        }
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    176186                $p = self::factory()->post->create( array(
    177187                        'post_type' => 'post',
    178188                        'post_name' => 'foo',
     189                        'post_status' => 'publish',
     190                        'post_type' => 'post',
     191                        'post_parent' => 0,
    179192                ) );
    180193
    181                 $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
     194                $found = wp_unique_post_slug( '2015', $p );
    182195                $this->assertEquals( '2015-2', $found );
    183196        }
    184197
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    192205                        'post_type' => 'post',
    193206                        'post_name' => 'foo',
    194207                        'post_status' => 'publish',
     208                        'post_type' => 'post',
     209                        'post_parent' => 0,
    195210                ) );
    196211
    197                 $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
     212                $found = wp_unique_post_slug( '2015', $p );
    198213                $this->assertEquals( '2015-2', $found );
    199214        }
    200215
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    207222                $p = self::factory()->post->create( array(
    208223                        'post_type' => 'post',
    209224                        'post_name' => 'foo',
     225                        'post_status' => 'publish',
     226                        'post_type' => 'post',
     227                        'post_parent' => 0,
    210228                ) );
    211229
    212                 $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
     230                $found = wp_unique_post_slug( '2015', $p );
    213231                $this->assertEquals( '2015', $found );
    214232        }
    215233
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    222240                $p = self::factory()->post->create( array(
    223241                        'post_type' => 'post',
    224242                        'post_name' => 'foo',
     243                        'post_status' => 'publish',
     244                        'post_type' => 'post',
     245                        'post_parent' => 0,
    225246                ) );
    226247
    227                 $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
     248                $found = wp_unique_post_slug( '11', $p );
    228249                $this->assertEquals( '11-2', $found );
    229250        }
    230251
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    237258                $p = self::factory()->post->create( array(
    238259                        'post_type' => 'post',
    239260                        'post_name' => 'foo',
     261                        'post_status' => 'publish',
     262                        'post_type' => 'post',
     263                        'post_parent' => 0,
    240264                ) );
    241265
    242                 $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
     266                $found = wp_unique_post_slug( '11', $p );
    243267                $this->assertEquals( '11', $found );
    244268        }
    245269
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    252276                $p = self::factory()->post->create( array(
    253277                        'post_type' => 'post',
    254278                        'post_name' => 'foo',
     279                        'post_status' => 'publish',
     280                        'post_type' => 'post',
     281                        'post_parent' => 0,
    255282                ) );
    256283
    257                 $found = wp_unique_post_slug( '13', $p, 'publish', 'post', 0 );
     284                $found = wp_unique_post_slug( '13', $p );
    258285                $this->assertEquals( '13', $found );
    259286        }
    260287
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    267294                $p = self::factory()->post->create( array(
    268295                        'post_type' => 'post',
    269296                        'post_name' => 'foo',
     297                        'post_status' => 'publish',
     298                        'post_type' => 'post',
     299                        'post_parent' => 0,
    270300                ) );
    271301
    272                 $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
     302                $found = wp_unique_post_slug( '30', $p );
    273303                $this->assertEquals( '30-2', $found );
    274304        }
    275305
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    282312                $p = self::factory()->post->create( array(
    283313                        'post_type' => 'post',
    284314                        'post_name' => 'foo',
     315                        'post_status' => 'publish',
     316                        'post_type' => 'post',
     317                        'post_parent' => 0,
    285318                ) );
    286319
    287                 $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
     320                $found = wp_unique_post_slug( '30', $p );
    288321                $this->assertEquals( '30', $found );
    289322        }
    290323
    291324        /**
    292325         * @ticket 5305
     326         * @expectedDeprecated wp_unique_post_slug
    293327         */
    294328        public function test_daylike_slugs_should_not_be_suffixed_for_invalid_day_numbers() {
    295329                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    305339
    306340        /**
    307341         * @ticket 34971
     342         * @expectedDeprecated wp_unique_post_slug
    308343         */
    309344        public function test_embed_slug_should_be_suffixed_for_posts() {
    310345                $this->set_permalink_structure( '/%postname%/' );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    320355
    321356        /**
    322357         * @ticket 34971
     358         * @expectedDeprecated wp_unique_post_slug
    323359         */
    324360        public function test_embed_slug_should_be_suffixed_for_pages() {
    325361                $this->set_permalink_structure( '/%postname%/' );
    class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { 
    335371
    336372        /**
    337373         * @ticket 34971
     374         * @expectedDeprecated wp_unique_post_slug
    338375         */
    339376        public function test_embed_slug_should_be_suffixed_for_attachments() {
    340377                $this->set_permalink_structure( '/%postname%/' );