PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.16
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.16
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / class-berqdatabase.php

class-berqdatabase.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.16, at inc/class-berqdatabase.php

167 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 if (!class_exists('berqDatabase')) {
5
6 class berqDatabase
7 {
8 // Dispatch a cleanup task by key. Returns an array with a 'count' and 'message',
9 // or false if the task key is unknown. Shared by the AJAX handler and the cron callback
10 // so manual and scheduled runs behave identically.
11 public static function run_task($task, $args = [])
12 {
13 switch ($task) {
14 case 'revisions':
15 $count = self::clean_revisions(isset($args['limit']) ? (int) $args['limit'] : 5);
16 return ['count' => $count, 'message' => sprintf(__('%d revision(s) deleted.', 'searchpro'), $count)];
17
18 case 'auto_drafts':
19 $count = self::clean_auto_drafts();
20 return ['count' => $count, 'message' => sprintf(__('%d auto-draft/trashed post(s) deleted.', 'searchpro'), $count)];
21
22 case 'transients':
23 $count = self::clean_transients();
24 return ['count' => $count, 'message' => sprintf(__('%d expired transient(s) deleted.', 'searchpro'), $count)];
25
26 case 'spam_comments':
27 $count = self::clean_spam_comments();
28 return ['count' => $count, 'message' => sprintf(__('%d spam/trashed comment(s) deleted.', 'searchpro'), $count)];
29
30 case 'orphaned_meta':
31 $count = self::clean_orphaned_postmeta();
32 return ['count' => $count, 'message' => sprintf(__('%d orphaned meta row(s) deleted.', 'searchpro'), $count)];
33
34 case 'optimize_tables':
35 $tables = self::optimize_tables();
36 return ['count' => $tables, 'message' => sprintf(__('%d table(s) optimized.', 'searchpro'), $tables)];
37
38 default:
39 return false;
40 }
41 }
42
43 // Delete post revisions beyond the configured limit per post, oldest first.
44 public static function clean_revisions($limit)
45 {
46 global $wpdb;
47
48 $deleted = 0;
49
50 $post_ids = $wpdb->get_col(
51 "SELECT DISTINCT post_parent FROM {$wpdb->posts} WHERE post_type = 'revision' AND post_parent != 0"
52 );
53
54 foreach ($post_ids as $post_id) {
55 $revisions = wp_get_post_revisions($post_id, ['order' => 'DESC']);
56
57 if (count($revisions) <= $limit) {
58 continue;
59 }
60
61 $excess = array_slice($revisions, $limit);
62
63 foreach ($excess as $revision) {
64 if (wp_delete_post_revision($revision->ID)) {
65 $deleted++;
66 }
67 }
68 }
69
70 return $deleted;
71 }
72
73 // Delete auto-draft posts and posts already in the trash.
74 public static function clean_auto_drafts()
75 {
76 global $wpdb;
77
78 $post_ids = $wpdb->get_col(
79 "SELECT ID FROM {$wpdb->posts} WHERE post_status IN ('auto-draft', 'trash')"
80 );
81
82 $deleted = 0;
83 foreach ($post_ids as $post_id) {
84 if (wp_delete_post($post_id, true)) {
85 $deleted++;
86 }
87 }
88
89 return $deleted;
90 }
91
92 // Delete expired transients (core helper) plus any orphaned timeout rows it leaves behind.
93 public static function clean_transients()
94 {
95 global $wpdb;
96
97 $before = (int) $wpdb->get_var(
98 "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_%' OR option_name LIKE '\\_site\\_transient\\_%'"
99 );
100
101 delete_expired_transients(true);
102
103 // Orphaned _transient_timeout_* rows with no matching _transient_ row
104 $wpdb->query(
105 "DELETE t FROM {$wpdb->options} t
106 LEFT JOIN {$wpdb->options} v ON v.option_name = REPLACE(t.option_name, '_transient_timeout_', '_transient_')
107 WHERE t.option_name LIKE '\\_transient\\_timeout\\_%' AND v.option_id IS NULL"
108 );
109
110 $after = (int) $wpdb->get_var(
111 "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_%' OR option_name LIKE '\\_site\\_transient\\_%'"
112 );
113
114 return max(0, $before - $after);
115 }
116
117 // Delete spam and trashed comments.
118 public static function clean_spam_comments()
119 {
120 global $wpdb;
121
122 $comment_ids = $wpdb->get_col(
123 "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved IN ('spam', 'trash')"
124 );
125
126 $deleted = 0;
127 foreach ($comment_ids as $comment_id) {
128 if (wp_delete_comment($comment_id, true)) {
129 $deleted++;
130 }
131 }
132
133 return $deleted;
134 }
135
136 // Delete postmeta rows whose parent post no longer exists.
137 public static function clean_orphaned_postmeta()
138 {
139 global $wpdb;
140
141 return (int) $wpdb->query(
142 "DELETE pm FROM {$wpdb->postmeta} pm
143 LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
144 WHERE p.ID IS NULL"
145 );
146 }
147
148 // Run OPTIMIZE TABLE on every table owned by this WordPress install.
149 public static function optimize_tables()
150 {
151 global $wpdb;
152
153 $tables = $wpdb->get_col("SHOW TABLES LIKE '" . $wpdb->esc_like($wpdb->prefix) . "%'");
154
155 $optimized = 0;
156 foreach ($tables as $table) {
157 if ($wpdb->query("OPTIMIZE TABLE `$table`") !== false) {
158 $optimized++;
159 }
160 }
161
162 return $optimized;
163 }
164 }
165
166 }
167