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

This commit is contained in:
mateuswetah 2021-11-01 10:52:56 -03:00
commit 7c53707b57
3 changed files with 45 additions and 3 deletions

View File

@ -347,6 +347,10 @@ class Items extends Repository {
$args['orderby'] = $new_order; $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' ); $args = apply_filters( 'tainacan_fetch_args', $args, 'items' );
$should_filter = is_user_logged_in() && ! isset($args['post_status']) && sizeof($cpt) > 1; $should_filter = is_user_logged_in() && ! isset($args['post_status']) && sizeof($cpt) > 1;
@ -698,4 +702,40 @@ class Items extends Repository {
return $response; 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;
}
} }

View File

@ -131,7 +131,8 @@
modelSearch:'', modelSearch:'',
collectionType: '', collectionType: '',
collectionMessage: '', collectionMessage: '',
displayRelatedItemMetadata: [] displayRelatedItemMetadata: [],
isMetaqueryRelationshipEnabled: tainacan_plugin && tainacan_plugin.tainacan_enable_relationship_metaquery == true ? tainacan_plugin.tainacan_enable_relationship_metaquery : false
} }
}, },
computed: { computed: {
@ -205,7 +206,7 @@
this.metadata = []; this.metadata = [];
for (let metadatum of 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.metadata.push( metadatum );
this.hasMetadata = true; this.hasMetadata = true;
this.checkMetadata(); this.checkMetadata();

View File

@ -307,7 +307,8 @@ class Admin {
'repository_name' => get_bloginfo('name'), 'repository_name' => get_bloginfo('name'),
'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE, 'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE,
'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(), '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 = [ $maps = [