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 / trash.php

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

234 lines 6.7 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 /**
6 * Class WP_Optimization_trash
7 */
8 class WP_Optimization_trash extends WP_Optimization {
9
10 public $available_for_auto = true;
11
12 public $auto_default = true;
13
14 public $setting_default = true;
15
16 public $available_for_saving = true;
17
18 public $ui_sort_order = 3010;
19
20 protected $setting_id = 'trash';
21
22 protected $auto_id = 'trash';
23
24 /**
25 * Prepare data for preview widget.
26 *
27 * @param array $params
28 *
29 * @return array
30 */
31 public function preview($params) {
32
33 $retention_subquery = '';
34
35 if ('true' === $this->retention_enabled) {
36 $retention_subquery = ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
37 }
38
39 // get data requested for preview.
40 // `$this->wpdb->prepare` is global `$wpdb->prepare`
41 // phpcs:disable
42 $sql = $this->wpdb->prepare(
43 "SELECT `ID`, `post_title`, `post_date`".
44 " FROM `" . $this->wpdb->posts . "`".
45 " WHERE post_status = 'trash'".
46 $retention_subquery.
47 " ORDER BY `ID` LIMIT %d, %d;",
48 array(
49 $params['offset'],
50 $params['limit'],
51 )
52 );
53
54 $posts = $this->wpdb->get_results($sql, ARRAY_A);
55 // phpcs:enable
56
57 // fix empty revision titles.
58 if (!empty($posts)) {
59 foreach ($posts as $key => $post) {
60 $args = array(
61 'post_status' => 'trash',
62 'post_type' => 'post',
63 );
64 $posts[$key]['post_title'] = array(
65 'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'],
66 'url' => add_query_arg($args, 'edit.php'),
67 );
68 }
69 }
70
71 // get total count auto-draft for optimization.
72 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
73
74 if ('true' === $this->retention_enabled) {
75 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
76 }
77 $sql .= ';';
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' => 'ID',
83 'columns' => array(
84 'ID' => __('ID', 'wp-optimize'),
85 'post_title' => __('Title', 'wp-optimize'),
86 'post_date' => __('Date', 'wp-optimize'),
87 ),
88 'offset' => $params['offset'],
89 'limit' => $params['limit'],
90 'total' => $total,
91 'data' => $this->htmlentities_array($posts, array('ID')),
92 'message' => $total > 0 ? '' : __('No trashed posts found', 'wp-optimize'),
93 );
94 }
95
96 /**
97 * Do actions after optimize() function.
98 */
99 public function after_optimize() {
100 // translators: %s is the number of posts removed from trash
101 $message = sprintf(_n('%s post removed from Trash', '%s posts removed from Trash', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
102
103 if ($this->is_multisite_mode()) {
104 // translators: %s is the number of sites
105 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
106 }
107
108 $this->logger->info($message);
109 $this->register_output($message);
110 }
111
112 /**
113 * Do optimization.
114 */
115 public function optimize() {
116 // `$this->wpdb` is global `$wpdb`
117 // phpcs:disable
118 $remove_ids_sql = "SELECT ID FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
119
120 if ('true' === $this->retention_enabled) {
121 $remove_ids_sql .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
122 }
123
124 // get trashed post ids.
125 $post_remove_ids = $this->wpdb->get_col($remove_ids_sql);
126
127 // if optimize called from preview dialog then get posted ids.
128 if (isset($this->data['ids'])) {
129 // Ensure ids are integers
130 $safe_ids = array_map('absint', $this->data['ids']);
131 $post_remove_ids = array_intersect($post_remove_ids, $safe_ids);
132 }
133
134 // remove related data for trashed posts.
135 if (!empty($post_remove_ids)) {
136 $post_remove_ids = join(',', $post_remove_ids);
137
138 // remove related postmeta.
139 $clean = "DELETE FROM `" . $this->wpdb->postmeta . "` WHERE post_id IN (" . $post_remove_ids . ");";
140 $this->wpdb->query($clean);
141
142 // remove related term relationships.
143 $clean = "DELETE FROM `" . $this->wpdb->term_relationships . "` WHERE object_id IN (" . $post_remove_ids . ");";
144 $this->wpdb->query($clean);
145
146 // remove related comments and commentmeta.
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_post_ID IN (" . $post_remove_ids . ");";
148 $this->wpdb->query($clean);
149 }
150
151 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
152
153 if ('true' === $this->retention_enabled) {
154 $clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
155 }
156
157 // if posted ids in params, then remove only selected items. used by preview widget.
158 if (isset($this->data['ids'])) {
159 $clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')';
160 }
161
162 $clean .= ';';
163
164 // remove trashed posts.
165 $posttrash = $this->query($clean);
166 $this->processed_count += $posttrash;
167 // phpcs:enable
168 }
169
170 /**
171 * Do actions after get_info() function.
172 */
173 public function after_get_info() {
174 if ($this->found_count > 0) {
175 // translators: %s is the number of trashed posts
176 $message = sprintf(_n('%s trashed post in your database', '%s trashed posts in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
177 } else {
178 $message = __('No trashed posts found', 'wp-optimize');
179 }
180
181 if ($this->is_multisite_mode()) {
182 // translators: %s is the number of sites
183 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
184 }
185
186 // add preview link for output.
187 if ($this->found_count > 0) {
188 $message = $this->get_preview_link($message);
189 }
190
191 $this->register_output($message);
192 }
193
194 /**
195 * Return info about not optimized records
196 */
197 public function get_info() {
198
199 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
200 if ('true' === $this->retention_enabled) {
201 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
202 }
203 $sql .= ';';
204
205 $trash = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used
206 $this->found_count += $trash;
207
208 }
209
210 /**
211 * Return settings label
212 *
213 * @return string
214 */
215 public function settings_label() {
216
217 if ('true' === $this->retention_enabled) {
218 // translators: %d is the number of weeks
219 return sprintf(__('Clean trashed posts which are older than %d weeks', 'wp-optimize'), $this->retention_period);
220 } else {
221 return __('Clean all trashed posts', 'wp-optimize');
222 }
223 }
224
225 /**
226 * Return description
227 *
228 * @return string
229 */
230 public function get_auto_option_description() {
231 return __('Remove trashed posts', 'wp-optimize');
232 }
233 }
234