| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_General_Indexation_Action; |
| 6 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Indexing_Complete_Action; |
| 7 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action; |
| 8 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action; |
| 9 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action; |
| 10 |
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action; |
| 11 |
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action; |
| 12 |
use Yoast\WP\SEO\Conditionals\Get_Request_Conditional; |
| 13 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 14 |
use Yoast\WP\SEO\Conditionals\Yoast_Admin_And_Dashboard_Conditional; |
| 15 |
use Yoast\WP\SEO\Helpers\Indexing_Helper; |
| 16 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Background_Indexing_Integration. |
| 20 |
* |
| 21 |
* @package Yoast\WP\SEO\Integrations\Admin |
| 22 |
*/ |
| 23 |
class Background_Indexing_Integration implements Integration_Interface { |
| 24 |
|
| 25 |
/** |
| 26 |
* The post indexing action. |
| 27 |
* |
| 28 |
* @var Indexable_Post_Indexation_Action |
| 29 |
*/ |
| 30 |
protected $post_indexation; |
| 31 |
|
| 32 |
/** |
| 33 |
* The term indexing action. |
| 34 |
* |
| 35 |
* @var Indexable_Term_Indexation_Action |
| 36 |
*/ |
| 37 |
protected $term_indexation; |
| 38 |
|
| 39 |
/** |
| 40 |
* The post type archive indexing action. |
| 41 |
* |
| 42 |
* @var Indexable_Post_Type_Archive_Indexation_Action |
| 43 |
*/ |
| 44 |
protected $post_type_archive_indexation; |
| 45 |
|
| 46 |
/** |
| 47 |
* Represents the general indexing. |
| 48 |
* |
| 49 |
* @var Indexable_General_Indexation_Action |
| 50 |
*/ |
| 51 |
protected $general_indexation; |
| 52 |
|
| 53 |
/** |
| 54 |
* Represents the indexing completed action. |
| 55 |
* |
| 56 |
* @var Indexable_Indexing_Complete_Action |
| 57 |
*/ |
| 58 |
protected $complete_indexation_action; |
| 59 |
|
| 60 |
/** |
| 61 |
* The post link indexing action. |
| 62 |
* |
| 63 |
* @var Post_Link_Indexing_Action |
| 64 |
*/ |
| 65 |
protected $post_link_indexing_action; |
| 66 |
|
| 67 |
/** |
| 68 |
* The term link indexing action. |
| 69 |
* |
| 70 |
* @var Term_Link_Indexing_Action |
| 71 |
*/ |
| 72 |
protected $term_link_indexing_action; |
| 73 |
|
| 74 |
/** |
| 75 |
* Represents the indexing helper. |
| 76 |
* |
| 77 |
* @var Indexing_Helper |
| 78 |
*/ |
| 79 |
protected $indexing_helper; |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the conditionals based on which this integration should be active. |
| 83 |
* |
| 84 |
* @return array The array of conditionals. |
| 85 |
*/ |
| 86 |
public static function get_conditionals() { |
| 87 |
return [ |
| 88 |
Yoast_Admin_And_Dashboard_Conditional::class, |
| 89 |
Migrations_Conditional::class, |
| 90 |
Get_Request_Conditional::class, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Shutdown_Indexing_Integration constructor. |
| 96 |
* |
| 97 |
* @param Indexable_Post_Indexation_Action $post_indexation The post indexing action. |
| 98 |
* @param Indexable_Term_Indexation_Action $term_indexation The term indexing action. |
| 99 |
* @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation The post type archive indexing action. |
| 100 |
* @param Indexable_General_Indexation_Action $general_indexation The general indexing action. |
| 101 |
* @param Indexable_Indexing_Complete_Action $complete_indexation_action The complete indexing action. |
| 102 |
* @param Post_Link_Indexing_Action $post_link_indexing_action The post indexing action. |
| 103 |
* @param Term_Link_Indexing_Action $term_link_indexing_action The term indexing action. |
| 104 |
* @param Indexing_Helper $indexing_helper The indexing helper. |
| 105 |
*/ |
| 106 |
public function __construct( |
| 107 |
Indexable_Post_Indexation_Action $post_indexation, |
| 108 |
Indexable_Term_Indexation_Action $term_indexation, |
| 109 |
Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation, |
| 110 |
Indexable_General_Indexation_Action $general_indexation, |
| 111 |
Indexable_Indexing_Complete_Action $complete_indexation_action, |
| 112 |
Post_Link_Indexing_Action $post_link_indexing_action, |
| 113 |
Term_Link_Indexing_Action $term_link_indexing_action, |
| 114 |
Indexing_Helper $indexing_helper |
| 115 |
) { |
| 116 |
$this->post_indexation = $post_indexation; |
| 117 |
$this->term_indexation = $term_indexation; |
| 118 |
$this->post_type_archive_indexation = $post_type_archive_indexation; |
| 119 |
$this->general_indexation = $general_indexation; |
| 120 |
$this->complete_indexation_action = $complete_indexation_action; |
| 121 |
$this->post_link_indexing_action = $post_link_indexing_action; |
| 122 |
$this->term_link_indexing_action = $term_link_indexing_action; |
| 123 |
$this->indexing_helper = $indexing_helper; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Register hooks. |
| 128 |
*/ |
| 129 |
public function register_hooks() { |
| 130 |
\add_action( 'admin_init', [ $this, 'register_shutdown_indexing' ], 10 ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Enqueues the required scripts. |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function register_shutdown_indexing() { |
| 139 |
if ( $this->should_index_on_shutdown( $this->get_shutdown_limit() ) ) { |
| 140 |
\register_shutdown_function( [ $this, 'index' ] ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Run a single indexing pass of each indexing action. Intended for use as a shutdown function. |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function index() { |
| 150 |
$this->post_indexation->index(); |
| 151 |
$this->term_indexation->index(); |
| 152 |
$this->general_indexation->index(); |
| 153 |
$this->post_type_archive_indexation->index(); |
| 154 |
$this->post_link_indexing_action->index(); |
| 155 |
$this->term_link_indexing_action->index(); |
| 156 |
$this->complete_indexation_action->complete(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Retrieves the shutdown limit. This limit is the amount of indexables that is generated in the background. |
| 161 |
* |
| 162 |
* @return int The shutdown limit. |
| 163 |
*/ |
| 164 |
protected function get_shutdown_limit() { |
| 165 |
/** |
| 166 |
* Filter 'wpseo_shutdown_indexation_limit' - Allow filtering the number of objects that can be indexed during shutdown. |
| 167 |
* |
| 168 |
* @api int The maximum number of objects indexed. |
| 169 |
*/ |
| 170 |
return \apply_filters( 'wpseo_shutdown_indexation_limit', 25 ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Determine whether background indexation should be performed. |
| 175 |
* |
| 176 |
* @param int $shutdown_limit The shutdown limit used to determine whether indexation should be run. |
| 177 |
* |
| 178 |
* @return bool Should background indexation be performed. |
| 179 |
*/ |
| 180 |
public function should_index_on_shutdown( $shutdown_limit ) { |
| 181 |
$total = $this->indexing_helper->get_limited_filtered_unindexed_count( $shutdown_limit ); |
| 182 |
|
| 183 |
return ( $total > 0 && $total < $shutdown_limit ); |
| 184 |
} |
| 185 |
} |
| 186 |
|