background-indexing-integration.php
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
namespace Yoast\WP\SEO\Integrations\Admin;
use Yoast\WP\SEO\Actions\Indexing\Indexable_General_Indexation_Action;
use Yoast\WP\SEO\Actions\Indexing\Indexable_Indexing_Complete_Action;
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action;
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action;
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action;
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action;
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action;
use Yoast\WP\SEO\Conditionals\Get_Request_Conditional;
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
use Yoast\WP\SEO\Conditionals\WP_CRON_Enabled_Conditional;
use Yoast\WP\SEO\Conditionals\Yoast_Admin_And_Dashboard_Conditional;
use Yoast\WP\SEO\Helpers\Indexable_Helper;
use Yoast\WP\SEO\Helpers\Indexing_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
/**
* Class Background_Indexing_Integration.
*
* @package Yoast\WP\SEO\Integrations\Admin
*/
class Background_Indexing_Integration implements Integration_Interface {
/**
* The post indexing action.
*
* @var Indexable_Post_Indexation_Action
*/
protected $post_indexation;
/**
* The term indexing action.
*
* @var Indexable_Term_Indexation_Action
*/
protected $term_indexation;
/**
* The post type archive indexing action.
*
* @var Indexable_Post_Type_Archive_Indexation_Action
*/
protected $post_type_archive_indexation;
/**
* Represents the general indexing.
*
* @var Indexable_General_Indexation_Action
*/
protected $general_indexation;
/**
* Represents the indexing completed action.
*
* @var Indexable_Indexing_Complete_Action
*/
protected $complete_indexation_action;
/**
* The post link indexing action.
*
* @var Post_Link_Indexing_Action
*/
protected $post_link_indexing_action;
/**
* The term link indexing action.
*
* @var Term_Link_Indexing_Action
*/
protected $term_link_indexing_action;
/**
* Represents the indexing helper.
*
* @var Indexing_Helper
*/
protected $indexing_helper;
/**
* An object that checks if we are on the Yoast admin or on the dashboard page.
*
* @var Yoast_Admin_And_Dashboard_Conditional
*/
protected $yoast_admin_and_dashboard_conditional;
/**
* An object that checks if we are handling a GET request.
*
* @var Get_Request_Conditional
*/
private $get_request_conditional;
/**
* An object that checks if WP_CRON is enabled.
*
* @var WP_CRON_Enabled_Conditional
*/
private $wp_cron_enabled_conditional;
/**
* The indexable helper
*
* @var Indexable_Helper
*/
private $indexable_helper;
/**
* Returns the conditionals based on which this integration should be active.
*
* @return array The array of conditionals.
*/
public static function get_conditionals() {
return [
Migrations_Conditional::class,
];
}
/**
* Shutdown_Indexing_Integration constructor.
*
* @param Indexable_Post_Indexation_Action $post_indexation The post indexing action.
* @param Indexable_Term_Indexation_Action $term_indexation The term indexing action.
* @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation The post type archive indexing action.
* @param Indexable_General_Indexation_Action $general_indexation The general indexing action.
* @param Indexable_Indexing_Complete_Action $complete_indexation_action The complete indexing action.
* @param Post_Link_Indexing_Action $post_link_indexing_action The post indexing action.
* @param Term_Link_Indexing_Action $term_link_indexing_action The term indexing action.
* @param Indexing_Helper $indexing_helper The indexing helper.
* @param Indexable_Helper $indexable_helper The indexable helper.
* @param Yoast_Admin_And_Dashboard_Conditional $yoast_admin_and_dashboard_conditional An object that checks if we are on the Yoast admin or on the dashboard page.
* @param Get_Request_Conditional $get_request_conditional An object that checks if we are handling a GET request.
* @param WP_CRON_Enabled_Conditional $wp_cron_enabled_conditional An object that checks if WP_CRON is enabled.
*/
public function __construct(
Indexable_Post_Indexation_Action $post_indexation,
Indexable_Term_Indexation_Action $term_indexation,
Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation,
Indexable_General_Indexation_Action $general_indexation,
Indexable_Indexing_Complete_Action $complete_indexation_action,
Post_Link_Indexing_Action $post_link_indexing_action,
Term_Link_Indexing_Action $term_link_indexing_action,
Indexing_Helper $indexing_helper,
Indexable_Helper $indexable_helper,
Yoast_Admin_And_Dashboard_Conditional $yoast_admin_and_dashboard_conditional,
Get_Request_Conditional $get_request_conditional,
WP_CRON_Enabled_Conditional $wp_cron_enabled_conditional
) {
$this->post_indexation = $post_indexation;
$this->term_indexation = $term_indexation;
$this->post_type_archive_indexation = $post_type_archive_indexation;
$this->general_indexation = $general_indexation;
$this->complete_indexation_action = $complete_indexation_action;
$this->post_link_indexing_action = $post_link_indexing_action;
$this->term_link_indexing_action = $term_link_indexing_action;
$this->indexing_helper = $indexing_helper;
$this->indexable_helper = $indexable_helper;
$this->yoast_admin_and_dashboard_conditional = $yoast_admin_and_dashboard_conditional;
$this->get_request_conditional = $get_request_conditional;
$this->wp_cron_enabled_conditional = $wp_cron_enabled_conditional;
}
/**
* Register hooks.
*/
public function register_hooks() {
\add_action( 'admin_init', [ $this, 'register_shutdown_indexing' ] );
\add_action( 'wpseo_indexable_index_batch', [ $this, 'index' ] );
// phpcs:ignore WordPress.WP.CronInterval -- The sniff doesn't understand values with parentheses. https://github.com/WordPress/WordPress-Coding-Standards/issues/2025
\add_filter( 'cron_schedules', [ $this, 'add_cron_schedule' ] );
\add_action( 'admin_init', [ $this, 'schedule_cron_indexing' ], 11 );
$this->add_limit_filters();
}
/**
* Adds the filters that change the indexing limits.
*
* @return void.
*/
public function add_limit_filters() {
\add_filter( 'wpseo_post_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
\add_filter( 'wpseo_post_type_archive_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
\add_filter( 'wpseo_term_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
\add_filter( 'wpseo_prominent_words_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
\add_filter( 'wpseo_link_indexing_limit', [ $this, 'throttle_cron_link_indexing' ] );
}
/**
* Enqueues the required scripts.
*
* @return void
*/
public function register_shutdown_indexing() {
if ( $this->should_index_on_shutdown( $this->get_shutdown_limit() ) ) {
$this->register_shutdown_function( 'index' );
}
}
/**
* Run a single indexing pass of each indexing action. Intended for use as a shutdown function.
*
* @return void
*/
public function index() {
if ( \wp_doing_cron() && ! $this->should_index_on_cron() ) {
$this->unschedule_cron_indexing();
return;
}
$this->post_indexation->index();
$this->term_indexation->index();
$this->general_indexation->index();
$this->post_type_archive_indexation->index();
$this->post_link_indexing_action->index();
$this->term_link_indexing_action->index();
if ( $this->indexing_helper->get_limited_filtered_unindexed_count_background( 1 ) === 0 ) {
// We set this as complete, even though prominent words might not be complete. But that's the way we always treated that.
$this->complete_indexation_action->complete();
}
}
/**
* Adds the 'Every fifteen minutes' cron schedule to WP-Cron.
*
* @param array $schedules The existing schedules.
*
* @return array The schedules containing the fifteen_minutes schedule.
*/
public function add_cron_schedule( $schedules ) {
if ( ! \is_array( $schedules ) ) {
return $schedules;
}
$schedules['fifteen_minutes'] = [
'interval' => ( 15 * MINUTE_IN_SECONDS ),
'display' => \esc_html__( 'Every fifteen minutes', 'wordpress-seo' ),
];
return $schedules;
}
/**
* Schedule background indexing every 15 minutes if the index isn't already up to date.
*
* @return void
*/
public function schedule_cron_indexing() {
/**
* Filter: 'wpseo_unindexed_count_queries_ran' - Informs whether the expensive unindexed count queries have been ran already.
*
* @internal
* @api bool
*/
$have_queries_ran = \apply_filters( 'wpseo_unindexed_count_queries_ran', false );
if ( ( ! $this->yoast_admin_and_dashboard_conditional->is_met() || ! $this->get_request_conditional->is_met() ) && ! $have_queries_ran ) {
return;
}
if ( ! \wp_next_scheduled( 'wpseo_indexable_index_batch' ) && $this->should_index_on_cron() ) {
\wp_schedule_event( ( \time() + \HOUR_IN_SECONDS ), 'fifteen_minutes', 'wpseo_indexable_index_batch' );
}
}
/**
* Limit cron indexing to 15 indexables per batch instead of 25.
*
* @param int $indexation_limit The current limit (filter input).
*
* @return int The new batch limit.
*/
public function throttle_cron_indexing( $indexation_limit ) {
if ( \wp_doing_cron() ) {
/**
* Filter: 'wpseo_cron_indexing_limit_size' - Adds the possibility to limit the number of items that are indexed when in cron action.
*
* @api int $limit Maximum number of indexables to be indexed per indexing action.
*/
return \apply_filters( 'wpseo_cron_indexing_limit_size', 15 );
}
return $indexation_limit;
}
/**
* Limit cron indexing to 3 links per batch instead of 5.
*
* @param int $link_indexation_limit The current limit (filter input).
*
* @return int The new batch limit.
*/
public function throttle_cron_link_indexing( $link_indexation_limit ) {
if ( \wp_doing_cron() ) {
/**
* Filter: 'wpseo_cron_link_indexing_limit_size' - Adds the possibility to limit the number of links that are indexed when in cron action.
*
* @api int $limit Maximum number of link indexables to be indexed per link indexing action.
*/
return \apply_filters( 'wpseo_cron_link_indexing_limit_size', 3 );
}
return $link_indexation_limit;
}
/**
* Determine whether cron indexation should be performed.
*
* @return bool Should cron indexation be performed.
*/
protected function should_index_on_cron() {
if ( ! $this->indexable_helper->should_index_indexables() ) {
return false;
}
// The filter supersedes everything when preventing cron indexation.
if ( \apply_filters( 'Yoast\WP\SEO\enable_cron_indexing', true ) !== true ) {
return false;
}
return $this->indexing_helper->get_limited_filtered_unindexed_count_background( 1 ) > 0;
}
/**
* Determine whether background indexation should be performed.
*
* @param int $shutdown_limit The shutdown limit used to determine whether indexation should be run.
*
* @return bool Should background indexation be performed.
*/
protected function should_index_on_shutdown( $shutdown_limit ) {
if ( ! $this->yoast_admin_and_dashboard_conditional->is_met() || ! $this->get_request_conditional->is_met() ) {
return false;
}
if ( ! $this->indexable_helper->should_index_indexables() ) {
return false;
}
if ( $this->wp_cron_enabled_conditional->is_met() ) {
return false;
}
$total_unindexed = $this->indexing_helper->get_limited_filtered_unindexed_count_background( $shutdown_limit );
if ( $total_unindexed === 0 || $total_unindexed > $shutdown_limit ) {
return false;
}
return true;
}
/**
* Retrieves the shutdown limit. This limit is the amount of indexables that is generated in the background.
*
* @return int The shutdown limit.
*/
protected function get_shutdown_limit() {
/**
* Filter 'wpseo_shutdown_indexation_limit' - Allow filtering the number of objects that can be indexed during shutdown.
*
* @api int The maximum number of objects indexed.
*/
return \apply_filters( 'wpseo_shutdown_indexation_limit', 25 );
}
/**
* Removes the cron indexing job from the scheduled event queue.
*
* @return void
*/
protected function unschedule_cron_indexing() {
$scheduled = \wp_next_scheduled( 'wpseo_indexable_index_batch' );
if ( $scheduled ) {
\wp_unschedule_event( $scheduled, 'wpseo_indexable_index_batch' );
}
}
/**
* Registers a method to be executed on shutdown.
* This wrapper mostly exists for making this class more unittestable.
*
* @param string $method_name The name of the method on the current instance to register.
*
* @return void
*/
protected function register_shutdown_function( $method_name ) {
\register_shutdown_function( [ $this, $method_name ] );
}
}