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

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

245 lines 7.3 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_revisions extends WP_Optimization {
6
7 public $ui_sort_order = 1000;
8
9 public $available_for_auto = true;
10
11 public $auto_default = true;
12
13 public $setting_default = true;
14
15 public $available_for_saving = true;
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 post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
30 }
31
32 // get data requested for preview.
33 $sql = $this->wpdb->prepare(
34 "SELECT `ID`, `post_title`, `post_date`".
35 " FROM `" . $this->wpdb->posts . "`".
36 " WHERE post_type = 'revision'".
37 $retention_subquery.
38 " ORDER BY `ID` LIMIT %d, %d;",
39 array(
40 $params['offset'],
41 $params['limit'],
42 )
43 );
44
45 $posts = $this->wpdb->get_results($sql, ARRAY_A);
46
47 // fix empty revision titles.
48 if (!empty($posts)) {
49 foreach ($posts as $key => $post) {
50 $posts[$key]['post_title'] = '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'];
51 }
52 }
53
54 // get total count revisions for optimization.
55 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'".$retention_subquery.";";
56
57 $total = $this->wpdb->get_var($sql);
58
59 return array(
60 'id_key' => 'ID',
61 'columns' => array(
62 'ID' => __('ID', 'wp-optimize'),
63 'post_title' => __('Title', 'wp-optimize'),
64 'post_date' => __('Date', 'wp-optimize'),
65 ),
66 'offset' => $params['offset'],
67 'limit' => $params['limit'],
68 'total' => $total,
69 'data' => $this->htmlentities_array($posts, array('ID')),
70 'message' => $total > 0 ? '' : __('No post revisions found', 'wp-optimize'),
71 );
72 }
73
74 /**
75 * Do actions after optimize() function.
76 */
77 public function after_optimize() {
78 $message = sprintf(_n('%s post revision deleted', '%s post revisions deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
79
80 if ($this->is_multisite_mode()) {
81 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
82 }
83
84 $this->logger->info($message);
85 $this->register_output($message);
86 }
87
88 /**
89 * Do optimization.
90 */
91 public function optimize() {
92
93 if ('true' == $this->revisions_retention_enabled) {
94 $this->optimize_by_posts();
95 return;
96 }
97
98 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'";
99
100 if ('true' == $this->retention_enabled) {
101 $clean .= '
102 AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
103 }
104
105 // if posted ids in params, then remove only selected items. used by preview widget.
106 if (isset($this->data['ids'])) {
107 $clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')';
108 }
109
110 $clean .= ';';
111
112 $revisions = $this->query($clean);
113 $this->processed_count += $revisions;
114
115 // clean orphaned post meta.
116 $clean = "DELETE pm FROM `" . $this->wpdb->postmeta . "` pm LEFT JOIN `" . $this->wpdb->posts . "` p ON pm.post_id = p.ID WHERE p.ID IS NULL";
117 $this->query($clean);
118 }
119
120 /**
121 * Optimize post revisions but keep `x` revisions for each post
122 */
123 private function optimize_by_posts() {
124 // get data requested for preview.
125 $sql = "SELECT `post_parent`, GROUP_CONCAT(`ID`)".
126 " FROM `" . $this->wpdb->posts . "`".
127 " WHERE post_type = 'revision'".
128 " GROUP BY `post_parent`".
129 " ORDER BY `post_parent`";
130
131 $results = $this->wpdb->get_results($sql, ARRAY_N);
132 $post_parents = array();
133 $revisions = '';
134 foreach ($results as $row) {
135 array_push($post_parents, $row[0]);
136 $tmp = explode(',', $row[1]);
137 rsort($tmp);
138 $tmp = implode(',', array_slice($tmp, $this->revisions_retention_count));
139 if ('' !== $tmp) {
140 $revisions .= $tmp . ',';
141 }
142 }
143 $revisions = rtrim($revisions, ',');
144 $revisions = empty($revisions) ? array() : explode(',', $revisions);
145
146 while (count($revisions) > 0) {
147 $delete_this_time = array_splice($revisions, 0, min(count($revisions), 250));
148 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE `ID` IN (" . implode(',', $delete_this_time) . ")";
149 $count = $this->query($clean);
150 $this->processed_count += $count;
151 }
152
153 // clean orphaned post meta.
154 $clean = "DELETE pm FROM `" . $this->wpdb->postmeta . "` pm LEFT JOIN `" . $this->wpdb->posts . "` p ON pm.post_id = p.ID WHERE p.ID IS NULL";
155 $this->query($clean);
156 }
157
158 /**
159 * Do actions after get_info() function.
160 */
161 public function after_get_info() {
162 if ($this->found_count > 0) {
163 $message = sprintf(_n('%s post revision in your database', '%s post revisions in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
164 } else {
165 $message = __('No post revisions found', 'wp-optimize');
166 }
167
168 if ($this->is_multisite_mode()) {
169 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
170 }
171
172 // add preview link to message.
173 if ($this->found_count > 0) {
174 $message = $this->get_preview_link($message);
175 }
176
177 $this->register_output($message);
178 }
179
180 public function get_info() {
181 if ('true' == $this->revisions_retention_enabled) {
182 $sql = "SELECT `post_parent`, GROUP_CONCAT(`ID`)".
183 " FROM `" . $this->wpdb->posts . "`".
184 " WHERE post_type = 'revision'".
185 " GROUP BY `post_parent`".
186 " ORDER BY `post_parent`";
187
188 $results = $this->wpdb->get_results($sql, ARRAY_N);
189 $post_parents = array();
190 $revisions = '';
191 foreach ($results as $row) {
192 array_push($post_parents, $row[0]);
193 $tmp = explode(',', $row[1]);
194 rsort($tmp);
195 $tmp = implode(',', array_slice($tmp, $this->revisions_retention_count));
196 if (!empty($tmp)) {
197 $revisions .= $tmp . ',';
198 }
199 }
200 $revisions = rtrim($revisions, ',');
201 if (!empty($revisions)) {
202 $revisions = explode(',', $revisions);
203 $this->found_count += count($revisions);
204 }
205 } else {
206 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'";
207
208 if ('true' == $this->retention_enabled) {
209 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
210 }
211 $sql .= ';';
212
213 $revisions = $this->wpdb->get_var($sql);
214
215 $this->found_count += $revisions;
216 }
217 }
218
219 /**
220 * Returns appropriate label string based on option value
221 *
222 * @return string
223 */
224 public function settings_label() {
225
226 if ('true' == $this->retention_enabled && 'true' == $this->revisions_retention_enabled) {
227 return sprintf(__('Clean post revisions which are older than %d weeks and keep at least %d revisions', 'wp-optimize'), $this->retention_period, $this->revisions_retention_count);
228 }
229
230 if ('true' == $this->retention_enabled && 'false' == $this->revisions_retention_enabled) {
231 return sprintf(__('Clean post revisions which are older than %d weeks', 'wp-optimize'), $this->retention_period);
232 }
233
234 if ('false' == $this->retention_enabled && 'true' == $this->revisions_retention_enabled) {
235 return sprintf(__('Clean post revisions but keep at least %d revisions', 'wp-optimize'), $this->revisions_retention_count);
236 }
237
238 return __('Clean all post revisions', 'wp-optimize');
239 }
240
241 public function get_auto_option_description() {
242 return __('Clean all post revisions', 'wp-optimize');
243 }
244 }
245