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

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

204 lines 6.0 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_unapproved extends WP_Optimization {
6
7 public $available_for_auto = true;
8
9 public $auto_default = false;
10
11 public $setting_default = true;
12
13 public $available_for_saving = true;
14
15 public $ui_sort_order = 4000;
16
17 /**
18 * Prepare data for preview widget.
19 *
20 * @param array $params
21 *
22 * @return array
23 */
24 public function preview($params) {
25
26 $retention_subquery = '';
27
28 if ('true' === $this->retention_enabled) {
29 $retention_subquery = ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
30 }
31
32 // get data requested for preview.
33 // `$this->wpdb->prepare` is global `$wpdb->prepare`
34 // phpcs:disable
35 $sql = $this->wpdb->prepare("
36 SELECT
37 c.comment_ID,
38 c.comment_author,
39 c.comment_content,
40 p.post_title,
41 p.ID,
42 c.comment_date".
43 " FROM " . $this->wpdb->comments . " c" .
44 " INNER JOIN " . $this->wpdb->posts . " p" .
45 " ON c.comment_post_ID = p.ID" .
46 " WHERE c.comment_approved = '0'".
47 $retention_subquery.
48 " ORDER BY `comment_ID` LIMIT %d, %d",
49 array(
50 $params['offset'],
51 $params['limit'],
52 )
53 );
54
55 $comments = $this->wpdb->get_results($sql, ARRAY_A);
56 // phpcs:enable
57
58 // fix empty revision titles.
59 if (!empty($comments)) {
60 foreach ($comments as $key => $comment) {
61 $comments[$key]['post_title'] = array(
62 'text' => '' === $comment['post_title'] ? '('.__('no title', 'wp-optimize').')' : $comment['post_title'],
63 'url' => get_edit_post_link($comment['ID'], ''),
64 );
65 $args = array(
66 'action' => 'editcomment',
67 'c' => $comment['comment_ID'],
68 );
69 $comments[$key]['comment_content'] = array(
70 'text' => $comment['comment_content'],
71 'url' => add_query_arg($args, 'comment.php'),
72 );
73 }
74 }
75
76 // get total count comments for optimization.
77 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0' ".$retention_subquery.";";
78
79 $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used
80
81 return array(
82 'id_key' => 'comment_ID',
83 'columns' => array(
84 'comment_ID' => __('ID', 'wp-optimize'),
85 'comment_author' => __('Author', 'wp-optimize'),
86 'comment_content' => __('Comment', 'wp-optimize'),
87 'post_title' => __('Post', 'wp-optimize'),
88 'comment_date' => __('Date', 'wp-optimize'),
89 ),
90 'offset' => $params['offset'],
91 'limit' => $params['limit'],
92 'total' => $total,
93 'data' => $this->htmlentities_array($comments, array('comment_ID')),
94 'message' => $total > 0 ? '' : __('No unapproved comments found', 'wp-optimize'),
95 );
96 }
97
98 /**
99 * Do actions after optimize() function.
100 */
101 public function after_optimize() {
102 // translators: %s is the number of unapproved comments deleted
103 $message = sprintf(_n('%s unapproved comment deleted', '%s unapproved comments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
104
105 if ($this->is_multisite_mode()) {
106 // translators: %s is the number of sites
107 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
108 }
109
110 $this->logger->info($message);
111 $this->register_output($message);
112
113 $awaiting_mod = wp_count_comments();
114 $awaiting_mod = $awaiting_mod->moderated;
115
116 $this->register_meta('awaiting_mod', $awaiting_mod);
117 }
118
119 /**
120 * Unapproved-comments / unapproved
121 * need to check what IDs were previously used before (dom ID, settings ID),
122 * as there was a note about unapproved-comments in the source, which isn't in use now here.
123 */
124 public function optimize() {
125 $clean = "DELETE c, cm FROM `" . $this->wpdb->comments . "` c LEFT JOIN `" . $this->wpdb->commentmeta . "` cm ON c.comment_ID = cm.comment_id WHERE comment_approved = '0' ";
126
127 if ('true' === $this->retention_enabled) {
128 $clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
129 }
130
131 // if posted ids in params, then remove only selected items. used by preview widget.
132 if (isset($this->data['ids'])) {
133 $clean .= ' AND c.comment_ID in ('.join(',', $this->data['ids']).')';
134 }
135
136 $clean .= ';';
137
138 $comments = $this->query($clean);
139 $this->processed_count += $comments;
140 }
141
142 /**
143 * Do actions after get_info() function.
144 */
145 public function after_get_info() {
146 if ($this->found_count) {
147 // translators: %s is the number of unapproved comments
148 $message = sprintf(_n('%s unapproved comment found', '%s unapproved comments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
149 } else {
150 $message = __('No unapproved comments found', 'wp-optimize');
151 }
152
153 if ($this->is_multisite_mode()) {
154 // translators: %s is the number of sites
155 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
156 }
157
158 // add preview link for output.
159 if ($this->found_count > 0) {
160 $message = $this->get_preview_link($message);
161 }
162
163 $this->register_output($message);
164 }
165
166 /**
167 * Get count of unoptimized items.
168 */
169 public function get_info() {
170 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0'";
171
172 if ('true' === $this->retention_enabled) {
173 $sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
174 }
175 $sql .= ';';
176
177 $comments = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used
178 $this->found_count += $comments;
179 }
180
181 /**
182 * Returns settings label
183 *
184 * @return string
185 */
186 public function settings_label() {
187 if ('true' == $this->retention_enabled) {
188 // translators: %d is the number of weeks
189 return sprintf(__('Remove unapproved comments which are older than %d weeks', 'wp-optimize'), $this->retention_period);
190 } else {
191 return __('Remove unapproved comments', 'wp-optimize');
192 }
193 }
194
195 /**
196 * Returns description
197 *
198 * @return string
199 */
200 public function get_auto_option_description() {
201 return __('Remove unapproved comments', 'wp-optimize');
202 }
203 }
204