Fetch all terms of a taxonomy

This commit is contained in:
weryques 2018-01-18 10:45:31 -02:00
parent 027fd43030
commit ab081cc992
2 changed files with 115 additions and 0 deletions

View File

@ -31,6 +31,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check')
)
)
);
@ -196,6 +201,63 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
$term = new Entities\Term($this->terms_repository->fetch($request['term_id']));
return $this->terms_repository->can_edit($term);
}
/**
* @param mixed $item
* @param WP_REST_Request $request
*
* @return array|mixed|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
if(is_array($item)){
$prepared = [];
foreach ($item as $term){
$prepared[] = $term->__toArray();
}
return $prepared;
}
return $item;
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
if(!empty($taxonomy)){
$args = json_decode($request->get_body(), true);
$terms = $this->terms_repository->fetch($args, $taxonomy);
$prepared_terms = $this->prepare_item_for_response($terms, $request);
return new WP_REST_Response($prepared_terms, 200);
}
return new WP_REST_Response([
'error_message' => "Taxonomy with this id ($taxonomy_id) not found.",
'taxonomy_id' => $taxonomy_id
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_items_permissions_check( $request ) {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
return $this->taxonomy_repository->can_read($taxonomy);
}
}
?>

View File

@ -112,6 +112,59 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
}
public function test_fetch_terms(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Gender',
'description' => 'Music types',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Rock',
'user' => get_current_user_id(),
),
true
);
$this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Trap',
'user' => get_current_user_id(),
),
true
);
$show_empty = json_encode([
'hide_empty' => false
]);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/terms/taxonomy/' . $taxonomy->get_id()
);
$request->set_body($show_empty);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$termA = $data[0];
$termB = $data[1];
$this->assertEquals('Trap', $termB['name']);
$this->assertEquals('Rock', $termA['name']);
}
}
?>