PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
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 / spam.php

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

269 lines 8.1 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_spam extends WP_Optimization {
6
7 public $available_for_auto = true;
8
9 public $auto_default = true;
10
11 public $setting_default = true;
12
13 public $available_for_saving = true;
14
15 public $ui_sort_order = 3500;
16
17 protected $dom_id = 'clean-comments';
18
19 protected $setting_id = 'spams';
20
21 protected $auto_id = 'spams';
22
23 private $processed_spam_count;
24
25 private $processed_trash_count;
26
27 private $found_spam_count;
28
29 private $found_trash_count;
30
31 /**
32 * Prepare data for preview widget.
33 *
34 * @param array $params
35 *
36 * @return array
37 */
38 public function preview($params) {
39
40 // get clicked comment type link.
41 $type = isset($params['type']) && 'spam' === $params['type'] ? 'spam' : 'trash';
42
43 $retention_subquery = '';
44
45 if ('true' === $this->retention_enabled) {
46 $retention_subquery = ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
47 }
48
49 // get data requested for preview.
50 // `$this->wpdb->prepare` is global `$wpdb->prepare`
51 // phpcs:disable
52 $sql = $this->wpdb->prepare(
53 "SELECT comment_ID, comment_author, SUBSTR(comment_content, 1, 128) AS comment_content FROM".
54 " `" . $this->wpdb->comments . "`".
55 " WHERE comment_approved = '{$type}'".
56 $retention_subquery.
57 " ORDER BY `comment_ID` LIMIT %d, %d;",
58 array(
59 $params['offset'],
60 $params['limit'],
61 )
62 );
63
64 $comments = $this->wpdb->get_results($sql, ARRAY_A);
65 // phpcs:enable
66
67 // fix empty revision titles.
68 if (!empty($comments)) {
69 foreach ($comments as $key => $comment) {
70 $args = array(
71 'comment_status' => $type,
72 );
73 $comments[$key]['comment_content'] = array(
74 'text' => $comment['comment_content'],
75 'url' => add_query_arg($args, 'edit-comments.php'),
76 );
77 }
78 }
79
80 // get total count comments for optimization.
81 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '{$type}' ".$retention_subquery.";";
82
83 $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe no user input used
84
85 return array(
86 'id_key' => 'comment_ID',
87 'columns' => array(
88 'comment_ID' => __('ID', 'wp-optimize'),
89 'comment_author' => __('Author', 'wp-optimize'),
90 'comment_content' => __('Comment', 'wp-optimize'),
91 ),
92 'offset' => $params['offset'],
93 'limit' => $params['limit'],
94 'total' => $total,
95 'data' => $this->htmlentities_array($comments, array('comment_ID')),
96 'message' => $total > 0 ? '' : __('No spam or trashed comments found', 'wp-optimize'),
97 );
98 }
99
100 /**
101 * Do actions before optimize() function.
102 */
103 public function before_optimize() {
104 $this->processed_spam_count = 0;
105 $this->processed_trash_count = 0;
106 }
107
108 /**
109 * Do actions after optimize() function.
110 */
111 public function after_optimize() {
112 // translators: %s: number of spam comments deleted
113 $message = sprintf(_n('%s spam comment deleted', '%s spam comments deleted', $this->processed_spam_count, 'wp-optimize'), number_format_i18n($this->processed_spam_count));
114 // translators: %s: number of comments removed from Trash
115 $message1 = sprintf(_n('%s comment removed from Trash', '%s comments removed from Trash', $this->processed_trash_count, 'wp-optimize'), number_format_i18n($this->processed_trash_count));
116
117
118 if ($this->is_multisite_mode()) {
119 // translators: %s: number of sites
120 $blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
121
122 $message .= ' '.$blogs_count_text;
123 $message1 .= ' '.$blogs_count_text;
124 }
125
126 $this->logger->info($message);
127 $this->logger->info($message1);
128
129 $this->register_output($message);
130 $this->register_output($message1);
131 }
132
133 /**
134 * Do optimization.
135 */
136 public function optimize() {
137 // remove spam comments.
138
139 $this->processed_spam_count += $this->get_count_comments('spam');
140 $this->delete_comments_by_type('spam');
141
142 $this->processed_trash_count += $this->get_count_comments('trash');
143 $this->delete_comments_by_type('trash');
144 }
145
146 /**
147 * Delete comments by $type along with comments meta from database.
148 *
149 * @param string $type comment type.
150 * @return mixed
151 */
152 public function delete_comments_by_type($type) {
153 $clean = "DELETE c, cm FROM `" . $this->wpdb->comments . "` c LEFT JOIN `" . $this->wpdb->commentmeta . "` cm ON c.comment_ID = cm.comment_id WHERE c.comment_approved = '{$type}'";
154
155 if ('true' === $this->retention_enabled) {
156 $clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
157 }
158
159 // if posted ids in params, then remove only selected items. used by preview widget.
160 if (isset($this->data['ids'])) {
161 $clean .= ' AND c.comment_ID in ('.join(',', $this->data['ids']).')';
162 }
163
164 $clean .= ';';
165 return $this->query($clean);
166 }
167
168 /**
169 * Do actions before get_info() function.
170 */
171 public function before_get_info() {
172 $this->found_spam_count = 0;
173 $this->found_trash_count = 0;
174 }
175
176 /**
177 * Do actions after get_info() function.
178 */
179 public function after_get_info() {
180
181 if ($this->found_spam_count > 0) {
182 // translators: %s: number of spam comments
183 $message = sprintf(_n('%s spam comment found', '%s spam comments found', $this->found_spam_count, 'wp-optimize'), number_format_i18n($this->found_spam_count));
184
185 // if current version is not premium and Preview feature not supported then
186 // add to message Review link to comments page
187 if (!WP_Optimize::is_premium()) {
188 $message .= ' | <a id="wp-optimize-edit-comments-spam" href="'.admin_url('edit-comments.php?comment_status=spam').'">'.' '.__('Review', 'wp-optimize').'</a>';
189 }
190
191 } else {
192 $message = __('No spam comments found', 'wp-optimize');
193 }
194
195 if ($this->found_trash_count > 0) {
196 // translators: %s is the number of trashed comments
197 $message1 = sprintf(_n('%s trashed comment found', '%s trashed comments found', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count));
198
199 // if current version is not premium and Preview feature not supported then
200 // add to message Review link to comments page
201 if (!WP_Optimize::is_premium()) {
202 $message1 .= ' | <a id="wp-optimize-edit-comments-trash" href="'.admin_url('edit-comments.php?comment_status=trash').'">'.' '.__('Review', 'wp-optimize').'</a>';
203 }
204 } else {
205 $message1 = __('No trashed comments found', 'wp-optimize');
206 }
207
208 if ($this->is_multisite_mode()) {
209 // translators: %s is the number of sites
210 $blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
211
212 $message .= ' '.$blogs_count_text;
213 $message1 .= ' '.$blogs_count_text;
214 }
215
216 // add preview link to message.
217 if ($this->found_spam_count > 0) {
218 $message = $this->get_preview_link($message, array('data-type' => 'spam'));
219 }
220
221 // add preview link to message.
222 if ($this->found_trash_count > 0) {
223 $message1 = $this->get_preview_link($message1, array('data-type' => 'trash'));
224 }
225
226 $this->register_output($message);
227 $this->register_output($message1);
228 }
229
230 /**
231 * Count records those can be optimized.
232 */
233 public function get_info() {
234 $this->found_spam_count += $this->get_count_comments('spam');
235 $this->found_trash_count += $this->get_count_comments('trash');
236 }
237
238 /**
239 * Returns count comments by $type.
240 *
241 * @param string $type comment type.
242 * @return mixed
243 */
244 public function get_count_comments($type) {
245 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '{$type}'";
246 if ('true' === $this->retention_enabled) {
247 $sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
248 }
249 $sql .= ';';
250
251 return $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe
252 }
253
254 /**
255 * Do actions after get_info() function.
256 *
257 * @return string
258 */
259 public function settings_label() {
260
261 if ('true' === $this->retention_enabled) {
262 // translators: %s is the number of weeks
263 return sprintf(__('Remove spam and trashed comments which are older than %d weeks', 'wp-optimize'), $this->retention_period);
264 } else {
265 return __('Remove spam and trashed comments', 'wp-optimize');
266 }
267 }
268 }
269