From 0acbdcd780bdedf03796dd78d9dab8b127402ac7 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Oct 2021 11:04:22 -0300 Subject: [PATCH 1/4] feat: add function `parse_relationship_metaquery` #624 --- .../repositories/class-tainacan-items.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 97de52af4..b09b2f20a 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -347,6 +347,10 @@ class Items extends Repository { $args['orderby'] = $new_order; } + if ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') || true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) { + $args = $this->parse_relationship_metaquery($args); + } + $args = apply_filters( 'tainacan_fetch_args', $args, 'items' ); $should_filter = is_user_logged_in() && ! isset($args['post_status']) && sizeof($cpt) > 1; @@ -698,4 +702,40 @@ class Items extends Repository { return $response; } + private function parse_relationship_metaquery ($args) { + if( isset($args['meta_query']) ) { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + foreach($args['meta_query'] as $idx => $meta) { + $meta_id = $meta['key']; + $metadata = $Tainacan_Metadata->fetch($meta_id); + if($metadata->get_metadata_type() === 'Tainacan\\Metadata_Types\\Relationship') + { + $options = $metadata->get_metadata_type_options(); + if( isset($options) && isset($options['search']) ) { + $this->relationsip_metaquery = array( + 'meta_id' => $meta_id, + 'search_meta_id' => $options['search'], + 'search_meta_value' => $args['meta_query'][$idx]['value'] + ); + $args['meta_query'][$idx]['compare'] = '!='; + $args['meta_query'][$idx]['value'] = ''; + add_filter( 'posts_where' , array($this, 'posts_where_relationship_metaquery'), 10, 1 ); + return $args; + } + } + } + } + return $args; + } + + function posts_where_relationship_metaquery( $where ) { + $meta_id = $this->relationsip_metaquery['meta_id']; + $search_meta_id = $this->relationsip_metaquery['search_meta_id']; + $search_meta_value = $this->relationsip_metaquery['search_meta_value']; + $SQL_related_item = " SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_key=$search_meta_id AND meta_value LIKE '%$search_meta_value%'"; + $where .= " AND (wp_postmeta.meta_key = '$meta_id' AND wp_postmeta.meta_value IN ( $SQL_related_item ) ) "; + remove_filter( 'posts_where', array($this, 'posts_where_relationship_metaquery') ); + return $where; + } + } From 776fa3b32ddd65f4992f28b37af8a7178dbc9948 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Oct 2021 11:09:25 -0300 Subject: [PATCH 2/4] feat: adds param `tainacan_enable_relationship_metaquery` in js. --- src/views/class-tainacan-admin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/views/class-tainacan-admin.php b/src/views/class-tainacan-admin.php index 3ea2d2263..affb23986 100644 --- a/src/views/class-tainacan-admin.php +++ b/src/views/class-tainacan-admin.php @@ -307,7 +307,8 @@ class Admin { 'repository_name' => get_bloginfo('name'), 'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE, 'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(), - 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key") + 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key"), + 'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') || true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) ]; $maps = [ From 01c105e3083b706d624f6fca577e0906457a1809 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Oct 2021 11:15:16 -0300 Subject: [PATCH 3/4] fix: test condition to defined const --- src/classes/repositories/class-tainacan-items.php | 2 +- src/views/class-tainacan-admin.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index b09b2f20a..967048945 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -347,7 +347,7 @@ class Items extends Repository { $args['orderby'] = $new_order; } - if ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') || true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) { + if ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) { $args = $this->parse_relationship_metaquery($args); } diff --git a/src/views/class-tainacan-admin.php b/src/views/class-tainacan-admin.php index affb23986..2e758b051 100644 --- a/src/views/class-tainacan-admin.php +++ b/src/views/class-tainacan-admin.php @@ -308,7 +308,7 @@ class Admin { 'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE, 'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(), 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key"), - 'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') || true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) + 'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) ]; $maps = [ From 1c34c3d2d4e68e58a93a78e8b13c52dc656e8920 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Fri, 29 Oct 2021 11:38:12 -0300 Subject: [PATCH 4/4] Checks new constant do display relationship metadata as avialable for search #624 --- .../metadata-types/relationship/FormRelationship.vue | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/views/admin/components/metadata-types/relationship/FormRelationship.vue b/src/views/admin/components/metadata-types/relationship/FormRelationship.vue index 5808352bf..cd0c19853 100644 --- a/src/views/admin/components/metadata-types/relationship/FormRelationship.vue +++ b/src/views/admin/components/metadata-types/relationship/FormRelationship.vue @@ -131,7 +131,8 @@ modelSearch:'', collectionType: '', collectionMessage: '', - displayRelatedItemMetadata: [] + displayRelatedItemMetadata: [], + isMetaqueryRelationshipEnabled: tainacan_plugin && tainacan_plugin.tainacan_enable_relationship_metaquery == true ? tainacan_plugin.tainacan_enable_relationship_metaquery : false } }, computed: { @@ -205,7 +206,7 @@ this.metadata = []; for (let metadatum of metadata) { - if (metadatum.metadata_type_object.component !== 'tainacan-relationship' && metadatum.metadata_type_object.component !== 'tainacan-compound') { + if ( (metadatum.metadata_type_object.component !== 'tainacan-relationship' || this.isMetaqueryRelationshipEnabled) && metadatum.metadata_type_object.component !== 'tainacan-compound' ) { this.metadata.push( metadatum ); this.hasMetadata = true; this.checkMetadata();