add methods for bulk trash and delete #69
This commit is contained in:
parent
2ec3d34ddf
commit
abbad9bd42
|
@ -261,6 +261,73 @@ class Bulk_Edit {
|
|||
|
||||
}
|
||||
|
||||
public function trash_items() {
|
||||
if (!$this->get_id()) {
|
||||
return new \WP_Error( 'no_id', __( 'Bulk Edit group not initialized', 'tainacan' ) );
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$select_q = $this->_build_select( 'post_id' );
|
||||
|
||||
$select_insert = "SELECT ID, '_wp_trash_meta_status', post_status FROM $wpdb->posts WHERE ID IN ($select_q)";
|
||||
$select_insert_time = $wpdb->prepare("SELECT ID, '_wp_trash_meta_time', %s FROM $wpdb->posts WHERE ID IN ($select_q)", time());
|
||||
|
||||
$query_original_status = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) $select_insert";
|
||||
$query_trash_time = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) $select_insert_time";
|
||||
|
||||
$wpdb->query($query_original_status);
|
||||
$wpdb->query($query_trash_time);
|
||||
|
||||
|
||||
$query = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID IN ($select_q)";
|
||||
|
||||
// TODO trash comments?
|
||||
|
||||
return $wpdb->query($query);
|
||||
|
||||
}
|
||||
|
||||
public function untrash_items() {
|
||||
if (!$this->get_id()) {
|
||||
return new \WP_Error( 'no_id', __( 'Bulk Edit group not initialized', 'tainacan' ) );
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$select_q = $this->_build_select( 'post_id' );
|
||||
|
||||
// restore status
|
||||
|
||||
$query_restore = "UPDATE $wpdb->posts SET post_status = (SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_status' AND post_id = ID) WHERE ID IN ($select_q)";
|
||||
$query_delete_meta1 = "DELETE FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_status' AND post_id IN ( SELECT implicitTemp.post_id FROM ($select_q) implicitTemp )";
|
||||
$query_delete_meta2 = "DELETE FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND post_id IN ( SELECT implicitTemp.post_id FROM ($select_q) implicitTemp )";
|
||||
|
||||
$affected = $wpdb->query( $query_restore );
|
||||
$wpdb->query( $query_delete_meta1 );
|
||||
$wpdb->query( $query_delete_meta2 );
|
||||
|
||||
// TODO untrash comments?
|
||||
|
||||
return $affected;
|
||||
|
||||
}
|
||||
|
||||
public function delete_items() {
|
||||
if (!$this->get_id()) {
|
||||
return new \WP_Error( 'no_id', __( 'Bulk Edit group not initialized', 'tainacan' ) );
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$select_q = $this->_build_select( 'post_id' );
|
||||
|
||||
$query_delete = "DELETE FROM $wpdb->posts WHERE ID IN ($select_q)";
|
||||
|
||||
return $wpdb->query($query_delete);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the current group of items
|
||||
|
|
|
@ -842,6 +842,104 @@ class BulkEdit extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* @group trash
|
||||
*/
|
||||
function test_trash() {
|
||||
|
||||
$ids = array_slice($this->items_ids, 2, 17);
|
||||
|
||||
$bulk = new \Tainacan\Bulk_Edit([
|
||||
'items_ids' => $ids,
|
||||
]);
|
||||
|
||||
|
||||
$this->assertEquals( 17, $bulk->trash_items() );
|
||||
|
||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
$trashed = $Tainacan_Items->fetch_ids(['post_status' => 'trash', 'posts_per_page' => -1]);
|
||||
$rest = $Tainacan_Items->fetch_ids(['posts_per_page' => -1]);
|
||||
|
||||
|
||||
$this->assertEquals(17, sizeof($trashed));
|
||||
$this->assertEquals(40 - 17, sizeof($rest));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @group trash
|
||||
*/
|
||||
function test_untrash() {
|
||||
|
||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
$items = $Tainacan_Items->fetch(['posts_per_page' => -1], [], 'OBJECT');
|
||||
|
||||
// Lets set 17 as private
|
||||
$i = 1;
|
||||
foreach ($items as $item) {
|
||||
|
||||
If ($i > 17) break;
|
||||
|
||||
$item->set_status('private');
|
||||
$item->validate();
|
||||
$Tainacan_Items->update($item);
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
|
||||
$bulk = new \Tainacan\Bulk_Edit([
|
||||
'items_ids' => $this->items_ids,
|
||||
]);
|
||||
|
||||
$this->assertEquals( 40, $bulk->trash_items() ); // trash all items
|
||||
|
||||
$trashed = $Tainacan_Items->fetch_ids(['post_status' => 'trash', 'posts_per_page' => -1]);
|
||||
$rest = $Tainacan_Items->fetch_ids(['posts_per_page' => -1]);
|
||||
|
||||
$this->assertEquals(40, sizeof($trashed));
|
||||
$this->assertEquals(0, sizeof($rest));
|
||||
|
||||
$this->assertEquals( 40, $bulk->untrash_items() ); // untrash all items
|
||||
|
||||
$trashed = $Tainacan_Items->fetch_ids(['post_status' => 'trash', 'posts_per_page' => -1]);
|
||||
$private = $Tainacan_Items->fetch_ids(['post_status' => 'private', 'posts_per_page' => -1]);
|
||||
$public = $Tainacan_Items->fetch_ids(['post_status' => 'publish', 'posts_per_page' => -1]);
|
||||
|
||||
$this->assertEquals(0, sizeof($trashed));
|
||||
$this->assertEquals(17, sizeof($private));
|
||||
$this->assertEquals(40 - 17, sizeof($public));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @group trash
|
||||
*/
|
||||
function test_delete_items() {
|
||||
|
||||
$ids = array_slice($this->items_ids, 2, 17);
|
||||
|
||||
$bulk = new \Tainacan\Bulk_Edit([
|
||||
'items_ids' => $ids,
|
||||
]);
|
||||
|
||||
$this->assertEquals( 17, $bulk->delete_items() );
|
||||
|
||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
$trashed = $Tainacan_Items->fetch_ids(['post_status' => 'trash', 'posts_per_page' => -1]);
|
||||
$rest = $Tainacan_Items->fetch_ids(['posts_per_page' => -1]);
|
||||
|
||||
|
||||
$this->assertEquals(0, sizeof($trashed));
|
||||
$this->assertEquals(40 - 17, sizeof($rest));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue