|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Adapters: Batch Analysis Adapter. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 3.14.2 |
|
6
|
|
|
* @package Wordlift |
|
7
|
|
|
* @subpackage Wordlift/includes |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Define the {@link Wordlift_Batch_Analysis_Adapter} class. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 3.14.2 |
|
14
|
|
|
* @package Wordlift |
|
15
|
|
|
* @subpackage Wordlift/includes |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Batch_Analysis_Adapter { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.17.0 |
|
23
|
|
|
* @access private |
|
24
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $log; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The {@link Wordlift_Batch_Analysis_Service} instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 3.17.0 |
|
32
|
|
|
* @access private |
|
33
|
|
|
* @var \Wordlift_Batch_Analysis_Service $batch_analysis_service The {@link Wordlift_Batch_Analysis_Service} instance. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $batch_analysis_service; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Wordlift_Batch_Analysis_Adapter constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @since 3.14.2 |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Wordlift_Batch_Analysis_Service $batch_analysis_service |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( $batch_analysis_service ) { |
|
45
|
|
|
|
|
46
|
|
|
$this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
47
|
|
|
|
|
48
|
|
|
$this->batch_analysis_service = $batch_analysis_service; |
|
49
|
|
|
|
|
50
|
|
|
add_action( 'wp_ajax_wl_batch_analysis_complete', array( |
|
51
|
|
|
$this, |
|
52
|
|
|
'complete', |
|
53
|
|
|
) ); |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Submit the posts for batch analysis. |
|
59
|
|
|
* |
|
60
|
|
|
* @since 3.14.2 |
|
61
|
|
|
*/ |
|
62
|
|
|
public function submit() { |
|
63
|
|
|
|
|
64
|
|
|
// Get the parameters from the $_REQUEST. |
|
65
|
|
|
$params = self::create_params_from_request(); |
|
66
|
|
|
|
|
67
|
|
|
// Submit the request. |
|
68
|
|
|
$count = $this->batch_analysis_service->submit( $params ); |
|
69
|
|
|
|
|
70
|
|
|
// Clear any buffer. |
|
71
|
|
|
ob_clean(); |
|
72
|
|
|
|
|
73
|
|
|
// Send the response. |
|
74
|
|
|
wp_send_json_success( array( 'count' => $count ) ); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Submit the posts for batch analysis. |
|
80
|
|
|
* |
|
81
|
|
|
* @since 3.14.2 |
|
82
|
|
|
*/ |
|
83
|
|
|
public function submit_posts() { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$this->log->trace( 'Received Batch Analysis request for posts...' ); |
|
86
|
|
|
|
|
87
|
|
|
if ( empty( $_REQUEST['post'] ) ) { |
|
88
|
|
|
$this->log->error( 'Batch Analysis request for posts missing the post(s) id.' ); |
|
89
|
|
|
|
|
90
|
|
|
wp_send_json_error( 'The `post` parameter is required.' ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Get the parameters from the $_REQUEST. |
|
94
|
|
|
$params = self::create_params_from_request(); |
|
95
|
|
|
|
|
96
|
|
|
// Submit the request. |
|
97
|
|
|
$count = $this->batch_analysis_service->submit_posts( $params ); |
|
98
|
|
|
|
|
99
|
|
|
// Clear any buffer. |
|
100
|
|
|
ob_clean(); |
|
101
|
|
|
|
|
102
|
|
|
// Send the response. |
|
103
|
|
|
wp_send_json_success( array( 'count' => $count ) ); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function complete() { |
|
108
|
|
|
|
|
109
|
|
|
$this->batch_analysis_service->complete(); |
|
110
|
|
|
|
|
111
|
|
|
wp_send_json_success(); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* A helper function to create the parameters from the $_REQUEST. |
|
118
|
|
|
* |
|
119
|
|
|
* @since 3.17.0 |
|
120
|
|
|
* |
|
121
|
|
|
* @return array An array or parameters. |
|
122
|
|
|
*/ |
|
123
|
|
|
private static function create_params_from_request() { |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
// Build params array and check if param exists. |
|
126
|
|
|
// @codingStandardsIgnoreStart, Ignore phpcs indentation errors. |
|
127
|
|
|
$params = array( |
|
128
|
|
|
// Get the `links` parameter, or use `default` if not provided. |
|
129
|
|
|
'links' => isset( $_REQUEST['links'] ) ? $_REQUEST['links'] : 'default', |
|
130
|
|
|
// If `include_annotated` is set to `yes`, the set the parameter to true. |
|
131
|
|
|
'include_annotated' => isset( $_REQUEST['include_annotated'] ) && 'yes' === $_REQUEST['include_annotated'], |
|
132
|
|
|
// Set the minimum amount of occurrences, use `1` by default. |
|
133
|
|
|
'min_occurrences' => isset( $_REQUEST['min_occurrences'] ) && is_numeric( $_REQUEST['min_occurrences'] ) ? intval( $_REQUEST['min_occurrences'] ) : 1, |
|
134
|
|
|
// Set the `post_type` to `post` if none provided. |
|
135
|
|
|
'post_type' => isset( $_REQUEST['post_type'] ) ? (array) $_REQUEST['post_type'] : 'post', |
|
136
|
|
|
// Set the exclude array. |
|
137
|
|
|
'exclude' => isset( $_REQUEST['exclude'] ) ? (array) $_REQUEST['exclude'] : array(), |
|
138
|
|
|
// Set the `from` date, or null if not provided. |
|
139
|
|
|
'from' => isset( $_REQUEST['from'] ) ? $_REQUEST['from'] : null, |
|
140
|
|
|
// Set the `to` date, or null if not provided. |
|
141
|
|
|
'to' => isset( $_REQUEST['to'] ) ? $_REQUEST['to'] : null, |
|
142
|
|
|
// |
|
143
|
|
|
'ids' => isset( $_REQUEST['post'] ) ? wp_parse_id_list( (array) $_REQUEST['post'] ) : array(), |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
// @codingStandardsIgnoreEnd |
|
147
|
|
|
|
|
148
|
|
|
return $params; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Cancel the batch analysis for the specified post. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 3.14.0 |
|
155
|
|
|
*/ |
|
156
|
|
View Code Duplication |
public function cancel() { |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
if ( ! isset( $_REQUEST['post'] ) ) { |
|
159
|
|
|
wp_die( 'The `post` parameter is required.' ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] ); |
|
163
|
|
|
|
|
164
|
|
|
// Clear any buffer. |
|
165
|
|
|
ob_clean(); |
|
166
|
|
|
|
|
167
|
|
|
// Send the response. |
|
168
|
|
|
wp_send_json_success( array( 'count' => $count ) ); |
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Clear warnings for the specified post. |
|
174
|
|
|
* |
|
175
|
|
|
* @since 3.14.0 |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function clear_warning() { |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
if ( ! isset( $_REQUEST['post'] ) ) { |
|
180
|
|
|
wp_die( 'The `post` parameter is required.' ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] ); |
|
184
|
|
|
|
|
185
|
|
|
// Clear any buffer. |
|
186
|
|
|
ob_clean(); |
|
187
|
|
|
|
|
188
|
|
|
// Send the response. |
|
189
|
|
|
wp_send_json_success(); |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: