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


/**
 * Get recent petition
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_recent_petitions') ): 
    function conikal_recent_petitions($paged = false, $country_selected = false, $posts_per_page = false) {
        global $campoal_options;
        global $current_user;
        
        $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'] : '';
        $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($posts_per_page)) {
            $posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 10;
        }

        if (empty($paged)) {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        }

        $args = array(
            'post_type'         => 'petition',
            'posts_per_page'    => $posts_per_page,
            'paged'             => $paged,
            'post_status'       => array('publish'),
        );
        $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' => '>='
            ));
        }

        // 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'
            ));
        }

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

        $query = new WP_Query($args);

        wp_reset_query();
        wp_reset_postdata();

        return $query;
    }
endif;


/**
 * Load more recent petitions
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_load_recent_petitions') ): 
    function conikal_load_recent_petitions() {
        check_ajax_referer('load_petitions_ajax_nonce', 'security');
        global $campoal_options;
                
        $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']) : 2;
        $country = isset($_POST['country']) ? sanitize_text_field($_POST['country']) : 'WORLD';

        // get recent petitions data
        $petitions = conikal_recent_petitions($paged, $country);

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

        if ($petitions->have_posts()) {
            echo json_encode(array('status' => true, 'found_posts' => count($arrayPetitions), 'total' => $petitions->found_posts, 'per_page' => $posts_per_page, 'paged' => $paged, '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_recent_petitions', 'conikal_load_recent_petitions' );
add_action( 'wp_ajax_conikal_load_recent_petitions', 'conikal_load_recent_petitions' );

?>