• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.98
/src/integrations/admin/background-indexing-integration.php
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\WP_CRON_Enabled_Conditional;
15
use Yoast\WP\SEO\Conditionals\Yoast_Admin_And_Dashboard_Conditional;
16
use Yoast\WP\SEO\Helpers\Indexable_Helper;
17
use Yoast\WP\SEO\Helpers\Indexing_Helper;
18
use Yoast\WP\SEO\Integrations\Integration_Interface;
19

20
/**
21
 * Class Background_Indexing_Integration.
22
 *
23
 * @package Yoast\WP\SEO\Integrations\Admin
24
 */
25
class Background_Indexing_Integration implements Integration_Interface {
26

27
        /**
28
         * The post indexing action.
29
         *
30
         * @var Indexable_Post_Indexation_Action
31
         */
32
        protected $post_indexation;
33

34
        /**
35
         * The term indexing action.
36
         *
37
         * @var Indexable_Term_Indexation_Action
38
         */
39
        protected $term_indexation;
40

41
        /**
42
         * The post type archive indexing action.
43
         *
44
         * @var Indexable_Post_Type_Archive_Indexation_Action
45
         */
46
        protected $post_type_archive_indexation;
47

48
        /**
49
         * Represents the general indexing.
50
         *
51
         * @var Indexable_General_Indexation_Action
52
         */
53
        protected $general_indexation;
54

55
        /**
56
         * Represents the indexing completed action.
57
         *
58
         * @var Indexable_Indexing_Complete_Action
59
         */
60
        protected $complete_indexation_action;
61

62
        /**
63
         * The post link indexing action.
64
         *
65
         * @var Post_Link_Indexing_Action
66
         */
67
        protected $post_link_indexing_action;
68

69
        /**
70
         * The term link indexing action.
71
         *
72
         * @var Term_Link_Indexing_Action
73
         */
74
        protected $term_link_indexing_action;
75

76
        /**
77
         * Represents the indexing helper.
78
         *
79
         * @var Indexing_Helper
80
         */
81
        protected $indexing_helper;
82

83
        /**
84
         * An object that checks if we are on the Yoast admin or on the dashboard page.
85
         *
86
         * @var Yoast_Admin_And_Dashboard_Conditional
87
         */
88
        protected $yoast_admin_and_dashboard_conditional;
89

90
        /**
91
         * An object that checks if we are handling a GET request.
92
         *
93
         * @var Get_Request_Conditional
94
         */
95
        private $get_request_conditional;
96

97
        /**
98
         * An object that checks if WP_CRON is enabled.
99
         *
100
         * @var WP_CRON_Enabled_Conditional
101
         */
102
        private $wp_cron_enabled_conditional;
103

104
        /**
105
         * The indexable helper
106
         *
107
         * @var Indexable_Helper
108
         */
109
        private $indexable_helper;
110

111
        /**
112
         * Returns the conditionals based on which this integration should be active.
113
         *
114
         * @return array The array of conditionals.
115
         */
116
        public static function get_conditionals() {
2✔
117
                return [
1✔
118
                        Migrations_Conditional::class,
2✔
119
                ];
1✔
120
        }
121

122
        /**
123
         * Shutdown_Indexing_Integration constructor.
124
         *
125
         * @param Indexable_Post_Indexation_Action              $post_indexation                       The post indexing action.
126
         * @param Indexable_Term_Indexation_Action              $term_indexation                       The term indexing action.
127
         * @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation          The post type archive indexing action.
128
         * @param Indexable_General_Indexation_Action           $general_indexation                    The general indexing action.
129
         * @param Indexable_Indexing_Complete_Action            $complete_indexation_action            The complete indexing action.
130
         * @param Post_Link_Indexing_Action                     $post_link_indexing_action             The post indexing action.
131
         * @param Term_Link_Indexing_Action                     $term_link_indexing_action             The term indexing action.
132
         * @param Indexing_Helper                               $indexing_helper                       The indexing helper.
133
         * @param Indexable_Helper                              $indexable_helper                      The indexable helper.
134
         * @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.
135
         * @param Get_Request_Conditional                       $get_request_conditional               An object that checks if we are handling a GET request.
136
         * @param WP_CRON_Enabled_Conditional                   $wp_cron_enabled_conditional           An object that checks if WP_CRON is enabled.
137
         */
138
        public function __construct(
2✔
139
                Indexable_Post_Indexation_Action $post_indexation,
140
                Indexable_Term_Indexation_Action $term_indexation,
141
                Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation,
142
                Indexable_General_Indexation_Action $general_indexation,
143
                Indexable_Indexing_Complete_Action $complete_indexation_action,
144
                Post_Link_Indexing_Action $post_link_indexing_action,
145
                Term_Link_Indexing_Action $term_link_indexing_action,
146
                Indexing_Helper $indexing_helper,
147
                Indexable_Helper $indexable_helper,
148
                Yoast_Admin_And_Dashboard_Conditional $yoast_admin_and_dashboard_conditional,
149
                Get_Request_Conditional $get_request_conditional,
150
                WP_CRON_Enabled_Conditional $wp_cron_enabled_conditional
151
        ) {
1✔
152
                $this->post_indexation                       = $post_indexation;
2✔
153
                $this->term_indexation                       = $term_indexation;
2✔
154
                $this->post_type_archive_indexation          = $post_type_archive_indexation;
2✔
155
                $this->general_indexation                    = $general_indexation;
2✔
156
                $this->complete_indexation_action            = $complete_indexation_action;
2✔
157
                $this->post_link_indexing_action             = $post_link_indexing_action;
2✔
158
                $this->term_link_indexing_action             = $term_link_indexing_action;
2✔
159
                $this->indexing_helper                       = $indexing_helper;
2✔
160
                $this->indexable_helper                      = $indexable_helper;
2✔
161
                $this->yoast_admin_and_dashboard_conditional = $yoast_admin_and_dashboard_conditional;
2✔
162
                $this->get_request_conditional               = $get_request_conditional;
2✔
163
                $this->wp_cron_enabled_conditional           = $wp_cron_enabled_conditional;
2✔
164
        }
1✔
165

166
        /**
167
         * Register hooks.
168
         */
169
        public function register_hooks() {
2✔
170
                \add_action( 'admin_init', [ $this, 'register_shutdown_indexing' ] );
2✔
171
                \add_action( 'wpseo_indexable_index_batch', [ $this, 'index' ] );
2✔
172
                // phpcs:ignore WordPress.WP.CronInterval -- The sniff doesn't understand values with parentheses. https://github.com/WordPress/WordPress-Coding-Standards/issues/2025
173
                \add_filter( 'cron_schedules', [ $this, 'add_cron_schedule' ] );
2✔
174
                \add_action( 'admin_init', [ $this, 'schedule_cron_indexing' ], 11 );
2✔
175

176
                $this->add_limit_filters();
2✔
177
        }
1✔
178

179
        /**
180
         * Adds the filters that change the indexing limits.
181
         *
182
         * @return void.
183
         */
184
        public function add_limit_filters() {
2✔
185
                \add_filter( 'wpseo_post_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
2✔
186
                \add_filter( 'wpseo_post_type_archive_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
2✔
187
                \add_filter( 'wpseo_term_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
2✔
188
                \add_filter( 'wpseo_prominent_words_indexation_limit', [ $this, 'throttle_cron_indexing' ] );
2✔
189
                \add_filter( 'wpseo_link_indexing_limit', [ $this, 'throttle_cron_link_indexing' ] );
2✔
190
        }
1✔
191

192
        /**
193
         * Enqueues the required scripts.
194
         *
195
         * @return void
196
         */
197
        public function register_shutdown_indexing() {
12✔
198
                if ( $this->should_index_on_shutdown( $this->get_shutdown_limit() ) ) {
12✔
199
                        $this->register_shutdown_function( 'index' );
2✔
200
                }
201
        }
6✔
202

203
        /**
204
         * Run a single indexing pass of each indexing action. Intended for use as a shutdown function.
205
         *
206
         * @return void
207
         */
208
        public function index() {
12✔
209
                if ( \wp_doing_cron() && ! $this->should_index_on_cron() ) {
12✔
210
                        $this->unschedule_cron_indexing();
8✔
211

212
                        return;
8✔
213
                }
214

215
                $this->post_indexation->index();
4✔
216
                $this->term_indexation->index();
4✔
217
                $this->general_indexation->index();
4✔
218
                $this->post_type_archive_indexation->index();
4✔
219
                $this->post_link_indexing_action->index();
4✔
220
                $this->term_link_indexing_action->index();
4✔
221

222
                if ( $this->indexing_helper->get_limited_filtered_unindexed_count_background( 1 ) === 0 ) {
4✔
223
                        // We set this as complete, even though prominent words might not be complete. But that's the way we always treated that.
224
                        $this->complete_indexation_action->complete();
4✔
225
                }
226
        }
2✔
227

228
        /**
229
         * Adds the 'Every fifteen minutes' cron schedule to WP-Cron.
230
         *
231
         * @param array $schedules The existing schedules.
232
         *
233
         * @return array The schedules containing the fifteen_minutes schedule.
234
         */
235
        public function add_cron_schedule( $schedules ) {
4✔
236
                if ( ! \is_array( $schedules ) ) {
4✔
237
                        return $schedules;
2✔
238
                }
239

240
                $schedules['fifteen_minutes'] = [
2✔
241
                        'interval' => ( 15 * MINUTE_IN_SECONDS ),
2✔
242
                        'display'  => \esc_html__( 'Every fifteen minutes', 'wordpress-seo' ),
2✔
243
                ];
1✔
244

245
                return $schedules;
2✔
246
        }
247

248
        /**
249
         * Schedule background indexing every 15 minutes if the index isn't already up to date.
250
         *
251
         * @return void
252
         */
253
        public function schedule_cron_indexing() {
20✔
254
                /**
255
                 * Filter: 'wpseo_unindexed_count_queries_ran' - Informs whether the expensive unindexed count queries have been ran already.
256
                 *
257
                 * @internal
258
                 * @api bool
259
                 */
260
                $have_queries_ran = \apply_filters( 'wpseo_unindexed_count_queries_ran', false );
20✔
261

262
                if ( ( ! $this->yoast_admin_and_dashboard_conditional->is_met() || ! $this->get_request_conditional->is_met() ) && ! $have_queries_ran ) {
20✔
263
                        return;
4✔
264
                }
265

266
                if ( ! \wp_next_scheduled( 'wpseo_indexable_index_batch' ) && $this->should_index_on_cron() ) {
16✔
267
                        \wp_schedule_event( ( \time() + \HOUR_IN_SECONDS ), 'fifteen_minutes', 'wpseo_indexable_index_batch' );
6✔
268
                }
269
        }
8✔
270

271
        /**
272
         * Limit cron indexing to 15 indexables per batch instead of 25.
273
         *
274
         * @param int $indexation_limit The current limit (filter input).
275
         *
276
         * @return int The new batch limit.
277
         */
278
        public function throttle_cron_indexing( $indexation_limit ) {
4✔
279
                if ( \wp_doing_cron() ) {
4✔
280
                        /**
281
                         * Filter: 'wpseo_cron_indexing_limit_size' - Adds the possibility to limit the number of items that are indexed when in cron action.
282
                         *
283
                         * @api int $limit Maximum number of indexables to be indexed per indexing action.
284
                         */
285
                        return \apply_filters( 'wpseo_cron_indexing_limit_size', 15 );
2✔
286
                }
287

288
                return $indexation_limit;
2✔
289
        }
290

291
        /**
292
         * Limit cron indexing to 3 links per batch instead of 5.
293
         *
294
         * @param int $link_indexation_limit The current limit (filter input).
295
         *
296
         * @return int The new batch limit.
297
         */
298
        public function throttle_cron_link_indexing( $link_indexation_limit ) {
4✔
299
                if ( \wp_doing_cron() ) {
4✔
300
                        /**
301
                         * Filter: 'wpseo_cron_link_indexing_limit_size' - Adds the possibility to limit the number of links that are indexed when in cron action.
302
                         *
303
                         * @api int $limit Maximum number of link indexables to be indexed per link indexing action.
304
                         */
305
                        return \apply_filters( 'wpseo_cron_link_indexing_limit_size', 3 );
2✔
306
                }
307

308
                return $link_indexation_limit;
2✔
309
        }
310

311
        /**
312
         * Determine whether cron indexation should be performed.
313
         *
314
         * @return bool Should cron indexation be performed.
315
         */
316
        protected function should_index_on_cron() {
24✔
317
                if ( ! $this->indexable_helper->should_index_indexables() ) {
24✔
318
                        return false;
6✔
319
                }
320

321
                // The filter supersedes everything when preventing cron indexation.
322
                if ( \apply_filters( 'Yoast\WP\SEO\enable_cron_indexing', true ) !== true ) {
18✔
323
                        return false;
4✔
324
                }
325

326
                return $this->indexing_helper->get_limited_filtered_unindexed_count_background( 1 ) > 0;
14✔
327
        }
328

329
        /**
330
         * Determine whether background indexation should be performed.
331
         *
332
         * @param int $shutdown_limit The shutdown limit used to determine whether indexation should be run.
333
         *
334
         * @return bool Should background indexation be performed.
335
         */
336
        protected function should_index_on_shutdown( $shutdown_limit ) {
12✔
337
                if ( ! $this->yoast_admin_and_dashboard_conditional->is_met() || ! $this->get_request_conditional->is_met() ) {
12✔
338
                        return false;
4✔
339
                }
340

341
                if ( ! $this->indexable_helper->should_index_indexables() ) {
8✔
342
                        return false;
2✔
343
                }
344

345
                if ( $this->wp_cron_enabled_conditional->is_met() ) {
6✔
346
                        return false;
2✔
347
                }
348

349
                $total_unindexed = $this->indexing_helper->get_limited_filtered_unindexed_count_background( $shutdown_limit );
4✔
350
                if ( $total_unindexed === 0 || $total_unindexed > $shutdown_limit ) {
4✔
351
                        return false;
2✔
352
                }
353

354
                return true;
2✔
355
        }
356

357
        /**
358
         * Retrieves the shutdown limit. This limit is the amount of indexables that is generated in the background.
359
         *
360
         * @return int The shutdown limit.
361
         */
362
        protected function get_shutdown_limit() {
12✔
363
                /**
364
                 * Filter 'wpseo_shutdown_indexation_limit' - Allow filtering the number of objects that can be indexed during shutdown.
365
                 *
366
                 * @api int The maximum number of objects indexed.
367
                 */
368
                return \apply_filters( 'wpseo_shutdown_indexation_limit', 25 );
12✔
369
        }
370

371
        /**
372
         * Removes the cron indexing job from the scheduled event queue.
373
         *
374
         * @return void
375
         */
376
        protected function unschedule_cron_indexing() {
2✔
377
                $scheduled = \wp_next_scheduled( 'wpseo_indexable_index_batch' );
2✔
378
                if ( $scheduled ) {
2✔
379
                        \wp_unschedule_event( $scheduled, 'wpseo_indexable_index_batch' );
2✔
380
                }
381
        }
1✔
382

383
        /**
384
         * Registers a method to be executed on shutdown.
385
         * This wrapper mostly exists for making this class more unittestable.
386
         *
387
         * @param string $method_name The name of the method on the current instance to register.
388
         *
389
         * @return void
390
         */
391
        protected function register_shutdown_function( $method_name ) {
×
392
                \register_shutdown_function( [ $this, $method_name ] );
×
393
        }
394
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc