<?php 
/**
 * @package WordPress
 * @subpackage Campoal
 */


/**
 * Petition taxonomy query
 *
 * @since Campoal 2.0.0
 */
if( !function_exists('conikal_petitions_taxonomy_query') ): 
    function conikal_petitions_taxonomy_query($country_selected = false) {
        global $campoal_options;
        global $current_user;

        $minimum_signature = isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;
        $posts_per_page = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : 10;

        $country_selection_mode = isset($campoal_options['conikal_country_selection_mode_field']) ? $campoal_options['conikal_country_selection_mode_field'] : 0;
        $filter_by_country = isset($campoal_options['conikal_filter_by_country_field']) ? $campoal_options['conikal_filter_by_country_field'] : 0;
        $sortby_default = isset($campoal_options['conikal_sortby_default_field']) ? $campoal_options['conikal_sortby_default_field'] : 'date';
        $order_default = isset($campoal_options['conikal_order_default_field']) ? $campoal_options['conikal_order_default_field'] : 'DESC';
        
        // if no country select param will use user selection instead
        if (empty($country_selected)) {
            $country_selected       = conikal_get_country_selected($current_user);
        }

        $order      = isset($_GET['order']) && !empty($_GET['order']) ? sanitize_text_field($_GET['order']) : $order_default;
        $orderby    = isset($_GET['orderby']) && !empty($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : $sortby_default;

        $term = get_queried_object();
        $term_id = $term ? $term->term_id : '';
        $year     = get_query_var('year');
        $monthnum = get_query_var('monthnum');
        $day = get_query_var('day');
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $args = array( 
            'posts_per_page' 	=> $posts_per_page,
            'paged' 			=> $paged,
            'post_type' 		=> 'petition',
            'post_status' 		=> 'publish',
            'order'       		=> $order,
            'orderby' 			=> conikal_meta_key_ordering_filter($orderby),
        );

        // add meta key or supporters ordering
        if ($orderby == 'supporters') {
            $args['meta_key'] = 'petition_sign';
        }
        if ($orderby == 'views') {
            $args['meta_key'] = 'post_views_count';
        }

        if(is_date()) {
            $args['year'] = $year;
            $args['monthnum'] = $monthnum;
            $args['day'] = $day;
        }

        $args['tax_query'] = array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'petition_topics',
                'terms'    => $term_id,
            )
        );
        $args['tag_id'] = $term_id;
        $args['meta_query'] = array('relation' => 'AND');

        // only show petitions meet minimum signatures
        if ($minimum_signature != 0) {
            array_push($args['meta_query'], array(
                'key'     => 'petition_sign',
                'value'   => $minimum_signature,
                'type'    => 'NUMERIC',
                'compare' => '>='
            ));
        }

        // only show petition matched with country selected
        if ($country_selection_mode && $filter_by_country && $country_selected != 'WORLD') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_country',
                'value'   => $country_selected,
                'compare' => 'IN'
            ));
        }

        $query = new WP_Query( $args );

        wp_reset_query();
        wp_reset_postdata();

        return $query;
    }
endif;