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

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

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

0.0
/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() {
52
                \add_action( self::START_HOOK, [ $this, 'run_cleanup' ] );
×
53
                \add_action( self::CRON_HOOK, [ $this, 'run_cleanup_cron' ] );
×
54
                \add_action( 'wpseo_deactivate', [ $this, 'reset_cleanup' ] );
×
55
        }
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() {
63
                return [];
×
64
        }
65

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

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

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

80
                        if ( $items_cleaned === false ) {
×
81
                                return;
×
82
                        }
83

84
                        if ( $items_cleaned < $limit ) {
×
85
                                continue;
×
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 );
×
90

91
                        return;
×
92
                }
93
        }
94

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

150
                        ]
×
151
                );
×
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() {
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 );
×
196

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

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

204
        /**
205
         * Resets and stops the cleanup integration.
206
         *
207
         * @return void
208
         */
209
        public function reset_cleanup() {
210
                \delete_option( self::CURRENT_TASK_OPTION );
×
211
                \wp_unschedule_hook( self::CRON_HOOK );
×
212
        }
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 ) {
222
                \update_option( self::CURRENT_TASK_OPTION, $task_name );
×
223
                \wp_schedule_event(
×
224
                        ( \time() + \HOUR_IN_SECONDS ),
×
225
                        'hourly',
×
226
                        self::CRON_HOOK
×
227
                );
×
228
        }
229

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

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

241
                        return;
×
242
                }
243

244
                $limit = $this->get_limit();
×
245
                $tasks = $this->get_cleanup_tasks();
×
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 ] ) ) {
×
249
                        $current_task_name = \key( $tasks );
×
250
                }
251

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

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

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

266
                                return;
×
267
                        }
268

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

274
                                        return;
×
275
                                }
276

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

280
                                return;
×
281
                        }
282

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

© 2026 Coveralls, Inc