function wordlift_ajax_related_posts($http_raw_data = null)
{
    // Extract filtering conditions
    if (!isset($_GET["post_id"]) || !is_numeric($_GET["post_id"])) {
        wp_die('Post id missing or invalid!');
        return;
    }
    $post_id = $_GET["post_id"];
    // Get the current post
    $post = get_post($post_id);
    wl_write_log("Going to find posts related to current with post id: {$post_id} ...");
    // Extract filtering conditions
    $filtering_entity_uris = null == $http_raw_data ? file_get_contents("php://input") : $http_raw_data;
    $filtering_entity_uris = json_decode($filtering_entity_uris);
    $filtering_entity_ids = wl_get_entity_post_ids_by_uris($filtering_entity_uris);
    $related_posts = array();
    // If the current post is an antity
    // related posts to the current entity are returned
    if (Wordlift_Entity_Service::TYPE_NAME == $post->post_type) {
        $filtering_entity_ids = array($post_id);
    }
    if (!empty($filtering_entity_ids)) {
        $related_posts = wl_core_get_posts(array('get' => 'posts', 'related_to__in' => $filtering_entity_ids, 'post__not_in' => array($post_id), 'post_type' => 'post', 'post_status' => 'publish', 'as' => 'subject'));
        foreach ($related_posts as $post_obj) {
            $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail'));
            $post_obj->thumbnail = $thumbnail ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
            $post_obj->link = get_edit_post_link($post_obj->ID, 'none');
            $post_obj->permalink = get_post_permalink($post_obj->ID);
        }
    }
    wl_core_send_json($related_posts);
}
/**
 * Execute the {@link wl_entity_get_by_title} function via AJAX.
 *
 * @since 3.0.0
 */
function wl_entity_ajax_get_by_title()
{
    // Get the parameter.
    if (!isset($_GET['title']) || empty($_GET['title'])) {
        wp_die('The title parameter is required.');
    }
    $title = $_GET['title'];
    // Get the edit link.
    $post_type_object = get_post_type_object(WL_ENTITY_TYPE_NAME);
    $edit_link = $post_type_object->_edit_link . '&action=edit';
    // Prepare the response with the edit link.
    $response = new StdClass();
    $response->edit_link = $edit_link;
    $response->results = wl_entity_get_by_title($title);
    wl_core_send_json($response);
}
/**
 * Execute the {@link wl_entity_get_by_title} function via AJAX.
 *
 * @since 3.0.0
 */
function wl_entity_ajax_get_by_title()
{
    // Get the title to search.
    if (empty($_GET['title'])) {
        wp_die('The title parameter is required.');
    }
    $title = $_GET['title'];
    // Are we searching for a specific title or for a containing title?
    $autocomplete = isset($_GET['autocomplete']);
    // Are we searching also for the aliases?
    $include_alias = isset($_GET['alias']);
    // Get the edit link.
    $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME);
    $edit_link = $post_type_object->_edit_link . '&action=edit';
    // Prepare the response with the edit link.
    $response = array('edit_link' => $edit_link, 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias));
    wl_core_send_json($response);
}
/**
 * Print both global or post related places in json. It's executed via Ajax
 *
 * @uses wl_shortcode_geomap_get_places() in order to retrieve places
 * @uses wl_shortcode_geomap_prepare_map() in order to encode retireved places in a Leaflet friendly format
 *
 * @param array $places An array of place posts.
 *
 * @return array An array of place posts.
 */
function wl_shortcode_geomap_ajax()
{
    // Get the post Id.
    $post_id = isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null;
    $places = wl_shortcode_geomap_get_places($post_id);
    $map_data = wl_shortcode_geomap_prepare_map($places);
    wl_core_send_json($map_data);
}
/**
 * Retrieve related entities and output them in JSON.
 *
 * @uses wl_shortcode_chord_get_relations()
 * @uses wl_shortcode_chord_get_graph()
 */
function wl_shortcode_chord_ajax()
{
    $post_id = $_REQUEST['post_id'];
    $depth = $_REQUEST['depth'];
    $relations = wl_shortcode_chord_get_relations($post_id, $depth);
    $graph = wl_shortcode_chord_get_graph($relations);
    wl_core_send_json($graph);
}
function wl_shortcode_faceted_search_ajax($http_raw_data = null)
{
    // Entity ID must be defined
    if (!isset($_GET['entity_id'])) {
        wp_die('No entity_id given');
        return;
    }
    $entity_id = $_GET['entity_id'];
    // If the current post is not an entity post an exception needs to be raised
    $entity = get_post($entity_id);
    if (Wordlift_Entity_Service::TYPE_NAME !== $entity->post_type) {
        wp_die('Faceted search supports only entity posts');
        return;
    }
    // Which type was requested?
    if (isset($_GET['type'])) {
        $required_type = $_GET['type'];
    } else {
        $required_type = null;
    }
    // Extract filtering conditions
    $filtering_entity_uris = null == $http_raw_data ? file_get_contents("php://input") : $http_raw_data;
    $filtering_entity_uris = json_decode($filtering_entity_uris);
    // Set up data structures
    $referencing_post_ids = wl_core_get_related_post_ids($entity_id, array('status' => 'publish'));
    $results = array();
    if ('posts' == $required_type) {
        // Required filtered posts.
        wl_write_log("Going to find related posts for the current entity [ entity ID :: {$entity_id} ]");
        if (empty($filtering_entity_uris)) {
            // No filter, just get referencing posts
            foreach ($referencing_post_ids as $post_obj_id) {
                $post_obj = get_post($post_obj_id);
                $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail'));
                $post_obj->thumbnail = $thumbnail ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
                $post_obj->permalink = get_post_permalink($post_obj->ID);
                $results[] = $post_obj;
            }
        } else {
            $filtering_entity_ids = wl_get_entity_post_ids_by_uris($filtering_entity_uris);
            // Search posts that reference all the filtering entities.
            $filtered_posts = wl_core_get_posts(array('get' => 'posts', 'post__in' => $referencing_post_ids, 'related_to__in' => $filtering_entity_ids, 'post_type' => 'post', 'as' => 'subject'));
            foreach ($filtered_posts as $post_obj) {
                $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail'));
                $post_obj->thumbnail = $thumbnail ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
                $post_obj->permalink = get_post_permalink($post_obj->ID);
                $results[] = $post_obj;
            }
            $results = $filtered_posts;
        }
    } else {
        global $wpdb;
        wl_write_log("Going to find related entities for the current entity [ entity ID :: {$entity_id} ]");
        // Retrieve Wordlift relation instances table name
        $table_name = wl_core_get_relation_instances_table_name();
        $ids = implode(',', $referencing_post_ids);
        // TODO - if an entity is related with different predicates each predicate impacts on counter
        $query = <<<EOF
            SELECT object_id as ID, count( object_id ) as counter 
            FROM {$table_name} 
            WHERE subject_id IN ({$ids}) and object_id != {$entity_id}
            GROUP BY object_id;
EOF;
        wl_write_log("Going to find related entities for the current entity [ entity ID :: {$entity_id} ] [ query :: {$query} ]");
        $entities = $wpdb->get_results($query, OBJECT);
        wl_write_log("Entities found " . count($entities));
        foreach ($entities as $obj) {
            $entity = get_post($obj->ID);
            $entity = wl_serialize_entity($entity);
            $entity['counter'] = $obj->counter;
            $results[] = $entity;
        }
    }
    wl_core_send_json($results);
}