PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.19
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.19
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 3.8.0 All 110 releases
wp-optimize / optimizations / spam.php

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

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