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

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

223 lines 6.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('WPO_VERSION')) 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 $sql = $this->wpdb->prepare(
41 "SELECT `ID`, `post_title`, `post_date`".
42 " FROM `" . $this->wpdb->posts . "`".
43 " WHERE post_status = 'trash'".
44 $retention_subquery.
45 " ORDER BY `ID` LIMIT %d, %d;",
46 array(
47 $params['offset'],
48 $params['limit'],
49 )
50 );
51
52 $posts = $this->wpdb->get_results($sql, ARRAY_A);
53
54 // fix empty revision titles.
55 if (!empty($posts)) {
56 foreach ($posts as $key => $post) {
57 $args = array(
58 'post_status' => 'trash',
59 'post_type' => 'post',
60 );
61 $posts[$key]['post_title'] = array(
62 'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'],
63 'url' => add_query_arg($args, 'edit.php'),
64 );
65 }
66 }
67
68 // get total count auto-draft for optimization.
69 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
70
71 if ('true' == $this->retention_enabled) {
72 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
73 }
74 $sql .= ';';
75
76 $total = $this->wpdb->get_var($sql);
77
78 return array(
79 'id_key' => 'ID',
80 'columns' => array(
81 'ID' => __('ID', 'wp-optimize'),
82 'post_title' => __('Title', 'wp-optimize'),
83 'post_date' => __('Date', 'wp-optimize'),
84 ),
85 'offset' => $params['offset'],
86 'limit' => $params['limit'],
87 'total' => $total,
88 'data' => $this->htmlentities_array($posts, array('ID')),
89 'message' => $total > 0 ? '' : __('No trashed posts found', 'wp-optimize'),
90 );
91 }
92
93 /**
94 * Do actions after optimize() function.
95 */
96 public function after_optimize() {
97 $message = sprintf(_n('%s post removed from Trash', '%s posts removed from Trash', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
98
99 if ($this->is_multisite_mode()) {
100 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
101 }
102
103 $this->logger->info($message);
104 $this->register_output($message);
105 }
106
107 /**
108 * Do optimization.
109 */
110 public function optimize() {
111
112 $remove_ids_sql = "SELECT ID FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
113
114 if ('true' == $this->retention_enabled) {
115 $remove_ids_sql .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
116 }
117
118 // get trashed post ids.
119 $post_remove_ids = $this->wpdb->get_col($remove_ids_sql);
120
121 // if optimize called from preview dialog then get posted ids.
122 if (isset($this->data['ids'])) {
123 $post_remove_ids = array_intersect($post_remove_ids, $this->data['ids']);
124 }
125
126 // remove related data for trashed posts.
127 if (!empty($post_remove_ids)) {
128 $post_remove_ids = join(',', $post_remove_ids);
129
130 // remove related postmeta.
131 $clean = "DELETE FROM `" . $this->wpdb->postmeta . "` WHERE post_id IN (" . $post_remove_ids . ");";
132 $this->wpdb->query($clean);
133
134 // remove related term relationships.
135 $clean = "DELETE FROM `" . $this->wpdb->term_relationships . "` WHERE object_id IN (" . $post_remove_ids . ");";
136 $this->wpdb->query($clean);
137
138 // remove related comments and commentmeta.
139 $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 . ");";
140 $this->wpdb->query($clean);
141 }
142
143 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
144
145 if ('true' == $this->retention_enabled) {
146 $clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
147 }
148
149 // if posted ids in params, then remove only selected items. used by preview widget.
150 if (isset($this->data['ids'])) {
151 $clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')';
152 }
153
154 $clean .= ';';
155
156 // remove trashed posts.
157 $posttrash = $this->query($clean);
158 $this->processed_count += $posttrash;
159
160 }
161
162 /**
163 * Do actions after get_info() function.
164 */
165 public function after_get_info() {
166 if ($this->found_count > 0) {
167 $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));
168 } else {
169 $message = __('No trashed posts found', 'wp-optimize');
170 }
171
172 if ($this->is_multisite_mode()) {
173 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
174 }
175
176 // add preview link for output.
177 if (0 != $this->found_count && null != $this->found_count) {
178 $message = $this->get_preview_link($message);
179 }
180
181 $this->register_output($message);
182 }
183
184 /**
185 * Return info about not optimized records
186 */
187 public function get_info() {
188
189 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
190 if ('true' == $this->retention_enabled) {
191 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
192 }
193 $sql .= ';';
194
195 $trash = $this->wpdb->get_var($sql);
196 $this->found_count += $trash;
197
198 }
199
200 /**
201 * Return settings label
202 *
203 * @return string|void
204 */
205 public function settings_label() {
206
207 if ('true' == $this->retention_enabled) {
208 return sprintf(__('Clean trashed posts which are older than %d weeks', 'wp-optimize'), $this->retention_period);
209 } else {
210 return __('Clean all trashed posts', 'wp-optimize');
211 }
212 }
213
214 /**
215 * Return description
216 *
217 * @return string|void
218 */
219 public function get_auto_option_description() {
220 return __('Remove trashed posts', 'wp-optimize');
221 }
222 }
223