Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/js/_enqueues/admin/inline-edit-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,21 @@ window.wp = window.wp || {};
} );
} );

var $bulkCategories = $( '#bulk-edit .inline-edit-categories input[name="post_category[]"]' );

/*
* Reset category states left over from a previous Bulk Edit session.
* Otherwise a category checked for a previous selection stays checked
* and gets assigned to posts that are not in it.
*/
$bulkCategories
.prop( { checked: false, indeterminate: false } )
.removeAttr( 'aria-label' )
.off( 'change.inlineEditPost' );
$( '#bulk-edit .inline-edit-categories input[name="indeterminate_post_category[]"]' ).remove();

// Compute initial states.
$( '.inline-edit-categories input[name="post_category[]"]' ).each( function() {
$bulkCategories.each( function() {
if ( categories[ $( this ).val() ] == checkedPosts.length ) {
// If the number of checked categories matches the number of selected posts, then all posts are in this category.
$( this ).prop( 'checked', true );
Expand All @@ -266,13 +279,13 @@ window.wp = window.wp || {};
}
} );

$( '.inline-edit-categories input[name="post_category[]"]:indeterminate' ).on( 'change', function() {
$bulkCategories.filter( ':indeterminate' ).on( 'change.inlineEditPost', function() {
// Remove accessible label text. Remove the indeterminate flags as there was a specific state change.
$( this ).removeAttr( 'aria-label' ).parent().find( 'input[name="indeterminate_post_category[]"]' ).remove();
} );

$( '.inline-edit-save button' ).on( 'click', function() {
$( '.inline-edit-categories input[name="post_category[]"]' ).prop( 'indeterminate', false );
$( '#bulk-edit .inline-edit-save button' ).off( 'click.inlineEditPost' ).on( 'click.inlineEditPost', function() {
$bulkCategories.prop( 'indeterminate', false );
} );

/**
Expand Down
16 changes: 14 additions & 2 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,9 @@ function bulk_edit_posts( $post_data = null ) {
continue;
}

$post = get_post( $post_id );
$tax_names = get_object_taxonomies( $post );
$post = get_post( $post_id );
$tax_names = get_object_taxonomies( $post );
$remove_all_categories = false;

foreach ( $tax_names as $tax_name ) {
$taxonomy_obj = get_taxonomy( $tax_name );
Expand Down Expand Up @@ -680,6 +681,13 @@ function bulk_edit_posts( $post_data = null ) {
$determinate_cats = array_diff( $new_cats, $indeterminate_post_category );
$post_data['post_category'] = array_unique( array_merge( $indeterminate_cats, $determinate_cats ) );

/*
* Every category was unchecked for this post. wp_insert_post() would assign
* the default category, so remove the categories after the post is updated.
*/
$remove_all_categories = ! array_filter( array_map( 'absint', $post_data['post_category'] ) )
&& current_user_can( get_taxonomy( 'category' )->cap->assign_terms );

unset( $post_data['tax_input']['category'] );
}

Expand Down Expand Up @@ -720,6 +728,10 @@ function bulk_edit_posts( $post_data = null ) {
update_post_meta( $post_id, '_edit_last', get_current_user_id() );
$updated[] = $post_id;

if ( $remove_all_categories && $post_id ) {
wp_set_object_terms( $post_id, array(), 'category' );
}

if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
if ( 'sticky' === $post_data['sticky'] ) {
stick_post( $post_id );
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,92 @@ public function test_bulk_edit_if_some_categories_removed() {
$this->assertSame( $updated_terms3, array( $term3 ), 'Post 3 should only have term 3.' );
}

/**
* Tests that unchecking the default category in Bulk Edit removes it from posts
* that have no other category, rather than reassigning it.
*
* @ticket 59374
*
* @covers ::bulk_edit_posts
*/
public function test_bulk_edit_removing_default_category_from_posts_without_other_categories() {
wp_set_current_user( self::$admin_id );

$default_category = (int) get_option( 'default_category' );
$post_ids = self::factory()->post->create_many( 2 );

$this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[0] ), 'Post 1 should start in the default category.' );
$this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should start in the default category.' );

// The Bulk Edit category checklist always submits a hidden value of 0.
$request = array(
'_status' => -1,
'post' => $post_ids,
'post_category' => array( '0' ),
);

bulk_edit_posts( $request );

$this->assertSame( array(), wp_get_post_categories( $post_ids[0] ), 'Post 1 should have no categories.' );
$this->assertSame( array(), wp_get_post_categories( $post_ids[1] ), 'Post 2 should have no categories.' );
}

/**
* Tests that unchecking the default category in Bulk Edit keeps the other
* (indeterminate) categories and removes the default category from all posts.
*
* @ticket 59374
*
* @covers ::bulk_edit_posts
*/
public function test_bulk_edit_removing_default_category_from_posts_with_mixed_categories() {
wp_set_current_user( self::$admin_id );

$default_category = (int) get_option( 'default_category' );
$term1 = wp_create_category( 'test1' );
$post_ids = self::factory()->post->create_many( 2 );

wp_set_post_categories( $post_ids[1], array( $default_category, $term1 ) );

// The default category is checked for all posts, term 1 is indeterminate.
$request = array(
'_status' => -1,
'post' => $post_ids,
'post_category' => array( '0' ),
'indeterminate_post_category' => array( (string) $term1 ),
);

bulk_edit_posts( $request );

$this->assertSame( array(), wp_get_post_categories( $post_ids[0] ), 'Post 1 should have no categories.' );
$this->assertSame( array( $term1 ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should only have term 1.' );
}

/**
* Tests that Bulk Edit keeps the default category when it stays checked.
*
* @ticket 59374
*
* @covers ::bulk_edit_posts
*/
public function test_bulk_edit_keeps_default_category_when_left_checked() {
wp_set_current_user( self::$admin_id );

$default_category = (int) get_option( 'default_category' );
$post_ids = self::factory()->post->create_many( 2 );

$request = array(
'_status' => -1,
'post' => $post_ids,
'post_category' => array( '0', (string) $default_category ),
);

bulk_edit_posts( $request );

$this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[0] ), 'Post 1 should keep the default category.' );
$this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should keep the default category.' );
}

/**
* Tests that `bulk_edit_posts()` fires the 'bulk_edit_posts' action.
*
Expand Down
Loading