<?php

dragblock_ajax_register('dragblock_database_loader');

function dragblock_database_loader()
{
    dragblock_ajax_request_verify_die('data');
    $data =  json_decode(sanitize_text_field(wp_unslash($_POST['data'])), true);    
    $data['fields'] = 'ids';
    if (!isset($data['post_status'])) {
        $data['post_status'] = 'publish';
    }

    // $post_ids = get_posts($data);
    $query_posts = new WP_Query(dragblock_wp_query_args_processor($data));
    $post_ids = $query_posts->posts;
    $posts = array();

    foreach ($post_ids as $id) {
        $post = array();
        $post['id'] = $id;
        $post['title'] = dragblock_shortcode_post_title(null, $id);
        $post['snippet'] = dragblock_shortcode_post_snippet(null, $id);
        $post['comment_number'] = dragblock_shortcode_post_comment_number(null, $id);
        $post['post_count'] = dragblock_shortcode_post_view_count(null, $id);
        $post['image_src'] = dragblock_shortcode_post_image_src(null, $id);
        $post['image_caption'] = dragblock_shortcode_post_image_caption(null, $id);
		$post['image_alt'] = dragblock_shortcode_post_image_alt(null, $id);
		$post['image_desc'] = dragblock_shortcode_post_image_desc(null, $id);
        $post['date'] = dragblock_shortcode_post_date(null, $id);
        $post['author_name'] = dragblock_shortcode_post_author_name(null, $id);
        $post['author_avatar_src'] = dragblock_shortcode_post_author_avatar_src(null, $id);
        $post['author_bio'] = dragblock_shortcode_post_author_bio(null, $id);
        $post['cat_name'] = dragblock_shortcode_post_cat_name(null, $id);
        $posts[] = $post;
    }

    // fill the post data here
    dragblock_ajax_succeed_die($posts);
}
