From d9416118dae4b157cf0bf3f2ab4da585e615611a Mon Sep 17 00:00:00 2001 From: weryques Date: Wed, 28 Feb 2018 15:20:14 -0300 Subject: [PATCH] Revert some features --- src/api/class-tainacan-rest-controller.php | 102 +++++++++--------- ...s-tainacan-rest-collections-controller.php | 5 +- ...s-tainacan-rest-field-types-controller.php | 3 - .../class-tainacan-rest-items-controller.php | 32 +----- .../class-tainacan-rest-logs-controller.php | 6 +- .../repositories/class-tainacan-items.php | 19 +--- .../class-tainacan-repository.php | 18 ---- tests/test-api-queries.php | 7 +- 8 files changed, 62 insertions(+), 130 deletions(-) diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index f380b64e0..6790842e7 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -22,57 +22,57 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { return $object; } - /** - * @param $entity - * - * @param $map - * @param $context - * - * @return array - */ - protected function get_only_needed_attributes($entity, $map, $context = null){ - - $entity_prepared = [ - 'id' => $entity->get_id(), - 'description' => $entity->get_description(), - ]; - - if($context === 'edit'){ - $entity_prepared['current_user_can_edit'] = $entity->can_edit(); - } - - 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)){ - $entity_prepared['title'] = $entity->get_title(); - } - - if(array_key_exists('featured_image', $map)){ - $entity_prepared['featured_image'] = $entity->get_featured_img(); - } - - if(array_key_exists('columns', $map)){ - $entity_prepared['columns'] = $entity->get_columns(); - } - - if(array_key_exists('status', $map)){ - $entity_prepared['status'] = $entity->get_status(); - } - - return $entity_prepared; - } +// /** +// * @param $entity +// * +// * @param $map +// * @param $context +// * +// * @return array +// */ +// protected function get_only_needed_attributes($entity, $map, $context = null){ +// +// $entity_prepared = [ +// 'id' => $entity->get_id(), +// 'description' => $entity->get_description(), +// ]; +// +// if($context === 'edit'){ +// $entity_prepared['current_user_can_edit'] = $entity->can_edit(); +// } +// +// 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)){ +// $entity_prepared['title'] = $entity->get_title(); +// } +// +// if(array_key_exists('featured_image', $map)){ +// $entity_prepared['featured_image'] = $entity->get_featured_img(); +// } +// +// if(array_key_exists('columns', $map)){ +// $entity_prepared['columns'] = $entity->get_columns(); +// } +// +// if(array_key_exists('status', $map)){ +// $entity_prepared['status'] = $entity->get_status(); +// } +// +// return $entity_prepared; +// } /** * @param $request diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 24fe406ce..afd4f1142 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -82,9 +82,6 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { $collections = $this->collections_repository->fetch($args); - $map = $this->collections_repository->get_map(); - - $response = []; if($collections->have_posts()){ while ($collections->have_posts()){ @@ -92,7 +89,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { $collection = new Entities\Collection($collections->post); - array_push($response, $this->get_only_needed_attributes($collection, $map, $request['context'])); + array_push($response, $this->prepare_item_for_response($collection, $request)); } wp_reset_postdata(); diff --git a/src/api/endpoints/class-tainacan-rest-field-types-controller.php b/src/api/endpoints/class-tainacan-rest-field-types-controller.php index e46b7f479..1f585675f 100644 --- a/src/api/endpoints/class-tainacan-rest-field-types-controller.php +++ b/src/api/endpoints/class-tainacan-rest-field-types-controller.php @@ -1,9 +1,6 @@ get_terms(); - - foreach ($item_terms as $index => $term){ - $term_id = $term['term_id']; - - $item_array['terms'][$term_id]['name'] = $term['name']; - $item_array['terms'][$term_id]['description'] = $term['description']; - $item_array['terms'][$term_id]['taxonomy'] = $term['taxonomy']; - } - - return $item_array; - } - /** * @param $item_object * @param $item_array @@ -134,8 +113,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $item_arr['current_user_can_edit'] = $item->can_edit(); } - $prep = $this->add_metadata_to_item($item, $item_arr); - return $this->add_terms_to_item($item, $prep); + return $this->add_metadata_to_item($item, $item_arr); } return $item; @@ -167,8 +145,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $collection_id = $request['collection_id']; $items = $this->items_repository->fetch($args, $collection_id, 'WP_Query'); - $map = $this->items_repository->get_map(); - $response = []; if ($items->have_posts()) { while ( $items->have_posts() ) { @@ -176,11 +152,9 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $item = new Entities\Item($items->post); - $limited_item = $this->get_only_needed_attributes($item, $map, $request['context']); - $limited_item = $this->add_metadata_to_item($item, $limited_item); - $limited_item = $this->add_terms_to_item($item, $limited_item); + $prepared_item = $this->prepare_item_for_response($item, $request); - array_push($response, $limited_item); + array_push($response, $prepared_item); } wp_reset_postdata(); diff --git a/src/api/endpoints/class-tainacan-rest-logs-controller.php b/src/api/endpoints/class-tainacan-rest-logs-controller.php index cb153d3c1..f306f7566 100644 --- a/src/api/endpoints/class-tainacan-rest-logs-controller.php +++ b/src/api/endpoints/class-tainacan-rest-logs-controller.php @@ -68,16 +68,14 @@ class TAINACAN_REST_Logs_Controller extends TAINACAN_REST_Controller { $logs = $this->logs_repository->fetch($args); - $map = $this->logs_repository->get_map(); - $response = []; if($logs->have_posts()){ while ($logs->have_posts()){ $logs->the_post(); - $collection = new Entities\Log($logs->post); + $log = new Entities\Log($logs->post); - array_push($response, $this->get_only_needed_attributes($collection, $map)); + array_push($response, $this->prepare_item_for_response($log, $request)); } wp_reset_postdata(); diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index f527fc59f..a111ffbe3 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -118,8 +118,6 @@ class Items extends Repository { public function insert($item) { - global $Tainacan_Fields; - $map = $this->get_map(); // get collection to determine post type @@ -159,22 +157,7 @@ class Items extends Repository { add_post_meta($id, $prop, wp_slash( $value )); } } - } elseif($mapped['map'] == 'terms'){ - $values = $item->get_mapped_property($prop); - - if($values) { - $res = []; - - foreach ($values as $value){ - $taxonomy = get_term($value)->taxonomy; - $res[] = wp_set_post_terms( $item->WP_Post->ID, $value, $taxonomy ); - } - - if ( ! is_array( $res ) ) { - throw new \InvalidArgumentException( 'The id of post or taxonomy name or term name may be invalid. Response = ' . $res ); - } - } - } + } } do_action('tainacan-insert', $item); diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 70b80cfa2..eea697ad7 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -314,24 +314,6 @@ abstract class Repository { $property = $attachments_prepared; } - } elseif ($mapped === 'terms'){ - $taxonomies = get_taxonomies('', 'names'); - - $terms_prepared = []; - foreach($taxonomies as $taxonomy){ - if(stristr($taxonomy, 'tnc_tax_')) { - $terms = isset( $entity->WP_Post->ID ) ? wp_get_post_terms( $entity->WP_Post->ID, $taxonomy ) : null; - - if ( is_array( $terms ) ) { - foreach ( $terms as $term ) { - $term = new Entities\Term( $term->term_id, $term->taxonomy ); - array_push( $terms_prepared, $term->__toArray() ); - } - } - } - } - - $property = $terms_prepared; } else { $property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null; } diff --git a/tests/test-api-queries.php b/tests/test-api-queries.php index c8ebf41bc..d11de41ba 100644 --- a/tests/test-api-queries.php +++ b/tests/test-api-queries.php @@ -3,7 +3,7 @@ namespace Tainacan\Tests; /** - * @group queries + * @group api * **/ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { @@ -78,7 +78,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { 'description' => 'Item in collection A', 'status' => 'publish', 'collection' => $collectionA, - 'terms' => [$termA] ], true ); @@ -90,7 +89,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { 'description' => 'Item in collection A', 'status' => 'private', 'collection' => $collectionA, - 'terms' => [$termA] ], true ); @@ -285,6 +283,9 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { ] ]; + wp_set_post_terms($itemA1->get_id(), $termA, $taxonomyA->get_db_identifier()); + wp_set_post_terms($itemA2->get_id(), $termA, $taxonomyA->get_db_identifier()); + $tax_query_request_collections = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items'); $tax_query_request_collections->set_query_params($tax_query);