Completed
Push — develop ( ce465f...dc3813 )
by David
02:29 queued 11s
created

wordlift_analyzer.php ➔ wl_analyze_content()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
1
<?php
2
3
use Wordlift\Analysis\Response\Analysis_Response_Ops;
4
5
/**
6
 * Receive some content, run a remote analysis task and return the results. The content is read from the body
7
 * input (php://input).
8
 *
9
 * @since 1.0.0
10
 * @since 3.24.2 this function doesn't fail anymore. If an error occurres in the analysis, then an empty response
11
 *  is returned.
12
 * @uses  wl_analyze_content() to analyze the provided content.
13
 */
14
function wl_ajax_analyze_action() {
15
16
	check_admin_referer( 'wl_analyze' );
17
18
	$data      = filter_input( INPUT_POST, 'data' );
19
20
	wp_send_json_success( wl_analyze_content( $data ) );
21
22
}
23
24
add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' );
25
26
/**
27
 * Analyze the provided content. The analysis will make use of the method *wl_ajax_analyze_action*
28
 * provided by the WordLift plugin.
29
 *
30
 * @param string $data The data structure containing information about the content to analyze as a string.
31
 *
32
 * @return string Returns null on failure, or the WP_Error, or a WP_Response with the response.
33
 *
34
 * @uses  wl_configuration_get_analyzer_url() to get the API for the analysis.
35
 *
36
 * @since 1.0.0
37
 * @since 3.24.2 We don't return an error anymore, but an empty analysis response. This is required to allow the editor
38
 *   to manage entities or to manually add them even when analysis isn't available.
39
 */
40
function wl_analyze_content( $data ) {
41
42
	// Set the content type to the request content type or to text/plain by default.
43
	$content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain';
44
45
	add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' );
46
	$json = Wordlift_Api_Service::get_instance()
47
	                            ->post_custom_content_type( 'analysis/single', $data, $content_type );
48
	remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' );
49
50
	// If it's an error log it.
51
	if ( is_wp_error( $json ) ) {
52
		$request_body = json_decode( $data, true );
53
54
		return Analysis_Response_Ops::create( json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ) )
55
		                            ->make_entities_local()
56
		                            ->add_occurrences( $request_body['content'] )
57
		                            ->get_json();
58
	}
59
60
	/*
61
	 * We pass the response to the Analysis_Response_Ops to ensure that we make remote entities local.
62
	 *
63
	 * @see https://github.com/insideout10/wordlift-plugin/issues/944
64
	 * @since 3.21.5
65
	 */
66
67
	// Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate
68
	// the occurrences for the local entities.
69
	if ( 0 === strpos( $content_type, 'application/json' ) ) {
70
		$request_json    = json_decode( $data );
71
		$request_content = $request_json->content;
72
	} else {
73
		$request_content = $data;
74
	}
75
76
	return Analysis_Response_Ops::create( $json )
77
	                            ->make_entities_local()
78
	                            ->add_occurrences( $request_content )
79
	                            ->get_json();
80
81
}
82
83
function wl_use_analysis_on_api_wordlift_io( $value ) {
84
85
	return preg_replace( '|https://api\.wordlift\.it/|', 'https://api.wordlift.io/', $value );
86
}
87