function test_wl_configuration_analyzer_url()
 {
     // Set the WordLift Key.
     $wordlift_key = uniqid();
     wl_configuration_set_key($wordlift_key);
     $this->assertEquals(WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "analyses?key={$wordlift_key}", wl_configuration_get_analyzer_url());
     // Set the Redlink Key.
     $redlink_key = uniqid();
     $redlink_application_name = uniqid();
     $redlink_api_url = uniqid();
     wl_configuration_set_key('');
     wl_configuration_set_redlink_key($redlink_key);
     wl_configuration_set_redlink_application_name($redlink_application_name);
     wl_configuration_set_api_url($redlink_api_url);
     $this->assertStringStartsWith("{$redlink_api_url}/analysis/{$redlink_application_name}/enhance?key={$redlink_key}", wl_configuration_get_analyzer_url());
 }
Пример #2
0
/**
 * Analyze the provided content. The analysis will make use of the method *wl_ajax_analyze_action*
 * provided by the WordLift plugin.
 *
 * @since 1.0.0
 *
 * @uses wl_configuration_get_analyzer_url() to get the API for the analysis.
 *
 * @param string $content The content to analyze.
 *
 * @return string Returns null on failure, or the WP_Error, or a WP_Response with the response.
 */
function wl_analyze_content($content)
{
    // Get the analyzer URL.
    $url = wl_configuration_get_analyzer_url();
    // Prepare the request.
    $args = array_merge_recursive(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array('method' => 'POST', 'headers' => array('Accept' => 'application/json', 'Content-type' => 'text/plain'), 'httpversion' => '1.0', 'body' => $content));
    $response = wp_remote_post($url, $args);
    // If an error has been raised, return the error.
    if (is_wp_error($response) || 200 !== (int) $response['response']['code']) {
        $body = is_wp_error($response) ? $response->get_error_message() : $response['body'];
        wl_write_log("error [ url :: {$url} ][ args :: ");
        wl_write_log(var_export($args, true));
        wl_write_log('][ response :: ');
        wl_write_log("\n" . var_export($response, true));
        wl_write_log("][ body :: ");
        wl_write_log("\n" . $body);
        wl_write_log("]");
        return null;
    }
    wl_write_log("[ url :: {$url} ][ response code :: " . $response['response']['code'] . " ]");
    return $response['body'];
}