function wl_ajax_sparql()
{
    header('Access-Control-Allow-Origin: *');
    // Get the query slug.
    $slug = $_GET['slug'];
    // Get the output format: csv or geojson.
    $format = empty($_GET['format']) ? 'csv' : $_GET['format'];
    // TODO: define an output format.
    $output = 'csv';
    $accept = 'text/csv';
    // Get the query.
    $query = wl_sparql_replace_params(wl_sparql_get_query_by_slug($slug), $_GET);
    $dataset = wl_sparql_get_query_dataset_by_slug($slug);
    // Print out the query if the debug flag is set.
    if (isset($_GET['_debug']) && 1 == $_GET['_debug']) {
        wp_die($query);
    }
    if (empty($query)) {
        wp_die("Cannot find a SPARQL query [ slug :: {$slug} ]");
    }
    $url = wl_configuration_get_query_select_url($output, $dataset) . urlencode($query);
    // Prepare the request.
    $args = array('method' => 'GET', 'headers' => array('Accept' => $accept));
    // Send the request. Raise actions before and after the request is being sent.
    do_action('wl_sparql_pre_request', $url, $args, $query);
    // Send the request via caching if the module is available.
    $response = function_exists('wl_caching_remote_request') ? wl_caching_remote_request($url, $args) : wp_remote_post($url, $args);
    do_action('wl_sparql_post_request', $url, $args, $query, $response);
    // If an error has been raised, return the error.
    if (is_wp_error($response) || 200 !== (int) $response['response']['code']) {
        echo "wl_execute_sparql_query ================================\n";
        if (is_wp_error($response)) {
            var_dump($response);
        } else {
            echo " request : \n";
            var_dump($args);
            echo " response: \n";
            var_dump($response);
            echo " response body: \n";
            echo $response['body'];
        }
        echo "=======================================================\n";
        return false;
    }
    // Output the data according to the chosen format.
    switch ($format) {
        case 'geojson':
            wl_csv_to_geojson($response['body']);
            break;
        case 'json':
            wl_csv_to_json($response['body']);
            break;
        default:
            echo $response['body'];
    }
    wp_die();
}
 function test_caching_expired()
 {
     $url = 'http://example.org/';
     $args = array('method' => 'GET');
     // ensure a previous cache file doesn't exists.
     $hash_0 = wl_caching_hash($url, $args);
     wl_caching_delete($hash_0);
     // Cache for 5 seconds.
     $response_0 = wl_caching_remote_request($url, $args, false, 5);
     #$this->assertFalse( wl_caching_response_is_cached( $response_0 ) );
     // Check that the first request is still cached.
     $response_1 = wl_caching_remote_request($url, $args);
     $this->assertTrue(wl_caching_response_is_cached($response_1));
     // Wait 5 seconds and check that another request is not cached.
     sleep(5);
     $response_2 = wl_caching_remote_request($url, $args);
     $this->assertFalse(wl_caching_response_is_cached($response_2));
 }
/**
 * Load a remote graph. A suffix is added automatically to the URL using the $wl_entity_view_suffix.
 *
 * @since 3.0.0
 *
 * @param string $url The URL.
 * @return null|object A graph instance or null if the JSON is invalid.
 */
function wl_jsonld_load_remote($url)
{
    global $wl_entity_view_suffix;
    // TODO: validate the URI.
    // Build the full URI.
    $url_with_suffix = $url . $wl_entity_view_suffix;
    wl_write_log("Loading URL [ url :: {$url_with_suffix} ]");
    // Use the caching method if it's loaded.
    if (function_exists('wl_caching_remote_request')) {
        $response = wl_caching_remote_request($url_with_suffix, array('method' => 'GET'));
    } else {
        $response = wp_remote_get($url_with_suffix);
    }
    // TODO: handle errors.
    if (is_wp_error($response)) {
        wl_write_log("An error occurred loading URL [ url :: {$url_with_suffix} ]");
        wp_die(var_export($response, true));
    }
    // Decode the JSON and return it.
    $json = json_decode($response['body'], true);
    // The json is invalid.
    if (null === $json) {
        wl_write_log("Cannot decode the JSON [ url :: {$url_with_suffix} ]");
        return null;
    }
    // The JSON doesn't have a graph.
    if (!isset($json[0]['@graph'][0])) {
        wl_write_log("Cannot locate the graph [ url :: {$url_with_suffix} ]");
        return null;
    }
    return $json[0]['@graph'][0];
}