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

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

188 lines 5.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('ABSPATH')) die('No direct access allowed');
4
5 class WP_Optimization_autodraft extends WP_Optimization {
6
7 public $available_for_auto = true;
8
9 public $auto_default = true;
10
11 public $setting_default = true;
12
13 public $available_for_saving = true;
14
15 public $ui_sort_order = 3000;
16
17 protected $setting_id = 'drafts';
18
19 protected $auto_id = 'drafts';
20
21 /**
22 * Prepare data for preview widget.
23 *
24 * @param array $params
25 *
26 * @return array
27 */
28 public function preview($params) {
29
30 // get data requested for preview.
31
32 $retention_subquery = '';
33
34 if ('true' === $this->retention_enabled) {
35 $retention_subquery = ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
36 }
37
38 // Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb`
39 // phpcs:disable
40 $sql = $this->wpdb->prepare(
41 "SELECT `ID`, `post_title`, `post_date` FROM `" . $this->wpdb->posts . "`".
42 " WHERE post_status = 'auto-draft'".
43 $retention_subquery.
44 " ORDER BY `ID` LIMIT %d, %d;",
45 array(
46 $params['offset'],
47 $params['limit'],
48 )
49 );
50
51 $posts = $this->wpdb->get_results($sql, ARRAY_A);
52 // phpcs:enable
53
54 // fix empty revision titles.
55 if (!empty($posts)) {
56 foreach ($posts as $key => $post) {
57 $posts[$key]['post_title'] = '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'];
58 }
59 }
60
61 // get total count auto-draft for optimization.
62 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft' ".$retention_subquery.";";
63
64 $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL is safe, no user input used
65
66 return array(
67 'id_key' => 'ID',
68 'columns' => array(
69 'ID' => __('ID', 'wp-optimize'),
70 'post_title' => __('Title', 'wp-optimize'),
71 'post_date' => __('Date', 'wp-optimize'),
72 ),
73 'offset' => $params['offset'],
74 'limit' => $params['limit'],
75 'total' => $total,
76 'data' => $this->htmlentities_array($posts, array('ID')),
77 'message' => $total > 0 ? '' : __('No auto draft posts found', 'wp-optimize'),
78 );
79 }
80
81 /**
82 * Do actions after optimize() function.
83 */
84 public function after_optimize() {
85 // translators: %s is the number of auto-drafts deleted
86 $info_message = sprintf(_n('%s auto draft deleted', '%s auto drafts deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
87
88 if ($this->is_multisite_mode()) {
89 // translators: %s is the number of sites
90 $info_message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
91 }
92
93 $this->logger->info($info_message);
94 $this->register_output($info_message);
95
96 }
97
98 /**
99 * Do optimization.
100 */
101 public function optimize() {
102 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft'";
103
104 if ('true' === $this->retention_enabled) {
105 $clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
106 }
107
108 // if posted ids in params, then remove only selected items. used by preview widget.
109 if (isset($this->data['ids'])) {
110 $clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')';
111 }
112
113 $clean .= ';';
114
115 $this->processed_count += $this->query($clean);
116
117 // clean orphaned post meta.
118 $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";
119 $this->query($clean);
120
121 }
122
123 /**
124 * Do actions after get_info() function.
125 */
126 public function after_get_info() {
127
128 if (0 !== $this->found_count && null !== $this->found_count) {
129 // translators: %s is the number of auto-drafts
130 $message = sprintf(_n('%s auto draft post in your database', '%s auto draft posts in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
131 } else {
132 $message = __('No auto draft posts found', 'wp-optimize');
133 }
134
135 if ($this->is_multisite_mode()) {
136 // translators: %s is the number of sites
137 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
138 }
139
140 // add preview link for output.
141 if (0 !== $this->found_count && null !== $this->found_count) {
142 $message = $this->get_preview_link($message);
143 }
144
145 $this->register_output($message);
146
147 }
148
149 /**
150 * Get count of unoptimized items.
151 */
152 public function get_info() {
153 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft'";
154
155 if ('true' === $this->retention_enabled) {
156 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
157 }
158 $sql .= ';';
159
160 $this->found_count += $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL is safe, no user input used
161 }
162
163 /**
164 * Return settings label
165 *
166 * @return string
167 */
168 public function settings_label() {
169
170 if ('true' === $this->retention_enabled) {
171 // translators: %s is the retention period in weeks
172 return sprintf(__('Clean auto draft posts which are older than %s weeks', 'wp-optimize'), $this->retention_period);
173 } else {
174 return __('Clean all auto-draft posts', 'wp-optimize');
175 }
176
177 }
178
179 /**
180 * Return description
181 *
182 * @return string
183 */
184 public function get_auto_option_description() {
185 return __('Remove auto-draft posts', 'wp-optimize');
186 }
187 }
188