PluginProbe
Manage – Centralized site maintenance and monitoring / trunk
Manage – Centralized site maintenance and monitoring vtrunk
1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4
manage / modules / api / components / optimizations.php

optimizations.php in Manage – Centralized site maintenance and monitoring trunk, at modules/api/components/optimizations.php

447 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Manage\Modules\Api\Components;
3
4 use Manage\Modules\Api\Classes\Route;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 class Optimizations extends Route {
11
12 public function register_routes() {
13 register_rest_route( static::NAMESPACE, '/optimizations/database', [
14 'methods' => 'POST',
15 'permission_callback' => [ $this, 'token_authentication' ],
16 'callback' => [ $this, 'optimize_database' ],
17 ] );
18
19 register_rest_route( static::NAMESPACE, '/optimizations/cache', [
20 'methods' => 'POST',
21 'permission_callback' => [ $this, 'token_authentication' ],
22 'callback' => [ $this, 'clear_cache' ],
23 ] );
24
25 register_rest_route( static::NAMESPACE, '/optimizations/transients', [
26 'methods' => 'POST',
27 'permission_callback' => [ $this, 'token_authentication' ],
28 'callback' => [ $this, 'cleanup_expired_transients' ],
29 ] );
30
31 register_rest_route( static::NAMESPACE, '/optimizations/spam-comments', [
32 'methods' => 'POST',
33 'permission_callback' => [ $this, 'token_authentication' ],
34 'callback' => [ $this, 'cleanup_spam_comments' ],
35 ] );
36
37 register_rest_route( static::NAMESPACE, '/optimizations/trashed-comments', [
38 'methods' => 'POST',
39 'permission_callback' => [ $this, 'token_authentication' ],
40 'callback' => [ $this, 'cleanup_trashed_comments' ],
41 ] );
42
43 register_rest_route( static::NAMESPACE, '/optimizations/auto-drafts', [
44 'methods' => 'POST',
45 'permission_callback' => [ $this, 'token_authentication' ],
46 'callback' => [ $this, 'cleanup_auto_drafts' ],
47 ] );
48
49 register_rest_route( static::NAMESPACE, '/optimizations/revisions', [
50 'methods' => 'POST',
51 'permission_callback' => [ $this, 'token_authentication' ],
52 'callback' => [ $this, 'cleanup_revisions' ],
53 ] );
54
55 register_rest_route( static::NAMESPACE, '/optimizations/trashed-posts', [
56 'methods' => 'POST',
57 'permission_callback' => [ $this, 'token_authentication' ],
58 'callback' => [ $this, 'cleanup_trashed_posts' ],
59 ] );
60 }
61
62 public function optimize_database() {
63 global $wpdb;
64
65 try {
66 $tables = $wpdb->get_results( 'SHOW TABLES', ARRAY_N );
67 $optimized_tables = [];
68
69 foreach ( $tables as $table ) {
70 $table_name = $table[0];
71 $result = $wpdb->query(
72 $wpdb->prepare( 'OPTIMIZE TABLE `%s`', $table_name )
73 );
74
75 if ( false !== $result ) {
76 $optimized_tables[] = $table_name;
77 }
78 }
79
80 return rest_ensure_response( [
81 'status' => 'success',
82 'message' => 'Database optimization completed successfully.',
83 'optimized_tables' => count( $optimized_tables ),
84 'tables' => $optimized_tables,
85 ] );
86
87 } catch ( \Exception $e ) {
88 return new \WP_Error(
89 'database_optimization_failed',
90 'Database optimization failed: ' . $e->getMessage(),
91 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
92 );
93 }
94 }
95
96 public function clear_cache() {
97 try {
98 // Clear WordPress object cache
99 if ( function_exists( 'wp_cache_flush' ) ) {
100 wp_cache_flush();
101 }
102
103 // Clear opcache if available
104 if ( function_exists( 'opcache_reset' ) ) {
105 opcache_reset();
106 }
107
108 // Clear common caching plugins
109 $cache_plugins_cleared = [];
110
111 // WP Rocket
112 if ( function_exists( 'rocket_clean_domain' ) ) {
113 rocket_clean_domain();
114 $cache_plugins_cleared[] = 'WP Rocket';
115 }
116
117 // W3 Total Cache
118 if ( function_exists( 'w3tc_flush_all' ) ) {
119 w3tc_flush_all();
120 $cache_plugins_cleared[] = 'W3 Total Cache';
121 }
122
123 // WP Super Cache
124 if ( function_exists( 'wp_cache_clear_cache' ) ) {
125 wp_cache_clear_cache();
126 $cache_plugins_cleared[] = 'WP Super Cache';
127 }
128
129 // LiteSpeed Cache
130 if ( class_exists( 'LiteSpeed\Purge' ) ) {
131 \LiteSpeed\Purge::purge_all();
132 $cache_plugins_cleared[] = 'LiteSpeed Cache';
133 }
134
135 // Elementor Cache
136 if ( class_exists( '\Elementor\Plugin' ) ) {
137 if ( method_exists( '\Elementor\Plugin', 'instance' ) ) {
138 $elementor = \Elementor\Plugin::instance();
139
140 if ( isset( $elementor->files_manager ) && method_exists( $elementor->files_manager, 'clear_cache' ) ) {
141 $elementor->files_manager->clear_cache();
142 }
143 }
144
145 $cache_plugins_cleared[] = 'Elementor';
146 }
147
148 return rest_ensure_response( [
149 'status' => 'success',
150 'message' => 'Cache cleared successfully.',
151 'cache_plugins_cleared' => $cache_plugins_cleared,
152 ] );
153
154 } catch ( \Exception $e ) {
155 return new \WP_Error(
156 'cache_clear_failed',
157 'Cache clearing failed: ' . $e->getMessage(),
158 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
159 );
160 }
161 }
162
163 public function cleanup_expired_transients() {
164 global $wpdb;
165
166 try {
167 $current_time = time();
168 $deleted_transients = 0;
169
170 $timeout_options = $wpdb->get_results(
171 $wpdb->prepare(
172 "SELECT option_name, option_value FROM {$wpdb->options}
173 WHERE option_name LIKE %s
174 AND option_value < %d",
175 '_transient_timeout_%',
176 $current_time
177 )
178 );
179
180 foreach ( $timeout_options as $timeout_option ) {
181 $transient_name = str_replace( '_transient_timeout_', '', $timeout_option->option_name );
182
183 $deleted_timeout = $wpdb->delete(
184 $wpdb->options,
185 [ 'option_name' => $timeout_option->option_name ],
186 [ '%s' ]
187 );
188
189 $deleted_transient = $wpdb->delete(
190 $wpdb->options,
191 [ 'option_name' => '_transient_' . $transient_name ],
192 [ '%s' ]
193 );
194
195 if ( $deleted_timeout || $deleted_transient ) {
196 $deleted_transients++;
197 }
198 }
199
200 if ( is_multisite() ) {
201 $site_timeout_options = $wpdb->get_results(
202 $wpdb->prepare(
203 "SELECT option_name, option_value FROM {$wpdb->options}
204 WHERE option_name LIKE %s
205 AND option_value < %d",
206 '_site_transient_timeout_%',
207 $current_time
208 )
209 );
210
211 foreach ( $site_timeout_options as $timeout_option ) {
212 $transient_name = str_replace( '_site_transient_timeout_', '', $timeout_option->option_name );
213
214 $deleted_timeout = $wpdb->delete(
215 $wpdb->options,
216 [ 'option_name' => $timeout_option->option_name ],
217 [ '%s' ]
218 );
219
220 $deleted_transient = $wpdb->delete(
221 $wpdb->options,
222 [ 'option_name' => '_site_transient_' . $transient_name ],
223 [ '%s' ]
224 );
225
226 if ( $deleted_timeout || $deleted_transient ) {
227 $deleted_transients++;
228 }
229 }
230 }
231
232 return rest_ensure_response( [
233 'status' => 'success',
234 'message' => 'Expired transients cleanup completed successfully.',
235 'deleted_transients' => $deleted_transients,
236 ] );
237
238 } catch ( \Exception $e ) {
239 return new \WP_Error(
240 'transients_cleanup_failed',
241 'Transients cleanup failed: ' . $e->getMessage(),
242 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
243 );
244 }
245 }
246
247 public function cleanup_spam_comments() {
248 global $wpdb;
249
250 try {
251 $spam_comments = $wpdb->get_results(
252 "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = 'spam'"
253 );
254
255 $spam_count = count( $spam_comments );
256
257 if ( 0 === $spam_count ) {
258 return rest_ensure_response( [
259 'status' => 'success',
260 'message' => 'No spam comments found to cleanup.',
261 'deleted_comments' => 0,
262 ] );
263 }
264
265 $deleted_comments = 0;
266 foreach ( $spam_comments as $comment ) {
267 if ( wp_delete_comment( $comment->comment_ID, true ) ) {
268 $deleted_comments++;
269 }
270 }
271
272 return rest_ensure_response( [
273 'status' => 'success',
274 'message' => 'Spam comments cleanup completed successfully.',
275 'deleted_comments' => $deleted_comments,
276 'spam_comments_found' => $spam_count,
277 ] );
278 } catch ( \Exception $e ) {
279 return new \WP_Error(
280 'spam_comments_cleanup_failed',
281 'Spam comments cleanup failed: ' . $e->getMessage(),
282 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
283 );
284 }
285 }
286
287 public function cleanup_trashed_comments() {
288 global $wpdb;
289
290 try {
291 $trashed_comments = $wpdb->get_results(
292 "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = 'trash'"
293 );
294
295 $trashed_count = count( $trashed_comments );
296
297 if ( 0 === $trashed_count ) {
298 return rest_ensure_response( [
299 'status' => 'success',
300 'message' => 'No trashed comments found to cleanup.',
301 'deleted_comments' => 0,
302 ] );
303 }
304
305 $deleted_comments = 0;
306 foreach ( $trashed_comments as $comment ) {
307 if ( wp_delete_comment( $comment->comment_ID, true ) ) {
308 $deleted_comments++;
309 }
310 }
311
312 return rest_ensure_response( [
313 'status' => 'success',
314 'message' => 'Trashed comments cleanup completed successfully.',
315 'deleted_comments' => $deleted_comments,
316 'trashed_comments_found' => $trashed_count,
317 ] );
318 } catch ( \Exception $e ) {
319 return new \WP_Error(
320 'trashed_comments_cleanup_failed',
321 'Trashed comments cleanup failed: ' . $e->getMessage(),
322 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
323 );
324 }
325 }
326
327 public function cleanup_auto_drafts() {
328 global $wpdb;
329
330 try {
331 $auto_drafts = $wpdb->get_results(
332 "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'auto-draft'"
333 );
334
335 $auto_drafts_count = count( $auto_drafts );
336
337 if ( 0 === $auto_drafts_count ) {
338 return rest_ensure_response( [
339 'status' => 'success',
340 'message' => 'No auto drafts found to cleanup.',
341 'deleted_posts' => 0,
342 ] );
343 }
344
345 $deleted_posts = 0;
346 foreach ( $auto_drafts as $post ) {
347 if ( wp_delete_post( $post->ID, true ) ) {
348 $deleted_posts++;
349 }
350 }
351
352 return rest_ensure_response( [
353 'status' => 'success',
354 'message' => 'Auto drafts cleanup completed successfully.',
355 'deleted_posts' => $deleted_posts,
356 'auto_drafts_found' => $auto_drafts_count,
357 ] );
358 } catch ( \Exception $e ) {
359 return new \WP_Error(
360 'auto_drafts_cleanup_failed',
361 'Auto drafts cleanup failed: ' . $e->getMessage(),
362 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
363 );
364 }
365 }
366
367 public function cleanup_revisions() {
368 global $wpdb;
369
370 try {
371 $revisions = $wpdb->get_results(
372 "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'revision'"
373 );
374
375 $revisions_count = count( $revisions );
376
377 if ( 0 === $revisions_count ) {
378 return rest_ensure_response( [
379 'status' => 'success',
380 'message' => 'No revisions found to cleanup.',
381 'deleted_posts' => 0,
382 ] );
383 }
384
385 $deleted_posts = 0;
386 foreach ( $revisions as $post ) {
387 if ( wp_delete_post( $post->ID, true ) ) {
388 $deleted_posts++;
389 }
390 }
391
392 return rest_ensure_response( [
393 'status' => 'success',
394 'message' => 'Revisions cleanup completed successfully.',
395 'deleted_posts' => $deleted_posts,
396 'revisions_found' => $revisions_count,
397 ] );
398 } catch ( \Exception $e ) {
399 return new \WP_Error(
400 'revisions_cleanup_failed',
401 'Revisions cleanup failed: ' . $e->getMessage(),
402 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
403 );
404 }
405 }
406
407 public function cleanup_trashed_posts() {
408 global $wpdb;
409
410 try {
411 $trashed_posts = $wpdb->get_results(
412 "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'trash'"
413 );
414
415 $trashed_posts_count = count( $trashed_posts );
416
417 if ( 0 === $trashed_posts_count ) {
418 return rest_ensure_response( [
419 'status' => 'success',
420 'message' => 'No trashed posts found to cleanup.',
421 'deleted_posts' => 0,
422 ] );
423 }
424
425 $deleted_posts = 0;
426 foreach ( $trashed_posts as $post ) {
427 if ( wp_delete_post( $post->ID, true ) ) {
428 $deleted_posts++;
429 }
430 }
431
432 return rest_ensure_response( [
433 'status' => 'success',
434 'message' => 'Trashed posts cleanup completed successfully.',
435 'deleted_posts' => $deleted_posts,
436 'trashed_posts_found' => $trashed_posts_count,
437 ] );
438 } catch ( \Exception $e ) {
439 return new \WP_Error(
440 'trashed_posts_cleanup_failed',
441 'Trashed posts cleanup failed: ' . $e->getMessage(),
442 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
443 );
444 }
445 }
446 }
447