PluginProbe
SweepPress: Website Cleanup and Optimization / 2.2
SweepPress: Website Cleanup and Optimization v2.2
trunk 1.1 1.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.8 2.0 2.1 2.1.1 2.2 2.3 2.3.1 2.4 2.4.1 2.4.2 2.5 2.6 3.0 3.1 3.1.1 3.2 All 39 releases
sweeppress / core / basic / Sweep.php

Sweep.php in SweepPress: Website Cleanup and Optimization 2.2, at core/basic/Sweep.php

337 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dev4Press\Plugin\SweepPress\Basic;
4
5 use Dev4Press\Plugin\SweepPress\Base\Sweeper;
6 use Dev4Press\Plugin\SweepPress\Sweeper\ActionSchedulerActionsCanceled;
7 use Dev4Press\Plugin\SweepPress\Sweeper\ActionSchedulerActionsComplete;
8 use Dev4Press\Plugin\SweepPress\Sweeper\ActionSchedulerActionsFailed;
9 use Dev4Press\Plugin\SweepPress\Sweeper\ActionSchedulerLog;
10 use Dev4Press\Plugin\SweepPress\Sweeper\ActionSchedulerLogOrphans;
11 use Dev4Press\Plugin\SweepPress\Sweeper\AkismetMeta;
12 use Dev4Press\Plugin\SweepPress\Sweeper\AllNetworkTransients;
13 use Dev4Press\Plugin\SweepPress\Sweeper\AllTransients;
14 use Dev4Press\Plugin\SweepPress\Sweeper\CommentmetaOrphans;
15 use Dev4Press\Plugin\SweepPress\Sweeper\CommentsOrphans;
16 use Dev4Press\Plugin\SweepPress\Sweeper\CommentsSpam;
17 use Dev4Press\Plugin\SweepPress\Sweeper\CommentsTrash;
18 use Dev4Press\Plugin\SweepPress\Sweeper\CommentsUnapproved;
19 use Dev4Press\Plugin\SweepPress\Sweeper\CommentsUserAgent;
20 use Dev4Press\Plugin\SweepPress\Sweeper\CronJobs;
21 use Dev4Press\Plugin\SweepPress\Sweeper\ExpiredTransients;
22 use Dev4Press\Plugin\SweepPress\Sweeper\InactiveSignups;
23 use Dev4Press\Plugin\SweepPress\Sweeper\NetworkExpiredTransients;
24 use Dev4Press\Plugin\SweepPress\Sweeper\OptimizeTables;
25 use Dev4Press\Plugin\SweepPress\Sweeper\PingbacksCleanup;
26 use Dev4Press\Plugin\SweepPress\Sweeper\PostmetaEdits;
27 use Dev4Press\Plugin\SweepPress\Sweeper\PostmetaLocks;
28 use Dev4Press\Plugin\SweepPress\Sweeper\PostmetaOembeds;
29 use Dev4Press\Plugin\SweepPress\Sweeper\PostmetaOld;
30 use Dev4Press\Plugin\SweepPress\Sweeper\PostmetaOrphans;
31 use Dev4Press\Plugin\SweepPress\Sweeper\PostsAutoDraft;
32 use Dev4Press\Plugin\SweepPress\Sweeper\PostsDraftRevisions;
33 use Dev4Press\Plugin\SweepPress\Sweeper\PostsOrphanedRevisions;
34 use Dev4Press\Plugin\SweepPress\Sweeper\PostsRevisions;
35 use Dev4Press\Plugin\SweepPress\Sweeper\PostsSpam;
36 use Dev4Press\Plugin\SweepPress\Sweeper\PostsTrash;
37 use Dev4Press\Plugin\SweepPress\Sweeper\RepairTables;
38 use Dev4Press\Plugin\SweepPress\Sweeper\RSSFeeds;
39 use Dev4Press\Plugin\SweepPress\Sweeper\TermmetaOrphans;
40 use Dev4Press\Plugin\SweepPress\Sweeper\TermsOrphans;
41 use Dev4Press\Plugin\SweepPress\Sweeper\TrackbacksCleanup;
42 use Dev4Press\Plugin\SweepPress\Sweeper\UsermetaOrphans;
43 use Dev4Press\v41\Core\Scope;
44
45 class Sweep {
46 /** @var array */
47 protected $_scopes;
48 /** @var array */
49 protected $_categories;
50 /** @var \Dev4Press\Plugin\SweepPress\Base\Sweeper[] */
51 protected $_sweepers;
52 protected $_counts = array(
53 'total' => 0,
54 'disabled' => 0,
55 'auto' => 0,
56 'quick' => 0,
57 'scheduled' => 0,
58 'monitor' => 0,
59 'bulk' => 0
60 );
61
62 public function __construct() {
63 $this->_scopes = array(
64 'blog' => __( "Blog", "sweeppress" ),
65 'network' => __( "Network", "sweeppress" )
66 );
67
68 $this->_categories = array(
69 'posts' => __( "Posts", "sweeppress" ),
70 'comments' => __( "Comments", "sweeppress" ),
71 'terms' => __( "Terms", "sweeppress" ),
72 'users' => __( "Users", "sweeppress" ),
73 'options' => __( "Options", "sweeppress" ),
74 'database' => __( "Database", "sweeppress" )
75 );
76
77 $this->_sweepers = array(
78 'posts-auto-draft' => PostsAutoDraft::instance(),
79 'posts-spam' => PostsSpam::instance(),
80 'posts-trash' => PostsTrash::instance(),
81 'posts-revisions' => PostsRevisions::instance(),
82 'posts-draft-revisions' => PostsDraftRevisions::instance(),
83 'posts-orphaned-revisions' => PostsOrphanedRevisions::instance(),
84 'postmeta-locks' => PostmetaLocks::instance(),
85 'postmeta-edits' => PostmetaEdits::instance(),
86 'postmeta-old' => PostmetaOld::instance(),
87 'postmeta-oembeds' => PostmetaOembeds::instance(),
88 'postmeta-orphans' => PostmetaOrphans::instance(),
89 'comments-spam' => CommentsSpam::instance(),
90 'comments-trash' => CommentsTrash::instance(),
91 'comments-unapproved' => CommentsUnapproved::instance(),
92 'comments-orphans' => CommentsOrphans::instance(),
93 'comments-user-agent' => CommentsUserAgent::instance(),
94 'commentmeta-orphans' => CommentmetaOrphans::instance(),
95 'pingbacks-cleanup' => PingbacksCleanup::instance(),
96 'trackbacks-cleanup' => TrackbacksCleanup::instance(),
97 'akismet-meta' => AkismetMeta::instance(),
98 'terms-orphans' => TermsOrphans::instance(),
99 'termmeta-orphans' => TermmetaOrphans::instance(),
100 'usermeta-orphans' => UsermetaOrphans::instance(),
101 'expired-transients' => ExpiredTransients::instance(),
102 'rss-feeds' => RSSFeeds::instance(),
103 'all-transients' => AllTransients::instance(),
104 'cron-jobs' => CronJobs::instance()
105 );
106
107 if ( apply_filters( 'sweeppress_sweepers_allow_db', SWEEPPRESS_SWEEPERS_ALLOW_DB ) ) {
108 $this->_sweepers[ 'optimize-tables' ] = OptimizeTables::instance();
109 $this->_sweepers[ 'repair-tables' ] = RepairTables::instance();
110 }
111
112 if ( sweeppress_is_actionscheduler_active() ) {
113 $this->_categories[ 'actionscheduler' ] = __( "ActionScheduler", "sweeppress" );
114
115 $this->_sweepers[ 'actionscheduler-log-orphans' ] = ActionSchedulerLogOrphans::instance();
116 $this->_sweepers[ 'actionscheduler-log' ] = ActionSchedulerLog::instance();
117 $this->_sweepers[ 'actionscheduler-actions-canceled' ] = ActionSchedulerActionsCanceled::instance();
118 $this->_sweepers[ 'actionscheduler-actions-complete' ] = ActionSchedulerActionsComplete::instance();
119 $this->_sweepers[ 'actionscheduler-actions-failed' ] = ActionSchedulerActionsFailed::instance();
120 } else {
121 $this->_counts[ 'disabled' ] += 5;
122 }
123
124 if ( Scope::instance()->is_multisite() && Scope::instance()->is_main_blog() ) {
125 $this->_categories[ 'network' ] = __( "Network", "sweeppress" );
126
127 $this->_sweepers[ 'inactive-signups' ] = InactiveSignups::instance();
128 $this->_sweepers[ 'all-network-transients' ] = AllNetworkTransients::instance();
129 $this->_sweepers[ 'network-expired-transients' ] = NetworkExpiredTransients::instance();
130 } else {
131 $this->_counts[ 'disabled' ] += 3;
132 }
133
134 foreach ( $this->_sweepers as $sweeper ) {
135 $this->_counts['total'] ++;
136
137 if ( $sweeper->for_auto_cleanup() ) {
138 $this->_counts['auto'] ++;
139 }
140
141 if ( $sweeper->for_quick_cleanup() ) {
142 $this->_counts['quick'] ++;
143 }
144
145 if ( $sweeper->for_scheduled_cleanup() ) {
146 $this->_counts['scheduled'] ++;
147 }
148
149 if ( $sweeper->for_monitor_task() ) {
150 $this->_counts['monitor'] ++;
151 }
152
153 if ( $sweeper->for_bulk_network() ) {
154 $this->_counts['bulk'] ++;
155 }
156 }
157 }
158
159 public static function instance() : Sweep {
160 static $instance = null;
161
162 if ( is_null( $instance ) ) {
163 $instance = new Sweep();
164 }
165
166 return $instance;
167 }
168
169 public function get_sweepers_count( string $what = 'total' ) : int {
170 return $this->_counts[ $what ] ?? 0;
171 }
172
173 public function get_category_label( string $category ) : string {
174 return $this->_categories[ $category ] ?? __( "Unknown", "sweeppress" );
175 }
176
177 public function get_sweeper_title( string $sweeper ) : string {
178 if ( $this->is_sweeper_valid( $sweeper ) ) {
179 return $this->_sweepers[ $sweeper ]->title();
180 }
181
182 return __( "Unknown Sweeper", "sweeppress" );
183 }
184
185 public function is_sweeper_task_valid( string $sweeper, string $task ) : bool {
186 if ( $this->is_sweeper_valid( $sweeper ) ) {
187 return $this->sweeper( $sweeper )->is_task_valid( $task );
188 }
189
190 return false;
191 }
192
193 public function is_sweeper_valid( string $sweeper ) : bool {
194 return isset( $this->_sweepers[ $sweeper ] );
195 }
196
197 public function is_sweeper_single_task( string $sweeper ) : ?bool {
198 if ( $this->is_sweeper_valid( $sweeper ) ) {
199 return $this->_sweepers[ $sweeper ]->for_single_task();
200 }
201
202 return null;
203 }
204
205 /** @return \Dev4Press\Plugin\SweepPress\Base\Sweeper[][] */
206 public function available() : array {
207 $list = array();
208
209 foreach ( $this->_sweepers as $sweeper ) {
210 if ( $sweeper->is_available() ) {
211 if ( ! isset( $list[ $sweeper->get_category() ] ) ) {
212 $list[ $sweeper->get_category() ] = array();
213 }
214
215 $list[ $sweeper->get_category() ][] = $sweeper;
216 }
217 }
218
219 return $list;
220 }
221
222 public function categories() : array {
223 return $this->_categories;
224 }
225
226 public function all( $all = false ) : array {
227 $list = array();
228
229 foreach ( $this->_sweepers as $sweeper ) {
230 if ( $all || $sweeper->is_available() ) {
231 $list[] = array(
232 'cat' => $sweeper->get_category(),
233 'category' => $this->get_category_label( $sweeper->get_category() ),
234 'name' => $sweeper->title(),
235 'code' => $sweeper->get_code(),
236 'scope' => $sweeper->get_scope(),
237 'version' => $sweeper->get_version(),
238 'info' => $sweeper->description(),
239 'auto' => $sweeper->for_auto_cleanup(),
240 'quick' => $sweeper->for_quick_cleanup(),
241 'cron' => $sweeper->for_scheduled_cleanup(),
242 'monitor' => $sweeper->for_monitor_task()
243 );
244 }
245 }
246
247 return $list;
248 }
249
250 public function sweeper( $code ) : ?Sweeper {
251 if ( isset( $this->_sweepers[ $code ] ) ) {
252 return $this->_sweepers[ $code ];
253 }
254
255 return null;
256 }
257
258 public function auto_sweep( string $source ) : array {
259 $list = $this->available();
260 $args = array();
261
262 foreach ( $list as $sweepers ) {
263 foreach ( $sweepers as $sweeper ) {
264 if ( $sweeper->for_auto_cleanup() && $sweeper->is_sweepable() ) {
265 $args[ $sweeper->get_code() ] = true;
266 }
267 }
268 }
269
270 return $this->sweep( $args, $source );
271 }
272
273 public function sweep( array $items, string $source ) : array {
274 $results = array(
275 'timer' => array(
276 'started' => microtime( true ),
277 ),
278 'stats' => array(
279 'source' => $source,
280 'records' => 0,
281 'size' => 0,
282 'jobs' => 0,
283 'tasks' => 0,
284 'sweepers' => array()
285 ),
286 'sweepers' => array()
287 );
288
289 foreach ( $items as $sweeper => $list ) {
290 $obj = $this->sweeper( $sweeper );
291
292 if ( $obj && $obj->is_allowed() && $obj->is_sweepable() ) {
293 $_started = microtime( true );
294 $tasks = $list === true ? $obj->list_sweepable_tasks_keys() : $list;
295
296 $results['sweepers'][ $sweeper ] = $obj->sweep( $tasks );
297
298 $_ended = microtime( true );
299
300 $results['stats']['jobs'] += 1;
301 $results['stats']['tasks'] += count( $tasks );
302 $results['stats']['records'] += $results['sweepers'][ $sweeper ]['records'];
303 $results['stats']['size'] += $results['sweepers'][ $sweeper ]['size'];
304
305 $results['stats']['sweepers'][ $sweeper ] = array(
306 'records' => $results['sweepers'][ $sweeper ]['records'],
307 'size' => $results['sweepers'][ $sweeper ]['size'],
308 'time' => $_ended - $_started
309 );
310
311 $results['sweepers'][ $sweeper ]['time'] = $_ended - $_started;
312 } else {
313 $results['sweepers'][ $sweeper ] = false;
314 }
315 }
316
317 $results['timer']['ended'] = microtime( true );
318 $results['stats']['time'] = $results['timer']['ended'] - $results['timer']['started'];
319
320 sweeppress_settings()->log_statistics( $results['stats'] );
321
322 return $results;
323 }
324
325 public function run_dashboard() {
326 $list = $this->available();
327
328 foreach ( $list as $category => $sweepers ) {
329 foreach ( $sweepers as $sweeper ) {
330 if ( $sweeper->for_auto_cleanup() || $sweeper->for_quick_cleanup() ) {
331 $sweeper->get_tasks();
332 }
333 }
334 }
335 }
336 }
337