<?php

/**
 * AJAX library
 */
function dragblock_ajax_register($action, $callback = '')
{
    if (!$callback) {
        $callback = $action;
    }
    if (is_admin()) :
        add_action('wp_ajax_nopriv_' . $action, $callback);
        add_action('wp_ajax_' . $action, $callback);
    endif; // is_admin for ajax    
}


function dragblock_ajax_error_die($text)
{
    echo json_encode(array('error' => $text));
    die();
}
function dragblock_ajax_request_verify_die($fields = array())
{
    if (empty($_POST['nonce'])) {
        dragblock_ajax_error_die(__('Empty nonce', 'dragblock'));
    }
    if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), DRAGBLOCK_NONCE_SLUG)) {
        dragblock_ajax_error_die(__('Timeout! Please reload the page.', 'dragblock'));
    }
    if (is_string($fields)) {
        $fields = explode(',', $fields);
    }

    if (!empty($fields)) {
        foreach ($fields as $field) {
            $field = trim($field);
            if (!isset($_POST[$field])) {
                dragblock_ajax_error_die(sprintf(__('Missing required field: %s', 'dragblock'), $field));
            }
        }
    }
}

function dragblock_ajax_succeed_die($ret)
{
    echo json_encode($ret);
    die();
}

dragblock_ajax_register('dragblock_url_search');
function dragblock_url_search()
{
    dragblock_ajax_request_verify_die('search');
    $search = sanitize_text_field(wp_unslash($_POST['search']));

    // Get all registered taxonomies
    $taxonomies = get_taxonomies();


    // Initialize an array to store all terms
    $results = array();

    // Loop through each taxonomy
    foreach ($taxonomies as $taxonomy) {
        // Get terms for the current taxonomy and keyword
        $terms = get_terms(array(
            'taxonomy' => $taxonomy,
            'name__like' => $search,
            'hide_empty' => false,
        ));

        // Add terms to the array
        foreach ($terms as $term) {
            $permalink = get_term_link($term);
            $results[$permalink] = array(
                'title' => $term->name,
                'type' => $taxonomy,
            );
        }
    }

    // Set up the query arguments for posts
    $args_posts = array(
        's' => $search, // Search for the keyword in post titles
        'post_type' => 'any', // Adjust post type if needed
        'status' => 'published',
        // 'posts_per_page' => -1, // Get all posts
    );

    // Get posts
    $posts = get_posts($args_posts);

    // Loop through each post
    foreach ($posts as $post) {
        // Get the permalink and title of the post
        $permalink = get_permalink($post);
        $title = get_the_title($post);

        // Add the post to the array
        $results[$permalink] = array(
            'title' => $title,
            'type' => get_post_type($post),
        );
    }

    // Output the merged results in the desired format
    dragblock_ajax_succeed_die(array(
        'search' => $search,
        'results' => $results
    ));
}
