Merge branch 'develop' of github.com:tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2022-07-29 11:40:11 -03:00
commit d17fcb7532
5 changed files with 78 additions and 18 deletions

View File

@ -390,14 +390,14 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
public function delete_item( $request ) { public function delete_item( $request ) {
$metadata_section_id = $request['metadata_section_id']; $metadata_section_id = $request['metadata_section_id'];
$metadatum_section = $this->metadata_sections_repository->fetch($metadata_section_id); $metadatum_section = $this->metadata_sections_repository->fetch($metadata_section_id);
if (! $metadatum_section instanceof Entities\Metadatum) { if (! $metadatum_section instanceof Entities\Metadata_Section) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'), 'error_message' => __('Metadata section with this ID was not found', 'tainacan'),
'item_id' => $metadata_section_id 'item_id' => $metadata_section_id
], 400); ], 400);
} }
$metadatum_section_trashed = $this->metadata_sections_repository->trash($metadatum_section); $metadatum_section_trashed = $this->metadata_sections_repository->delete($metadatum_section);
$prepared = $this->prepare_item_for_response($metadatum_section_trashed, $request); $prepared = $this->prepare_item_for_response($metadatum_section_trashed, $request);
return new \WP_REST_Response($prepared, 200); return new \WP_REST_Response($prepared, 200);
} }

View File

@ -186,21 +186,21 @@ class Collections extends Repository {
'title' => __( 'Thumbnail', 'tainacan' ), 'title' => __( 'Thumbnail', 'tainacan' ),
'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' ) 'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' )
], ],
'comment_status' => [ 'comment_status' => [
'map' => 'comment_status', 'map' => 'comment_status',
'title' => __( 'Comment Status', 'tainacan' ), 'title' => __( 'Comment Status', 'tainacan' ),
'type' => 'string', 'type' => 'string',
'description' => __( 'Collection comment status: "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ), 'description' => __( 'Collection comment status: "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ),
'default' => get_default_comment_status(Entities\Collection::get_post_type()), 'default' => get_default_comment_status(Entities\Collection::get_post_type()),
'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )), 'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )),
], ],
'allow_comments' => [ 'allow_comments' => [
'map' => 'meta', 'map' => 'meta',
'title' => __( 'Allow enabling comments on items', 'tainacan' ), 'title' => __( 'Allow enabling comments on items', 'tainacan' ),
'type' => 'string', 'type' => 'string',
'description' => __( 'If this option is enabled, items of this collection can be set to enable a comments section on their page. "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ), 'description' => __( 'If this option is enabled, items of this collection can be set to enable a comments section on their page. "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ),
'default' => 'closed', 'default' => 'closed',
'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )), 'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )),
], ],
'submission_anonymous_user' => [ 'submission_anonymous_user' => [
'map' => 'meta', 'map' => 'meta',
@ -311,7 +311,7 @@ class Collections extends Repository {
* @see \Tainacan\Repositories\Repository::insert() * @see \Tainacan\Repositories\Repository::insert()
*/ */
public function insert( $collection ) { public function insert( $collection ) {
$this->pre_process( $collection ); $this->pre_process( $collection );
$this->handle_parent_order_clone( $collection ); $this->handle_parent_order_clone( $collection );
$new_collection = parent::insert( $collection ); $new_collection = parent::insert( $collection );

View File

@ -42,6 +42,7 @@ class Metadata extends Repository {
add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']); add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']);
add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_not_allow_insert_new_terms']); add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_not_allow_insert_new_terms']);
add_action('tainacan-insert-tainacan-metadatum', [$this, 'hook_metadata_update_order']);
} }
@ -1734,4 +1735,38 @@ class Metadata extends Repository {
return false; return false;
} }
/**
* When a metadata is saved, if the metadata section changes, the ordering needs to be updated
*
* @param \Tainacan\Entities\Metadatum $metadata
* @return void
*/
public function hook_metadata_update_order($metadata) {
$tainacan_metadata_sections_repository = \tainacan_metadata_sections();
$tainacan_collections_repository = \tainacan_collections();
$metadata_section_id = $metadata->get_metadata_section_id();
$metadata_section = $tainacan_metadata_sections_repository->fetch($metadata_section_id);
if ( $metadata_section instanceof \Tainacan\Entities\Metadata_Section ) {
$collection = $metadata_section->get_collection();
$metadata_sections_order = $collection->get_metadata_section_order();
if( empty($metadata_sections_order) ) {
return;
}
foreach( $metadata_sections_order as &$metadata_section_order ) {
$pos = array_search($metadata->get_id(), array_column($metadata_section_order['metadata_order'], 'id'));
if($pos !== false) {
if( $metadata_section_id != $metadata_section_order['id']) {
array_splice($metadata_section_order['metadata_order'], $pos, 1);
}
} else if($metadata_section_id == $metadata_section_order['id']) {
$metadata_section_order['metadata_order'][] = ["id" => $metadata->get_id(), "enabled" => $metadata->get_enabled_for_collection()];
}
}
$collection->set_metadata_section_order($metadata_sections_order);
if($collection->validate()) {
$tainacan_collections_repository->update($collection);
}
}
}
} }

View File

@ -71,3 +71,12 @@ function tainacan_terms() {
function tainacan_roles() { function tainacan_roles() {
return \Tainacan\Roles::get_instance(); return \Tainacan\Roles::get_instance();
} }
/**
* Retrieve the singleton Metadata Repository instance
* @return \Tainacan\Repositories\Metadata The Tainacan Metadata Repository
*/
function tainacan_metadata_sections() {
return \Tainacan\Repositories\Metadata_Sections::get_instance();
}

View File

@ -319,6 +319,22 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$this->assertFalse($metadata_ordinate_enabled[1]->get_enabled_for_collection()); $this->assertFalse($metadata_ordinate_enabled[1]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection()); $this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection());
//changing the metadata section of the metadata without changing the ordering
$metadatum2->set_metadata_section_id($metadata_section_a->get_id());
$this->assertTrue($metadatum2->validate(), json_encode($metadatum2->get_errors()));
$Tainacan_Metadata->update($metadatum2);
$metadatum_c->set_metadata_section_id($metadata_section_1->get_id());
$this->assertTrue($metadatum_c->validate(), json_encode($metadatum_c->get_errors()));
$Tainacan_Metadata->update($metadatum_c);
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 8, count($metadata_ordinate_enabled) );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() );
$this->assertEquals( 'metadatum_b', $metadata_ordinate_enabled[3]->get_name() );
$this->assertEquals( 'metadatum2', $metadata_ordinate_enabled[5]->get_name() );
$this->assertEquals( 'metadatum_c', $metadata_ordinate_enabled[2]->get_name() );
} }
} }