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