Skip to content

Commit 0615d63

Browse files
authored
Merge pull request #20005 from Yoast/19950-revisit-background-indexing-with-wp-cron
2 parents 95bf91e + 8078ed7 commit 0615d63

9 files changed

Lines changed: 1170 additions & 96 deletions

File tree

src/commands/index-command.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Yoast\WP\SEO\Actions\Indexing\Indexing_Prepare_Action;
1515
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action;
1616
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action;
17+
use Yoast\WP\SEO\Helpers\Indexable_Helper;
1718
use Yoast\WP\SEO\Main;
1819

1920
/**
@@ -77,6 +78,13 @@ class Index_Command implements Command_Interface {
7778
*/
7879
private $prepare_indexing_action;
7980

81+
/**
82+
* Represents the indexable helper.
83+
*
84+
* @var Indexable_Helper
85+
*/
86+
protected $indexable_helper;
87+
8088
/**
8189
* Generate_Indexables_Command constructor.
8290
*
@@ -96,6 +104,7 @@ class Index_Command implements Command_Interface {
96104
* action.
97105
* @param Term_Link_Indexing_Action $term_link_indexing_action The term link indexation
98106
* action.
107+
* @param Indexable_Helper $indexable_helper The indexable helper.
99108
*/
100109
public function __construct(
101110
Indexable_Post_Indexation_Action $post_indexation_action,
@@ -105,7 +114,8 @@ public function __construct(
105114
Indexable_Indexing_Complete_Action $complete_indexation_action,
106115
Indexing_Prepare_Action $prepare_indexing_action,
107116
Post_Link_Indexing_Action $post_link_indexing_action,
108-
Term_Link_Indexing_Action $term_link_indexing_action
117+
Term_Link_Indexing_Action $term_link_indexing_action,
118+
Indexable_Helper $indexable_helper
109119
) {
110120
$this->post_indexation_action = $post_indexation_action;
111121
$this->term_indexation_action = $term_indexation_action;
@@ -115,6 +125,7 @@ public function __construct(
115125
$this->prepare_indexing_action = $prepare_indexing_action;
116126
$this->post_link_indexing_action = $post_link_indexing_action;
117127
$this->term_link_indexing_action = $term_link_indexing_action;
128+
$this->indexable_helper = $indexable_helper;
118129
}
119130

120131
/**
@@ -158,6 +169,14 @@ public static function get_namespace() {
158169
* @return void
159170
*/
160171
public function index( $args = null, $assoc_args = null ) {
172+
if ( ! $this->indexable_helper->should_index_indexables() ) {
173+
WP_CLI::log(
174+
\__( 'Your WordPress environment is running on a non-production site. Indexables can only be created on production environments. Please check your `WP_ENVIRONMENT_TYPE` settings.', 'wordpress-seo' )
175+
);
176+
177+
return;
178+
}
179+
161180
if ( ! isset( $assoc_args['network'] ) ) {
162181
$this->run_indexation_actions( $assoc_args );
163182

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Yoast\WP\SEO\Conditionals;
4+
5+
/**
6+
* Class that checks if WP_CRON is enabled.
7+
*/
8+
class WP_CRON_Enabled_Conditional implements Conditional {
9+
10+
/**
11+
* Checks if WP_CRON is enabled.
12+
*
13+
* @return bool True when WP_CRON is enabled.
14+
*/
15+
public function is_met() {
16+
return ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON );
17+
}
18+
}

src/helpers/indexing-helper.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class Indexing_Helper {
4848
*/
4949
protected $indexing_actions;
5050

51+
/**
52+
* The indexation actions that can be done in the background.
53+
*
54+
* @var Indexation_Action_Interface[]|Limited_Indexing_Action_Interface[]
55+
*/
56+
protected $background_indexing_actions;
57+
5158
/**
5259
* The indexable repository.
5360
*
@@ -100,6 +107,9 @@ public function set_indexing_actions(
100107
$post_link_indexing_action,
101108
$term_link_indexing_action,
102109
];
110+
111+
// Coincidentally, the background indexing actions are the same with the Free indexing actions for now.
112+
$this->background_indexing_actions = $this->indexing_actions;
103113
}
104114

105115
/**
@@ -307,15 +317,20 @@ public function get_filtered_unindexed_count() {
307317
/**
308318
* Returns a limited number of unindexed objects.
309319
*
310-
* @param int $limit Limit the number of unindexed objects that are counted.
320+
* @param int $limit Limit the number of unindexed objects that are counted.
321+
* @param Indexation_Action_Interface[]|Limited_Indexing_Action_Interface[] $actions The actions whose counts will be calculated.
311322
*
312323
* @return int The total number of unindexed objects.
313324
*/
314-
public function get_limited_unindexed_count( $limit ) {
325+
public function get_limited_unindexed_count( $limit, $actions = [] ) {
315326
$unindexed_count = 0;
316327

317-
foreach ( $this->indexing_actions as $indexing_action ) {
318-
$unindexed_count += $indexing_action->get_limited_unindexed_count( $limit - $unindexed_count + 1 );
328+
if ( empty( $actions ) ) {
329+
$actions = $this->indexing_actions;
330+
}
331+
332+
foreach ( $actions as $action ) {
333+
$unindexed_count += $action->get_limited_unindexed_count( $limit - $unindexed_count + 1 );
319334
if ( $unindexed_count > $limit ) {
320335
return $unindexed_count;
321336
}
@@ -332,7 +347,7 @@ public function get_limited_unindexed_count( $limit ) {
332347
* @return int The total number of unindexed objects.
333348
*/
334349
public function get_limited_filtered_unindexed_count( $limit ) {
335-
$unindexed_count = $this->get_limited_unindexed_count( $limit );
350+
$unindexed_count = $this->get_limited_unindexed_count( $limit, $this->indexing_actions );
336351

337352
if ( $unindexed_count > $limit ) {
338353
return $unindexed_count;
@@ -348,4 +363,29 @@ public function get_limited_filtered_unindexed_count( $limit ) {
348363
*/
349364
return \apply_filters( 'wpseo_indexing_get_limited_unindexed_count', $unindexed_count, $limit );
350365
}
366+
367+
/**
368+
* Returns the total number of unindexed objects that can be indexed in the background and applies a filter for third party integrations.
369+
*
370+
* @param int $limit Limit the number of unindexed objects that are counted.
371+
*
372+
* @return int The total number of unindexed objects that can be indexed in the background.
373+
*/
374+
public function get_limited_filtered_unindexed_count_background( $limit ) {
375+
$unindexed_count = $this->get_limited_unindexed_count( $limit, $this->background_indexing_actions );
376+
377+
if ( $unindexed_count > $limit ) {
378+
return $unindexed_count;
379+
}
380+
381+
/**
382+
* Filter: 'wpseo_indexing_get_limited_unindexed_count_background' - Allow changing the amount of unindexed objects that can be indexed in the background,
383+
* and allow for a maximum number of items counted to improve performance.
384+
*
385+
* @param int $unindexed_count The amount of unindexed objects.
386+
* @param int|false $limit Limit the number of unindexed objects that need to be counted.
387+
* False if it doesn't need to be limited.
388+
*/
389+
return \apply_filters( 'wpseo_indexing_get_limited_unindexed_count_background', $unindexed_count, $limit );
390+
}
351391
}

0 commit comments

Comments
 (0)