Make WordPress Core


Ignore:
Timestamp:
02/09/2024 07:48:41 PM (8 months ago)
Author:
joedolson
Message:

Quick/Bulk Edit: Pre-fill category fields with their status.

Pre-fill category fields in the Quick/Bulk Edit form with their current status.

When bulk editing, if only some of the selected items are in a given category, the category's checkbox will display a line to indicate an indeterminate status.

Originally committed in [56172], but reverted due to a bug that removed all categories. Updated commit fixes the bug, adds unit tests, and improves the accessibility of the indeterminate state checkboxes.

Props pavelevap, scribu, chasedsiedu, helen, joshcanhelp, ubernaut, Cyberchicken, laumindproductscomau, SergeyBiryukov, Marcoevich, tomybyte, thinkluke, virtality-marketing-solutions, Michalooki, dmsnell, itecrs, pannelars, WHSajid, samba45, Mte90, johnbillion, tomluckies, soulseekah, francina, oglekler, ajmcfadyen, mukesh27, costdev, hellofromTonya, peterwilsoncc, joedolson, pbiron, oglekler, webcommsat, jorbin, ajmcfadyen, huzaifaalmesbah.
Fixes #11302.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r57068 r57580  
    383383            $this->assertSame( 'aside', get_post_format( $post_id ) );
    384384        }
     385    }
     386
     387    /**
     388     * @ticket 11302
     389     */
     390    public function test_bulk_edit_if_categories_unchanged() {
     391        wp_set_current_user( self::$admin_id );
     392
     393        $post_ids = self::factory()->post->create_many( 3 );
     394
     395        wp_set_post_categories( $post_ids[0], array( 'test1', 'test2' ) );
     396        wp_set_post_categories( $post_ids[1], array( 'test2', 'test3' ) );
     397        wp_set_post_categories( $post_ids[2], array( 'test1', 'test3' ) );
     398
     399        $terms1 = wp_get_post_categories( $post_ids[0] );
     400        $terms2 = wp_get_post_categories( $post_ids[1] );
     401        $terms3 = wp_get_post_categories( $post_ids[2] );
     402
     403        $indeterminate_categories = array_merge( $terms1, $terms2, $terms3 );
     404
     405        $request = array(
     406            '_status'                     => -1,
     407            'post'                        => $post_ids,
     408            'indeterminate_post_category' => $indeterminate_categories,
     409        );
     410
     411        bulk_edit_posts( $request );
     412
     413        $updated_terms1 = wp_get_post_categories( $post_ids[0] );
     414        $updated_terms2 = wp_get_post_categories( $post_ids[1] );
     415        $updated_terms3 = wp_get_post_categories( $post_ids[2] );
     416
     417        $this->assertSame( $terms1, $updated_terms1, 'Post 1 should have terms 1 and 2.' );
     418        $this->assertSame( $terms2, $updated_terms2, 'Post 2 should have terms 2 and 3.' );
     419        $this->assertSame( $terms3, $updated_terms3, 'Post 3 should have terms 1 and 3.' );
     420    }
     421
     422    /**
     423     * @ticket 11302
     424     */
     425    public function test_bulk_edit_if_some_categories_added() {
     426        wp_set_current_user( self::$admin_id );
     427
     428        $post_ids = self::factory()->post->create_many( 3 );
     429        $term1    = wp_create_category( 'test1' );
     430        $term2    = wp_create_category( 'test2' );
     431        $term3    = wp_create_category( 'test3' );
     432        $term4    = wp_create_category( 'test4' );
     433
     434        wp_set_post_categories( $post_ids[0], array( $term1, $term2 ) );
     435        wp_set_post_categories( $post_ids[1], array( $term2, $term3 ) );
     436        wp_set_post_categories( $post_ids[2], array( $term1, $term3 ) );
     437
     438        $terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
     439        $terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
     440        $terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
     441        // All existing categories are indeterminate.
     442        $indeterminate = array_unique( array_merge( $terms1, $terms2, $terms3 ) );
     443        // Add new category.
     444        $categories[] = $term4;
     445
     446        $request = array(
     447            '_status'                     => -1,
     448            'post'                        => $post_ids,
     449            'post_category'               => $categories,
     450            'indeterminate_post_category' => $indeterminate,
     451        );
     452
     453        bulk_edit_posts( $request );
     454
     455        $updated_terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
     456        $updated_terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
     457        $updated_terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
     458
     459        // Each post should have the same categories as before and add term 4.
     460        $this->assertSame( array( $term1, $term2, $term4 ), $updated_terms1, 'Post should have terms 1, 2, and 4.' );
     461        $this->assertSame( array( $term2, $term3, $term4 ), $updated_terms2, 'Post should have terms 2, 3, and 4.' );
     462        $this->assertSame( array( $term1, $term3, $term4 ), $updated_terms3, 'Post should have terms 1, 3, and 4.' );
     463    }
     464
     465    /**
     466     * @ticket 11302
     467     */
     468    public function test_bulk_edit_if_some_categories_removed() {
     469        wp_set_current_user( self::$admin_id );
     470
     471        $post_ids = self::factory()->post->create_many( 3 );
     472        $term1    = wp_create_category( 'test1' );
     473        $term2    = wp_create_category( 'test2' );
     474        $term3    = wp_create_category( 'test3' );
     475
     476        wp_set_post_categories( $post_ids[0], array( $term1, $term2 ) );
     477        wp_set_post_categories( $post_ids[1], array( $term2, $term3 ) );
     478        wp_set_post_categories( $post_ids[2], array( $term1, $term3 ) );
     479
     480        $terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
     481        $terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
     482        $terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
     483
     484        // Terms 2 and 3 are in indeterminate state.
     485        $indeterminate = array( $term2, $term3 );
     486        // Remove term 1 from selected categories.
     487        $categories = array_unique( array_merge( $terms1, $terms2, $terms3 ) );
     488        $remove_key = array_search( $term1, $categories, true );
     489        unset( $categories[ $remove_key ] );
     490
     491        $request = array(
     492            '_status'                     => -1,
     493            'post'                        => $post_ids,
     494            'post_category'               => $categories,
     495            'indeterminate_post_category' => $indeterminate,
     496        );
     497
     498        bulk_edit_posts( $request );
     499
     500        $updated_terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
     501        $updated_terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
     502        $updated_terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
     503
     504        // Post 1 should only have term 2.
     505        $this->assertSame( $updated_terms1, array( $term2 ), 'Post 1 should only have term 2.' );
     506        // Post 2 should be unchanged.
     507        $this->assertSame( $terms2, $updated_terms2, 'Post 2 should be unchanged.' );
     508        // Post 3 should only have term 3.
     509        $this->assertSame( $updated_terms3, array( $term3 ), 'Post 3 should only have term 3.' );
    385510    }
    386511
Note: See TracChangeset for help on using the changeset viewer.