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

139 lines 3.8 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 * Do actions after optimize() function.
26 */
27 public function after_optimize() {
28 $message = sprintf(_n('%s post removed from Trash', '%s posts removed from Trash', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
29
30 if ($this->is_multisite_mode()) {
31 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
32 }
33
34 $this->logger->info($message);
35 $this->register_output($message);
36 }
37
38 /**
39 * Do optimization.
40 */
41 public function optimize() {
42
43 $remove_ids_sql = "SELECT ID FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
44
45 if ('true' == $this->retention_enabled) {
46 $remove_ids_sql .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
47 }
48
49 // get trashed post ids.
50 $post_remove_ids = $this->wpdb->get_col($remove_ids_sql);
51
52 // remove related data for trashed posts.
53 if (!empty($post_remove_ids)) {
54 $post_remove_ids = join(',', $post_remove_ids);
55
56 // remove related postmeta.
57 $clean = "DELETE FROM `" . $this->wpdb->postmeta . "` WHERE post_id IN (" . $post_remove_ids . ");";
58 $this->wpdb->query($clean);
59
60 // remove related term relationships.
61 $clean = "DELETE FROM `" . $this->wpdb->term_relationships . "` WHERE object_id IN (" . $post_remove_ids . ");";
62 $this->wpdb->query($clean);
63
64 // remove related comments and commentmeta.
65 $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 . ");";
66 $this->wpdb->query($clean);
67 }
68
69 $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
70
71 if ('true' == $this->retention_enabled) {
72 $clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
73 }
74
75 $clean .= ';';
76
77 // remove trashed posts.
78 $posttrash = $this->query($clean);
79 $this->processed_count += $posttrash;
80
81 }
82
83 /**
84 * Do actions after get_info() function.
85 */
86 public function after_get_info() {
87 if ($this->found_count > 0) {
88 $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));
89 } else {
90 $message = __('No trashed posts found', 'wp-optimize');
91 }
92
93 if ($this->is_multisite_mode()) {
94 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
95 }
96
97 $this->register_output($message);
98 }
99
100 /**
101 * Return info about not optimized records
102 */
103 public function get_info() {
104
105 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'";
106 if ('true' == $this->retention_enabled) {
107 $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK';
108 }
109 $sql .= ';';
110
111 $trash = $this->wpdb->get_var($sql);
112 $this->found_count += $trash;
113
114 }
115
116 /**
117 * Return settings label
118 *
119 * @return string|void
120 */
121 public function settings_label() {
122
123 if ('true' == $this->retention_enabled) {
124 return sprintf(__('Clean trashed posts which are older than %d weeks', 'wp-optimize'), $this->retention_period);
125 } else {
126 return __('Clean all trashed posts', 'wp-optimize');
127 }
128 }
129
130 /**
131 * Return description
132 *
133 * @return string|void
134 */
135 public function get_auto_option_description() {
136 return __('Remove trashed posts', 'wp-optimize');
137 }
138 }
139