set_status method in bulk edit api endpoint (#69)

This commit is contained in:
Leo Germani 2018-08-26 17:45:11 -03:00
parent d0ed1f80c7
commit 507f33de32
3 changed files with 134 additions and 0 deletions

View File

@ -82,6 +82,21 @@ class REST_Bulkedit_Controller extends REST_Controller {
'permission_callback' => array($this, 'bulk_edit_permissions_check'),
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<group_id>[0-9a-f]+)/set_status',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'set_status'),
'permission_callback' => array($this, 'bulk_edit_permissions_check'),
'args' => [
'value' => [
'type' => 'string',
'description' => __( 'The new status value', 'tainacan' ),
],
],
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<group_id>[0-9a-f]+)/set',
array(
@ -221,6 +236,33 @@ class REST_Bulkedit_Controller extends REST_Controller {
return $this->generic_action('replace_value', $request, ['old_value', 'new_value']);
}
public function set_status($request) {
$body = json_decode($request->get_body(), true);
if( !isset($body['value']) ){
return new \WP_REST_Response([
'error_message' => __('Value must be provided', 'tainacan'),
], 400);
}
$group_id = $request['group_id'];
$args = ['id' => $group_id];
$bulk = new \Tainacan\Bulk_Edit($args);
$action = $bulk->set_status($body['value']);
if ( is_wp_error($action) ) {
return new \WP_REST_Response([
'error_message' => $action->get_error_message(),
], 400);
} else {
return new \WP_REST_Response($action, 200);
}
}
public function trash_items($request) {

View File

@ -141,6 +141,33 @@ class Bulk_Edit {
return $wpdb->prepare( "SELECT $fields FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $this->meta_key, $this->get_id() );
}
/**
* Sets the status to all items in the current group
*
*/
public function set_status($value) {
if (!$this->get_id()) {
return new \WP_Error( 'no_id', __( 'Bulk Edit group not initialized', 'tainacan' ) );
}
$possible_values = ['draft', 'publish', 'private'];
// Specific validation
if (!in_array($value, $possible_values)) {
return new \WP_Error( 'invalid_action', __( 'Invalid status', 'tainacan' ) );
}
global $wpdb;
$select_q = $this->_build_select( 'post_id' );
$query = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s WHERE ID IN ($select_q)", $value);
return $wpdb->query($query);
}
/**
* Adds a value to a metadatum to all items in the current group

View File

@ -605,6 +605,35 @@ class BulkEdit extends TAINACAN_UnitApiTestCase {
$this->assertEquals(20, $items->found_posts);
}
function test_set_status() {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$ids = array_slice($this->items_ids, 4, 11);
$bulk = new \Tainacan\Bulk_Edit([
'items_ids' => $ids,
]);
$id = $bulk->get_id();
$bulk->set_status('draft');
$items = $Tainacan_Items->fetch([
'status' => 'draft',
'posts_per_page' => -1
]);
$this->assertEquals(11, $items->found_posts);
$items = $Tainacan_Items->fetch([
'publish' => 'draft',
'posts_per_page' => -1
]);
$this->assertEquals(29, $items->found_posts);
}
function test_set_regular_multi_meta() {
@ -840,6 +869,42 @@ class BulkEdit extends TAINACAN_UnitApiTestCase {
$this->assertEquals(14, $items->found_posts);
}
/**
* @group api
*/
public function test_api_set_status() {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$ids = array_slice($this->items_ids, 2, 14);
$bulk = new \Tainacan\Bulk_Edit([
'items_ids' => $ids,
]);
$body = json_encode([
'value' => 'private'
]);
$request = new \WP_REST_Request(
'POST', $this->api_baseroute . '/' . $bulk->get_id() . '/set_status'
);
$request->set_body( $body );
$response = $this->server->dispatch($request);
$items = $Tainacan_Items->fetch([
'status' => 'private',
'posts_per_page' => -1
]);
$this->assertEquals(14, $items->found_posts);
}
/**