PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.6.1
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.6.1
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / optimizations / commentmeta.php

commentmeta.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.6.1, at optimizations/commentmeta.php

235 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 class WP_Optimization_commentmeta extends WP_Optimization {
6
7 public $ui_sort_order = 9000;
8
9 public $available_for_saving = true;
10
11 private $processed_trash_count;
12
13 private $processed_akismet_count;
14
15 private $found_trash_count;
16
17 private $found_akismet_count;
18
19 /**
20 * Prepare data for preview widget.
21 *
22 * @param array $params
23 *
24 * @return array
25 */
26 public function preview($params) {
27
28 // get clicked comment type link.
29 $type = isset($params['type']) && 'akismet' === $params['type'] ? 'akismet' : 'trash';
30
31 // get data requested for preview.
32 // Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb`
33 // phpcs:disable
34 $sql = $this->wpdb->prepare(
35 "SELECT * FROM `" . $this->wpdb->commentmeta . "`".
36 " WHERE ".
37 ('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : "").
38 ('akismet' === $type ? "meta_key LIKE" : "").
39 " %s ORDER BY `comment_ID` LIMIT %d, %d;",
40 array(
41 ('akismet' === $type ? '%akismet%' : ''),
42 $params['offset'],
43 $params['limit'],
44 )
45 );
46
47 $posts = $this->wpdb->get_results($sql, ARRAY_A);
48 // phpcs:enable
49
50 // fix empty revision titles.
51 if (!empty($posts)) {
52 foreach ($posts as $key => $post) {
53 $posts[$key]['post_title'] = array(
54 'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'],
55 'url' => get_edit_post_link($post['ID']),
56 );
57 }
58 }
59
60 // get total count comments for optimization.
61 // Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb`
62 // phpcs:disable
63 $sql = $this->wpdb->prepare(
64 "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "`".
65 "WHERE ".
66 ('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : "").
67 ('akismet' === $type ? "meta_key LIKE " : "").
68 " %s;",
69 array(
70 ('akismet' === $type ? '%akismet%' : ''),
71 )
72 );
73
74 $total = $this->wpdb->get_var($sql);
75 // phpcs:enable
76
77 return array(
78 'id_key' => 'meta_id',
79 'columns' => array(
80 'meta_id' => __('ID', 'wp-optimize'),
81 'comment_id' => __('Comment ID', 'wp-optimize'),
82 'meta_key' => __('Meta Key', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This is not a query
83 'meta_value' => __('Meta Value', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- This is not a query
84 ),
85 'offset' => $params['offset'],
86 'limit' => $params['limit'],
87 'total' => $total,
88 'data' => $this->htmlentities_array($posts, array('comment_ID')),
89 'message' => $total > 0 ? '' : __('No orphaned comment metadata in your database', 'wp-optimize'),
90 );
91 }
92
93 /**
94 * Do actions before optimize() function.
95 */
96 public function before_optimize() {
97 $this->processed_trash_count = 0;
98 $this->processed_akismet_count = 0;
99 }
100
101 /**
102 * Do actions after optimize() function.
103 */
104 public function after_optimize() {
105 // translators: %s is a number of unused comment metadata item removed
106 $message = sprintf(_n('%s unused comment metadata item removed', '%s unused comment metadata items removed', $this->processed_trash_count, 'wp-optimize'), number_format_i18n($this->processed_trash_count));
107 // translators: %s is a number of unused akismet comment metadata item removed
108 $message1 = sprintf(_n('%s unused akismet comment metadata item removed', '%s unused akismet comment metadata items removed', $this->processed_akismet_count, 'wp-optimize'), number_format_i18n($this->processed_akismet_count));
109
110 if ($this->is_multisite_mode()) {
111 // translators: %s is a number of sites
112 $blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
113 $message .= $blogs_count_text;
114 $message1 .= $blogs_count_text;
115 }
116
117 $this->logger->info($message);
118 $this->register_output($message);
119
120 $this->logger->info($message1);
121 $this->register_output($message1);
122 }
123
124 /**
125 * This needs reviewing when we review the whole cron-run set of options.
126 */
127 public function optimize() {
128 $clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)";
129
130 // if posted ids in params, then remove only selected items. used by preview widget.
131 if (isset($this->data['ids'])) {
132 $clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')';
133 }
134
135 $clean .= ";";
136
137 $commentstrash_meta = $this->query($clean);
138 $this->processed_trash_count += $commentstrash_meta;
139
140 $clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE meta_key LIKE '%akismet%'";
141
142 // if posted ids in params, then remove only selected items. used by preview widget.
143 if (isset($this->data['ids'])) {
144 $clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')';
145 }
146
147 $clean .= ";";
148
149 $commentstrash_meta2 = $this->query($clean);
150 $this->processed_akismet_count += $commentstrash_meta2;
151 }
152
153 /**
154 * Do actions before get_info().
155 */
156 public function before_get_info() {
157
158 $this->found_trash_count = 0;
159 $this->found_akismet_count = 0;
160
161 }
162
163 /**
164 * Do actions after get_info() function.
165 */
166 public function after_get_info() {
167
168 if ($this->found_trash_count > 0) {
169 // translators: %s is a number of orphaned comment metadata
170 $message = sprintf(_n('%s orphaned comment metadata in your database', '%s orphaned comment metadata in your database', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count));
171 } else {
172 $message = __('No orphaned comment metadata in your database', 'wp-optimize');
173 }
174
175 if ($this->found_akismet_count > 0) {
176 // translators: %s is a number of unused Akismet comment metadata
177 $message1 = sprintf(_n('%s unused Akismet comment meta rows in your database', '%s unused Akismet meta rows in your database', $this->found_akismet_count, 'wp-optimize'), number_format_i18n($this->found_akismet_count));
178 } else {
179 $message1 = __('No Akismet comment meta rows in your database', 'wp-optimize');
180 }
181
182 if ($this->is_multisite_mode()) {
183 // translators: %s is a number of sites
184 $blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
185 $message .= $blogs_count_text;
186 $message1 .= $blogs_count_text;
187 }
188
189 if ($this->found_trash_count > 0) {
190 $message = $this->get_preview_link($message, array('data-type' => 'trash'));
191 }
192
193 if ($this->found_akismet_count > 0) {
194 $message1 = $this->get_preview_link($message1, array('data-type' => 'akismet'));
195 }
196
197 $this->register_output($message);
198 $this->register_output($message1);
199
200 }
201
202 /**
203 * Get count of unoptimized items.
204 */
205 public function get_info() {
206
207 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);";
208 $commentmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used
209 $this->found_trash_count += $commentmeta;
210
211 $sql = "SELECT COUNT(*) FROM `".$this->wpdb->commentmeta."` WHERE meta_key LIKE '%akismet%';";
212 $akismetmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used
213 $this->found_akismet_count += $akismetmeta;
214
215 }
216
217 /**
218 * Return settings label
219 *
220 * @return string
221 */
222 public function settings_label() {
223 return __('Clean comment metadata', 'wp-optimize');
224 }
225
226 /**
227 * Return description
228 *
229 * @return string
230 */
231 public function get_auto_option_description() {
232 return __('Clean comment metadata', 'wp-optimize');
233 }
234 }
235