PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | src/helpers/indexing-helper.php +109 -23 18.8 → trunk View file →
@@ -11,8 +11,9 @@
11 11 use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action;
12 12 use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action;
13 13 use Yoast\WP\SEO\Config\Indexing_Reasons;
14 14 use Yoast\WP\SEO\Integrations\Admin\Indexing_Notification_Integration;
15 +use Yoast\WP\SEO\Repositories\Indexable_Repository;
15 16 use Yoast_Notification_Center;
16 17
17 18 /**
18 19 * A helper object for indexing.
@@ -47,8 +48,22 @@
47 48 */
48 49 protected $indexing_actions;
49 50
50 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 +
58 + /**
59 + * The indexable repository.
60 + *
61 + * @var Indexable_Repository
62 + */
63 + protected $indexable_repository;
64 +
65 + /**
51 66 * Indexing_Helper constructor.
52 67 *
53 68 * @param Options_Helper $options_helper The options helper.
54 69 * @param Date_Helper $date_helper The date helper.
@@ -74,8 +89,10 @@
74 89 * @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation The posttype indexing action.
75 90 * @param Indexable_General_Indexation_Action $general_indexation The general indexing (homepage etc) action.
76 91 * @param Post_Link_Indexing_Action $post_link_indexing_action The post crosslink indexing action.
77 92 * @param Term_Link_Indexing_Action $term_link_indexing_action The term crossling indexing action.
93 + *
94 + * @return void
78 95 */
79 96 public function set_indexing_actions(
80 97 Indexable_Post_Indexation_Action $post_indexation,
81 98 Indexable_Term_Indexation_Action $term_indexation,
@@ -91,20 +108,24 @@
91 108 $general_indexation,
92 109 $post_link_indexing_action,
93 110 $term_link_indexing_action,
94 111 ];
112 +
113 + // Coincidentally, the background indexing actions are the same with the Free indexing actions for now.
114 + $this->background_indexing_actions = $this->indexing_actions;
95 115 }
96 116
97 117 /**
98 - * Sets several database options when the indexing process is started.
118 + * Sets the indexable repository for the indexing helper class.
99 119 *
100 - * @deprecated 17.4 This method was renamed to prepare for internal consistency.
101 - * @codeCoverageIgnore
120 + * @required
102 121 *
122 + * @param Indexable_Repository $indexable_repository The indexable repository.
123 + *
103 124 * @return void
104 125 */
105 - public function start() {
106 - $this->prepare();
126 + public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
127 + $this->indexable_repository = $indexable_repository;
107 128 }
108 129
109 130 /**
110 131 * Prepares the indexing process by setting several database options and removing the indexing notification.
@@ -120,22 +141,10 @@
120 141
121 142 /**
122 143 * Sets several database options when the indexing process is finished.
123 144 *
124 - * @deprecated 17.4 This method was renamed to complete for internal consistency.
125 - * @codeCoverageIgnore
126 - *
127 145 * @return void
128 146 */
129 - public function finish() {
130 - $this->complete();
131 - }
132 -
133 - /**
134 - * Sets several database options when the indexing process is finished.
135 - *
136 - * @return void
137 - */
138 147 public function complete() {
139 148 $this->set_reason( '' );
140 149 $this->set_started( null );
141 150 }
@@ -163,12 +172,14 @@
163 172 }
164 173
165 174 /**
166 175 * Removes any pre-existing notification, so that a new notification (with a possible new reason) can be added.
176 + *
177 + * @return void
167 178 */
168 179 protected function remove_indexing_notification() {
169 180 $this->notification_center->remove_notification_by_id(
170 - Indexing_Notification_Integration::NOTIFICATION_ID
181 + Indexing_Notification_Integration::NOTIFICATION_ID,
171 182 );
172 183 }
173 184
174 185 /**
@@ -231,8 +242,17 @@
231 242 return $this->options_helper->get( 'indexing_first_time', true );
232 243 }
233 244
234 245 /**
246 + * Gets a boolean that indicates whether or not the indexing of the indexables has completed.
247 + *
248 + * @return bool Whether the indexing of the indexables has completed.
249 + */
250 + public function is_finished_indexables_indexing() {
251 + return $this->options_helper->get( 'indexables_indexing_completed', false );
252 + }
253 +
254 + /**
235 255 * Returns the total number of unindexed objects.
236 256 *
237 257 * @return int The total number of unindexed objects.
238 258 */
@@ -246,8 +266,44 @@
246 266 return $unindexed_count;
247 267 }
248 268
249 269 /**
270 + * Returns the amount of un-indexed posts expressed in percentage, which will be needed to set a threshold.
271 + *
272 + * @param int $unindexed_count The number of unindexed objects.
273 + *
274 + * @return int The amount of unindexed posts expressed in percentage.
275 + */
276 + public function get_unindexed_percentage( $unindexed_count ) {
277 + // Gets the amount of indexed objects in the site.
278 + $indexed_count = $this->indexable_repository->get_total_number_of_indexables();
279 + // The total amount of objects in the site.
280 + $total_objects_count = ( $indexed_count + $unindexed_count );
281 +
282 + return ( ( $unindexed_count / $total_objects_count ) * 100 );
283 + }
284 +
285 + /**
286 + * Returns whether the SEO optimization button should show.
287 + *
288 + * @return bool Whether the SEO optimization button should show.
289 + */
290 + public function should_show_optimization_button() {
291 + // Gets the amount of unindexed objects in the site.
292 + $unindexed_count = $this->get_filtered_unindexed_count();
293 +
294 + // If the amount of unidexed posts is <10 don't show configuration button.
295 + if ( $unindexed_count <= 10 ) {
296 + return false;
297 + }
298 + // If the amount of unidexed posts is >10, but the total amount of unidexed posts is ≤4% of the total amount of objects in the site, don't show configuration button.
299 + if ( $this->get_unindexed_percentage( $unindexed_count ) <= 4 ) {
300 + return false;
301 + }
302 + return true;
303 + }
304 +
305 + /**
250 306 * Returns the total number of unindexed objects and applies a filter for third party integrations.
251 307 *
252 308 * @return int The total number of unindexed objects.
253 309 */
@@ -264,17 +320,22 @@
264 320
265 321 /**
266 322 * Returns a limited number of unindexed objects.
267 323 *
268 - * @param int $limit Limit the number of unindexed objects that are counted.
324 + * @param int $limit Limit the number of unindexed objects that are counted.
325 + * @param Indexation_Action_Interface[]|Limited_Indexing_Action_Interface[] $actions The actions whose counts will be calculated.
269 326 *
270 327 * @return int The total number of unindexed objects.
271 328 */
272 - public function get_limited_unindexed_count( $limit ) {
329 + public function get_limited_unindexed_count( $limit, $actions = [] ) {
273 330 $unindexed_count = 0;
274 331
275 - foreach ( $this->indexing_actions as $indexing_action ) {
276 - $unindexed_count += $indexing_action->get_limited_unindexed_count( $limit - $unindexed_count + 1 );
332 + if ( empty( $actions ) ) {
333 + $actions = $this->indexing_actions;
334 + }
335 +
336 + foreach ( $actions as $action ) {
337 + $unindexed_count += $action->get_limited_unindexed_count( $limit - $unindexed_count + 1 );
277 338 if ( $unindexed_count > $limit ) {
278 339 return $unindexed_count;
279 340 }
280 341 }
@@ -289,9 +350,9 @@
289 350 *
290 351 * @return int The total number of unindexed objects.
291 352 */
292 353 public function get_limited_filtered_unindexed_count( $limit ) {
293 - $unindexed_count = $this->get_limited_unindexed_count( $limit );
354 + $unindexed_count = $this->get_limited_unindexed_count( $limit, $this->indexing_actions );
294 355
295 356 if ( $unindexed_count > $limit ) {
296 357 return $unindexed_count;
297 358 }
@@ -304,6 +365,31 @@
304 365 * @param int|false $limit Limit the number of unindexed objects that need to be counted.
305 366 * False if it doesn't need to be limited.
306 367 */
307 368 return \apply_filters( 'wpseo_indexing_get_limited_unindexed_count', $unindexed_count, $limit );
369 + }
370 +
371 + /**
372 + * Returns the total number of unindexed objects that can be indexed in the background and applies a filter for third party integrations.
373 + *
374 + * @param int $limit Limit the number of unindexed objects that are counted.
375 + *
376 + * @return int The total number of unindexed objects that can be indexed in the background.
377 + */
378 + public function get_limited_filtered_unindexed_count_background( $limit ) {
379 + $unindexed_count = $this->get_limited_unindexed_count( $limit, $this->background_indexing_actions );
380 +
381 + if ( $unindexed_count > $limit ) {
382 + return $unindexed_count;
383 + }
384 +
385 + /**
386 + * Filter: 'wpseo_indexing_get_limited_unindexed_count_background' - Allow changing the amount of unindexed objects that can be indexed in the background,
387 + * and allow for a maximum number of items counted to improve performance.
388 + *
389 + * @param int $unindexed_count The amount of unindexed objects.
390 + * @param int|false $limit Limit the number of unindexed objects that need to be counted.
391 + * False if it doesn't need to be limited.
392 + */
393 + return \apply_filters( 'wpseo_indexing_get_limited_unindexed_count_background', $unindexed_count, $limit );
308 394 }
309 395 }