From 75e24112659a7d78e0e22d79f937aeff21e5f933 Mon Sep 17 00:00:00 2001 From: weryques Date: Wed, 17 Oct 2018 15:20:51 -0300 Subject: [PATCH] Refactoring some code in rest facets and metatada repository --- .../class-tainacan-rest-facets-controller.php | 52 ++++++++----------- .../repositories/class-tainacan-metadata.php | 4 +- .../class-tainacan-repository.php | 5 +- tests/test-api-metadata.php | 15 ++---- 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-facets-controller.php b/src/api/endpoints/class-tainacan-rest-facets-controller.php index d06173060..8ad06ad10 100644 --- a/src/api/endpoints/class-tainacan-rest-facets-controller.php +++ b/src/api/endpoints/class-tainacan-rest-facets-controller.php @@ -50,7 +50,7 @@ class REST_Facets_Controller extends REST_Controller { register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[\d]+)', array( array( 'methods' => \WP_REST_Server::READABLE, - 'callback' => array($this, 'get_item'), + 'callback' => array($this, 'get_items'), 'permission_callback' => array($this, 'get_items_permissions_check') ) )); @@ -58,7 +58,7 @@ class REST_Facets_Controller extends REST_Controller { register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( array( 'methods' => \WP_REST_Server::READABLE, - 'callback' => array($this, 'get_item'), + 'callback' => array($this, 'get_items'), 'permission_callback' => array($this, 'get_items_permissions_check') ) )); @@ -69,7 +69,7 @@ class REST_Facets_Controller extends REST_Controller { * * @return \WP_Error|\WP_REST_Response */ - public function get_item( $request ) { + public function get_items( $request ) { $metadatum_id = $request['metadatum_id']; $metadatum = $this->metadatum_repository->fetch($metadatum_id); @@ -249,22 +249,18 @@ class REST_Facets_Controller extends REST_Controller { $realResponse = []; foreach( $selected as $index => $value ){ - - $row = ['mvalue' => $value, 'metadatum_id' => $metadatum_id ]; + $row = (object) ['mvalue' => $value, 'metadatum_id' => $metadatum_id ]; $realResponse[] = $row; - } foreach( $rawValues as $index => $row0 ){ - if( in_array($row0, $selected) ){ - continue; - } + if( !in_array($row0, $selected) ){ + $realResponse[] = (object) ['mvalue' => $row0, 'metadatum_id' => $metadatum_id]; - $realResponse[] = ['mvalue' => $row0, 'metadatum_id' => $metadatum_id]; - - if( isset($request['number']) && count($realResponse) >= $request['number']){ - break; + if( isset($request['number']) && count($realResponse) >= $request['number']){ + break; + } } } @@ -300,8 +296,7 @@ class REST_Facets_Controller extends REST_Controller { foreach ( $response as $key => $item ) { if( $type === 'Tainacan\Metadata_Types\Taxonomy' ){ - - $row = [ + $result[] = [ 'label' => $item['name'], 'value' => $item['id'], 'img' => ( isset($item['header_image']) ) ? $item['header_image'] : false , @@ -311,10 +306,8 @@ class REST_Facets_Controller extends REST_Controller { 'taxonomy_id' => $this->taxonomy->WP_Post->ID, 'taxonomy' => ( isset($item['taxonomy']) ) ? $item['taxonomy'] : false, ]; - } else if( $type === 'Tainacan\Metadata_Types\Relationship' ){ - - $row = [ + $result[] = [ 'label' => $item['title'], 'value' => $item['id'], 'img' => ( isset($item['thumbnail']['thumb']) ) ? $item['thumbnail']['thumb'] : false, @@ -322,21 +315,16 @@ class REST_Facets_Controller extends REST_Controller { 'total_children' => 0, 'type' => 'Relationship' ]; - } else { - - $row = [ - 'label' => $item['mvalue'], - 'value' => $item['mvalue'], + $result[] = [ + 'label' => $item->mvalue, + 'value' => $item->mvalue, 'img' => false, 'parent' => false, 'total_children' => 0, 'type' => 'Text' ]; - } - - $result[] = $row; } } @@ -355,7 +343,7 @@ class REST_Facets_Controller extends REST_Controller { if ( $offset !== '' && $number) { $per_page = (int) $number; - $page = ceil( ( ( (int) $offset ) / $per_page ) + 1 ); + //$page = ceil( ( ( (int) $offset ) / $per_page ) + 1 ); $this->total_items = count( $response ); @@ -382,7 +370,7 @@ class REST_Facets_Controller extends REST_Controller { if(isset($args['number'], $args['offset'])){ $number = $args['number']; - $offset = $args['offset']; + //$offset = $args['offset']; unset( $args['number'], $args['offset'] ); $total_terms = wp_count_terms( $this->taxonomy->get_db_identifier(), $args ); @@ -392,7 +380,7 @@ class REST_Facets_Controller extends REST_Controller { } $per_page = (int) $number; - $page = ceil( ( ( (int) $offset ) / $per_page ) + 1 ); + //$page = ceil( ( ( (int) $offset ) / $per_page ) + 1 ); $this->total_items = (int) $total_terms ; @@ -494,13 +482,15 @@ class REST_Facets_Controller extends REST_Controller { } /** - * + * @param $rows + * + * @return array */ private function get_values( $rows ){ $values = []; foreach( $rows as $row ){ - $values[] = $row['mvalue']; + $values[] = $row->mvalue; } return $values; diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index 7c06919c3..542e47115 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -996,7 +996,7 @@ class Metadata extends Repository { ); } - $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); + $pre_result = $wpdb->get_results( $sql_string, OBJECT ); if ( ! empty( $pre_result ) ) { foreach ( $pre_result as $pre ) { @@ -1045,7 +1045,7 @@ class Metadata extends Repository { ); } - $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); + $pre_result = $wpdb->get_results( $sql_string, OBJECT ); if ( ! empty( $pre_result ) ) { foreach ( $pre_result as $pre ) { diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 4a01d1f8b..c72601f4b 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -753,10 +753,11 @@ abstract class Repository { $key_array = array(); foreach ( $array as $val ) { - if ( ! in_array( $val[ $key ], $key_array ) ) { - $key_array[ $i ] = $val[ $key ]; + if ( ! in_array( $val->$key, $key_array ) ) { + $key_array[ $i ] = $val->$key; $temp_array[ $i ] = $val; } + $i ++; } diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 47934d1f5..1420c6f3b 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -483,15 +483,10 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { //======================= - $query = [ - 'fetch' => 'all_metadatum_values' - ]; - $request = new \WP_REST_Request( 'GET', - $this->namespace . '/collection/' . $collection->get_id() . '/metadata/' . $metadatum->get_id() + $this->namespace . '/collection/' . $collection->get_id() . '/facets/' . $metadatum->get_id() ); - $request->set_query_params($query); //======================= @@ -503,7 +498,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $data1 = $response1->get_data(); $this->assertCount(1, $data1); - $this->assertEquals('12/12/2017', $data1[0]['mvalue']); + $this->assertEquals('12/12/2017', $data1[0]['value']); //======================= @@ -515,7 +510,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $data1 = $response1->get_data(); $this->assertCount(1, $data1); - $this->assertEquals('12/12/2017', $data1[0]['mvalue']); + $this->assertEquals('12/12/2017', $data1[0]['value']); //======================= @@ -528,8 +523,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { // Only two without duplicates $this->assertCount(2, $data2); - $this->assertEquals('12/12/2017', $data2[0]['mvalue']); - $this->assertEquals('02/03/2018', $data2[1]['mvalue']); + $this->assertEquals('12/12/2017', $data2[0]['value']); + $this->assertEquals('02/03/2018', $data2[1]['value']); } }