From 170d1033bdc07655049e81f48f77675febcaa730 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Tue, 18 May 2021 10:45:42 -0300 Subject: [PATCH 1/2] fix: bulk edit for single values of compound metadata --- ...lass-tainacan-rest-bulkedit-controller.php | 3 ++- .../class-tainacan-bulk-edit-process.php | 22 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-bulkedit-controller.php b/src/classes/api/endpoints/class-tainacan-rest-bulkedit-controller.php index c1ad30ec9..40ca5129f 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-bulkedit-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-bulkedit-controller.php @@ -382,8 +382,9 @@ class REST_Bulkedit_Controller extends REST_Controller { global $Tainacan_Generic_Process_Handler; $process = $Tainacan_Generic_Process_Handler->get_process_instance_by_session_id($bulk_id); if ($process !== false) { + $bulk_edit_value = isset($body['new_value']) ? $body['new_value'] : (isset($body['value']) ? $body['value'] : null); $bulk_edit_data = [ - "value" => isset($body['new_value']) ? $body['new_value'] : $body['value'], + "value" => $bulk_edit_value, "method" => $method, "old_value" => isset($body['old_value']) ? $body['old_value'] : null, "metadatum_id" => isset($body['metadatum_id']) ? $body['metadatum_id'] : null, diff --git a/src/classes/generic-background-process/class-tainacan-bulk-edit-process.php b/src/classes/generic-background-process/class-tainacan-bulk-edit-process.php index 62eb39b8e..28554df36 100644 --- a/src/classes/generic-background-process/class-tainacan-bulk-edit-process.php +++ b/src/classes/generic-background-process/class-tainacan-bulk-edit-process.php @@ -278,7 +278,8 @@ class Bulk_Edit_Process extends Generic_Process { private function clear_value(\Tainacan\Entities\Item $item) { $metadatum = $this->metadatum_repository->fetch($this->bulk_edit_data['metadatum_id']); - $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum ); + $parent_meta_id = $this->get_parent_meta_id($item, $metadatum); + $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum, null, $parent_meta_id ); $item_metadata->set_value(""); return $this->save_item_metadata($item_metadata, $item); } @@ -286,8 +287,9 @@ class Bulk_Edit_Process extends Generic_Process { private function set_value(\Tainacan\Entities\Item $item) { $metadatum = $this->metadatum_repository->fetch($this->bulk_edit_data['metadatum_id']); $value = $this->bulk_edit_data['value']; + $parent_meta_id = $this->get_parent_meta_id($item, $metadatum); - $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum ); + $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum, null, $parent_meta_id ); if($item_metadata->is_multiple()) { $value = is_array( $value ) ? $value : [$value]; @@ -300,6 +302,22 @@ class Bulk_Edit_Process extends Generic_Process { } + private function get_parent_meta_id($item, $metadatum) { + $metadatum_parent_id = $metadatum->get_parent(); + if ($metadatum_parent_id > 0) { + $metadatum_parent = $this->metadatum_repository->fetch($metadatum_parent_id); + $compoundItem = new Entities\Item_Metadata_Entity($item, $metadatum_parent); + $unique = !$compoundItem->is_multiple(); + $compoundValue = $compoundItem->get_value(); + if ( $unique && !empty($compoundValue) ) { + $key = array_keys($compoundValue)[0]; // get the first metadata ID, if the argument metadata does not exist + $parent_meta_id = $compoundValue[$key]->get_parent_meta_id(); + return $parent_meta_id; + } // elseif ((is_array($compoundValue) && sizeof($compoundValue) > 0)) + } + return null; + } + private function add_value(\Tainacan\Entities\Item $item) { $metadatum_id = $this->bulk_edit_data['metadatum_id']; $metadatum = $this->metadatum_repository->fetch($metadatum_id); From 42f9b85105df316e6bf98f6c329ae945e7fb496e Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Tue, 18 May 2021 17:04:13 -0300 Subject: [PATCH 2/2] feat: add counters `restrict` and `not_restrict` on reports #483 --- ...class-tainacan-rest-reports-controller.php | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php index f18694972..d9877ae0f 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php @@ -311,7 +311,9 @@ class REST_Reports_Controller extends REST_Controller { 'trash' => 0, 'draft' => 0, 'publish' => 0, - 'private' => 0 + 'private' => 0, + 'restrict' => 0, + 'not_restrict' => 0 ) ) ); @@ -339,7 +341,7 @@ class REST_Reports_Controller extends REST_Controller { $cached_object = $this->get_cache_object($key_cache_object, $request); if($cached_object !== false ) return new \WP_REST_Response($cached_object, 200); - $collections = $this->collections_repository->fetch([]); + $collections = $this->collections_repository->fetch(['status'=> ['publish', 'private', 'trash']]); $response['totals']['collections'] = array( 'total' => 0, 'trash' => 0, @@ -354,12 +356,20 @@ class REST_Reports_Controller extends REST_Controller { $response['totals']['collections']['total']++; $total_items = wp_count_posts( $collection->get_db_identifier(), 'readable' ); - if (isset($total_items->publish) || isset($total_items->private) || - isset($total_items->trash) || isset($total_items->draft)) { - $response['totals']['items']['trash'] += $total_items->trash; - $response['totals']['items']['draft'] += $total_items->draft; - $response['totals']['items']['publish'] += $total_items->publish; - $response['totals']['items']['private'] += $total_items->private; + $response['totals']['items']['trash'] += isset($total_items->trash) ? intval($total_items->trash) : 0; + $response['totals']['items']['draft'] += isset($total_items->draft) ? intval($total_items->draft) : 0; + $response['totals']['items']['publish'] += isset($total_items->publish)? intval($total_items->publish) : 0; + $response['totals']['items']['private'] += isset($total_items->private)? intval($total_items->private) : 0; + + if ( \is_post_status_viewable( $collection->get_status() ) === true ) { + $response['totals']['items']['not_restrict'] += isset($total_items->publish) ? intval($total_items->publish) : 0; + } else { + $response['totals']['items']['restrict'] += ( + (isset($total_items->trash) ? intval($total_items->trash) : 0) + + (isset($total_items->draft) ? intval($total_items->draft) : 0) + + (isset($total_items->publish) ? intval($total_items->publish) : 0) + + (isset($total_items->private) ? intval($total_items->private) : 0) + ); } } wp_reset_postdata(); @@ -376,19 +386,13 @@ class REST_Reports_Controller extends REST_Controller { ); $total_taxonomies = wp_count_posts( 'tainacan-taxonomy', 'readable' ); - if (isset($total_taxonomies->publish) || - isset($total_taxonomies->private) || - isset($total_taxonomies->trash) || - isset($total_taxonomies->draft)) { - - $response['totals']['taxonomies']['trash'] = intval($total_taxonomies->trash); - $response['totals']['taxonomies']['publish'] = intval($total_taxonomies->publish); - $response['totals']['taxonomies']['draft'] = intval($total_taxonomies->draft); - $response['totals']['taxonomies']['private'] = intval($total_taxonomies->private); - $response['totals']['taxonomies']['total'] = $response['totals']['taxonomies']['trash'] + $response['totals']['taxonomies']['publish'] + $response['totals']['taxonomies']['draft'] + $response['totals']['taxonomies']['private']; - $response['totals']['taxonomies']['used'] = $this->query_count_used_taxononomies(); - $response['totals']['taxonomies']['not_used'] = $response['totals']['taxonomies']['total'] - $response['totals']['taxonomies']['used']; - } + $response['totals']['taxonomies']['trash'] = isset($total_taxonomies->trash) ? intval($total_taxonomies->trash) : 0; + $response['totals']['taxonomies']['draft'] = isset($total_taxonomies->draft) ? intval($total_taxonomies->draft) : 0; + $response['totals']['taxonomies']['publish'] = isset($total_taxonomies->publish)? intval($total_taxonomies->publish) : 0; + $response['totals']['taxonomies']['private'] = isset($total_taxonomies->private)? intval($total_taxonomies->private) : 0; + $response['totals']['taxonomies']['total'] = $response['totals']['taxonomies']['trash'] + $response['totals']['taxonomies']['publish'] + $response['totals']['taxonomies']['draft'] + $response['totals']['taxonomies']['private']; + $response['totals']['taxonomies']['used'] = $this->query_count_used_taxononomies(); + $response['totals']['taxonomies']['not_used'] = $response['totals']['taxonomies']['total'] - $response['totals']['taxonomies']['used']; } $response['totals']['items']['total'] = ($response['totals']['items']['trash'] + $response['totals']['items']['draft'] + $response['totals']['items']['publish'] + $response['totals']['items']['private']); $this->set_cache_object($key_cache_object, $response);