<?php
use Elementor\Utils;
use Elementor\Group_Control_Image_Size;
use CrtAddons\Classes\Utilities;


if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * CRT_Ajax_Search setup
 *
 * @since 3.4.6
 */

 class CRT_Ajax_Search {

    public function __construct() {
        add_action('wp_ajax_crt_data_fetch' , [$this, 'data_fetch']);
        add_action('wp_ajax_nopriv_crt_data_fetch',[$this, 'data_fetch']);
    }

    public function data_fetch() {

        $nonce = $_POST['nonce'];

        if ( !wp_verify_nonce( $nonce, 'crt-addons-js' ) ) {
            return; // Get out of here, the nonce is rotten!
        }

		$all_post_types = [];

        if ( sanitize_text_field($_POST['crt_show_attachments']) == 'yes' ) {
            $all_post_types = ['attachment'];
        }

        foreach(  Utilities::get_custom_types_of( 'post', false ) as $key=>$value ) {
            array_push($all_post_types, $key);
        }
        
        $tax_query = '';

        if ( $_POST['crt_category'] != false && $_POST['crt_category'] != '' ) {   
                $tax_query = array(
                    array(
                        'taxonomy' => $_POST['crt_option_post_type'],
                        'field'    => 'term_id',
                        'terms'    => sanitize_text_field($_POST['crt_category']),
                    ),
                );

            // if ( isset($_POST['crt_option_post_type']) && !empty($_POST['crt_option_post_type']) ) {
            //     $tax_query = array(
            //         array(
            //             'taxonomy' => $_POST['crt_option_post_type'],
            //             'field'    => 'term_id',
            //             'terms'    => sanitize_text_field($_POST['crt_category']),
            //         ),
            //     );
            // } else {
            //     $tax_query = array(
            //         array(
            //             'taxonomy' => $_POST['crt_query_type'] == 'product' ? $_POST['crt_query_type'] . '_cat' : 'category',
            //             'field'    => 'term_id',
            //             'terms'    => sanitize_text_field($_POST['crt_category']),
            //         ),
            //     );
            // }
        } else if ( $_POST['crt_category'] == 0 && $_POST['crt_query_type'] != 'all' ) {
            if ( !empty($_POST['crt_option_post_type']) ) {
                $tax_query = array(
                    array(
                        'taxonomy' => $_POST['crt_option_post_type'],
                        'field'    => 'term_id',
                        'terms'    => sanitize_text_field($_POST['crt_category']),
                    ),
                );
            } else { 
                // Get the string from the POST data
                $taxonomy_type_string = $_POST['crt_taxonomy_type'];
            
                // Check if the string contains spaces
                if (strpos($taxonomy_type_string, ' ') !== false) {
                    // Split the string into an array based on spaces
                    $taxonomy_types = explode(' ', $taxonomy_type_string);
            
                
                    $tax_query = [
                        'relation' => 'OR'
                    ];
                    
                    foreach( $taxonomy_types as $taxonomy_type ) {
                        array_push($tax_query, [
                            'taxonomy' => $taxonomy_type,
                            'operator'    => 'EXISTS'
                        ]);
                    }
                } else {
                    // If there are no spaces, leave it as a single-item array
                    $taxonomy_types = $taxonomy_type_string;

                    $tax_query = array(
                        array(
                            'taxonomy' => $_POST['crt_taxonomy_type'],
                            'operator'    => 'EXISTS',
                        ),
                    );
                }
            } 
        }

        $keyword = sanitize_text_field( $_POST['crt_keyword'] );
        $can_view_protected_posts = current_user_can('read_private_posts');
        $meta_query = [];

        if ( 'yes' === sanitize_text_field( $_POST['crt_exclude_without_thumb'] ) ) {
            $meta_query[] = [
                'key' => '_thumbnail_id',
            ];
        }
        
        if ( ( 'yes' !== sanitize_text_field($_POST['crt_show_ps_pt'] ) ) || !$can_view_protected_posts ) {
            $args =
                [
                    'posts_per_page' => sanitize_text_field($_POST['crt_number_of_results']), 
                    's' => sanitize_text_field( $_POST['crt_keyword'] ),
                    'post_type' => $_POST['crt_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION') )  ? $all_post_types : array( sanitize_text_field($_POST['crt_query_type']) ),
                    'offset' => sanitize_text_field($_POST['crt_search_results_offset']),
                    'meta_query' => $meta_query ?: '',
                    'tax_query' => $tax_query,
                    'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish',
                    'post_password' => ''
                ];
        } else {
            $args =
                [
                    'posts_per_page' => sanitize_text_field($_POST['crt_number_of_results']), 
                    's' => sanitize_text_field( $_POST['crt_keyword'] ),
                    'post_type' => $_POST['crt_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION'))  ? $all_post_types : array( sanitize_text_field($_POST['crt_query_type']) ),
                    'offset' => sanitize_text_field($_POST['crt_search_results_offset']),
                    'meta_query' => $meta_query ?: '',
                    'tax_query' => $tax_query,
                    'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish',
                ];
        }

        $the_query = new \WP_Query( $args );

        if ( !$the_query->have_posts() && $keyword !== '' && 'yes' === sanitize_text_field( $_POST['crt_meta_query'] ) ) {
            $fallback_args = $args;
            $fallback_args['s'] = ''; // disable default search
            $fallback_args['meta_query'] = [
                [
                    'value'   => $keyword,
                    'compare' => 'LIKE'
                ]
            ];

            $the_query = new \WP_Query($fallback_args);
        }
        
        if( $the_query->have_posts() ) :
            $number_of_queried_posts = $the_query->found_posts;
            $post_count = 0;

                while( $the_query->have_posts() ) : $the_query->the_post();

                // if ( ( !has_post_thumbnail() && 'yes' === sanitize_text_field($_POST['crt_exclude_without_thumb'])) ) : 
                //     continue;
                // endif;

                ob_start();
                // the_post_thumbnail(sanitize_text_field($_POST['ajax_search_img_size']));
                the_post_thumbnail('medium');
                $post_thumb = ob_get_clean();
                ?>

                <li data-number-of-results="<?php echo esc_attr($the_query->found_posts); ?>">
                    
                    <?php if ( !post_password_required() || $can_view_protected_posts ) : ?>

                        <?php if ( 'yes' === sanitize_text_field($_POST['crt_show_ajax_thumbnail']) ) :
                            if ( has_post_thumbnail() ) :
                                echo '<a class="crt-ajax-img-wrap" target="'. esc_attr($_POST['crt_ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'">'.  wp_kses_post($post_thumb) .'</a>';
                                // echo '<a class="crt-ajax-img-wrap" target="'. sanitize_text_field($_POST['ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'">'.  '<img src="'. Group_Control_Image_Size::get_attachment_image_src( get_post_thumbnail_id(), 'ajax_search_image', [$_POST['ajax_search_image_size']] ) .'"/>' .'</a>';
                            else :
                                echo '<a class="crt-ajax-img-wrap" target="'. esc_attr($_POST['crt_ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'"><img src="'.esc_url(Utils::get_placeholder_image_src()).'"></a>';
                            endif ;
                        endif ; ?>

                        <div class="crt-ajax-search-content">
                            <a target="<?php echo esc_attr($_POST['crt_ajax_search_link_target']) ?>" class="crt-ajax-title" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title();?></a>

                            <?php if ( sanitize_text_field($_POST['crt_show_description']) == 'yes' ) : ?>
                                <p class="crt-ajax-desc"><a target="<?php echo esc_attr($_POST['crt_ajax_search_link_target']) ?>" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html(wp_trim_words(get_the_content(), sanitize_text_field($_POST['crt_number_of_words']))); ?></a></p>
                            <?php endif; ?>

                            <?php if ( 'yes' === sanitize_text_field($_POST['crt_show_product_price']) && 
                                    get_post_type() === 'product' && 
                                    class_exists('WooCommerce') ) :
                                $product = wc_get_product(get_the_ID());
                                if ($product) {
                                    $price_html = '<div class="crt-search-product-price">'. $product->get_price_html() .'</div>';

                                    echo wp_kses_post($price_html);
                                }
                            endif; ?>

                            <?php if ( sanitize_text_field($_POST['crt_show_view_result_btn']) ) : ?>
                                <a target="<?php echo esc_attr($_POST['crt_ajax_search_link_target']) ?>" class="crt-view-result" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo esc_html(sanitize_text_field($_POST['crt_view_result_text'])); ?></a>
                            <?php endif; ?>
                        </div>
                    
                    <?php else: ?>

                        <?php if ( 'yes' === sanitize_text_field($_POST['crt_show_ajax_thumbnail']) ) :
                            echo '<a class="crt-ajax-img-wrap" target="'. esc_attr($_POST['crt_ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'"><img src="'.esc_url(Utils::get_placeholder_image_src()).'"></a>';
                        endif ; ?>

                        <div class="crt-ajax-search-content">
                            <a target="<?php echo esc_attr($_POST['crt_ajax_search_link_target']) ?>" class="crt-ajax-title" href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title();?></a>
                        </div>
                    
                    <?php endif; ?>

                </li>
                <?php 
                $post_count++;
                endwhile;

            wp_reset_postdata();
            
        else :
            if (0 < sanitize_text_field($_POST['crt_search_results_offset'])) {
            } else {
                echo '<p class="crt-no-results">'. esc_html(sanitize_text_field($_POST['crt_no_results'])) .'</p>';
            }

        endif;
        
        die();
    }
 }

 new CRT_Ajax_Search();