diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index cdcc1e45f..d2f9546a7 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -282,7 +282,7 @@ class Fields extends Repository { * fetch field by collection, searches all field available * * @param Entities\Collection $collection - * @param array $args + * @param array $args WP_Query args plus disabled_fields * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) * * @return Array Entities\Field @@ -314,7 +314,7 @@ class Fields extends Repository { $args['meta_query'] = array( $meta_query ); } - return $this->order_result( $this->fetch( $args, $output ), $collection); + return $this->order_result( $this->fetch( $args, $output ), $collection, isset( $args['disabled_fields'] ) ? $args['disabled_fields'] : false ); } /** @@ -322,9 +322,10 @@ class Fields extends Repository { * * @param $result Response from method fetch * @param Entities\Collection $collection + * @param bool $disabled_fields Disabled fields wont appear on list * @return array or WP_Query ordinatte */ - public function order_result( $result, Entities\Collection $collection ){ + public function order_result( $result, Entities\Collection $collection, $disabled_fields = false ){ $order = $collection->get_fields_order(); if($order) { $order = ( is_array($order) ) ? $order : unserialize($order); @@ -335,9 +336,15 @@ class Fields extends Repository { foreach ( $result as $item ) { $id = $item->WP_Post->ID; - $index = array_search ( $id , $order); + $index = array_search ( $id , array_column( $order , 'id') ); + + if( $index !== false ) { + + // skipping fields disabled if the arg is set + if( $disabled_fields && !$order[$index]['enable'] ){ + continue; + } - if( $index !== false ){ $result_ordinate[$index] = $item; } else { $not_ordinate[] = $item; @@ -357,7 +364,7 @@ class Fields extends Repository { foreach ( $posts as $item ) { $id = $item->ID; - $index = array_search ( $id , $order); + $index = array_search ( $id , array_column( $order , 'id') ); if( $index !== false ){ $result_ordinate[$index] = $item; diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index e80130f99..d3664b2a2 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -98,9 +98,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $data = $response->get_data(); - $field = $data[0]; + $fields_names = array_map(function($field) {return $field['name'];}, $data); - $this->assertEquals('Data', $field['name']); + $this->assertContains('Data', $fields_names); ################### Get field of item with value ####################### diff --git a/tests/test-fields.php b/tests/test-fields.php index 915d77887..788b691d7 100644 --- a/tests/test-fields.php +++ b/tests/test-fields.php @@ -208,6 +208,75 @@ class Fields extends TAINACAN_UnitTestCase { $class = new RandomType; $this->assertEquals( 9, sizeof( $Tainacan_Fields->fetch_field_types() ) ); } + + /** + * + */ + function test_ordenation_fields(){ + global $Tainacan_Collections, $Tainacan_Fields; + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'teste' + ), + true + ); + + $type = $this->tainacan_field_factory->create_field('text'); + + $field1 = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'field1', + 'description' => 'descricao', + 'collection' => $collection, + 'field_type' => $type, + 'status' => 'publish' + ), + true + ); + + $field2 = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'field2', + 'description' => 'field2', + 'collection' => $collection, + 'field_type' => $type, + 'status' => 'publish' + ), + true + ); + + + $field3 = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'field3', + 'description' => 'field3', + 'collection' => $collection, + 'field_type' => $type, + 'status' => 'publish' + ), + true + ); + + $collection->set_fields_order( + [ + array( 'id' => $field3->get_id(), 'enable' => false ), + array( 'id' => $field2->get_id(), 'enable' => true ), + array( 'id' => $field1->get_id(), 'enable' => true ) + ]); + + $update_collection = $Tainacan_Collections->update( $collection ); + + $fields_ordinate = $Tainacan_Fields->fetch_by_collection( $update_collection, [], 'OBJECT' ); + $this->assertEquals( 'field3', $fields_ordinate[0]->get_name() ); + + $fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' ); + $this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() ); + } } /**