Refactoring API
New routes Update methods refactored Log Controller created and it test
This commit is contained in:
parent
695ada466e
commit
9ef5d3ad6e
|
@ -13,11 +13,20 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
|
|||
$entity_prepared = [
|
||||
'id' => $entity->get_id(),
|
||||
'description' => $entity->get_description(),
|
||||
'author_id' => $entity->get_author_id(),
|
||||
'creation_date' => $entity->get_creation_date(),
|
||||
'modification_date' => $entity->get_modification_date(),
|
||||
];
|
||||
|
||||
if(array_key_exists('modification_date', $map)){
|
||||
$entity_prepared['modification_date'] = $entity->get_modification_date();
|
||||
}
|
||||
|
||||
if(array_key_exists('creation_date', $map)){
|
||||
$entity_prepared['creation_date'] = $entity->get_creation_date();
|
||||
}
|
||||
|
||||
if(array_key_exists('author_id', $map)){
|
||||
$entity_prepared['author_id'] = $entity->get_author_id();
|
||||
}
|
||||
|
||||
if(array_key_exists('name', $map)){
|
||||
$entity_prepared['name'] = $entity->get_name();
|
||||
} elseif(array_key_exists('title', $map)){
|
||||
|
@ -35,6 +44,24 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
|
|||
return $entity_prepared;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filters
|
||||
* @param $map
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function unmap_filters($filters, $map){
|
||||
$unmapped = [];
|
||||
|
||||
if(!empty($filters)) {
|
||||
foreach ( $filters as $filter => $value ) {
|
||||
$unmapped[ $map[ $filter ]['map'] ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $unmapped;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -78,7 +78,18 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items($request){
|
||||
$collections = $this->collections_repository->fetch();
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->collections_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$collections = $this->collections_repository->fetch($args);
|
||||
|
||||
$response = $this->prepare_item_for_response($collections, $request);
|
||||
|
||||
|
|
|
@ -3,11 +3,12 @@
|
|||
use Tainacan\Entities;
|
||||
use Tainacan\Repositories;
|
||||
|
||||
class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
|
||||
class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
||||
private $field;
|
||||
private $item_metadata_repository;
|
||||
private $item_repository;
|
||||
private $collection_repository;
|
||||
private $field_repository;
|
||||
|
||||
public function __construct() {
|
||||
$this->namespace = 'tainacan/v2';
|
||||
|
@ -22,7 +23,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
*/
|
||||
public function init_objects() {
|
||||
$this->field = new Entities\Field();
|
||||
$this->metadata_repository = new Repositories\Fields();
|
||||
$this->field_repository = new Repositories\Fields();
|
||||
$this->item_metadata_repository = new Repositories\Item_Metadata();
|
||||
$this->item_repository = new Repositories\Items();
|
||||
$this->collection_repository = new Repositories\Collections();
|
||||
|
@ -103,7 +104,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
}
|
||||
|
||||
if($this->field->validate()) {
|
||||
$this->metadata_repository->insert( $this->field );
|
||||
$this->field_repository->insert( $this->field );
|
||||
|
||||
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
|
||||
|
||||
|
@ -173,9 +174,20 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
public function get_items( $request ) {
|
||||
$collection_id = $request['collection_id'];
|
||||
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->field_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$collection = new Entities\Collection($collection_id);
|
||||
|
||||
$collection_metadata = $this->metadata_repository->fetch_by_collection($collection, [], 'OBJECT');
|
||||
$collection_metadata = $this->field_repository->fetch_by_collection($collection, $args, 'OBJECT');
|
||||
|
||||
$prepared_item = $this->prepare_item_for_response($collection_metadata, $request);
|
||||
|
||||
|
@ -259,9 +271,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
$attributes[$att] = $value;
|
||||
}
|
||||
|
||||
$field = $this->metadata_repository->fetch($field_id);
|
||||
$field = $this->field_repository->fetch($field_id);
|
||||
|
||||
$updated_metadata = $this->metadata_repository->update($field, $attributes);
|
||||
$updated_metadata = $this->field_repository->update($field, $attributes);
|
||||
|
||||
if(!($updated_metadata instanceof Entities\Field)){
|
||||
return new WP_REST_Response($updated_metadata, 400);
|
|
@ -207,13 +207,19 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
|
|||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
if(!empty($body)){
|
||||
$attributes = ['ID' => $filter_id];
|
||||
$attributes = [];
|
||||
|
||||
foreach ($body as $att => $value){
|
||||
$attributes[$att] = $value;
|
||||
}
|
||||
|
||||
$updated_filter = $this->filter_repository->update($attributes);
|
||||
$filter = $this->filter_repository->fetch($filter_id);
|
||||
|
||||
$updated_filter = $this->filter_repository->update($filter, $attributes);
|
||||
|
||||
if(!($updated_filter instanceof Entities\Filter)){
|
||||
return new WP_REST_Response($updated_filter, 400);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($updated_filter->__toArray(), 200);
|
||||
}
|
||||
|
@ -267,7 +273,18 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
|
|||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$filters = $this->filter_repository->fetch([], 'OBJECT');
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filtersq = $body['filters'];
|
||||
|
||||
$map = $this->filter_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filtersq, $map);
|
||||
}
|
||||
|
||||
$filters = $this->filter_repository->fetch($args, 'OBJECT');
|
||||
|
||||
$response = $this->prepare_item_for_response($filters, $request);
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
private $item_metadata_repository;
|
||||
private $item_repository;
|
||||
private $collection_repository;
|
||||
private $field_repository;
|
||||
|
||||
public function __construct() {
|
||||
$this->namespace = 'tainacan/v2';
|
||||
|
|
|
@ -132,8 +132,19 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->items_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$collection_id = $request['collection_id'];
|
||||
$items = $this->items_repository->fetch([], $collection_id, 'WP_Query');
|
||||
$items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
|
||||
|
||||
$response = $this->prepare_item_for_response($items, $request);
|
||||
|
||||
|
@ -169,6 +180,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return object|Entities\Item|WP_Error
|
||||
* @throws Exception
|
||||
*/
|
||||
public function prepare_item_for_database( $request ) {
|
||||
|
||||
|
@ -206,6 +218,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$collection_id = $request['collection_id'];
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
use Tainacan\Entities;
|
||||
use Tainacan\Repositories;
|
||||
|
||||
class TAINACAN_REST_Logs_Controller extends TAINACAN_REST_Controller {
|
||||
private $logs_repository;
|
||||
private $log;
|
||||
|
||||
/**
|
||||
* TAINACAN_REST_Logs_Controller constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = 'tainacan/v2';
|
||||
$this->rest_base = 'logs';
|
||||
|
||||
add_action('rest_api_init', array($this, 'register_routes'));
|
||||
add_action('init', array($this, 'init_objects'));
|
||||
}
|
||||
|
||||
public function init_objects(){
|
||||
$this->logs_repository = new Repositories\Logs();
|
||||
$this->log = new Entities\Log();
|
||||
}
|
||||
|
||||
public function register_routes() {
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check'),
|
||||
)
|
||||
)
|
||||
);
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<log_id>[\d]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_item'),
|
||||
'permission_callback' => array($this, 'get_item_permissions_check'),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $item
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return array|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$prepared = [];
|
||||
$map = $this->logs_repository->get_map();
|
||||
|
||||
|
||||
if(is_array($item)){
|
||||
foreach ($item as $it){
|
||||
$prepared[] = $this->get_only_needed_attributes($it, $map);
|
||||
}
|
||||
|
||||
return $prepared;
|
||||
}
|
||||
|
||||
return $item->__toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->logs_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$logs = $this->logs_repository->fetch($args, 'OBJECT');
|
||||
|
||||
if(!empty($logs)) {
|
||||
$prepared_logs = $this->prepare_item_for_response( $logs, $request );
|
||||
|
||||
return new WP_REST_Response($prepared_logs, 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($logs, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
return $this->logs_repository->can_read($this->log);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$log_id = $request['log_id'];
|
||||
|
||||
$log = $this->logs_repository->fetch($log_id);
|
||||
|
||||
if(!empty($log)) {
|
||||
$prepared_log = $this->prepare_item_for_response( $log, $request );
|
||||
|
||||
return new WP_REST_Response($prepared_log, 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($log, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$log = $this->logs_repository->fetch($request['log_id']);
|
||||
|
||||
if($log instanceof Entities\Log){
|
||||
return $log->can_read();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -62,6 +62,16 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
|
|||
)
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
$this->namespace, '/' . $this->rest_base . '/(?P<taxonomy_id>[\d]+)/collection/(?P<collection_id>[\d]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array($this, 'update_item'),
|
||||
'permission_callback' => array($this, 'update_item_permissions_check')
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,7 +204,18 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
|
|||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$taxonomies = $this->taxonomy_repository->fetch([], 'OBJECT');
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->taxonomy_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$taxonomies = $this->taxonomy_repository->fetch($args, 'OBJECT');
|
||||
|
||||
$taxonomies_prepared = $this->prepare_item_for_response($taxonomies, $request);
|
||||
|
||||
|
@ -259,14 +280,26 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
if(!empty($body)){
|
||||
$attributes = ['ID' => $taxonomy_id];
|
||||
if(!empty($body) || isset($request['collection_id'])){
|
||||
$attributes = [];
|
||||
|
||||
foreach ($body as $att => $value){
|
||||
$attributes[$att] = $value;
|
||||
if(isset($request['collection_id'])) {
|
||||
$collection_id = $request['collection_id'];
|
||||
|
||||
$attributes = [ 'collection' => $collection_id ];
|
||||
} else {
|
||||
foreach ( $body as $att => $value ) {
|
||||
$attributes[ $att ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$updated_taxonomy = $this->taxonomy_repository->update($attributes);
|
||||
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
|
||||
|
||||
$updated_taxonomy = $this->taxonomy_repository->update($taxonomy, $attributes);
|
||||
|
||||
if(!($updated_taxonomy instanceof Entities\Taxonomy)){
|
||||
return new WP_REST_Response($updated_taxonomy, 400);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($updated_taxonomy->__toArray(), 200);
|
||||
}
|
||||
|
|
|
@ -190,12 +190,8 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
|
|||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
if(!empty($body)){
|
||||
$taxonomy_name = $this->taxonomy_repository->fetch($taxonomy_id)->get_db_identifier();
|
||||
|
||||
$identifiers = [
|
||||
'term_id' => $term_id,
|
||||
'tax_name' => $taxonomy_name
|
||||
];
|
||||
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
|
||||
$tax_name = $taxonomy->get_db_identifier();
|
||||
|
||||
$attributes = [];
|
||||
|
||||
|
@ -203,7 +199,13 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
|
|||
$attributes[$att] = $value;
|
||||
}
|
||||
|
||||
$updated_term = $this->terms_repository->update([$attributes, $identifiers]);
|
||||
$term = $this->terms_repository->fetch($term_id, $taxonomy);
|
||||
|
||||
$updated_term = $this->terms_repository->update([$term, $tax_name], $attributes);
|
||||
|
||||
if(!($updated_term instanceof Entities\Term)){
|
||||
return new WP_REST_Response($updated_term, 400);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($updated_term->__toArray(), 200);
|
||||
}
|
||||
|
@ -260,7 +262,16 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
|
||||
|
||||
$args = json_decode($request->get_body(), true);
|
||||
$body = json_decode($request->get_body(), true);
|
||||
$args = [];
|
||||
|
||||
if(isset($body['filters'])) {
|
||||
$filters = $body['filters'];
|
||||
|
||||
$map = $this->terms_repository->get_map();
|
||||
|
||||
$args = $this->unmap_filters($filters, $map);
|
||||
}
|
||||
|
||||
$terms = $this->terms_repository->fetch($args, $taxonomy);
|
||||
|
||||
|
|
|
@ -3,11 +3,12 @@
|
|||
$rest_controller = new TAINACAN_REST_Controller();
|
||||
$rest_collections_controller = new TAINACAN_REST_Collections_Controller();
|
||||
$rest_items_controller = new TAINACAN_REST_Items_Controller();
|
||||
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
|
||||
$rest_fields_controller = new TAINACAN_REST_Fields_Controller();
|
||||
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
|
||||
$rest_terms_controller = new TAINACAN_REST_Terms_Controller();
|
||||
$rest_filters_controller = new TAINACAN_REST_Filters_Controller();
|
||||
$rest_item_metadata_controller = new TAINACAN_REST_Item_Metadata_Controller();
|
||||
$rest_logs_controller = new TAINACAN_REST_Logs_Controller();
|
||||
// Add here other endpoints imports
|
||||
|
||||
?>
|
|
@ -228,7 +228,9 @@ class Fields extends Repository {
|
|||
*
|
||||
* @param array $args WP_Query args || int $args the field id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
*
|
||||
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function fetch( $args, $output = null ) {
|
||||
|
||||
|
@ -259,7 +261,9 @@ class Fields extends Repository {
|
|||
* @param Entities\Collection $collection
|
||||
* @param array $args
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
*
|
||||
* @return Array Entities\Field
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
|
||||
$collection_id = $collection->get_id();
|
||||
|
|
|
@ -169,19 +169,20 @@ class Filters extends Repository {
|
|||
}
|
||||
|
||||
public function update($object, $new_values = null){
|
||||
$map = $this->get_map();
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach ($object as $key => $value) {
|
||||
if($key != 'ID') {
|
||||
$entity[$map[$key]['map']] = $value ;
|
||||
} elseif ($key == 'ID'){
|
||||
$entity[$key] = (int) $value;
|
||||
foreach ($new_values as $key => $value) {
|
||||
try {
|
||||
$set_ = 'set_' . $key;
|
||||
$object->$set_( $value );
|
||||
} catch (\Error $error){
|
||||
return $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return new Entities\Filter(wp_update_post($entity));
|
||||
if($object->validate()){
|
||||
return $this->insert($object);
|
||||
}
|
||||
|
||||
return $object->get_errors();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,7 +78,10 @@ abstract class Repository {
|
|||
}
|
||||
}
|
||||
$obj->WP_Post->post_type = $obj::get_post_type();
|
||||
//$obj->WP_Post->post_status = 'publish';
|
||||
|
||||
if($obj instanceof Entities\Log) {
|
||||
$obj->WP_Post->post_status = 'publish';
|
||||
}
|
||||
|
||||
// TODO verificar se salvou mesmo
|
||||
$id = wp_insert_post($obj->WP_Post);
|
||||
|
|
|
@ -109,7 +109,8 @@ class Taxonomies extends Repository {
|
|||
|
||||
/**
|
||||
* @param Entities\Taxonomy $taxonomy
|
||||
* @return int
|
||||
*
|
||||
* @return Entities\Entity
|
||||
*/
|
||||
public function insert($taxonomy) {
|
||||
|
||||
|
@ -185,22 +186,20 @@ class Taxonomies extends Repository {
|
|||
}
|
||||
|
||||
public function update($object, $new_values = null){
|
||||
$map = $this->get_map();
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach ($object as $key => $value) {
|
||||
if($key != 'ID') {
|
||||
$entity[$map[$key]['map']] = $value ;
|
||||
} elseif ($key == 'ID'){
|
||||
$entity[$key] = (int) $value;
|
||||
foreach ($new_values as $key => $value) {
|
||||
try {
|
||||
$set_ = 'set_' . $key;
|
||||
$object->$set_( $value );
|
||||
} catch (\Error $error){
|
||||
return $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
$updated_taxonomy = new Entities\Taxonomy(wp_update_post($entity));
|
||||
$updated_taxonomy->register_taxonomy();
|
||||
if($object->validate()){
|
||||
return $this->insert($object);
|
||||
}
|
||||
|
||||
return $updated_taxonomy;
|
||||
return $object->get_errors();
|
||||
}
|
||||
|
||||
public function delete($args){
|
||||
|
|
|
@ -61,6 +61,10 @@ class Terms extends Repository {
|
|||
'on_error' => __('The user is empty or invalid', 'tainacan'),
|
||||
'validation' => v::numeric()->positive(),
|
||||
],
|
||||
'hide_empty' => [
|
||||
'map' => 'hide_empty',
|
||||
'type' => 'bool'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -156,20 +160,23 @@ class Terms extends Repository {
|
|||
}
|
||||
|
||||
public function update($object, $new_values = null){
|
||||
$map = $this->get_map();
|
||||
$tax_name = $object[1];
|
||||
$object = $object[0];
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach ($object[0] as $key => $value) {
|
||||
if($key != 'ID') {
|
||||
$entity[$map[$key]['map']] = $value ;
|
||||
foreach ($new_values as $key => $value) {
|
||||
try {
|
||||
$set_ = 'set_' . $key;
|
||||
$object->$set_( $value );
|
||||
} catch (\Error $error){
|
||||
return $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
$term_tax_ids = wp_update_term($object[1]['term_id'], $object[1]['tax_name'], $entity);
|
||||
$term_id = (int) $term_tax_ids['term_id'];
|
||||
if($object->validate()){
|
||||
return new Entities\Term($this->insert($object), $tax_name);
|
||||
}
|
||||
|
||||
return new Entities\Term($term_id, $object[1]['tax_name']);
|
||||
return $object->get_errors();
|
||||
}
|
||||
|
||||
public function delete($args){
|
||||
|
|
|
@ -48,4 +48,12 @@ trait Entity_Collections_Relation {
|
|||
$this->set_collections_ids($collections_ids);
|
||||
}
|
||||
|
||||
public function set_collection($new_collection_id){
|
||||
$this->collections = $this->get_mapped_property('collections_ids');
|
||||
|
||||
$collections[] = $new_collection_id;
|
||||
|
||||
$this->set_collections_ids($collections);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
/**
|
||||
*
|
||||
* @group api
|
||||
*
|
||||
*/
|
||||
class TAINACAN_REST_Logs_Controller extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public function test_get_logs(){
|
||||
$this->tainacan_entity_factory->create_entity(
|
||||
'log',
|
||||
[
|
||||
'title' => 'Log 1',
|
||||
'description' => 'Log number 1',
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$this->tainacan_entity_factory->create_entity(
|
||||
'log',
|
||||
[
|
||||
'title' => 'Log 2',
|
||||
'description' => 'Log number 2',
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$request = new \WP_REST_Request(
|
||||
'GET', $this->namespace . '/logs'
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals('Log 1', $data[1]['title']);
|
||||
$this->assertEquals('Log 2', $data[0]['title']);
|
||||
}
|
||||
|
||||
public function test_get_a_log(){
|
||||
$log = $this->tainacan_entity_factory->create_entity(
|
||||
'log',
|
||||
[
|
||||
'title' => 'Log',
|
||||
'description' => 'A description',
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$request = new \WP_REST_Request(
|
||||
'GET', $this->namespace . '/logs/' . $log->get_id()
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals('Log', $data['title']);
|
||||
$this->assertEquals($log->get_id(), $data['id']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -149,7 +149,9 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
|
|||
);
|
||||
|
||||
$show_empty = json_encode([
|
||||
'filters' => [
|
||||
'hide_empty' => false
|
||||
]
|
||||
]);
|
||||
|
||||
$request = new \WP_REST_Request(
|
||||
|
|
Loading…
Reference in New Issue