/**
 * Save the post to the triple store. Also saves the entities locally and on the triple store.
 *
 * @since 3.0.0
 *
 * @param int $post_id The post id being saved.
 */
function wl_linked_data_save_post_and_related_entities($post_id)
{
    // Ignore auto-saves
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // get the current post.
    $post = get_post($post_id);
    remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
    wl_write_log("[ post id :: {$post_id} ][ autosave :: false ][ post type :: {$post->post_type} ]");
    // Store mapping between tmp new entities uris and real new entities uri
    $entities_uri_mapping = array();
    // Store classification box mapping
    $entities_predicates_mapping = null;
    // Save the entities coming with POST data.
    if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) {
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_entities) :: ");
        wl_write_log(json_encode($_POST['wl_entities']));
        wl_write_log("]");
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_boxes) :: ");
        wl_write_log(json_encode($_POST['wl_boxes'], true));
        wl_write_log("]");
        $entities_via_post = $_POST['wl_entities'];
        $boxes_via_post = $_POST['wl_boxes'];
        foreach ($entities_via_post as $entity_uri => $entity) {
            // Local entities have a tmp uri with 'local-entity-'
            // These uris need to be rewritten here and replaced in the content
            if (preg_match('/^local-entity-.+/', $entity_uri) > 0) {
                // Build the proper uri
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($entity['label']));
                // Populate the mapping
                $entities_uri_mapping[$entity_uri] = $uri;
                // Override the entity obj
                $entities_via_post[$entity_uri]['uri'] = $uri;
            }
        }
        // Populate the $entities_predicates_mapping
        // Local Redlink uris need to be used here
        foreach ($boxes_via_post as $predicate => $entity_uris) {
            foreach ($entity_uris as $entity_uri) {
                wl_write_log("Going to map predicates for uri {$entity_uri} ");
                // Retrieve the entity label needed to build the uri
                $label = $entities_via_post[stripslashes($entity_uri)]['label'];
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label));
                wl_write_log("Going to map predicate {$predicate} to uri {$uri} ");
                $entities_predicates_mapping[$uri][] = $predicate;
            }
        }
        // Save entities and push them to Redlink
        // TODO: pass also latitude, longitude, etc.
        wl_save_entities(array_values($entities_via_post), $post_id);
    }
    // Replace tmp uris in content post if needed
    $updated_post_content = $post->post_content;
    // Save each entity and store the post id.
    foreach ($entities_uri_mapping as $tmp_uri => $uri) {
        $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
    }
    // Update the post content
    wp_update_post(array('ID' => $post->ID, 'post_content' => $updated_post_content));
    // Extract related/referenced entities from text.
    $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content);
    // Reset previously saved instances
    wl_core_delete_relation_instances($post_id);
    // Save relation instances
    foreach (array_unique($disambiguated_entities) as $referenced_entity_id) {
        wl_write_log(" Going to manage relation between Post {$post_id} and {$referenced_entity_id}");
        if ($entities_predicates_mapping) {
            wl_write_log(" Going to manage relation instances according to the following mapping");
            // Retrieve the entity uri
            $referenced_entity_uri = wl_get_entity_uri($referenced_entity_id);
            // Retrieve predicates for the current uri
            if (isset($entities_predicates_mapping[$referenced_entity_uri])) {
                foreach ($entities_predicates_mapping[$referenced_entity_uri] as $predicate) {
                    wl_write_log(" Going to add relation with predicate {$predicate}");
                    wl_core_add_relation_instance($post_id, $predicate, $referenced_entity_id);
                }
            } else {
                wl_write_log("Entity uri {$referenced_entity_uri} missing in the mapping");
                wl_write_log($entities_predicates_mapping);
            }
        } else {
            // Just for unit tests
            wl_core_add_relation_instance($post_id, 'what', $referenced_entity_id);
        }
        // TODO Check if is needed
        wl_linked_data_push_to_redlink($referenced_entity_id);
    }
    // Push the post to Redlink.
    wl_linked_data_push_to_redlink($post->ID);
    add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
}
 function testWlCoreDeleteRelationInstances()
 {
     // Create a post and an entity
     $post_id = wl_create_post('', 'post1', 'A post');
     $entity_id = wl_create_post('', 'entity1', 'An Entity', 'draft', 'entity');
     // No relations at this point
     $result = wl_tests_get_relation_instances_for($post_id);
     $this->assertCount(0, $result);
     // Insert relation and verify it
     $result = wl_core_add_relation_instance($post_id, WL_WHAT_RELATION, $entity_id);
     $this->assertTrue(is_numeric($result));
     // The methods return a record id
     $result = wl_core_add_relation_instance($post_id, WL_WHO_RELATION, $entity_id);
     $this->assertTrue(is_numeric($result));
     // The methods return a record id
     $result = wl_tests_get_relation_instances_for($post_id);
     $this->assertCount(2, $result);
     $result = wl_core_delete_relation_instances($post_id);
     $this->assertTrue($result);
     $result = wl_tests_get_relation_instances_for($post_id);
     $this->assertCount(0, $result);
 }