Adds GET endpoint to bulk edit API to get group info #139 #69

This commit is contained in:
leogermani 2018-10-04 15:38:08 -03:00
parent c8b9601572
commit 43f8118b80
3 changed files with 80 additions and 4 deletions

View File

@ -36,6 +36,15 @@ class REST_Bulkedit_Controller extends REST_Controller {
'args' => $this->get_create_params()
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<group_id>[0-9a-f]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'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]+)/add',
array(
@ -274,6 +283,32 @@ class REST_Bulkedit_Controller extends REST_Controller {
}
}
public function get_item($request) {
$group_id = $request['group_id'];
$args = ['id' => $group_id];
$bulk = new \Tainacan\Bulk_Edit($args);
$count = $bulk->count_posts();
if (0 === $count) {
return new \WP_REST_Response([
'error_message' => __('Group not found', 'tainacan'),
], 404);
}
$options = $bulk->get_options();
$return = [
'id' => $group_id,
'items_count' => $count,
'options' => $options
];
return new \WP_REST_Response($return, 200);
}
public function trash_items($request) {
$group_id = $request['group_id'];

View File

@ -143,12 +143,15 @@ class Bulk_Edit {
return $this->id;
}
// return the number of items selected in the current bulk group
/**
* return the number of items selected in the current bulk group
* @return int number of items in the group
*/
public function count_posts() {
global $wpdb;
$id = $this->get_id();
if (!empty($id)) {
return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(post_id) FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $this->meta_key, $id) );
return (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(post_id) FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $this->meta_key, $id) );
}
return 0;
}
@ -190,11 +193,11 @@ class Bulk_Edit {
return false;
}
private function save_options($value) {
public function save_options($value) {
update_option('tainacan_bulk_' . $this->get_id(), $value);
}
private function get_options() {
public function get_options() {
return get_option('tainacan_bulk_' . $this->get_id());
}

View File

@ -1390,6 +1390,44 @@ class BulkEdit extends TAINACAN_UnitApiTestCase {
}
function test_api_get_group() {
$query = [
'posts_per_page' => 22,
'orderby' => 'title',
'order' => 'ASC'
];
$bulk = new \Tainacan\Bulk_Edit([
'query' => $query,
'collection_id' => $this->collection->get_id()
]);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $bulk->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($bulk->get_id(), $data['id']);
$this->assertEquals($bulk->get_options()['order'], $data['options']['order']);
$this->assertEquals($bulk->get_options()['orderby'], $data['options']['orderby']);
$this->assertEquals($bulk->count_posts(), $data['items_count']);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/fefefe23232'
);
$response = $this->server->dispatch($request);
$this->assertEquals(404, $response->get_status());
}