PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.11
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.11
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 2.2.11, at optimizations/revisions.php

162 lines 4.5 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 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'";
93
94 if ('true' == $this->retention_enabled) {
95 $clean .= '
96 AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
97 }
98
99 // if posted ids in params, then remove only selected items. used by preview widget.
100 if (isset($this->data['ids'])) {
101 $clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')';
102 }
103
104 $clean .= ';';
105
106 $revisions = $this->query($clean);
107 $this->processed_count += $revisions;
108
109 // clean orphaned post meta.
110 $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";
111 $this->query($clean);
112 }
113
114 /**
115 * Do actions after get_info() function.
116 */
117 public function after_get_info() {
118 if ($this->found_count > 0) {
119 $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));
120 } else {
121 $message = __('No post revisions found', 'wp-optimize');
122 }
123
124 if ($this->is_multisite_mode()) {
125 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
126 }
127
128 // add preview link to message.
129 if ($this->found_count > 0) {
130 $message = $this->get_preview_link($message);
131 }
132
133 $this->register_output($message);
134 }
135
136 public function get_info() {
137 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'";
138
139 if ('true' == $this->retention_enabled) {
140 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
141 }
142 $sql .= ';';
143
144 $revisions = $this->wpdb->get_var($sql);
145
146 $this->found_count += $revisions;
147 }
148
149 public function settings_label() {
150
151 if ('true' == $this->retention_enabled) {
152 return sprintf(__('Clean post revisions which are older than %d weeks', 'wp-optimize'), $this->retention_period);
153 } else {
154 return __('Clean all post revisions', 'wp-optimize');
155 }
156 }
157
158 public function get_auto_option_description() {
159 return __('Remove auto revisions', 'wp-optimize');
160 }
161 }
162