• 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

89.52
/src/integrations/cleanup-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations;
4

5
use Closure;
6
use Yoast\WP\SEO\Repositories\Indexable_Cleanup_Repository;
7

8
/**
9
 * Adds cleanup hooks.
10
 */
11
class Cleanup_Integration implements Integration_Interface {
12

13
        /**
14
         * Identifier used to determine the current task.
15
         */
16
        const CURRENT_TASK_OPTION = 'wpseo-cleanup-current-task';
17

18
        /**
19
         * Identifier for the cron job.
20
         */
21
        const CRON_HOOK = 'wpseo_cleanup_cron';
22

23
        /**
24
         * Identifier for starting the cleanup.
25
         */
26
        const START_HOOK = 'wpseo_start_cleanup_indexables';
27

28
        /**
29
         * The cleanup repository.
30
         *
31
         * @var Indexable_Cleanup_Repository
32
         */
33
        private $cleanup_repository;
34

35
        /**
36
         * The constructor.
37
         *
38
         * @param Indexable_Cleanup_Repository $cleanup_repository The cleanup repository.
39
         */
40
        public function __construct( Indexable_Cleanup_Repository $cleanup_repository ) {
×
41
                $this->cleanup_repository = $cleanup_repository;
×
42
        }
43

44
        /**
45
         * Initializes the integration.
46
         *
47
         * This is the place to register hooks and filters.
48
         *
49
         * @return void
50
         */
51
        public function register_hooks() {
2✔
52
                \add_action( self::START_HOOK, [ $this, 'run_cleanup' ] );
2✔
53
                \add_action( self::CRON_HOOK, [ $this, 'run_cleanup_cron' ] );
2✔
54
                \add_action( 'wpseo_deactivate', [ $this, 'reset_cleanup' ] );
2✔
55
        }
1✔
56

57
        /**
58
         * Returns the conditionals based on which this loadable should be active.
59
         *
60
         * @return array The array of conditionals.
61
         */
62
        public static function get_conditionals() {
2✔
63
                return [];
2✔
64
        }
65

66
        /**
67
         * Starts the indexables cleanup.
68
         *
69
         * @return void
70
         */
71
        public function run_cleanup() {
6✔
72
                $this->reset_cleanup();
6✔
73

74
                $cleanups = $this->get_cleanup_tasks();
6✔
75
                $limit    = $this->get_limit();
6✔
76

77
                foreach ( $cleanups as $name => $action ) {
6✔
78
                        $items_cleaned = $action( $limit );
6✔
79

80
                        if ( $items_cleaned === false ) {
6✔
81
                                return;
2✔
82
                        }
83

84
                        if ( $items_cleaned < $limit ) {
4✔
85
                                continue;
2✔
86
                        }
87

88
                        // There are more items to delete for the current cleanup job, start a cronjob at the specified job.
89
                        $this->start_cron_job( $name );
2✔
90

91
                        return;
2✔
92
                }
93
        }
1✔
94

95
        /**
96
         * Returns an array of cleanup tasks.
97
         *
98
         * @return Closure[] The cleanup tasks.
99
         */
100
        public function get_cleanup_tasks() {
16✔
101
                return \array_merge(
16✔
102
                        [
8✔
103
                                'clean_indexables_with_object_type_and_object_sub_type_shop_order' => function ( $limit ) {
8✔
104
                                        return $this->cleanup_repository->clean_indexables_with_object_type_and_object_sub_type( 'post', 'shop_order', $limit );
8✔
105
                                },
16✔
106
                                'clean_indexables_by_post_status_auto-draft' => function ( $limit ) {
8✔
107
                                        return $this->cleanup_repository->clean_indexables_with_post_status( 'auto-draft', $limit );
8✔
108
                                },
16✔
109
                                'clean_indexables_for_non_publicly_viewable_post' => function ( $limit ) {
8✔
110
                                        return $this->cleanup_repository->clean_indexables_for_non_publicly_viewable_post( $limit );
2✔
111
                                },
16✔
112
                                'clean_indexables_for_non_publicly_viewable_taxonomies' => function ( $limit ) {
8✔
113
                                        return $this->cleanup_repository->clean_indexables_for_non_publicly_viewable_taxonomies( $limit );
2✔
114
                                },
16✔
115
                                'clean_indexables_for_non_publicly_viewable_post_type_archive_pages' => function ( $limit ) {
8✔
116
                                        return $this->cleanup_repository->clean_indexables_for_non_publicly_viewable_post_type_archive_pages( $limit );
2✔
117
                                },
16✔
118
                                'clean_indexables_for_authors_archive_disabled' => function ( $limit ) {
8✔
119
                                        return $this->cleanup_repository->clean_indexables_for_authors_archive_disabled( $limit );
2✔
120
                                },
16✔
121
                                'clean_indexables_for_authors_without_archive' => function ( $limit ) {
8✔
122
                                        return $this->cleanup_repository->clean_indexables_for_authors_without_archive( $limit );
2✔
123
                                },
16✔
124
                                'update_indexables_author_to_reassigned' => function ( $limit ) {
8✔
125
                                        return $this->cleanup_repository->update_indexables_author_to_reassigned( $limit );
2✔
126
                                },
16✔
127
                                'clean_orphaned_user_indexables_without_wp_user' => function ( $limit ) {
8✔
128
                                        return $this->cleanup_repository->clean_indexables_for_orphaned_users( $limit );
2✔
129
                                },
16✔
130
                                'clean_orphaned_user_indexables_without_wp_post' => function ( $limit ) {
8✔
131
                                        return $this->cleanup_repository->clean_indexables_for_object_type_and_source_table( 'posts', 'ID', 'post', $limit );
2✔
132
                                },
16✔
133
                                'clean_orphaned_user_indexables_without_wp_term' => function ( $limit ) {
8✔
134
                                        return $this->cleanup_repository->clean_indexables_for_object_type_and_source_table( 'terms', 'term_id', 'term', $limit );
2✔
135
                                },
16✔
136
                        ],
8✔
137
                        $this->get_additional_tasks(),
16✔
138
                        [
8✔
139
                                /* These should always be the last ones to be called. */
140
                                'clean_orphaned_content_indexable_hierarchy' => function ( $limit ) {
8✔
141
                                        return $this->cleanup_repository->cleanup_orphaned_from_table( 'Indexable_Hierarchy', 'indexable_id', $limit );
2✔
142
                                },
16✔
143
                                'clean_orphaned_content_seo_links_indexable_id' => function ( $limit ) {
8✔
144
                                        return $this->cleanup_repository->cleanup_orphaned_from_table( 'SEO_Links', 'indexable_id', $limit );
2✔
145
                                },
16✔
146
                                'clean_orphaned_content_seo_links_target_indexable_id' => function ( $limit ) {
8✔
147
                                        return $this->cleanup_repository->cleanup_orphaned_from_table( 'SEO_Links', 'target_indexable_id', $limit );
4✔
148
                                },
16✔
149

150
                        ]
8✔
151
                );
8✔
152
        }
153

154
        /**
155
         * Gets additional tasks from the 'wpseo_cleanup_tasks' filter.
156
         *
157
         * @return Closure[] Associative array of cleanup functions.
158
         */
159
        private function get_additional_tasks() {
×
160

161
                /**
162
                 * Filter: Adds the possibility to add addition cleanup functions.
163
                 *
164
                 * @api array Associative array with unique keys. Value should be a cleanup function that receives a limit.
165
                 */
166
                $additional_tasks = \apply_filters( 'wpseo_cleanup_tasks', [] );
×
167

168
                if ( ! \is_array( $additional_tasks ) ) {
×
169
                        return [];
×
170
                }
171

172
                foreach ( $additional_tasks as $key => $value ) {
×
173
                        if ( \is_int( $key ) ) {
×
174
                                return [];
×
175
                        }
176
                        if ( ( ! \is_object( $value ) ) || ! ( $value instanceof Closure ) ) {
×
177
                                return [];
×
178
                        }
179
                }
180

181
                return $additional_tasks;
×
182
        }
183

184
        /**
185
         * Gets the deletion limit for cleanups.
186
         *
187
         * @return int The limit for the amount of entities to be cleaned.
188
         */
189
        private function get_limit() {
16✔
190
                /**
191
                 * Filter: Adds the possibility to limit the number of items that are deleted from the database on cleanup.
192
                 *
193
                 * @api int $limit Maximum number of indexables to be cleaned up per query.
194
                 */
195
                $limit = \apply_filters( 'wpseo_cron_query_limit_size', 1000 );
16✔
196

197
                if ( ! \is_int( $limit ) ) {
16✔
198
                        $limit = 1000;
2✔
199
                }
200

201
                return \abs( $limit );
16✔
202
        }
203

204
        /**
205
         * Resets and stops the cleanup integration.
206
         *
207
         * @return void
208
         */
209
        public function reset_cleanup() {
6✔
210
                \delete_option( self::CURRENT_TASK_OPTION );
6✔
211
                \wp_unschedule_hook( self::CRON_HOOK );
6✔
212
        }
3✔
213

214
        /**
215
         * Starts the cleanup cron job.
216
         *
217
         * @param string $task_name The task name of the next cleanup task to run.
218
         *
219
         * @return void
220
         */
221
        private function start_cron_job( $task_name ) {
2✔
222
                \update_option( self::CURRENT_TASK_OPTION, $task_name );
2✔
223
                \wp_schedule_event(
2✔
224
                        ( \time() + \HOUR_IN_SECONDS ),
2✔
225
                        'hourly',
2✔
226
                        self::CRON_HOOK
2✔
227
                );
1✔
228
        }
1✔
229

230
        /**
231
         * The callback that is called for the cleanup cron job.
232
         *
233
         * @return void
234
         */
235
        public function run_cleanup_cron() {
12✔
236
                $current_task_name = \get_option( self::CURRENT_TASK_OPTION );
12✔
237

238
                if ( $current_task_name === false ) {
12✔
239
                        $this->reset_cleanup();
2✔
240

241
                        return;
2✔
242
                }
243

244
                $limit = $this->get_limit();
10✔
245
                $tasks = $this->get_cleanup_tasks();
10✔
246

247
                // The task may have been added by a filter that has been removed, in that case just start over.
248
                if ( ! isset( $tasks[ $current_task_name ] ) ) {
10✔
249
                        $current_task_name = \key( $tasks );
×
250
                }
251

252
                $current_task = \current( $tasks );
10✔
253
                while ( $current_task !== false ) {
10✔
254
                        // Skip the tasks that have already been done.
255
                        if ( \key( $tasks ) !== $current_task_name ) {
10✔
256
                                $current_task = \next( $tasks );
8✔
257
                                continue;
8✔
258
                        }
259

260
                        // Call the cleanup callback function that accompanies the current task.
261
                        $items_cleaned = $current_task( $limit );
10✔
262

263
                        if ( $items_cleaned === false ) {
10✔
264
                                $this->reset_cleanup();
2✔
265

266
                                return;
2✔
267
                        }
268

269
                        if ( $items_cleaned === 0 ) {
8✔
270
                                // Check if we are finished with all tasks.
271
                                if ( \next( $tasks ) === false ) {
4✔
272
                                        $this->reset_cleanup();
2✔
273

274
                                        return;
2✔
275
                                }
276

277
                                // Continue with the next task next time the cron job is run.
278
                                \update_option( self::CURRENT_TASK_OPTION, \key( $tasks ) );
2✔
279

280
                                return;
2✔
281
                        }
282

283
                        // There were items deleted for the current task, continue with the same task next cron call.
284
                        return;
4✔
285
                }
286
        }
287
}
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