Forked from ihslimn/JE_Query_Filter_Options_Generator.php
Last active
October 11, 2022 07:09
-
-
Save Crocoblock/166ec80ed064e4137ef8dbb047b8c5d2 to your computer and use it in GitHub Desktop.
Generate JetSmartFilters options from JetEngine Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class JE_Query_Filter_Options_Generator { | |
public function __construct() { | |
add_action( 'plugins_loaded', array( $this, 'maybe_add_filters' ) ); | |
} | |
public function maybe_add_filters() { | |
if ( ! function_exists( 'jet_engine' ) ) { | |
return; | |
} | |
add_filter( 'jet-smart-filters/post-type/meta-fields-settings', array( $this, 'add_options_to_list' ) ); | |
add_filter( 'jet-smart-filters/filters/filter-options', array( $this, 'apply_filter_options' ), 9999, 3 ); | |
} | |
public function add_options_to_list( $fields ) { | |
$queries = array( | |
'' => __( 'Select query...', 'jet-engine' ), | |
); | |
$queries = $queries + Jet_Engine\Query_Builder\Manager::instance()->get_queries_for_options(); | |
$insert = array( | |
'_je_query_use_query' => array( | |
'title' => __( 'Use JetEngine Query to generate options from', 'jet-smart-filters' ), | |
'type' => 'switcher', | |
'element' => 'control', | |
'conditions' => array( | |
'_filter_type' => array( 'checkboxes', 'radio', 'select' ), | |
), | |
), | |
'_je_query_id' => array( | |
'title' => __( 'Select query', 'jet-engine' ), | |
'type' => 'select', | |
'element' => 'control', | |
'options' => $queries, | |
'conditions' => array( | |
'_filter_type' => array( 'checkboxes', 'select', 'radio' ), | |
'_je_query_use_query' => array( true, 'true', '1' ), | |
), | |
), | |
'_je_query_value' => array( | |
'title' => __( 'Property to get value from', 'jet-engine' ), | |
'type' => 'text', | |
'value' => 'ID', | |
'element' => 'control', | |
'conditions' => array( | |
'_filter_type' => array( 'checkboxes', 'select', 'radio' ), | |
'_je_query_use_query' => array( true, 'true', '1' ), | |
), | |
), | |
'_je_query_label' => array( | |
'title' => __( 'Property to get label from', 'jet-engine' ), | |
'type' => 'text', | |
'value' => 'post_title', | |
'element' => 'control', | |
'conditions' => array( | |
'_filter_type' => array( 'checkboxes', 'select', 'radio' ), | |
'_je_query_use_query' => array( true, 'true', '1' ), | |
), | |
), | |
); | |
return $this->je_insert_after( $fields, '_source_post_type', $insert ); | |
} | |
public function apply_filter_options( $options, $filter_id, $filter ) { | |
$use_query = get_post_meta( $filter_id, '_je_query_use_query', true ); | |
$use_query = filter_var( $use_query, FILTER_VALIDATE_BOOLEAN ); | |
if ( ! $use_query ) { | |
return $options; | |
} | |
$query_id = get_post_meta( $filter_id, '_je_query_id', true ); | |
if ( ! $query_id ) { | |
return $options; | |
} | |
$query = Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id( $query_id ); | |
if ( ! $query ) { | |
return $options; | |
} | |
$value_prop = get_post_meta( $filter_id, '_je_query_value', true ); | |
$label_prop = get_post_meta( $filter_id, '_je_query_label', true ); | |
$objects = $query->get_items(); | |
$new_options = array(); | |
foreach ($objects as $object) { | |
$value = isset( $object->$value_prop ) ? $object->$value_prop : false; | |
$label = isset( $object->$label_prop ) ? $object->$label_prop : false; | |
if ( $value && $label && ! isset( $new_options[ $value ] ) ) { | |
$new_options[ $value ] = $label; | |
} | |
} | |
$type = get_post_meta( $filter_id, '_filter_type', true ); | |
switch( $type ) { | |
case 'select': | |
$placeholder = get_post_meta( $filter_id, '_placeholder', true ); | |
if ( ! $placeholder ) { | |
$placeholder = __( 'Select...', 'jet-engine' ); | |
} | |
$new_options = array( '' => $placeholder ) + $new_options; | |
break; | |
case 'radio': | |
case 'checkboxes': | |
$source = get_post_meta( $filter_id, '_data_source', true ); | |
if ( $source === 'taxonomies' ) { | |
$tax = get_post_meta( $filter_id, '_source_taxonomy', true ); | |
$by_parents = get_post_meta( $filter_id, '_group_by_parent', true ); | |
$by_parents = filter_var( $by_parents, FILTER_VALIDATE_BOOLEAN ); | |
if ( $by_parents ) { | |
foreach ( $new_options as $term_id => $term_name ) { | |
$term = get_term( $term_id, $tax ); | |
if ( ! $term || is_wp_error( $term ) ) { | |
continue; | |
} | |
$new_options[$term_id] = $term; | |
} | |
} | |
} | |
if ( $type === 'radio' ) { | |
$add_all_option = filter_var( get_post_meta( $filter_id, '_add_all_option', true ), FILTER_VALIDATE_BOOLEAN ); | |
$all_option_label = $add_all_option ? get_post_meta( $filter_id, '_all_option_label', true ) : false; | |
if ( $all_option_label ) { | |
if ( true === $by_parents ) { | |
$all_option = (object) array( | |
'term_id' => 'all', | |
'name' => htmlspecialchars( $all_option_label ) | |
); | |
} else { | |
$all_option = htmlspecialchars( $all_option_label ); | |
} | |
$new_options = array( 'all' => $all_option ) + $new_options; | |
} | |
} | |
break; | |
} | |
return $new_options; | |
} | |
public function je_insert_after( $source = array(), $after = null, $insert = array() ) { | |
$keys = array_keys( $source ); | |
$index = array_search( $after, $keys ); | |
if ( ! $source ) { | |
$source = array(); | |
} | |
if ( false === $index ) { | |
return $source + $insert; | |
} | |
$offset = $index + 1; | |
return array_slice( $source, 0, $offset, true ) + $insert + array_slice( $source, $offset, null, true ); | |
} | |
} | |
new JE_Query_Filter_Options_Generator(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Taxonomy_Filter_Manual_Options { | |
public function __construct() { | |
add_filter( 'jet-smart-filters/query/final-query', array( $this, 'modify_query' ) ); | |
add_filter( 'jet-smart-filters/query/meta-query-row', array( $this, 'clear_meta_row' ) ); | |
} | |
public function modify_query( $query ) { | |
if ( isset( $query['tax_query'] ) ) { | |
foreach ( $query['tax_query'] as $index => $item ) { | |
if ( is_array( $item ) && false !== strpos( $item['taxonomy'], '_meta_query_' ) ) { | |
$query['tax_query'][$index]['taxonomy'] = str_replace( '_meta_query_', '', $item['taxonomy'] ); | |
} | |
} | |
} | |
return $query; | |
} | |
public function clear_meta_row( $row ) { | |
if ( false !== strpos( $row['key'], '_tax_query_' ) ) { | |
$row = array(); | |
} | |
return $row; | |
} | |
} | |
new Taxonomy_Filter_Manual_Options(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment