Integration with ElasticPress #90
This commit is contained in:
parent
3af62e3539
commit
b23544d248
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
/**
|
||||
* This class implements the integration of Tainacan with ElasticPress, a WordPress plugin that connects your WordPress installation with Elastic Search
|
||||
*
|
||||
* https://github.com/10up/ElasticPress
|
||||
* https://www.elasticpress.io/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Tainacan;
|
||||
|
||||
class Elastic_Press {
|
||||
|
||||
function __construct($ajax_query=false) {
|
||||
add_action('init', [&$this, 'init']);
|
||||
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!class_exists('EP_API')) {
|
||||
return; // ElasticPress not active
|
||||
}
|
||||
add_filter('tainacan_fetch_args', [&$this, 'filter_args'], 10, 2);
|
||||
}
|
||||
|
||||
function filter_args($args, $type) {
|
||||
|
||||
if ($type == 'items') {
|
||||
$args['ep_integrate'] = true;
|
||||
$args = $this->add_items_args($args);
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
private function add_items_args($args) {
|
||||
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
if (is_array($args['post_type']) && isset($args['s']) && !empty($args['s'])) {
|
||||
|
||||
$meta_ids = [];
|
||||
$taxonomies = [];
|
||||
|
||||
foreach ( $args['post_type'] as $cpt ) {
|
||||
|
||||
$col = $Tainacan_Collections->fetch_by_db_identifier($cpt);
|
||||
|
||||
$taxonomies = array_merge( $taxonomies, get_object_taxonomies($cpt) );
|
||||
|
||||
if ($col) {
|
||||
|
||||
$metadata = $Tainacan_Metadata->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT');
|
||||
|
||||
foreach ($metadata as $meta) {
|
||||
$meta_ids[] = $meta->get_id();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$search_fields = [
|
||||
'post_title',
|
||||
'post_content',
|
||||
'post_excerpt'
|
||||
];
|
||||
|
||||
if (!empty($meta_ids)) {
|
||||
$search_fields['meta'] = array_unique($meta_ids);
|
||||
}
|
||||
if (!empty($taxonomies)) {
|
||||
$search_fields['taxonomies'] = array_unique($taxonomies);
|
||||
}
|
||||
|
||||
$args['search_fields'] = $search_fields;
|
||||
|
||||
}
|
||||
|
||||
return $args;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // END
|
|
@ -336,6 +336,8 @@ class Collections extends Repository {
|
|||
|
||||
// TODO: Pegar coleções registradas via código
|
||||
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'collections');
|
||||
|
||||
$wp_query = new \WP_Query( $args );
|
||||
|
||||
return $this->fetch_output( $wp_query, $output );
|
||||
|
|
|
@ -248,6 +248,8 @@ class Filters extends Repository {
|
|||
$args = $this->parse_fetch_args($args);
|
||||
|
||||
$args['post_type'] = Entities\Filter::get_post_type();
|
||||
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'filters');
|
||||
|
||||
$wp_query = new \WP_Query($args);
|
||||
return $this->fetch_output($wp_query, $output);
|
||||
|
|
|
@ -252,6 +252,8 @@ class Items extends Repository {
|
|||
|
||||
$args['post_type'] = $cpt;
|
||||
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'items');
|
||||
|
||||
$wp_query = new \WP_Query( $args );
|
||||
|
||||
return $this->fetch_output( $wp_query, $output );
|
||||
|
|
|
@ -200,6 +200,8 @@ class Logs extends Repository {
|
|||
|
||||
$args['post_type'] = Entities\Log::get_post_type();
|
||||
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'logs');
|
||||
|
||||
$wp_query = new \WP_Query( $args );
|
||||
|
||||
return $this->fetch_output( $wp_query, $output );
|
||||
|
|
|
@ -324,11 +324,31 @@ class Metadata extends Repository {
|
|||
$args = $this->parse_fetch_args($args);
|
||||
|
||||
$args['post_type'] = Entities\Metadatum::get_post_type();
|
||||
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'metadata');
|
||||
|
||||
$wp_query = new \WP_Query($args);
|
||||
return $this->fetch_output($wp_query, $output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch metadata IDs based on WP_Query args
|
||||
*
|
||||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* @param array $args WP_Query args || int $args the item id
|
||||
*
|
||||
* @return Array array of IDs;
|
||||
*/
|
||||
public function fetch_ids( $args = [] ) {
|
||||
|
||||
$args['fields'] = 'ids';
|
||||
|
||||
return $this->fetch( $args )->get_posts();
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch metadatum by collection, considering inheritance
|
||||
|
|
|
@ -333,6 +333,14 @@ abstract class Repository {
|
|||
}
|
||||
|
||||
$args['meta_query'] = $meta_query;
|
||||
|
||||
// Map orderby parameter
|
||||
if ( isset($args['orderby']) ) {
|
||||
if ( array_key_exists($args['orderby'], $map) ) {
|
||||
$args['orderby'] = $map[ $args['orderby'] ]['map'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $args;
|
||||
|
||||
|
|
|
@ -172,7 +172,9 @@ class Taxonomies extends Repository {
|
|||
|
||||
$args['post_type'] = Entities\Taxonomy::get_post_type();
|
||||
|
||||
$wp_query = new \WP_Query($args);
|
||||
$args = apply_filters('tainacan_fetch_args', $args, 'taxonomies');
|
||||
|
||||
$wp_query = new \WP_Query($args);
|
||||
return $this->fetch_output($wp_query, $output);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -136,5 +136,6 @@ require_once(__DIR__ . '/../theme-helper/template-tags.php');
|
|||
$Tainacan_Theme_Helper = \Tainacan\Theme_Helper::get_instance();
|
||||
|
||||
$Tainacan_Search_Engine = new \Tainacan\Search_Engine();
|
||||
$Tainacan_Elastic_press = new \Tainacan\Elastic_Press();
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue