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

/**
 * Get trending petitions
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_trending_petitions') ): 
    function conikal_trending_petitions($paged = false, $country_selected = false) {
        global $campoal_options;
        global $paged;
        global $current_user;

        $search_country = isset($_GET['search_country']) ? sanitize_text_field($_GET['search_country']) : '';
        $search_neighborhood = isset($_GET['search_neighborhood']) ? sanitize_text_field($_GET['search_neighborhood']) : '';      
        $search_state = isset($_GET['search_state']) ? sanitize_text_field($_GET['search_state']) : '';
        $search_city = isset($_GET['search_city']) ? sanitize_text_field($_GET['search_city']) : '';
        $search_category = isset($_GET['search_category']) ? sanitize_text_field($_GET['search_category']) : '0';
        $search_topics = isset($_GET['search_topcis']) ? sanitize_text_field($_GET['search_topcis']) : '0';
        $minimum_signature = isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;
        $show_petition_closed   = isset($campoal_options['conikal_show_petition_closed_field']) ? $campoal_options['conikal_show_petition_closed_field'] : 1;
        
        $posts_per_page_setting = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : '';
        $posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 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;
        
        // if no country select param will use user selection instead
        if (empty($country_selected)) {
            $country_selected       = conikal_get_country_selected($current_user);
        }

        if (empty($paged)) {
            $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'
        );

        if($search_category != '0' && $search_topics != '0') {
            $args['tax_query'] = array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'petition_category',
                    'field'    => 'id',
                    'terms'    => $search_category,
                ),
                array(
                    'taxonomy' => 'petition_topics_category',
                    'field'    => 'id',
                    'terms'    => $search_topics,
                ),
            );
        } elseif($search_category != '0') {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'petition_category',
                    'field'    => 'id',
                    'terms'    => $search_category,
                ),
            );
        } elseif($search_topics != '0') {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'petition_topics_category',
                    'field'    => 'id',
                    'terms'    => $search_topics,
                ),
            );
        }

        $args['meta_query'] = array('relation' => 'AND');

        if ($minimum_signature != 0) {
            array_push($args['meta_query'], array(
                'key'     => 'petition_sign',
                'value'   => $minimum_signature,
                'type'    => 'NUMERIC',
                'compare' => '>='
            ));
        }

        if ($show_petition_closed == 0) {
            array_push($args['meta_query'], array(
                'key'     => 'petition_status',
                'value'   => 2,
                'compare' => '!=',
                'type'    => 'NUMERIC'
            ));
        }

        if ($search_country != '') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_country',
                'value'   => $search_country,
                'compare' => 'IN'
            ));
        } elseif ($country_selection_mode && $filter_by_country && $country_selected != 'WORLD') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_country',
                'value'   => $country_selected,
                'compare' => 'IN'
            ));
        }

        if ($search_state != '') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_state',
                'value'   => $search_state,
            ));
        }

        if ($search_city != '') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_city',
                'value'   => $search_city,
            ));
        }

        if ($search_neighborhood != '') {
            array_push($args['meta_query'], array(
                'key'     => 'petition_neighborhood',
                'value'   => $search_neighborhood,
                'compare' => 'LIKE'
            ));
        }

        $query = new WP_Query($args);

        wp_reset_query();
        wp_reset_postdata();

        return $query;
    }
endif;


/**
 * Load more trending petition results
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_load_trending_petitions') ): 
    function conikal_load_trending_petitions() {
        check_ajax_referer('load_petitions_ajax_nonce', 'security');
        global $campoal_options;
        global $paged;

        $days_trending = isset($campoal_options['conikal_number_days_trending_field']) ? $campoal_options['conikal_number_days_trending_field'] : 14;

        $posts_per_page_setting = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : '';
        $posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 10;

        $paged = isset($_POST['paged']) ? sanitize_text_field($_POST['paged']) : 1;
        $country = isset($_POST['country']) ? sanitize_text_field($_POST['country']) : 'WORLD';

        // get trending petitions data
        $petitions = conikal_trending_petitions($paged, $country);

        // processing query and get meta post.
        $arrayPetitions = conikal_process_petition_query($petitions, $days_trending);

        if ($arrayPetitions) {
            foreach($arrayPetitions as $meta => $value) {
                $sort_trending[] = $value->trending;
                $sort_date[] = $value->date;
            }
            array_multisort($sort_trending, SORT_DESC, $sort_date, SORT_DESC, $arrayPetitions);

            echo json_encode(array('status' => true, 'found_posts' => count($arrayPetitions), 'total' => $petitions->found_posts, 'per_page' => $posts_per_page, 'petitions' => $arrayPetitions, 'message' => esc_html__('Petitions was loaded successfully.', 'campoal')));
            exit();
        } else {
            echo json_encode(array('status' => false, 'message' => esc_html__('Something went wrong. Not found petitions.', 'campoal')));
            exit();
        }

        die();
    }
endif;
add_action( 'wp_ajax_nopriv_conikal_load_trending_petitions', 'conikal_load_trending_petitions' );
add_action( 'wp_ajax_conikal_load_trending_petitions', 'conikal_load_trending_petitions' );


?>