Completed
Push — develop ( de39e4...30f360 )
by David
10:51 queued 07:41
created
src/includes/batch-analysis/class-wordlift-batch-analysis-service.php 2 patches
Indentation   +716 added lines, -716 removed lines patch added patch discarded remove patch
@@ -51,519 +51,519 @@  discard block
 block discarded – undo
51 51
  */
52 52
 class Wordlift_Batch_Analysis_Service {
53 53
 
54
-	/**
55
-	 * The list of states for the Batch Analysis:
56
-	 *  - STATE_META_KEY: the batch analysis state meta key,
57
-	 *  - STATE_SUBMIT: a post/page has been submitted for analysis,
58
-	 *  - STATE_REQUEST: the plugin requested an analysis for the submitted
59
-	 *      post/page,
60
-	 *  - STATE_SUCCESS: the analysis has completed successfully,
61
-	 *  - STATE_ERROR: the analysis returned an error.
62
-	 *
63
-	 * @since 3.14.2
64
-	 */
65
-	const STATE_META_KEY = '_wl_batch_analysis_state';
66
-	const STATE_SUBMIT = 0;
67
-	const STATE_REQUEST = 1;
68
-	// ### COMPLETE states.
69
-	const STATE_SUCCESS = 2;
70
-	const STATE_ERROR = 2;
71
-
72
-	/**
73
-	 * The submit timestamp meta key. A post may have more than one timestamp.
74
-	 *
75
-	 * @since 3.14.2
76
-	 */
77
-	const SUBMIT_TIMESTAMP_META_KEY = '_wl_batch_analysis_submit_timestamp';
78
-
79
-	/**
80
-	 * The request timestamp meta key. A post may have more than one timestamp.
81
-	 *
82
-	 * @since 3.14.2
83
-	 */
84
-	const REQUEST_TIMESTAMP_META_KEY = '_wl_batch_analysis_request_timestamp';
85
-
86
-	/**
87
-	 * The complete (success or error) timestamp meta key. A post may have more
88
-	 * than one timestamp.
89
-	 *
90
-	 * @since 3.14.2
91
-	 */
92
-	const COMPLETE_TIMESTAMP_META_KEY = '_wl_batch_analysis_complete_timestamp';
93
-
94
-	/**
95
-	 * The options setting meta key. A post may have more than one setting.
96
-	 *
97
-	 * @since 3.14.2
98
-	 */
99
-	const BATCH_ANALYSIS_OPTIONS_META_KEY = '_wl_batch_analysis_options';
100
-
101
-	/**
102
-	 * The warning timestamp meta key. A post has only zero/one value.
103
-	 *
104
-	 * @since 3.14.2
105
-	 */
106
-	const WARNING_META_KEY = '_wl_batch_analysis_warning';
107
-
108
-	/**
109
-	 * Option name.
110
-	 *
111
-	 * @since  3.14.0
112
-	 */
113
-	const OPTION_NAME = 'wl_analyze_batch';
114
-
115
-	/**
116
-	 * Name of waiting to be processed queue array inside the option.
117
-	 *
118
-	 * @since  3.14.0
119
-	 */
120
-	const ANALYZE_QUEUE = 'queue';
121
-
122
-	/**
123
-	 * Name of waiting for response queue array inside the option.
124
-	 *
125
-	 * @since  3.14.0
126
-	 */
127
-	const RESPONSE_QUEUE = 'processing';
128
-
129
-	/**
130
-	 * Regular expressions that match interpolation errors.
131
-	 *
132
-	 * @since  3.17.0
133
-	 */
134
-	private static $interpolation_patterns = array(
135
-		// Matches word before the annotation.
136
-		'~(\w)<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>~',
137
-		// Matches word after the annotation.
138
-		'~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>(\w)~',
139
-		// Matches space in the beginning of annotation name.
140
-		'~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(\s)(.*?)<\/[a-z]+>~',
141
-	);
142
-
143
-	/**
144
-	 * The {@link Wordlift} plugin instance.
145
-	 *
146
-	 * @since  3.14.0
147
-	 * @access private
148
-	 * @var \Wordlift $plugin The {@link Wordlift} plugin instance.
149
-	 */
150
-	private $plugin;
151
-
152
-	/**
153
-	 * The {@link Wordlift_Configuration_Service} instance.
154
-	 *
155
-	 * @since  3.14.0
156
-	 * @access private
157
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
158
-	 */
159
-	private $configuration_service;
160
-
161
-	/**
162
-	 * The {@link Wordlift_Cache_Service} instance.
163
-	 *
164
-	 * @since  3.17.0
165
-	 * @access protected
166
-	 * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance.
167
-	 */
168
-	private $cache_service;
169
-
170
-	/**
171
-	 * A {@link Wordlift_Log_Service} instance.
172
-	 *
173
-	 * @since  3.14.2
174
-	 * @access private
175
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
176
-	 */
177
-	private $log;
178
-
179
-	/**
180
-	 * The {@link Class_Wordlift_Batch_Analys_Service} instance.
181
-	 *
182
-	 * @since 3.14.0
183
-	 *
184
-	 * @param \Wordlift                       $plugin                The {@link Wordlift} plugin instance.
185
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
186
-	 * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} instance.
187
-	 */
188
-	public function __construct( $plugin, $configuration_service, $cache_service ) {
189
-
190
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Service' );
191
-
192
-		$this->plugin                = $plugin;
193
-		$this->configuration_service = $configuration_service;
194
-		$this->cache_service         = $cache_service;
195
-
196
-		add_action( 'wl_async_wl_batch_analysis_request', array(
197
-			$this,
198
-			'request',
199
-		) );
200
-		add_action( 'wl_async_wl_batch_analysis_complete', array(
201
-			$this,
202
-			'complete',
203
-		) );
204
-
205
-	}
206
-
207
-	/**
208
-	 * Submit posts for Batch Analysis.
209
-	 *
210
-	 * @since 3.14.2
211
-	 *
212
-	 * @param array       $args               {
213
-	 *                                        A list of options for the Batch Analysis.
214
-	 *
215
-	 * @type string       $link               Either `default`, `no` or `yes` (`default` is used if not specified):
216
-	 *                                        * `default` doesn't set the link option - entities
217
-	 *                                           will be linked if configured so in WordLift settings.
218
-	 *                                        * `yes` links the entities.
219
-	 *                                        * `no` doesn't link the entities.
220
-	 *                                        This value is forwarded to WLS' Batch Analysis end-point.
221
-	 * @type int          $min_occurrences    The minimum number of occurrences to select
222
-	 *                                        an entity. Default `1`.
223
-	 * @type bool         $include_annotated  Whether to include annotated posts in selection.
224
-	 *                                        Default `false`.
225
-	 * @type array|int    $include            Explicitly include the specified {@link WP_Post}s.
226
-	 * @type array|int    $exclude            Explicitly exclude the specified {@link WP_Post}s.
227
-	 * @type string|null  $from               An optional date from filter (used in `post_date_gmt`).
228
-	 * @type string|null  $to                 An optional date from filter (used in `post_date_gmt`).
229
-	 * @type array|string $post_type          Specify the post type(s), by default only `post`.
230
-	 *                      }
231
-	 *
232
-	 * @return int The number of submitted {@link WP_Post}s or false on error.
233
-	 */
234
-	public function submit( $args ) {
235
-		global $wpdb;
236
-
237
-		// Parse the parameters.
238
-		$params = wp_parse_args( $args, array(
239
-			'links'             => 'default',
240
-			'min_occurrences'   => 1,
241
-			'include_annotated' => false,
242
-			'exclude'           => array(),
243
-			'from'              => null,
244
-			'to'                => null,
245
-			'post_type'         => 'post',
246
-		) );
247
-
248
-		// Validation.
249
-		if ( ! in_array( $params['links'], array( 'default', 'yes', 'no' ) ) ) {
250
-			wp_die( '`link` must be one of the following: `default`, `yes` or `no`.' );
251
-		}
252
-
253
-		if ( ! is_numeric( $params['min_occurrences'] ) || 1 > $params['min_occurrences'] ) {
254
-			wp_die( '`min_occurrences` must greater or equal 1.' );
255
-		}
256
-
257
-		// Submit the posts/pages and return the number of affected results.
258
-		// We're using a SQL query here because we could have potentially
259
-		// thousands of rows.
260
-		$count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql( $params ) ); // WPCS: cache ok, db call ok.
261
-
262
-		// Request Batch Analysis (the operation is handled asynchronously).
263
-		do_action( 'wl_batch_analysis_request' );
264
-
265
-		// Divide the count by 3 to get the number of posts/pages queued.
266
-		return $count / 3;
267
-	}
268
-
269
-	/**
270
-	 * Submit one or more {@link WP_Posts} for Batch Analysis.
271
-	 *
272
-	 * @param array    $args            {
273
-	 *                                  An array of arguments.
274
-	 *
275
-	 * @type string    $link            The link option: `default`, `yes` or
276
-	 *                                  `no`. If not set `default`.
277
-	 * @type int       $min_occurrences The minimum number of occurrences. If
278
-	 *                                  not set `1`.
279
-	 * @type array|int $ids             An array of {@link WP_Post}s' ids or one
280
-	 *                                  single numeric {@link WP_Post} id.
281
-	 *                    }
282
-	 *
283
-	 * @return float|int
284
-	 */
285
-	public function submit_posts( $args ) {
286
-		global $wpdb;
287
-
288
-		// Parse the parameters.
289
-		$params = wp_parse_args( $args, array(
290
-			'links'           => 'default',
291
-			'min_occurrences' => 1,
292
-			'ids'             => array(),
293
-		) );
294
-
295
-		// Validation.
296
-		if ( empty( $params['ids'] ) ) {
297
-			wp_die( '`ids` cannot be empty.' );
298
-		}
299
-
300
-		// Submit the posts/pages and return the number of affected results.
301
-		// We're using a SQL query here because we could have potentially
302
-		// thousands of rows.
303
-		$count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql_for_ids( $params ) ); // WPCS: cache ok, db call ok.
304
-
305
-		// Request Batch Analysis (the operation is handled asynchronously).
306
-		do_action( 'wl_batch_analysis_request' );
307
-
308
-		// Divide the count by 3 to get the number of posts/pages queued.
309
-		return $count / 3;
310
-	}
311
-
312
-	/**
313
-	 * Cancel the Batch Analysis request for the specified {@link WP_Post}s.
314
-	 *
315
-	 * @since 3.14.2
316
-	 *
317
-	 * @param int|array $post_ids A single {@link WP_Post}'s id or an array of
318
-	 *                            {@link WP_Post}s' ids.
319
-	 *
320
-	 * @return false|int The number of cancelled {@link WP_Post}s or false on
321
-	 *                   error.
322
-	 */
323
-	public function cancel( $post_ids ) {
324
-		global $wpdb;
325
-
326
-		return $wpdb->query( $wpdb->prepare(
327
-			"
54
+    /**
55
+     * The list of states for the Batch Analysis:
56
+     *  - STATE_META_KEY: the batch analysis state meta key,
57
+     *  - STATE_SUBMIT: a post/page has been submitted for analysis,
58
+     *  - STATE_REQUEST: the plugin requested an analysis for the submitted
59
+     *      post/page,
60
+     *  - STATE_SUCCESS: the analysis has completed successfully,
61
+     *  - STATE_ERROR: the analysis returned an error.
62
+     *
63
+     * @since 3.14.2
64
+     */
65
+    const STATE_META_KEY = '_wl_batch_analysis_state';
66
+    const STATE_SUBMIT = 0;
67
+    const STATE_REQUEST = 1;
68
+    // ### COMPLETE states.
69
+    const STATE_SUCCESS = 2;
70
+    const STATE_ERROR = 2;
71
+
72
+    /**
73
+     * The submit timestamp meta key. A post may have more than one timestamp.
74
+     *
75
+     * @since 3.14.2
76
+     */
77
+    const SUBMIT_TIMESTAMP_META_KEY = '_wl_batch_analysis_submit_timestamp';
78
+
79
+    /**
80
+     * The request timestamp meta key. A post may have more than one timestamp.
81
+     *
82
+     * @since 3.14.2
83
+     */
84
+    const REQUEST_TIMESTAMP_META_KEY = '_wl_batch_analysis_request_timestamp';
85
+
86
+    /**
87
+     * The complete (success or error) timestamp meta key. A post may have more
88
+     * than one timestamp.
89
+     *
90
+     * @since 3.14.2
91
+     */
92
+    const COMPLETE_TIMESTAMP_META_KEY = '_wl_batch_analysis_complete_timestamp';
93
+
94
+    /**
95
+     * The options setting meta key. A post may have more than one setting.
96
+     *
97
+     * @since 3.14.2
98
+     */
99
+    const BATCH_ANALYSIS_OPTIONS_META_KEY = '_wl_batch_analysis_options';
100
+
101
+    /**
102
+     * The warning timestamp meta key. A post has only zero/one value.
103
+     *
104
+     * @since 3.14.2
105
+     */
106
+    const WARNING_META_KEY = '_wl_batch_analysis_warning';
107
+
108
+    /**
109
+     * Option name.
110
+     *
111
+     * @since  3.14.0
112
+     */
113
+    const OPTION_NAME = 'wl_analyze_batch';
114
+
115
+    /**
116
+     * Name of waiting to be processed queue array inside the option.
117
+     *
118
+     * @since  3.14.0
119
+     */
120
+    const ANALYZE_QUEUE = 'queue';
121
+
122
+    /**
123
+     * Name of waiting for response queue array inside the option.
124
+     *
125
+     * @since  3.14.0
126
+     */
127
+    const RESPONSE_QUEUE = 'processing';
128
+
129
+    /**
130
+     * Regular expressions that match interpolation errors.
131
+     *
132
+     * @since  3.17.0
133
+     */
134
+    private static $interpolation_patterns = array(
135
+        // Matches word before the annotation.
136
+        '~(\w)<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>~',
137
+        // Matches word after the annotation.
138
+        '~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>(\w)~',
139
+        // Matches space in the beginning of annotation name.
140
+        '~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(\s)(.*?)<\/[a-z]+>~',
141
+    );
142
+
143
+    /**
144
+     * The {@link Wordlift} plugin instance.
145
+     *
146
+     * @since  3.14.0
147
+     * @access private
148
+     * @var \Wordlift $plugin The {@link Wordlift} plugin instance.
149
+     */
150
+    private $plugin;
151
+
152
+    /**
153
+     * The {@link Wordlift_Configuration_Service} instance.
154
+     *
155
+     * @since  3.14.0
156
+     * @access private
157
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
158
+     */
159
+    private $configuration_service;
160
+
161
+    /**
162
+     * The {@link Wordlift_Cache_Service} instance.
163
+     *
164
+     * @since  3.17.0
165
+     * @access protected
166
+     * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance.
167
+     */
168
+    private $cache_service;
169
+
170
+    /**
171
+     * A {@link Wordlift_Log_Service} instance.
172
+     *
173
+     * @since  3.14.2
174
+     * @access private
175
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
176
+     */
177
+    private $log;
178
+
179
+    /**
180
+     * The {@link Class_Wordlift_Batch_Analys_Service} instance.
181
+     *
182
+     * @since 3.14.0
183
+     *
184
+     * @param \Wordlift                       $plugin                The {@link Wordlift} plugin instance.
185
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
186
+     * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} instance.
187
+     */
188
+    public function __construct( $plugin, $configuration_service, $cache_service ) {
189
+
190
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Service' );
191
+
192
+        $this->plugin                = $plugin;
193
+        $this->configuration_service = $configuration_service;
194
+        $this->cache_service         = $cache_service;
195
+
196
+        add_action( 'wl_async_wl_batch_analysis_request', array(
197
+            $this,
198
+            'request',
199
+        ) );
200
+        add_action( 'wl_async_wl_batch_analysis_complete', array(
201
+            $this,
202
+            'complete',
203
+        ) );
204
+
205
+    }
206
+
207
+    /**
208
+     * Submit posts for Batch Analysis.
209
+     *
210
+     * @since 3.14.2
211
+     *
212
+     * @param array       $args               {
213
+     *                                        A list of options for the Batch Analysis.
214
+     *
215
+     * @type string       $link               Either `default`, `no` or `yes` (`default` is used if not specified):
216
+     *                                        * `default` doesn't set the link option - entities
217
+     *                                           will be linked if configured so in WordLift settings.
218
+     *                                        * `yes` links the entities.
219
+     *                                        * `no` doesn't link the entities.
220
+     *                                        This value is forwarded to WLS' Batch Analysis end-point.
221
+     * @type int          $min_occurrences    The minimum number of occurrences to select
222
+     *                                        an entity. Default `1`.
223
+     * @type bool         $include_annotated  Whether to include annotated posts in selection.
224
+     *                                        Default `false`.
225
+     * @type array|int    $include            Explicitly include the specified {@link WP_Post}s.
226
+     * @type array|int    $exclude            Explicitly exclude the specified {@link WP_Post}s.
227
+     * @type string|null  $from               An optional date from filter (used in `post_date_gmt`).
228
+     * @type string|null  $to                 An optional date from filter (used in `post_date_gmt`).
229
+     * @type array|string $post_type          Specify the post type(s), by default only `post`.
230
+     *                      }
231
+     *
232
+     * @return int The number of submitted {@link WP_Post}s or false on error.
233
+     */
234
+    public function submit( $args ) {
235
+        global $wpdb;
236
+
237
+        // Parse the parameters.
238
+        $params = wp_parse_args( $args, array(
239
+            'links'             => 'default',
240
+            'min_occurrences'   => 1,
241
+            'include_annotated' => false,
242
+            'exclude'           => array(),
243
+            'from'              => null,
244
+            'to'                => null,
245
+            'post_type'         => 'post',
246
+        ) );
247
+
248
+        // Validation.
249
+        if ( ! in_array( $params['links'], array( 'default', 'yes', 'no' ) ) ) {
250
+            wp_die( '`link` must be one of the following: `default`, `yes` or `no`.' );
251
+        }
252
+
253
+        if ( ! is_numeric( $params['min_occurrences'] ) || 1 > $params['min_occurrences'] ) {
254
+            wp_die( '`min_occurrences` must greater or equal 1.' );
255
+        }
256
+
257
+        // Submit the posts/pages and return the number of affected results.
258
+        // We're using a SQL query here because we could have potentially
259
+        // thousands of rows.
260
+        $count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql( $params ) ); // WPCS: cache ok, db call ok.
261
+
262
+        // Request Batch Analysis (the operation is handled asynchronously).
263
+        do_action( 'wl_batch_analysis_request' );
264
+
265
+        // Divide the count by 3 to get the number of posts/pages queued.
266
+        return $count / 3;
267
+    }
268
+
269
+    /**
270
+     * Submit one or more {@link WP_Posts} for Batch Analysis.
271
+     *
272
+     * @param array    $args            {
273
+     *                                  An array of arguments.
274
+     *
275
+     * @type string    $link            The link option: `default`, `yes` or
276
+     *                                  `no`. If not set `default`.
277
+     * @type int       $min_occurrences The minimum number of occurrences. If
278
+     *                                  not set `1`.
279
+     * @type array|int $ids             An array of {@link WP_Post}s' ids or one
280
+     *                                  single numeric {@link WP_Post} id.
281
+     *                    }
282
+     *
283
+     * @return float|int
284
+     */
285
+    public function submit_posts( $args ) {
286
+        global $wpdb;
287
+
288
+        // Parse the parameters.
289
+        $params = wp_parse_args( $args, array(
290
+            'links'           => 'default',
291
+            'min_occurrences' => 1,
292
+            'ids'             => array(),
293
+        ) );
294
+
295
+        // Validation.
296
+        if ( empty( $params['ids'] ) ) {
297
+            wp_die( '`ids` cannot be empty.' );
298
+        }
299
+
300
+        // Submit the posts/pages and return the number of affected results.
301
+        // We're using a SQL query here because we could have potentially
302
+        // thousands of rows.
303
+        $count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql_for_ids( $params ) ); // WPCS: cache ok, db call ok.
304
+
305
+        // Request Batch Analysis (the operation is handled asynchronously).
306
+        do_action( 'wl_batch_analysis_request' );
307
+
308
+        // Divide the count by 3 to get the number of posts/pages queued.
309
+        return $count / 3;
310
+    }
311
+
312
+    /**
313
+     * Cancel the Batch Analysis request for the specified {@link WP_Post}s.
314
+     *
315
+     * @since 3.14.2
316
+     *
317
+     * @param int|array $post_ids A single {@link WP_Post}'s id or an array of
318
+     *                            {@link WP_Post}s' ids.
319
+     *
320
+     * @return false|int The number of cancelled {@link WP_Post}s or false on
321
+     *                   error.
322
+     */
323
+    public function cancel( $post_ids ) {
324
+        global $wpdb;
325
+
326
+        return $wpdb->query( $wpdb->prepare(
327
+            "
328 328
 			DELETE FROM $wpdb->postmeta
329 329
 			WHERE meta_key = %s
330 330
 				AND meta_value IN ( %d, %d )
331 331
 				AND post_id IN( " . implode( ',', wp_parse_id_list( $post_ids ) ) . " )
332 332
 			",
333
-			self::STATE_META_KEY,
334
-			self::STATE_SUBMIT,
335
-			self::STATE_REQUEST
336
-		) ); // WPCS: cache ok, db call ok.
333
+            self::STATE_META_KEY,
334
+            self::STATE_SUBMIT,
335
+            self::STATE_REQUEST
336
+        ) ); // WPCS: cache ok, db call ok.
337 337
 
338
-	}
338
+    }
339 339
 
340
-	/**
341
-	 * Request the batch analysis for submitted posts.
342
-	 *
343
-	 * @since 3.14.2
344
-	 */
345
-	public function request() {
340
+    /**
341
+     * Request the batch analysis for submitted posts.
342
+     *
343
+     * @since 3.14.2
344
+     */
345
+    public function request() {
346 346
 
347
-		$this->log->debug( 'Requesting analysis...' );
347
+        $this->log->debug( 'Requesting analysis...' );
348 348
 
349
-		// By default 5 posts of any post type are returned.
350
-		$posts = get_posts( array(
351
-			'fields'     => 'ids',
352
-			'meta_key'   => self::STATE_META_KEY,
353
-			'meta_value' => self::STATE_SUBMIT,
354
-			'orderby'    => 'ID',
355
-			'post_type'  => 'any',
356
-		) );
349
+        // By default 5 posts of any post type are returned.
350
+        $posts = get_posts( array(
351
+            'fields'     => 'ids',
352
+            'meta_key'   => self::STATE_META_KEY,
353
+            'meta_value' => self::STATE_SUBMIT,
354
+            'orderby'    => 'ID',
355
+            'post_type'  => 'any',
356
+        ) );
357 357
 
358
-		// Bail out if there are no submitted posts.
359
-		if ( empty( $posts ) ) {
360
-			$this->log->debug( 'No posts to submit found, checking for completed requests...' );
361
-
362
-			do_action( 'wl_batch_analysis_complete' );
358
+        // Bail out if there are no submitted posts.
359
+        if ( empty( $posts ) ) {
360
+            $this->log->debug( 'No posts to submit found, checking for completed requests...' );
361
+
362
+            do_action( 'wl_batch_analysis_complete' );
363 363
 
364
-			return;
365
-		}
364
+            return;
365
+        }
366 366
 
367
-		// Send a request for each post.
368
-		foreach ( $posts as $id ) {
367
+        // Send a request for each post.
368
+        foreach ( $posts as $id ) {
369 369
 
370
-			$this->log->debug( "Requesting analysis for post $id..." );
370
+            $this->log->debug( "Requesting analysis for post $id..." );
371 371
 
372
-			// Send the actual request to the remote service.
373
-			$result = $this->do_request( $id );
372
+            // Send the actual request to the remote service.
373
+            $result = $this->do_request( $id );
374 374
 
375
-			// Set an error if we received an error.
376
-			if ( is_wp_error( $result ) ) {
377
-				$this->log->error( "An error occurred while requesting a batch analysis for post $id: " . $result->get_error_message() );
375
+            // Set an error if we received an error.
376
+            if ( is_wp_error( $result ) ) {
377
+                $this->log->error( "An error occurred while requesting a batch analysis for post $id: " . $result->get_error_message() );
378 378
 
379
-				$this->set_state( $id, self::STATE_ERROR );
380
-			}
379
+                $this->set_state( $id, self::STATE_ERROR );
380
+            }
381 381
 
382
-		}
382
+        }
383 383
 
384
-		// Call the `wl_batch_analysis_request` action again. This is going
385
-		// to be handled by the async task.
386
-		do_action( 'wl_batch_analysis_request' );
384
+        // Call the `wl_batch_analysis_request` action again. This is going
385
+        // to be handled by the async task.
386
+        do_action( 'wl_batch_analysis_request' );
387 387
 
388
-	}
388
+    }
389 389
 
390
-	/**
391
-	 * Get the results for the Batch Analysis.
392
-	 *
393
-	 * @since 3.14.2
394
-	 */
395
-	public function complete() {
396
-
397
-		$this->log->debug( 'Requesting results...' );
398
-
399
-		// By default 5 posts of any post type are returned.
400
-		$posts = get_posts( array(
401
-			'fields'     => 'ids',
402
-			'meta_key'   => self::STATE_META_KEY,
403
-			'meta_value' => self::STATE_REQUEST,
404
-			'orderby'    => 'ID',
405
-			'post_type'  => 'any',
406
-		) );
390
+    /**
391
+     * Get the results for the Batch Analysis.
392
+     *
393
+     * @since 3.14.2
394
+     */
395
+    public function complete() {
396
+
397
+        $this->log->debug( 'Requesting results...' );
398
+
399
+        // By default 5 posts of any post type are returned.
400
+        $posts = get_posts( array(
401
+            'fields'     => 'ids',
402
+            'meta_key'   => self::STATE_META_KEY,
403
+            'meta_value' => self::STATE_REQUEST,
404
+            'orderby'    => 'ID',
405
+            'post_type'  => 'any',
406
+        ) );
407 407
 
408
-		// Bail out if there are no submitted posts.
409
-		if ( empty( $posts ) ) {
410
-			$this->log->debug( 'No posts in request state found.' );
408
+        // Bail out if there are no submitted posts.
409
+        if ( empty( $posts ) ) {
410
+            $this->log->debug( 'No posts in request state found.' );
411 411
 
412
-			return;
413
-		}
412
+            return;
413
+        }
414 414
 
415
-		// Send a request for each post.
416
-		foreach ( $posts as $id ) {
417
-			$this->log->debug( "Requesting results for post $id..." );
418
-
419
-			// Send the actual request to the remote service.
420
-			$response = $this->do_complete( $id );
421
-
422
-			// Move to the next item if we don't have a reply for this one.
423
-			if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
424
-				continue;
425
-			}
426
-
427
-			$this->log->debug( "Results received for post $id." );
428
-
429
-			// Save the returned content as new revision.
430
-			$json = json_decode( $response['body'] );
431
-
432
-			// Continue if the content isn't set.
433
-			if ( empty( $json->content ) ) {
434
-				// The post content is empty, so is should be marked as completed.
435
-				$this->log->error( "An error occurred while decoding the batch analysis response for post $id." );
436
-
437
-				$this->set_state( $id, self::STATE_ERROR );
438
-				continue;
439
-			}
440
-
441
-			// Set the warning flag if needed.
442
-			$this->set_warning_based_on_content( $json->content, $id );
443
-
444
-			// Get the content, cleaned up if there are interpolation errors.
445
-			$pre_content = $this->fix_interpolation_errors( $json->content, $id );
446
-
447
-			/**
448
-			 * Filter: 'wl_batch_analysis_update_post_content' - Allow third
449
-			 * parties to perform additional actions when the post content is
450
-			 * updated.
451
-			 *
452
-			 * @since  3.17.0
453
-			 * @api    string $data The {@link WP_Post}'s content.
454
-			 * @api    int    $id   The {@link WP_Post}'s id.
455
-			 */
456
-			$content = apply_filters( 'wl_batch_analysis_update_post_content', $pre_content, $id );
457
-
458
-			// Update the post content.
459
-			wp_update_post( array(
460
-				'ID'           => $id,
461
-				'post_content' => wp_slash( $content ),
462
-			) );
463
-
464
-			// Update the status.
465
-			$this->set_state( $id, self::STATE_SUCCESS );
466
-
467
-			// Invalidating the cache for the current post.
468
-			$this->cache_service->delete_cache( $id );
469
-
470
-			$this->log->debug( "Post $id updated with batch analysis results." );
471
-
472
-			// Set default entity type term for posts that didn't have any.
473
-			$this->maybe_set_default_term( $id );
474
-
475
-			// @todo: implement a kind of timeout that sets an error if the
476
-			// results haven't been received after a long time.
477
-		}
478
-
479
-		// Call the `wl_batch_analysis_request` action again. This is going
480
-		// to be handled by the async task.
481
-		do_action( 'wl_batch_analysis_complete' );
482
-
483
-	}
484
-
485
-	/**
486
-	 * Set a warning flag on the {@link WP_Post} if its content has suspicious
487
-	 * interpolations.
488
-	 *
489
-	 * @since 3.14.2
490
-	 *
491
-	 * @param string $content The {@link WP_Post}'s content.
492
-	 * @param int    $post_id The {@link WP_Post}'s id.
493
-	 *
494
-	 * @return string The content (for chaining operations).
495
-	 */
496
-	protected function set_warning_based_on_content( $content, $post_id ) {
497
-
498
-		// Check for suspicious interpolations.
499
-		$is_warning = $this->has_interpolation_errors( $content );
500
-
501
-		// Set the warning flag accordingly.
502
-		$this->set_warning( $post_id, $is_warning );
503
-
504
-	}
505
-
506
-	private function has_interpolation_errors( $content ) {
507
-		$matches = array();
508
-
509
-		return 0 < preg_match_all( '/\w<[a-z]+ id="urn:enhancement - [^"]+" class="[^"]+ " itemid="[^"]+" >/', $content, $matches )
510
-			   || 0 < preg_match_all( ' /<[ a - z ] + id = "urn:enhancement-[^"]+ " class="[^"]+" itemid = "[^"]+ ">\s/', $content, $matches );
511
-	}
512
-
513
-	/**
514
-	 * Fix interpolation errors raised by Batch Analysis
515
-	 *
516
-	 * @param string $content The {@link WP_Post}'s content.
517
-	 * @param int    $id      The {@link WP_Post}'s id.
518
-	 *
519
-	 * @since 3.17.0
520
-	 *
521
-	 * @return string Post content without interpolations.
522
-	 */
523
-	private function fix_interpolation_errors( $content, $id ) {
524
-
525
-		// Bail out if there are no interpolation errors.
526
-		if ( ! $this->has_interpolation_errors( $content ) ) {
527
-			return $content;
528
-		}
529
-
530
-		$this->log->debug( "Fixing post $id interpolations..." );
531
-
532
-		// Remove all interpolations from the content.
533
-		return preg_replace( self::$interpolation_patterns, '$1$2', $content );
534
-	}
535
-
536
-	/**
537
-	 * Clear the warning flag for the specified {@link WP_Post}s.
538
-	 *
539
-	 * @since 3.14.2
540
-	 *
541
-	 * @param int|array $post_ids A single {@link WP_Post}'s id or an array of
542
-	 *                            {@link WP_Post}s' ids.
543
-	 */
544
-	public function clear_warning( $post_ids ) {
545
-
546
-		foreach ( (array) $post_ids as $post_id ) {
547
-			delete_post_meta( $post_id, self::WARNING_META_KEY );
548
-		}
549
-
550
-	}
551
-
552
-	/**
553
-	 * Set the warning flag for the specified {@link WP_Post}.
554
-	 *
555
-	 * @since 3.14.2
556
-	 *
557
-	 * @param int  $post_id The {@link WP_Post}'s id.
558
-	 * @param bool $value   The flag's value.
559
-	 *
560
-	 * @return int|bool Meta ID if the key didn't exist, true on successful update,
561
-	 *                  false on failure.
562
-	 */
563
-	private function set_warning( $post_id, $value ) {
564
-
565
-		return update_post_meta( $post_id, self::WARNING_META_KEY, ( true === $value ? 'yes' : 'no' ) );
566
-	}
415
+        // Send a request for each post.
416
+        foreach ( $posts as $id ) {
417
+            $this->log->debug( "Requesting results for post $id..." );
418
+
419
+            // Send the actual request to the remote service.
420
+            $response = $this->do_complete( $id );
421
+
422
+            // Move to the next item if we don't have a reply for this one.
423
+            if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
424
+                continue;
425
+            }
426
+
427
+            $this->log->debug( "Results received for post $id." );
428
+
429
+            // Save the returned content as new revision.
430
+            $json = json_decode( $response['body'] );
431
+
432
+            // Continue if the content isn't set.
433
+            if ( empty( $json->content ) ) {
434
+                // The post content is empty, so is should be marked as completed.
435
+                $this->log->error( "An error occurred while decoding the batch analysis response for post $id." );
436
+
437
+                $this->set_state( $id, self::STATE_ERROR );
438
+                continue;
439
+            }
440
+
441
+            // Set the warning flag if needed.
442
+            $this->set_warning_based_on_content( $json->content, $id );
443
+
444
+            // Get the content, cleaned up if there are interpolation errors.
445
+            $pre_content = $this->fix_interpolation_errors( $json->content, $id );
446
+
447
+            /**
448
+             * Filter: 'wl_batch_analysis_update_post_content' - Allow third
449
+             * parties to perform additional actions when the post content is
450
+             * updated.
451
+             *
452
+             * @since  3.17.0
453
+             * @api    string $data The {@link WP_Post}'s content.
454
+             * @api    int    $id   The {@link WP_Post}'s id.
455
+             */
456
+            $content = apply_filters( 'wl_batch_analysis_update_post_content', $pre_content, $id );
457
+
458
+            // Update the post content.
459
+            wp_update_post( array(
460
+                'ID'           => $id,
461
+                'post_content' => wp_slash( $content ),
462
+            ) );
463
+
464
+            // Update the status.
465
+            $this->set_state( $id, self::STATE_SUCCESS );
466
+
467
+            // Invalidating the cache for the current post.
468
+            $this->cache_service->delete_cache( $id );
469
+
470
+            $this->log->debug( "Post $id updated with batch analysis results." );
471
+
472
+            // Set default entity type term for posts that didn't have any.
473
+            $this->maybe_set_default_term( $id );
474
+
475
+            // @todo: implement a kind of timeout that sets an error if the
476
+            // results haven't been received after a long time.
477
+        }
478
+
479
+        // Call the `wl_batch_analysis_request` action again. This is going
480
+        // to be handled by the async task.
481
+        do_action( 'wl_batch_analysis_complete' );
482
+
483
+    }
484
+
485
+    /**
486
+     * Set a warning flag on the {@link WP_Post} if its content has suspicious
487
+     * interpolations.
488
+     *
489
+     * @since 3.14.2
490
+     *
491
+     * @param string $content The {@link WP_Post}'s content.
492
+     * @param int    $post_id The {@link WP_Post}'s id.
493
+     *
494
+     * @return string The content (for chaining operations).
495
+     */
496
+    protected function set_warning_based_on_content( $content, $post_id ) {
497
+
498
+        // Check for suspicious interpolations.
499
+        $is_warning = $this->has_interpolation_errors( $content );
500
+
501
+        // Set the warning flag accordingly.
502
+        $this->set_warning( $post_id, $is_warning );
503
+
504
+    }
505
+
506
+    private function has_interpolation_errors( $content ) {
507
+        $matches = array();
508
+
509
+        return 0 < preg_match_all( '/\w<[a-z]+ id="urn:enhancement - [^"]+" class="[^"]+ " itemid="[^"]+" >/', $content, $matches )
510
+               || 0 < preg_match_all( ' /<[ a - z ] + id = "urn:enhancement-[^"]+ " class="[^"]+" itemid = "[^"]+ ">\s/', $content, $matches );
511
+    }
512
+
513
+    /**
514
+     * Fix interpolation errors raised by Batch Analysis
515
+     *
516
+     * @param string $content The {@link WP_Post}'s content.
517
+     * @param int    $id      The {@link WP_Post}'s id.
518
+     *
519
+     * @since 3.17.0
520
+     *
521
+     * @return string Post content without interpolations.
522
+     */
523
+    private function fix_interpolation_errors( $content, $id ) {
524
+
525
+        // Bail out if there are no interpolation errors.
526
+        if ( ! $this->has_interpolation_errors( $content ) ) {
527
+            return $content;
528
+        }
529
+
530
+        $this->log->debug( "Fixing post $id interpolations..." );
531
+
532
+        // Remove all interpolations from the content.
533
+        return preg_replace( self::$interpolation_patterns, '$1$2', $content );
534
+    }
535
+
536
+    /**
537
+     * Clear the warning flag for the specified {@link WP_Post}s.
538
+     *
539
+     * @since 3.14.2
540
+     *
541
+     * @param int|array $post_ids A single {@link WP_Post}'s id or an array of
542
+     *                            {@link WP_Post}s' ids.
543
+     */
544
+    public function clear_warning( $post_ids ) {
545
+
546
+        foreach ( (array) $post_ids as $post_id ) {
547
+            delete_post_meta( $post_id, self::WARNING_META_KEY );
548
+        }
549
+
550
+    }
551
+
552
+    /**
553
+     * Set the warning flag for the specified {@link WP_Post}.
554
+     *
555
+     * @since 3.14.2
556
+     *
557
+     * @param int  $post_id The {@link WP_Post}'s id.
558
+     * @param bool $value   The flag's value.
559
+     *
560
+     * @return int|bool Meta ID if the key didn't exist, true on successful update,
561
+     *                  false on failure.
562
+     */
563
+    private function set_warning( $post_id, $value ) {
564
+
565
+        return update_post_meta( $post_id, self::WARNING_META_KEY, ( true === $value ? 'yes' : 'no' ) );
566
+    }
567 567
 
568 568
 //	/**
569 569
 //	 * Get the post/page batch analysis state.
@@ -579,231 +579,231 @@  discard block
 block discarded – undo
579 579
 //		return get_post_meta( $post_id, self::STATE_META_KEY, true );
580 580
 //	}
581 581
 
582
-	/**
583
-	 * Set the post/page batch analysis state.
584
-	 *
585
-	 * @since 3.14.2
586
-	 *
587
-	 * @param int $post_id The {@link WP_Post}'s id.
588
-	 * @param int $value   The new state.
589
-	 *
590
-	 * @return int|bool Meta ID if the key didn't exist, true on successful update,
591
-	 *                  false on failure.
592
-	 */
593
-	private function set_state( $post_id, $value ) {
594
-
595
-		// Update the state.
596
-		$result = update_post_meta( $post_id, self::STATE_META_KEY, $value );
597
-
598
-		// Update timestamps as required.
599
-		switch ( $value ) {
600
-
601
-			// ### REQUEST state.
602
-			case self::STATE_REQUEST:
603
-				add_post_meta( $post_id, self::REQUEST_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
604
-				break;
605
-
606
-			// ### SUCCESS/ERROR state.
607
-			case self::STATE_SUCCESS:
608
-			case self::STATE_ERROR:
609
-				add_post_meta( $post_id, self::COMPLETE_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
610
-				break;
611
-		}
612
-
613
-		// Finally return the result.
614
-		return $result;
615
-	}
616
-
617
-	/**
618
-	 * Get the options setting for a {@link WP_Post}.
619
-	 *
620
-	 * If there are multiple link settings, only the last one is returned.
621
-	 *
622
-	 * @since 3.14.2
623
-	 *
624
-	 * @param int $post_id The {@link WP_Post}'s id.
625
-	 *
626
-	 * @return array The link settings.
627
-	 */
628
-	private function get_options( $post_id ) {
629
-
630
-		$values = get_post_meta( $post_id, self::BATCH_ANALYSIS_OPTIONS_META_KEY );
631
-
632
-		return end( $values ) ?: array(
633
-			'links'           => 'default',
634
-			'min_occurrences' => 1,
635
-		);
636
-	}
637
-
638
-	/**
639
-	 * Get the array of post IDS waiting in the queue to start processing.
640
-	 *
641
-	 * @since 3.14.0
642
-	 *
643
-	 * @return array The waiting to be processed post ids queue.
644
-	 */
645
-	public function waiting_for_analysis() {
646
-
647
-		return get_posts( array(
648
-			'posts_per_page' => - 1,
649
-			'fields'         => 'ids',
650
-			'post_status'    => 'any',
651
-			'meta_key'       => self::STATE_META_KEY,
652
-			'meta_value'     => self::STATE_SUBMIT,
653
-			'orderby'        => 'ID',
654
-			'post_type'      => 'any',
655
-			// Add any because posts from multiple posts types may be waiting.
656
-		) );
657
-	}
658
-
659
-	/**
660
-	 * Get the array of post IDS waiting for response.
661
-	 *
662
-	 * @deprecated
663
-	 * @since 3.14.0
664
-	 *
665
-	 * @return array The waiting for response post ids queue.
666
-	 */
667
-	public function waiting_for_response() {
668
-
669
-		return get_posts( array(
670
-			'posts_per_page' => - 1,
671
-			'fields'         => 'ids',
672
-			'post_status'    => 'any',
673
-			'meta_key'       => self::STATE_META_KEY,
674
-			'meta_value'     => self::STATE_REQUEST,
675
-			'orderby'        => 'ID',
676
-			'post_type'      => 'any',
677
-			// Add any because posts from multiple posts types may be waiting.
678
-		) );
679
-	}
680
-
681
-	/**
682
-	 * Request the analysis for the specified {@link WP_Post}.
683
-	 *
684
-	 * @since 3.14.2
685
-	 *
686
-	 * @param int $post_id The {@link WP_Post}'s id.
687
-	 *
688
-	 * @return WP_Error|array The response or WP_Error on failure.
689
-	 */
690
-	private function do_request( $post_id ) {
691
-
692
-		// Change the state to `REQUEST`.
693
-		$this->set_state( $post_id, self::STATE_REQUEST );
694
-
695
-		// Get the post.
696
-		$post = get_post( $post_id );
697
-
698
-		// Bail out if the post isn't found.
699
-		if ( null === $post ) {
700
-			$this->log->warn( "Post $post_id not found." );
701
-
702
-			return new WP_Error( 0, "Cannot find post $post_id." );
703
-		}
704
-
705
-		// Get the link setting.
706
-		$options = $this->get_options( $post_id );
707
-
708
-		$this->log->debug( 'Sending analysis request for post $post_id [ links :: ' . $options['links'] . ', min_occurrences :: ' . $options['min_occurrences'] . ' ] ...' );
709
-
710
-		// Get the batch analysis URL.
711
-		$url = $this->configuration_service->get_batch_analysis_url();
712
-
713
-		// Prepare the POST parameters.
714
-		$params = array(
715
-			'id'              => $post->ID,
716
-			'key'             => $this->configuration_service->get_key(),
717
-			'content'         => $post->post_content,
718
-			'contentLanguage' => $this->configuration_service->get_language_code(),
719
-			'version'         => $this->plugin->get_version(),
720
-			'scope'           => 'local',
721
-			'links'           => $options['links'],
722
-			'minOccurrences'  => $options['min_occurrences'],
723
-		);
724
-
725
-		// Get the HTTP options.
726
-		$args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
727
-			'method'      => 'POST',
728
-			'headers'     => array(
729
-				'Accept'       => 'application/json',
730
-				'Content-type' => 'application/json; charset=UTF-8',
731
-			),
732
-			// we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
733
-			'httpversion' => '1.0',
734
-			'body'        => wp_json_encode( $params ),
735
-		) );
736
-
737
-		$this->log->debug( "Posting analysis request for post $post_id to $url..." );
738
-
739
-		// Post the parameter.
740
-		return wp_remote_post( $url, $args );
741
-	}
742
-
743
-	/**
744
-	 * Get the Batch Analysis results for the specified {@link WP_Post}.
745
-	 *
746
-	 * @since 3.14.2
747
-	 *
748
-	 * @param int $post_id The {@link WP_Post}'s id.
749
-	 *
750
-	 * @return WP_Error|array The response or WP_Error on failure.
751
-	 */
752
-	private function do_complete( $post_id ) {
753
-
754
-		$post = get_post( $post_id );
755
-
756
-		if ( null === $post ) {
757
-			// Post was possibly deleted, just bailout.
758
-			return new WP_Error( 0, "Post $post_id not found . " );
759
-		}
760
-
761
-		$url = $this->configuration_service->get_batch_analysis_url();
762
-		$key = $this->configuration_service->get_key();
763
-		$url = $url . '/' . $post->ID . '?key=' . $key;
764
-
765
-		return wp_remote_get( $url, unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
766
-	}
767
-
768
-	/**
769
-	 * Get the {@link WP_Post}s' ids flagged with warnings.
770
-	 *
771
-	 * @since 3.14.2
772
-	 *
773
-	 * @return array An array of {@link WP_Post}s' ids.
774
-	 */
775
-	public function get_warnings() {
776
-
777
-		return get_posts( array(
778
-			'fields'      => 'ids',
779
-			'numberposts' => - 1,
780
-			'post_status' => 'any',
781
-			'meta_key'    => self::WARNING_META_KEY,
782
-			'meta_value'  => 'yes',
783
-			'post_type'   => 'any',
784
-			// Add any because posts from multiple posts types may be waiting.
785
-		) );
786
-	}
787
-
788
-	/**
789
-	 * Check whether the term has entity type associated and set default term if it hasn't.
790
-	 *
791
-	 * @since 3.17.0
792
-	 *
793
-	 * @param int $id The {@link WP_Post}'s id.
794
-	 */
795
-	private function maybe_set_default_term( $id ) {
796
-		// Check whether the post has any of the WordLift entity types.
797
-		$has_term = has_term( '', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, $id );
798
-
799
-		// Bail if the term is associated with entity types already.
800
-		if ( ! empty( $has_term ) ) {
801
-			return;
802
-		}
803
-
804
-		// Set the default `article` term.
805
-		wp_set_object_terms( $id, 'article', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
806
-
807
-	}
582
+    /**
583
+     * Set the post/page batch analysis state.
584
+     *
585
+     * @since 3.14.2
586
+     *
587
+     * @param int $post_id The {@link WP_Post}'s id.
588
+     * @param int $value   The new state.
589
+     *
590
+     * @return int|bool Meta ID if the key didn't exist, true on successful update,
591
+     *                  false on failure.
592
+     */
593
+    private function set_state( $post_id, $value ) {
594
+
595
+        // Update the state.
596
+        $result = update_post_meta( $post_id, self::STATE_META_KEY, $value );
597
+
598
+        // Update timestamps as required.
599
+        switch ( $value ) {
600
+
601
+            // ### REQUEST state.
602
+            case self::STATE_REQUEST:
603
+                add_post_meta( $post_id, self::REQUEST_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
604
+                break;
605
+
606
+            // ### SUCCESS/ERROR state.
607
+            case self::STATE_SUCCESS:
608
+            case self::STATE_ERROR:
609
+                add_post_meta( $post_id, self::COMPLETE_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
610
+                break;
611
+        }
612
+
613
+        // Finally return the result.
614
+        return $result;
615
+    }
616
+
617
+    /**
618
+     * Get the options setting for a {@link WP_Post}.
619
+     *
620
+     * If there are multiple link settings, only the last one is returned.
621
+     *
622
+     * @since 3.14.2
623
+     *
624
+     * @param int $post_id The {@link WP_Post}'s id.
625
+     *
626
+     * @return array The link settings.
627
+     */
628
+    private function get_options( $post_id ) {
629
+
630
+        $values = get_post_meta( $post_id, self::BATCH_ANALYSIS_OPTIONS_META_KEY );
631
+
632
+        return end( $values ) ?: array(
633
+            'links'           => 'default',
634
+            'min_occurrences' => 1,
635
+        );
636
+    }
637
+
638
+    /**
639
+     * Get the array of post IDS waiting in the queue to start processing.
640
+     *
641
+     * @since 3.14.0
642
+     *
643
+     * @return array The waiting to be processed post ids queue.
644
+     */
645
+    public function waiting_for_analysis() {
646
+
647
+        return get_posts( array(
648
+            'posts_per_page' => - 1,
649
+            'fields'         => 'ids',
650
+            'post_status'    => 'any',
651
+            'meta_key'       => self::STATE_META_KEY,
652
+            'meta_value'     => self::STATE_SUBMIT,
653
+            'orderby'        => 'ID',
654
+            'post_type'      => 'any',
655
+            // Add any because posts from multiple posts types may be waiting.
656
+        ) );
657
+    }
658
+
659
+    /**
660
+     * Get the array of post IDS waiting for response.
661
+     *
662
+     * @deprecated
663
+     * @since 3.14.0
664
+     *
665
+     * @return array The waiting for response post ids queue.
666
+     */
667
+    public function waiting_for_response() {
668
+
669
+        return get_posts( array(
670
+            'posts_per_page' => - 1,
671
+            'fields'         => 'ids',
672
+            'post_status'    => 'any',
673
+            'meta_key'       => self::STATE_META_KEY,
674
+            'meta_value'     => self::STATE_REQUEST,
675
+            'orderby'        => 'ID',
676
+            'post_type'      => 'any',
677
+            // Add any because posts from multiple posts types may be waiting.
678
+        ) );
679
+    }
680
+
681
+    /**
682
+     * Request the analysis for the specified {@link WP_Post}.
683
+     *
684
+     * @since 3.14.2
685
+     *
686
+     * @param int $post_id The {@link WP_Post}'s id.
687
+     *
688
+     * @return WP_Error|array The response or WP_Error on failure.
689
+     */
690
+    private function do_request( $post_id ) {
691
+
692
+        // Change the state to `REQUEST`.
693
+        $this->set_state( $post_id, self::STATE_REQUEST );
694
+
695
+        // Get the post.
696
+        $post = get_post( $post_id );
697
+
698
+        // Bail out if the post isn't found.
699
+        if ( null === $post ) {
700
+            $this->log->warn( "Post $post_id not found." );
701
+
702
+            return new WP_Error( 0, "Cannot find post $post_id." );
703
+        }
704
+
705
+        // Get the link setting.
706
+        $options = $this->get_options( $post_id );
707
+
708
+        $this->log->debug( 'Sending analysis request for post $post_id [ links :: ' . $options['links'] . ', min_occurrences :: ' . $options['min_occurrences'] . ' ] ...' );
709
+
710
+        // Get the batch analysis URL.
711
+        $url = $this->configuration_service->get_batch_analysis_url();
712
+
713
+        // Prepare the POST parameters.
714
+        $params = array(
715
+            'id'              => $post->ID,
716
+            'key'             => $this->configuration_service->get_key(),
717
+            'content'         => $post->post_content,
718
+            'contentLanguage' => $this->configuration_service->get_language_code(),
719
+            'version'         => $this->plugin->get_version(),
720
+            'scope'           => 'local',
721
+            'links'           => $options['links'],
722
+            'minOccurrences'  => $options['min_occurrences'],
723
+        );
724
+
725
+        // Get the HTTP options.
726
+        $args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
727
+            'method'      => 'POST',
728
+            'headers'     => array(
729
+                'Accept'       => 'application/json',
730
+                'Content-type' => 'application/json; charset=UTF-8',
731
+            ),
732
+            // we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
733
+            'httpversion' => '1.0',
734
+            'body'        => wp_json_encode( $params ),
735
+        ) );
736
+
737
+        $this->log->debug( "Posting analysis request for post $post_id to $url..." );
738
+
739
+        // Post the parameter.
740
+        return wp_remote_post( $url, $args );
741
+    }
742
+
743
+    /**
744
+     * Get the Batch Analysis results for the specified {@link WP_Post}.
745
+     *
746
+     * @since 3.14.2
747
+     *
748
+     * @param int $post_id The {@link WP_Post}'s id.
749
+     *
750
+     * @return WP_Error|array The response or WP_Error on failure.
751
+     */
752
+    private function do_complete( $post_id ) {
753
+
754
+        $post = get_post( $post_id );
755
+
756
+        if ( null === $post ) {
757
+            // Post was possibly deleted, just bailout.
758
+            return new WP_Error( 0, "Post $post_id not found . " );
759
+        }
760
+
761
+        $url = $this->configuration_service->get_batch_analysis_url();
762
+        $key = $this->configuration_service->get_key();
763
+        $url = $url . '/' . $post->ID . '?key=' . $key;
764
+
765
+        return wp_remote_get( $url, unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
766
+    }
767
+
768
+    /**
769
+     * Get the {@link WP_Post}s' ids flagged with warnings.
770
+     *
771
+     * @since 3.14.2
772
+     *
773
+     * @return array An array of {@link WP_Post}s' ids.
774
+     */
775
+    public function get_warnings() {
776
+
777
+        return get_posts( array(
778
+            'fields'      => 'ids',
779
+            'numberposts' => - 1,
780
+            'post_status' => 'any',
781
+            'meta_key'    => self::WARNING_META_KEY,
782
+            'meta_value'  => 'yes',
783
+            'post_type'   => 'any',
784
+            // Add any because posts from multiple posts types may be waiting.
785
+        ) );
786
+    }
787
+
788
+    /**
789
+     * Check whether the term has entity type associated and set default term if it hasn't.
790
+     *
791
+     * @since 3.17.0
792
+     *
793
+     * @param int $id The {@link WP_Post}'s id.
794
+     */
795
+    private function maybe_set_default_term( $id ) {
796
+        // Check whether the post has any of the WordLift entity types.
797
+        $has_term = has_term( '', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, $id );
798
+
799
+        // Bail if the term is associated with entity types already.
800
+        if ( ! empty( $has_term ) ) {
801
+            return;
802
+        }
803
+
804
+        // Set the default `article` term.
805
+        wp_set_object_terms( $id, 'article', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
806
+
807
+    }
808 808
 
809 809
 }
Please login to merge, or discard this patch.
Spacing   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -185,22 +185,22 @@  discard block
 block discarded – undo
185 185
 	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
186 186
 	 * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} instance.
187 187
 	 */
188
-	public function __construct( $plugin, $configuration_service, $cache_service ) {
188
+	public function __construct($plugin, $configuration_service, $cache_service) {
189 189
 
190
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Service' );
190
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Batch_Analysis_Service');
191 191
 
192 192
 		$this->plugin                = $plugin;
193 193
 		$this->configuration_service = $configuration_service;
194 194
 		$this->cache_service         = $cache_service;
195 195
 
196
-		add_action( 'wl_async_wl_batch_analysis_request', array(
196
+		add_action('wl_async_wl_batch_analysis_request', array(
197 197
 			$this,
198 198
 			'request',
199
-		) );
200
-		add_action( 'wl_async_wl_batch_analysis_complete', array(
199
+		));
200
+		add_action('wl_async_wl_batch_analysis_complete', array(
201 201
 			$this,
202 202
 			'complete',
203
-		) );
203
+		));
204 204
 
205 205
 	}
206 206
 
@@ -231,11 +231,11 @@  discard block
 block discarded – undo
231 231
 	 *
232 232
 	 * @return int The number of submitted {@link WP_Post}s or false on error.
233 233
 	 */
234
-	public function submit( $args ) {
234
+	public function submit($args) {
235 235
 		global $wpdb;
236 236
 
237 237
 		// Parse the parameters.
238
-		$params = wp_parse_args( $args, array(
238
+		$params = wp_parse_args($args, array(
239 239
 			'links'             => 'default',
240 240
 			'min_occurrences'   => 1,
241 241
 			'include_annotated' => false,
@@ -243,24 +243,24 @@  discard block
 block discarded – undo
243 243
 			'from'              => null,
244 244
 			'to'                => null,
245 245
 			'post_type'         => 'post',
246
-		) );
246
+		));
247 247
 
248 248
 		// Validation.
249
-		if ( ! in_array( $params['links'], array( 'default', 'yes', 'no' ) ) ) {
250
-			wp_die( '`link` must be one of the following: `default`, `yes` or `no`.' );
249
+		if ( ! in_array($params['links'], array('default', 'yes', 'no'))) {
250
+			wp_die('`link` must be one of the following: `default`, `yes` or `no`.');
251 251
 		}
252 252
 
253
-		if ( ! is_numeric( $params['min_occurrences'] ) || 1 > $params['min_occurrences'] ) {
254
-			wp_die( '`min_occurrences` must greater or equal 1.' );
253
+		if ( ! is_numeric($params['min_occurrences']) || 1 > $params['min_occurrences']) {
254
+			wp_die('`min_occurrences` must greater or equal 1.');
255 255
 		}
256 256
 
257 257
 		// Submit the posts/pages and return the number of affected results.
258 258
 		// We're using a SQL query here because we could have potentially
259 259
 		// thousands of rows.
260
-		$count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql( $params ) ); // WPCS: cache ok, db call ok.
260
+		$count = $wpdb->query(Wordlift_Batch_Analysis_Sql_Helper::get_sql($params)); // WPCS: cache ok, db call ok.
261 261
 
262 262
 		// Request Batch Analysis (the operation is handled asynchronously).
263
-		do_action( 'wl_batch_analysis_request' );
263
+		do_action('wl_batch_analysis_request');
264 264
 
265 265
 		// Divide the count by 3 to get the number of posts/pages queued.
266 266
 		return $count / 3;
@@ -282,28 +282,28 @@  discard block
 block discarded – undo
282 282
 	 *
283 283
 	 * @return float|int
284 284
 	 */
285
-	public function submit_posts( $args ) {
285
+	public function submit_posts($args) {
286 286
 		global $wpdb;
287 287
 
288 288
 		// Parse the parameters.
289
-		$params = wp_parse_args( $args, array(
289
+		$params = wp_parse_args($args, array(
290 290
 			'links'           => 'default',
291 291
 			'min_occurrences' => 1,
292 292
 			'ids'             => array(),
293
-		) );
293
+		));
294 294
 
295 295
 		// Validation.
296
-		if ( empty( $params['ids'] ) ) {
297
-			wp_die( '`ids` cannot be empty.' );
296
+		if (empty($params['ids'])) {
297
+			wp_die('`ids` cannot be empty.');
298 298
 		}
299 299
 
300 300
 		// Submit the posts/pages and return the number of affected results.
301 301
 		// We're using a SQL query here because we could have potentially
302 302
 		// thousands of rows.
303
-		$count = $wpdb->query( Wordlift_Batch_Analysis_Sql_Helper::get_sql_for_ids( $params ) ); // WPCS: cache ok, db call ok.
303
+		$count = $wpdb->query(Wordlift_Batch_Analysis_Sql_Helper::get_sql_for_ids($params)); // WPCS: cache ok, db call ok.
304 304
 
305 305
 		// Request Batch Analysis (the operation is handled asynchronously).
306
-		do_action( 'wl_batch_analysis_request' );
306
+		do_action('wl_batch_analysis_request');
307 307
 
308 308
 		// Divide the count by 3 to get the number of posts/pages queued.
309 309
 		return $count / 3;
@@ -320,20 +320,20 @@  discard block
 block discarded – undo
320 320
 	 * @return false|int The number of cancelled {@link WP_Post}s or false on
321 321
 	 *                   error.
322 322
 	 */
323
-	public function cancel( $post_ids ) {
323
+	public function cancel($post_ids) {
324 324
 		global $wpdb;
325 325
 
326
-		return $wpdb->query( $wpdb->prepare(
326
+		return $wpdb->query($wpdb->prepare(
327 327
 			"
328 328
 			DELETE FROM $wpdb->postmeta
329 329
 			WHERE meta_key = %s
330 330
 				AND meta_value IN ( %d, %d )
331
-				AND post_id IN( " . implode( ',', wp_parse_id_list( $post_ids ) ) . " )
331
+				AND post_id IN( ".implode(',', wp_parse_id_list($post_ids))." )
332 332
 			",
333 333
 			self::STATE_META_KEY,
334 334
 			self::STATE_SUBMIT,
335 335
 			self::STATE_REQUEST
336
-		) ); // WPCS: cache ok, db call ok.
336
+		)); // WPCS: cache ok, db call ok.
337 337
 
338 338
 	}
339 339
 
@@ -344,46 +344,46 @@  discard block
 block discarded – undo
344 344
 	 */
345 345
 	public function request() {
346 346
 
347
-		$this->log->debug( 'Requesting analysis...' );
347
+		$this->log->debug('Requesting analysis...');
348 348
 
349 349
 		// By default 5 posts of any post type are returned.
350
-		$posts = get_posts( array(
350
+		$posts = get_posts(array(
351 351
 			'fields'     => 'ids',
352 352
 			'meta_key'   => self::STATE_META_KEY,
353 353
 			'meta_value' => self::STATE_SUBMIT,
354 354
 			'orderby'    => 'ID',
355 355
 			'post_type'  => 'any',
356
-		) );
356
+		));
357 357
 
358 358
 		// Bail out if there are no submitted posts.
359
-		if ( empty( $posts ) ) {
360
-			$this->log->debug( 'No posts to submit found, checking for completed requests...' );
359
+		if (empty($posts)) {
360
+			$this->log->debug('No posts to submit found, checking for completed requests...');
361 361
 
362
-			do_action( 'wl_batch_analysis_complete' );
362
+			do_action('wl_batch_analysis_complete');
363 363
 
364 364
 			return;
365 365
 		}
366 366
 
367 367
 		// Send a request for each post.
368
-		foreach ( $posts as $id ) {
368
+		foreach ($posts as $id) {
369 369
 
370
-			$this->log->debug( "Requesting analysis for post $id..." );
370
+			$this->log->debug("Requesting analysis for post $id...");
371 371
 
372 372
 			// Send the actual request to the remote service.
373
-			$result = $this->do_request( $id );
373
+			$result = $this->do_request($id);
374 374
 
375 375
 			// Set an error if we received an error.
376
-			if ( is_wp_error( $result ) ) {
377
-				$this->log->error( "An error occurred while requesting a batch analysis for post $id: " . $result->get_error_message() );
376
+			if (is_wp_error($result)) {
377
+				$this->log->error("An error occurred while requesting a batch analysis for post $id: ".$result->get_error_message());
378 378
 
379
-				$this->set_state( $id, self::STATE_ERROR );
379
+				$this->set_state($id, self::STATE_ERROR);
380 380
 			}
381 381
 
382 382
 		}
383 383
 
384 384
 		// Call the `wl_batch_analysis_request` action again. This is going
385 385
 		// to be handled by the async task.
386
-		do_action( 'wl_batch_analysis_request' );
386
+		do_action('wl_batch_analysis_request');
387 387
 
388 388
 	}
389 389
 
@@ -394,55 +394,55 @@  discard block
 block discarded – undo
394 394
 	 */
395 395
 	public function complete() {
396 396
 
397
-		$this->log->debug( 'Requesting results...' );
397
+		$this->log->debug('Requesting results...');
398 398
 
399 399
 		// By default 5 posts of any post type are returned.
400
-		$posts = get_posts( array(
400
+		$posts = get_posts(array(
401 401
 			'fields'     => 'ids',
402 402
 			'meta_key'   => self::STATE_META_KEY,
403 403
 			'meta_value' => self::STATE_REQUEST,
404 404
 			'orderby'    => 'ID',
405 405
 			'post_type'  => 'any',
406
-		) );
406
+		));
407 407
 
408 408
 		// Bail out if there are no submitted posts.
409
-		if ( empty( $posts ) ) {
410
-			$this->log->debug( 'No posts in request state found.' );
409
+		if (empty($posts)) {
410
+			$this->log->debug('No posts in request state found.');
411 411
 
412 412
 			return;
413 413
 		}
414 414
 
415 415
 		// Send a request for each post.
416
-		foreach ( $posts as $id ) {
417
-			$this->log->debug( "Requesting results for post $id..." );
416
+		foreach ($posts as $id) {
417
+			$this->log->debug("Requesting results for post $id...");
418 418
 
419 419
 			// Send the actual request to the remote service.
420
-			$response = $this->do_complete( $id );
420
+			$response = $this->do_complete($id);
421 421
 
422 422
 			// Move to the next item if we don't have a reply for this one.
423
-			if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
423
+			if (is_wp_error($response) || ! isset($response['body'])) {
424 424
 				continue;
425 425
 			}
426 426
 
427
-			$this->log->debug( "Results received for post $id." );
427
+			$this->log->debug("Results received for post $id.");
428 428
 
429 429
 			// Save the returned content as new revision.
430
-			$json = json_decode( $response['body'] );
430
+			$json = json_decode($response['body']);
431 431
 
432 432
 			// Continue if the content isn't set.
433
-			if ( empty( $json->content ) ) {
433
+			if (empty($json->content)) {
434 434
 				// The post content is empty, so is should be marked as completed.
435
-				$this->log->error( "An error occurred while decoding the batch analysis response for post $id." );
435
+				$this->log->error("An error occurred while decoding the batch analysis response for post $id.");
436 436
 
437
-				$this->set_state( $id, self::STATE_ERROR );
437
+				$this->set_state($id, self::STATE_ERROR);
438 438
 				continue;
439 439
 			}
440 440
 
441 441
 			// Set the warning flag if needed.
442
-			$this->set_warning_based_on_content( $json->content, $id );
442
+			$this->set_warning_based_on_content($json->content, $id);
443 443
 
444 444
 			// Get the content, cleaned up if there are interpolation errors.
445
-			$pre_content = $this->fix_interpolation_errors( $json->content, $id );
445
+			$pre_content = $this->fix_interpolation_errors($json->content, $id);
446 446
 
447 447
 			/**
448 448
 			 * Filter: 'wl_batch_analysis_update_post_content' - Allow third
@@ -453,24 +453,24 @@  discard block
 block discarded – undo
453 453
 			 * @api    string $data The {@link WP_Post}'s content.
454 454
 			 * @api    int    $id   The {@link WP_Post}'s id.
455 455
 			 */
456
-			$content = apply_filters( 'wl_batch_analysis_update_post_content', $pre_content, $id );
456
+			$content = apply_filters('wl_batch_analysis_update_post_content', $pre_content, $id);
457 457
 
458 458
 			// Update the post content.
459
-			wp_update_post( array(
459
+			wp_update_post(array(
460 460
 				'ID'           => $id,
461
-				'post_content' => wp_slash( $content ),
462
-			) );
461
+				'post_content' => wp_slash($content),
462
+			));
463 463
 
464 464
 			// Update the status.
465
-			$this->set_state( $id, self::STATE_SUCCESS );
465
+			$this->set_state($id, self::STATE_SUCCESS);
466 466
 
467 467
 			// Invalidating the cache for the current post.
468
-			$this->cache_service->delete_cache( $id );
468
+			$this->cache_service->delete_cache($id);
469 469
 
470
-			$this->log->debug( "Post $id updated with batch analysis results." );
470
+			$this->log->debug("Post $id updated with batch analysis results.");
471 471
 
472 472
 			// Set default entity type term for posts that didn't have any.
473
-			$this->maybe_set_default_term( $id );
473
+			$this->maybe_set_default_term($id);
474 474
 
475 475
 			// @todo: implement a kind of timeout that sets an error if the
476 476
 			// results haven't been received after a long time.
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
 
479 479
 		// Call the `wl_batch_analysis_request` action again. This is going
480 480
 		// to be handled by the async task.
481
-		do_action( 'wl_batch_analysis_complete' );
481
+		do_action('wl_batch_analysis_complete');
482 482
 
483 483
 	}
484 484
 
@@ -493,21 +493,21 @@  discard block
 block discarded – undo
493 493
 	 *
494 494
 	 * @return string The content (for chaining operations).
495 495
 	 */
496
-	protected function set_warning_based_on_content( $content, $post_id ) {
496
+	protected function set_warning_based_on_content($content, $post_id) {
497 497
 
498 498
 		// Check for suspicious interpolations.
499
-		$is_warning = $this->has_interpolation_errors( $content );
499
+		$is_warning = $this->has_interpolation_errors($content);
500 500
 
501 501
 		// Set the warning flag accordingly.
502
-		$this->set_warning( $post_id, $is_warning );
502
+		$this->set_warning($post_id, $is_warning);
503 503
 
504 504
 	}
505 505
 
506
-	private function has_interpolation_errors( $content ) {
506
+	private function has_interpolation_errors($content) {
507 507
 		$matches = array();
508 508
 
509
-		return 0 < preg_match_all( '/\w<[a-z]+ id="urn:enhancement - [^"]+" class="[^"]+ " itemid="[^"]+" >/', $content, $matches )
510
-			   || 0 < preg_match_all( ' /<[ a - z ] + id = "urn:enhancement-[^"]+ " class="[^"]+" itemid = "[^"]+ ">\s/', $content, $matches );
509
+		return 0 < preg_match_all('/\w<[a-z]+ id="urn:enhancement - [^"]+" class="[^"]+ " itemid="[^"]+" >/', $content, $matches)
510
+			   || 0 < preg_match_all(' /<[ a - z ] + id = "urn:enhancement-[^"]+ " class="[^"]+" itemid = "[^"]+ ">\s/', $content, $matches);
511 511
 	}
512 512
 
513 513
 	/**
@@ -520,17 +520,17 @@  discard block
 block discarded – undo
520 520
 	 *
521 521
 	 * @return string Post content without interpolations.
522 522
 	 */
523
-	private function fix_interpolation_errors( $content, $id ) {
523
+	private function fix_interpolation_errors($content, $id) {
524 524
 
525 525
 		// Bail out if there are no interpolation errors.
526
-		if ( ! $this->has_interpolation_errors( $content ) ) {
526
+		if ( ! $this->has_interpolation_errors($content)) {
527 527
 			return $content;
528 528
 		}
529 529
 
530
-		$this->log->debug( "Fixing post $id interpolations..." );
530
+		$this->log->debug("Fixing post $id interpolations...");
531 531
 
532 532
 		// Remove all interpolations from the content.
533
-		return preg_replace( self::$interpolation_patterns, '$1$2', $content );
533
+		return preg_replace(self::$interpolation_patterns, '$1$2', $content);
534 534
 	}
535 535
 
536 536
 	/**
@@ -541,10 +541,10 @@  discard block
 block discarded – undo
541 541
 	 * @param int|array $post_ids A single {@link WP_Post}'s id or an array of
542 542
 	 *                            {@link WP_Post}s' ids.
543 543
 	 */
544
-	public function clear_warning( $post_ids ) {
544
+	public function clear_warning($post_ids) {
545 545
 
546
-		foreach ( (array) $post_ids as $post_id ) {
547
-			delete_post_meta( $post_id, self::WARNING_META_KEY );
546
+		foreach ((array) $post_ids as $post_id) {
547
+			delete_post_meta($post_id, self::WARNING_META_KEY);
548 548
 		}
549 549
 
550 550
 	}
@@ -560,9 +560,9 @@  discard block
 block discarded – undo
560 560
 	 * @return int|bool Meta ID if the key didn't exist, true on successful update,
561 561
 	 *                  false on failure.
562 562
 	 */
563
-	private function set_warning( $post_id, $value ) {
563
+	private function set_warning($post_id, $value) {
564 564
 
565
-		return update_post_meta( $post_id, self::WARNING_META_KEY, ( true === $value ? 'yes' : 'no' ) );
565
+		return update_post_meta($post_id, self::WARNING_META_KEY, (true === $value ? 'yes' : 'no'));
566 566
 	}
567 567
 
568 568
 //	/**
@@ -590,23 +590,23 @@  discard block
 block discarded – undo
590 590
 	 * @return int|bool Meta ID if the key didn't exist, true on successful update,
591 591
 	 *                  false on failure.
592 592
 	 */
593
-	private function set_state( $post_id, $value ) {
593
+	private function set_state($post_id, $value) {
594 594
 
595 595
 		// Update the state.
596
-		$result = update_post_meta( $post_id, self::STATE_META_KEY, $value );
596
+		$result = update_post_meta($post_id, self::STATE_META_KEY, $value);
597 597
 
598 598
 		// Update timestamps as required.
599
-		switch ( $value ) {
599
+		switch ($value) {
600 600
 
601 601
 			// ### REQUEST state.
602 602
 			case self::STATE_REQUEST:
603
-				add_post_meta( $post_id, self::REQUEST_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
603
+				add_post_meta($post_id, self::REQUEST_TIMESTAMP_META_KEY, current_time('mysql', true));
604 604
 				break;
605 605
 
606 606
 			// ### SUCCESS/ERROR state.
607 607
 			case self::STATE_SUCCESS:
608 608
 			case self::STATE_ERROR:
609
-				add_post_meta( $post_id, self::COMPLETE_TIMESTAMP_META_KEY, current_time( 'mysql', true ) );
609
+				add_post_meta($post_id, self::COMPLETE_TIMESTAMP_META_KEY, current_time('mysql', true));
610 610
 				break;
611 611
 		}
612 612
 
@@ -625,11 +625,11 @@  discard block
 block discarded – undo
625 625
 	 *
626 626
 	 * @return array The link settings.
627 627
 	 */
628
-	private function get_options( $post_id ) {
628
+	private function get_options($post_id) {
629 629
 
630
-		$values = get_post_meta( $post_id, self::BATCH_ANALYSIS_OPTIONS_META_KEY );
630
+		$values = get_post_meta($post_id, self::BATCH_ANALYSIS_OPTIONS_META_KEY);
631 631
 
632
-		return end( $values ) ?: array(
632
+		return end($values) ?: array(
633 633
 			'links'           => 'default',
634 634
 			'min_occurrences' => 1,
635 635
 		);
@@ -644,8 +644,8 @@  discard block
 block discarded – undo
644 644
 	 */
645 645
 	public function waiting_for_analysis() {
646 646
 
647
-		return get_posts( array(
648
-			'posts_per_page' => - 1,
647
+		return get_posts(array(
648
+			'posts_per_page' => -1,
649 649
 			'fields'         => 'ids',
650 650
 			'post_status'    => 'any',
651 651
 			'meta_key'       => self::STATE_META_KEY,
@@ -653,7 +653,7 @@  discard block
 block discarded – undo
653 653
 			'orderby'        => 'ID',
654 654
 			'post_type'      => 'any',
655 655
 			// Add any because posts from multiple posts types may be waiting.
656
-		) );
656
+		));
657 657
 	}
658 658
 
659 659
 	/**
@@ -666,8 +666,8 @@  discard block
 block discarded – undo
666 666
 	 */
667 667
 	public function waiting_for_response() {
668 668
 
669
-		return get_posts( array(
670
-			'posts_per_page' => - 1,
669
+		return get_posts(array(
670
+			'posts_per_page' => -1,
671 671
 			'fields'         => 'ids',
672 672
 			'post_status'    => 'any',
673 673
 			'meta_key'       => self::STATE_META_KEY,
@@ -675,7 +675,7 @@  discard block
 block discarded – undo
675 675
 			'orderby'        => 'ID',
676 676
 			'post_type'      => 'any',
677 677
 			// Add any because posts from multiple posts types may be waiting.
678
-		) );
678
+		));
679 679
 	}
680 680
 
681 681
 	/**
@@ -687,25 +687,25 @@  discard block
 block discarded – undo
687 687
 	 *
688 688
 	 * @return WP_Error|array The response or WP_Error on failure.
689 689
 	 */
690
-	private function do_request( $post_id ) {
690
+	private function do_request($post_id) {
691 691
 
692 692
 		// Change the state to `REQUEST`.
693
-		$this->set_state( $post_id, self::STATE_REQUEST );
693
+		$this->set_state($post_id, self::STATE_REQUEST);
694 694
 
695 695
 		// Get the post.
696
-		$post = get_post( $post_id );
696
+		$post = get_post($post_id);
697 697
 
698 698
 		// Bail out if the post isn't found.
699
-		if ( null === $post ) {
700
-			$this->log->warn( "Post $post_id not found." );
699
+		if (null === $post) {
700
+			$this->log->warn("Post $post_id not found.");
701 701
 
702
-			return new WP_Error( 0, "Cannot find post $post_id." );
702
+			return new WP_Error(0, "Cannot find post $post_id.");
703 703
 		}
704 704
 
705 705
 		// Get the link setting.
706
-		$options = $this->get_options( $post_id );
706
+		$options = $this->get_options($post_id);
707 707
 
708
-		$this->log->debug( 'Sending analysis request for post $post_id [ links :: ' . $options['links'] . ', min_occurrences :: ' . $options['min_occurrences'] . ' ] ...' );
708
+		$this->log->debug('Sending analysis request for post $post_id [ links :: '.$options['links'].', min_occurrences :: '.$options['min_occurrences'].' ] ...');
709 709
 
710 710
 		// Get the batch analysis URL.
711 711
 		$url = $this->configuration_service->get_batch_analysis_url();
@@ -723,7 +723,7 @@  discard block
 block discarded – undo
723 723
 		);
724 724
 
725 725
 		// Get the HTTP options.
726
-		$args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
726
+		$args = array_merge_recursive(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array(
727 727
 			'method'      => 'POST',
728 728
 			'headers'     => array(
729 729
 				'Accept'       => 'application/json',
@@ -731,13 +731,13 @@  discard block
 block discarded – undo
731 731
 			),
732 732
 			// we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
733 733
 			'httpversion' => '1.0',
734
-			'body'        => wp_json_encode( $params ),
735
-		) );
734
+			'body'        => wp_json_encode($params),
735
+		));
736 736
 
737
-		$this->log->debug( "Posting analysis request for post $post_id to $url..." );
737
+		$this->log->debug("Posting analysis request for post $post_id to $url...");
738 738
 
739 739
 		// Post the parameter.
740
-		return wp_remote_post( $url, $args );
740
+		return wp_remote_post($url, $args);
741 741
 	}
742 742
 
743 743
 	/**
@@ -749,20 +749,20 @@  discard block
 block discarded – undo
749 749
 	 *
750 750
 	 * @return WP_Error|array The response or WP_Error on failure.
751 751
 	 */
752
-	private function do_complete( $post_id ) {
752
+	private function do_complete($post_id) {
753 753
 
754
-		$post = get_post( $post_id );
754
+		$post = get_post($post_id);
755 755
 
756
-		if ( null === $post ) {
756
+		if (null === $post) {
757 757
 			// Post was possibly deleted, just bailout.
758
-			return new WP_Error( 0, "Post $post_id not found . " );
758
+			return new WP_Error(0, "Post $post_id not found . ");
759 759
 		}
760 760
 
761 761
 		$url = $this->configuration_service->get_batch_analysis_url();
762 762
 		$key = $this->configuration_service->get_key();
763
-		$url = $url . '/' . $post->ID . '?key=' . $key;
763
+		$url = $url.'/'.$post->ID.'?key='.$key;
764 764
 
765
-		return wp_remote_get( $url, unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
765
+		return wp_remote_get($url, unserialize(WL_REDLINK_API_HTTP_OPTIONS));
766 766
 	}
767 767
 
768 768
 	/**
@@ -774,15 +774,15 @@  discard block
 block discarded – undo
774 774
 	 */
775 775
 	public function get_warnings() {
776 776
 
777
-		return get_posts( array(
777
+		return get_posts(array(
778 778
 			'fields'      => 'ids',
779
-			'numberposts' => - 1,
779
+			'numberposts' => -1,
780 780
 			'post_status' => 'any',
781 781
 			'meta_key'    => self::WARNING_META_KEY,
782 782
 			'meta_value'  => 'yes',
783 783
 			'post_type'   => 'any',
784 784
 			// Add any because posts from multiple posts types may be waiting.
785
-		) );
785
+		));
786 786
 	}
787 787
 
788 788
 	/**
@@ -792,17 +792,17 @@  discard block
 block discarded – undo
792 792
 	 *
793 793
 	 * @param int $id The {@link WP_Post}'s id.
794 794
 	 */
795
-	private function maybe_set_default_term( $id ) {
795
+	private function maybe_set_default_term($id) {
796 796
 		// Check whether the post has any of the WordLift entity types.
797
-		$has_term = has_term( '', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, $id );
797
+		$has_term = has_term('', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, $id);
798 798
 
799 799
 		// Bail if the term is associated with entity types already.
800
-		if ( ! empty( $has_term ) ) {
800
+		if ( ! empty($has_term)) {
801 801
 			return;
802 802
 		}
803 803
 
804 804
 		// Set the default `article` term.
805
-		wp_set_object_terms( $id, 'article', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
805
+		wp_set_object_terms($id, 'article', Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME);
806 806
 
807 807
 	}
808 808
 
Please login to merge, or discard this patch.
src/includes/batch-analysis/class-wordlift-batch-analysis-adapter.php 2 patches
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -16,165 +16,165 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Batch_Analysis_Adapter {
18 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
-	}
51
-
52
-	/**
53
-	 * Submit the posts for batch analysis.
54
-	 *
55
-	 * @since 3.14.2
56
-	 */
57
-	public function submit() {
58
-
59
-		// Get the parameters from the $_REQUEST.
60
-		$params = self::create_params_from_request();
61
-
62
-		// Submit the request.
63
-		$count = $this->batch_analysis_service->submit( $params );
64
-
65
-		// Clear any buffer.
66
-		ob_clean();
67
-
68
-		// Send the response.
69
-		wp_send_json_success( array( 'count' => $count ) );
70
-
71
-	}
72
-
73
-	/**
74
-	 * Submit the posts for batch analysis.
75
-	 *
76
-	 * @since 3.14.2
77
-	 */
78
-	public function submit_posts() {
79
-
80
-		$this->log->trace( 'Received Batch Analysis request for posts...' );
81
-
82
-		if ( empty( $_REQUEST['post'] ) ) {
83
-			$this->log->error( 'Batch Analysis request for posts missing the post(s) id.' );
84
-
85
-			wp_send_json_error( 'The `post` parameter is required.' );
86
-		}
87
-
88
-		// Get the parameters from the $_REQUEST.
89
-		$params = self::create_params_from_request();
90
-
91
-		// Submit the request.
92
-		$count = $this->batch_analysis_service->submit_posts( $params );
93
-
94
-		// Clear any buffer.
95
-		ob_clean();
96
-
97
-		// Send the response.
98
-		wp_send_json_success( array( 'count' => $count ) );
99
-
100
-	}
101
-
102
-
103
-	/**
104
-	 * A helper function to create the parameters from the $_REQUEST.
105
-	 *
106
-	 * @since 3.17.0
107
-	 *
108
-	 * @return array An array or parameters.
109
-	 */
110
-	private static function create_params_from_request() {
111
-
112
-		// Build params array and check if param exists.
113
-		// @codingStandardsIgnoreStart, Ignore phpcs indentation errors.
114
-		$params = array(
115
-			// Get the `links` parameter, or use `default` if not provided.
116
-			'links'             => isset( $_REQUEST['links'] ) ? $_REQUEST['links'] : 'default',
117
-			// If `include_annotated` is set to `yes`, the set the parameter to true.
118
-			'include_annotated' => isset( $_REQUEST['include_annotated'] ) && 'yes' === $_REQUEST['include_annotated'],
119
-			// Set the minimum amount of occurrences, use `1` by default.
120
-			'min_occurrences'   => isset( $_REQUEST['min_occurrences'] ) && is_numeric( $_REQUEST['min_occurrences'] ) ? intval( $_REQUEST['min_occurrences'] ) : 1,
121
-			// Set the `post_type` to `post` if none provided.
122
-			'post_type'         => isset( $_REQUEST['post_type'] ) ? (array) $_REQUEST['post_type'] : 'post',
123
-			// Set the exclude array.
124
-			'exclude'           => isset( $_REQUEST['exclude'] ) ? (array) $_REQUEST['exclude'] : array(),
125
-			// Set the `from` date, or null if not provided.
126
-			'from'              => isset( $_REQUEST['from'] ) ? $_REQUEST['from'] : null,
127
-			// Set the `to` date, or null if not provided.
128
-			'to'                => isset( $_REQUEST['to'] ) ? $_REQUEST['to'] : null,
129
-			//
130
-			'ids'               => isset( $_REQUEST['post'] ) ? wp_parse_id_list( (array) $_REQUEST['post'] ) : array(),
131
-		);
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
+    }
51
+
52
+    /**
53
+     * Submit the posts for batch analysis.
54
+     *
55
+     * @since 3.14.2
56
+     */
57
+    public function submit() {
58
+
59
+        // Get the parameters from the $_REQUEST.
60
+        $params = self::create_params_from_request();
61
+
62
+        // Submit the request.
63
+        $count = $this->batch_analysis_service->submit( $params );
64
+
65
+        // Clear any buffer.
66
+        ob_clean();
67
+
68
+        // Send the response.
69
+        wp_send_json_success( array( 'count' => $count ) );
70
+
71
+    }
72
+
73
+    /**
74
+     * Submit the posts for batch analysis.
75
+     *
76
+     * @since 3.14.2
77
+     */
78
+    public function submit_posts() {
79
+
80
+        $this->log->trace( 'Received Batch Analysis request for posts...' );
81
+
82
+        if ( empty( $_REQUEST['post'] ) ) {
83
+            $this->log->error( 'Batch Analysis request for posts missing the post(s) id.' );
84
+
85
+            wp_send_json_error( 'The `post` parameter is required.' );
86
+        }
87
+
88
+        // Get the parameters from the $_REQUEST.
89
+        $params = self::create_params_from_request();
90
+
91
+        // Submit the request.
92
+        $count = $this->batch_analysis_service->submit_posts( $params );
93
+
94
+        // Clear any buffer.
95
+        ob_clean();
96
+
97
+        // Send the response.
98
+        wp_send_json_success( array( 'count' => $count ) );
99
+
100
+    }
101
+
102
+
103
+    /**
104
+     * A helper function to create the parameters from the $_REQUEST.
105
+     *
106
+     * @since 3.17.0
107
+     *
108
+     * @return array An array or parameters.
109
+     */
110
+    private static function create_params_from_request() {
111
+
112
+        // Build params array and check if param exists.
113
+        // @codingStandardsIgnoreStart, Ignore phpcs indentation errors.
114
+        $params = array(
115
+            // Get the `links` parameter, or use `default` if not provided.
116
+            'links'             => isset( $_REQUEST['links'] ) ? $_REQUEST['links'] : 'default',
117
+            // If `include_annotated` is set to `yes`, the set the parameter to true.
118
+            'include_annotated' => isset( $_REQUEST['include_annotated'] ) && 'yes' === $_REQUEST['include_annotated'],
119
+            // Set the minimum amount of occurrences, use `1` by default.
120
+            'min_occurrences'   => isset( $_REQUEST['min_occurrences'] ) && is_numeric( $_REQUEST['min_occurrences'] ) ? intval( $_REQUEST['min_occurrences'] ) : 1,
121
+            // Set the `post_type` to `post` if none provided.
122
+            'post_type'         => isset( $_REQUEST['post_type'] ) ? (array) $_REQUEST['post_type'] : 'post',
123
+            // Set the exclude array.
124
+            'exclude'           => isset( $_REQUEST['exclude'] ) ? (array) $_REQUEST['exclude'] : array(),
125
+            // Set the `from` date, or null if not provided.
126
+            'from'              => isset( $_REQUEST['from'] ) ? $_REQUEST['from'] : null,
127
+            // Set the `to` date, or null if not provided.
128
+            'to'                => isset( $_REQUEST['to'] ) ? $_REQUEST['to'] : null,
129
+            //
130
+            'ids'               => isset( $_REQUEST['post'] ) ? wp_parse_id_list( (array) $_REQUEST['post'] ) : array(),
131
+        );
132 132
 
133
-		// @codingStandardsIgnoreEnd
133
+        // @codingStandardsIgnoreEnd
134 134
 
135
-		return $params;
136
-	}
135
+        return $params;
136
+    }
137 137
 
138
-	/**
139
-	 * Cancel the batch analysis for the specified post.
140
-	 *
141
-	 * @since 3.14.0
142
-	 */
143
-	public function cancel() {
138
+    /**
139
+     * Cancel the batch analysis for the specified post.
140
+     *
141
+     * @since 3.14.0
142
+     */
143
+    public function cancel() {
144 144
 
145
-		if ( ! isset( $_REQUEST['post'] ) ) {
146
-			wp_die( 'The `post` parameter is required.' );
147
-		}
145
+        if ( ! isset( $_REQUEST['post'] ) ) {
146
+            wp_die( 'The `post` parameter is required.' );
147
+        }
148 148
 
149
-		$count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] );
149
+        $count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] );
150 150
 
151
-		// Clear any buffer.
152
-		ob_clean();
151
+        // Clear any buffer.
152
+        ob_clean();
153 153
 
154
-		// Send the response.
155
-		wp_send_json_success( array( 'count' => $count ) );
154
+        // Send the response.
155
+        wp_send_json_success( array( 'count' => $count ) );
156 156
 
157
-	}
157
+    }
158 158
 
159
-	/**
160
-	 * Clear warnings for the specified post.
161
-	 *
162
-	 * @since 3.14.0
163
-	 */
164
-	public function clear_warning() {
159
+    /**
160
+     * Clear warnings for the specified post.
161
+     *
162
+     * @since 3.14.0
163
+     */
164
+    public function clear_warning() {
165 165
 
166
-		if ( ! isset( $_REQUEST['post'] ) ) {
167
-			wp_die( 'The `post` parameter is required.' );
168
-		}
166
+        if ( ! isset( $_REQUEST['post'] ) ) {
167
+            wp_die( 'The `post` parameter is required.' );
168
+        }
169 169
 
170
-		$this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] );
171
-
172
-		// Clear any buffer.
173
-		ob_clean();
174
-
175
-		// Send the response.
176
-		wp_send_json_success();
170
+        $this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] );
171
+
172
+        // Clear any buffer.
173
+        ob_clean();
174
+
175
+        // Send the response.
176
+        wp_send_json_success();
177 177
 
178
-	}
178
+    }
179 179
 
180 180
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -41,9 +41,9 @@  discard block
 block discarded – undo
41 41
 	 *
42 42
 	 * @param \Wordlift_Batch_Analysis_Service $batch_analysis_service
43 43
 	 */
44
-	public function __construct( $batch_analysis_service ) {
44
+	public function __construct($batch_analysis_service) {
45 45
 
46
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
46
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
47 47
 
48 48
 		$this->batch_analysis_service = $batch_analysis_service;
49 49
 
@@ -60,13 +60,13 @@  discard block
 block discarded – undo
60 60
 		$params = self::create_params_from_request();
61 61
 
62 62
 		// Submit the request.
63
-		$count = $this->batch_analysis_service->submit( $params );
63
+		$count = $this->batch_analysis_service->submit($params);
64 64
 
65 65
 		// Clear any buffer.
66 66
 		ob_clean();
67 67
 
68 68
 		// Send the response.
69
-		wp_send_json_success( array( 'count' => $count ) );
69
+		wp_send_json_success(array('count' => $count));
70 70
 
71 71
 	}
72 72
 
@@ -77,25 +77,25 @@  discard block
 block discarded – undo
77 77
 	 */
78 78
 	public function submit_posts() {
79 79
 
80
-		$this->log->trace( 'Received Batch Analysis request for posts...' );
80
+		$this->log->trace('Received Batch Analysis request for posts...');
81 81
 
82
-		if ( empty( $_REQUEST['post'] ) ) {
83
-			$this->log->error( 'Batch Analysis request for posts missing the post(s) id.' );
82
+		if (empty($_REQUEST['post'])) {
83
+			$this->log->error('Batch Analysis request for posts missing the post(s) id.');
84 84
 
85
-			wp_send_json_error( 'The `post` parameter is required.' );
85
+			wp_send_json_error('The `post` parameter is required.');
86 86
 		}
87 87
 
88 88
 		// Get the parameters from the $_REQUEST.
89 89
 		$params = self::create_params_from_request();
90 90
 
91 91
 		// Submit the request.
92
-		$count = $this->batch_analysis_service->submit_posts( $params );
92
+		$count = $this->batch_analysis_service->submit_posts($params);
93 93
 
94 94
 		// Clear any buffer.
95 95
 		ob_clean();
96 96
 
97 97
 		// Send the response.
98
-		wp_send_json_success( array( 'count' => $count ) );
98
+		wp_send_json_success(array('count' => $count));
99 99
 
100 100
 	}
101 101
 
@@ -113,21 +113,21 @@  discard block
 block discarded – undo
113 113
 		// @codingStandardsIgnoreStart, Ignore phpcs indentation errors.
114 114
 		$params = array(
115 115
 			// Get the `links` parameter, or use `default` if not provided.
116
-			'links'             => isset( $_REQUEST['links'] ) ? $_REQUEST['links'] : 'default',
116
+			'links'             => isset($_REQUEST['links']) ? $_REQUEST['links'] : 'default',
117 117
 			// If `include_annotated` is set to `yes`, the set the parameter to true.
118
-			'include_annotated' => isset( $_REQUEST['include_annotated'] ) && 'yes' === $_REQUEST['include_annotated'],
118
+			'include_annotated' => isset($_REQUEST['include_annotated']) && 'yes' === $_REQUEST['include_annotated'],
119 119
 			// Set the minimum amount of occurrences, use `1` by default.
120
-			'min_occurrences'   => isset( $_REQUEST['min_occurrences'] ) && is_numeric( $_REQUEST['min_occurrences'] ) ? intval( $_REQUEST['min_occurrences'] ) : 1,
120
+			'min_occurrences'   => isset($_REQUEST['min_occurrences']) && is_numeric($_REQUEST['min_occurrences']) ? intval($_REQUEST['min_occurrences']) : 1,
121 121
 			// Set the `post_type` to `post` if none provided.
122
-			'post_type'         => isset( $_REQUEST['post_type'] ) ? (array) $_REQUEST['post_type'] : 'post',
122
+			'post_type'         => isset($_REQUEST['post_type']) ? (array) $_REQUEST['post_type'] : 'post',
123 123
 			// Set the exclude array.
124
-			'exclude'           => isset( $_REQUEST['exclude'] ) ? (array) $_REQUEST['exclude'] : array(),
124
+			'exclude'           => isset($_REQUEST['exclude']) ? (array) $_REQUEST['exclude'] : array(),
125 125
 			// Set the `from` date, or null if not provided.
126
-			'from'              => isset( $_REQUEST['from'] ) ? $_REQUEST['from'] : null,
126
+			'from'              => isset($_REQUEST['from']) ? $_REQUEST['from'] : null,
127 127
 			// Set the `to` date, or null if not provided.
128
-			'to'                => isset( $_REQUEST['to'] ) ? $_REQUEST['to'] : null,
128
+			'to'                => isset($_REQUEST['to']) ? $_REQUEST['to'] : null,
129 129
 			//
130
-			'ids'               => isset( $_REQUEST['post'] ) ? wp_parse_id_list( (array) $_REQUEST['post'] ) : array(),
130
+			'ids'               => isset($_REQUEST['post']) ? wp_parse_id_list((array) $_REQUEST['post']) : array(),
131 131
 		);
132 132
 
133 133
 		// @codingStandardsIgnoreEnd
@@ -142,17 +142,17 @@  discard block
 block discarded – undo
142 142
 	 */
143 143
 	public function cancel() {
144 144
 
145
-		if ( ! isset( $_REQUEST['post'] ) ) {
146
-			wp_die( 'The `post` parameter is required.' );
145
+		if ( ! isset($_REQUEST['post'])) {
146
+			wp_die('The `post` parameter is required.');
147 147
 		}
148 148
 
149
-		$count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] );
149
+		$count = $this->batch_analysis_service->cancel((array) $_REQUEST['post']);
150 150
 
151 151
 		// Clear any buffer.
152 152
 		ob_clean();
153 153
 
154 154
 		// Send the response.
155
-		wp_send_json_success( array( 'count' => $count ) );
155
+		wp_send_json_success(array('count' => $count));
156 156
 
157 157
 	}
158 158
 
@@ -163,11 +163,11 @@  discard block
 block discarded – undo
163 163
 	 */
164 164
 	public function clear_warning() {
165 165
 
166
-		if ( ! isset( $_REQUEST['post'] ) ) {
167
-			wp_die( 'The `post` parameter is required.' );
166
+		if ( ! isset($_REQUEST['post'])) {
167
+			wp_die('The `post` parameter is required.');
168 168
 		}
169 169
 
170
-		$this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] );
170
+		$this->batch_analysis_service->clear_warning((array) $_REQUEST['post']);
171 171
 
172 172
 		// Clear any buffer.
173 173
 		ob_clean();
Please login to merge, or discard this patch.