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


/**
 * Get signed petitions
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_signed_petitions') ): 
    function conikal_signed_petitions($user_id, $posts_per_page = false) {
        global $campoal_options;
        $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';
        $minimum_signature = isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;
        
        if (!$posts_per_page) {
            $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;
        }

        $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;

        $signed = get_user_meta($user_id, 'petition_sign', true);

        if($signed && $signed != ''){
            global $paged;

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post__in'	 			=> $signed,
                'posts_per_page' 		=> $posts_per_page,
                'paged' 				=> $paged,
                'post_type' 			=> 'petition',
                'post_status' 			=> 'publish',
                'ignore_sticky_posts' 	=> true,
                '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';
            }

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

            $query = new WP_Query($args);

            wp_reset_query();
            wp_reset_postdata();

            return $query;
        } else {
            return false;
        }
    }
endif;


/**
 * Load more signed petitions
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_load_signed_petitions') ): 
    function conikal_load_signed_petitions() {
        check_ajax_referer('load_petitions_ajax_nonce', 'security');
        global $campoal_options;
        
        $minimum_signature 		= isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;
        
        $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;

        $order      = isset($_POST['order']) && !empty($_POST['order']) ? sanitize_text_field($_POST['order']) : 'DESC';
        $orderby    = isset($_POST['orderby']) && !empty($_POST['orderby']) ? sanitize_text_field($_POST['orderby']) : 'ID';

        $user_id 	= isset($_POST['user_id']) ? sanitize_text_field($_POST['user_id']) : '';
        $author_id 	= isset($_POST['author_id']) ? sanitize_text_field($_POST['author_id']) : 1;
        $paged 		= isset($_POST['paged']) ? sanitize_text_field($_POST['paged']) : 2;
        $signed 	= get_user_meta($author_id, 'petition_sign', true);

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

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

        $petitions = new WP_Query($args);

        wp_reset_query();
        wp_reset_postdata();

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

        if ($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_signed_petitions', 'conikal_load_signed_petitions' );
add_action( 'wp_ajax_conikal_load_signed_petitions', 'conikal_load_signed_petitions' );
?>