| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) 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 |
$sql = $this->wpdb->prepare( |
| 39 |
"SELECT `ID`, `post_title`, `post_date` FROM `" . $this->wpdb->posts . "`". |
| 40 |
" WHERE post_status = 'auto-draft'". |
| 41 |
$retention_subquery. |
| 42 |
" ORDER BY `ID` LIMIT %d, %d;", |
| 43 |
array( |
| 44 |
$params['offset'], |
| 45 |
$params['limit'], |
| 46 |
) |
| 47 |
); |
| 48 |
|
| 49 |
$posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 50 |
|
| 51 |
// fix empty revision titles. |
| 52 |
if (!empty($posts)) { |
| 53 |
foreach ($posts as $key => $post) { |
| 54 |
$posts[$key]['post_title'] = '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title']; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// get total count auto-draft for optimization. |
| 59 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft' ".$retention_subquery.";"; |
| 60 |
|
| 61 |
$total = $this->wpdb->get_var($sql); |
| 62 |
|
| 63 |
return array( |
| 64 |
'id_key' => 'ID', |
| 65 |
'columns' => array( |
| 66 |
'ID' => __('ID', 'wp-optimize'), |
| 67 |
'post_title' => __('Title', 'wp-optimize'), |
| 68 |
'post_date' => __('Date', 'wp-optimize'), |
| 69 |
), |
| 70 |
'offset' => $params['offset'], |
| 71 |
'limit' => $params['limit'], |
| 72 |
'total' => $total, |
| 73 |
'data' => $this->htmlentities_array($posts, array('ID')), |
| 74 |
'message' => $total > 0 ? '' : __('No auto draft posts found', 'wp-optimize'), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Do actions after optimize() function. |
| 80 |
*/ |
| 81 |
public function after_optimize() { |
| 82 |
$info_message = sprintf(_n('%s auto draft deleted', '%s auto drafts deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 83 |
|
| 84 |
if ($this->is_multisite_mode()) { |
| 85 |
$info_message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 86 |
} |
| 87 |
|
| 88 |
$this->logger->info($info_message); |
| 89 |
$this->register_output($info_message); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Do optimization. |
| 95 |
*/ |
| 96 |
public function optimize() { |
| 97 |
$clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft'"; |
| 98 |
|
| 99 |
if ('true' == $this->retention_enabled) { |
| 100 |
$clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 101 |
} |
| 102 |
|
| 103 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 104 |
if (isset($this->data['ids'])) { |
| 105 |
$clean .= ' AND `ID` in ('.join(',', $this->data['ids']).')'; |
| 106 |
} |
| 107 |
|
| 108 |
$clean .= ';'; |
| 109 |
|
| 110 |
$this->processed_count += $this->query($clean); |
| 111 |
|
| 112 |
// clean orphaned post meta. |
| 113 |
$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"; |
| 114 |
$this->query($clean); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Do actions after get_info() function. |
| 120 |
*/ |
| 121 |
public function after_get_info() { |
| 122 |
|
| 123 |
if (0 != $this->found_count && null != $this->found_count) { |
| 124 |
$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)); |
| 125 |
} else { |
| 126 |
$message = __('No auto draft posts found', 'wp-optimize'); |
| 127 |
} |
| 128 |
|
| 129 |
if ($this->is_multisite_mode()) { |
| 130 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 131 |
} |
| 132 |
|
| 133 |
// add preview link for output. |
| 134 |
if (0 != $this->found_count && null != $this->found_count) { |
| 135 |
$message = $this->get_preview_link($message); |
| 136 |
} |
| 137 |
|
| 138 |
$this->register_output($message); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get count of unoptimized items. |
| 144 |
*/ |
| 145 |
public function get_info() { |
| 146 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'auto-draft'"; |
| 147 |
|
| 148 |
if ('true' == $this->retention_enabled) { |
| 149 |
$sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 150 |
} |
| 151 |
$sql .= ';'; |
| 152 |
|
| 153 |
$this->found_count += $this->wpdb->get_var($sql); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return settings label |
| 158 |
* |
| 159 |
* @return string|void |
| 160 |
*/ |
| 161 |
public function settings_label() { |
| 162 |
|
| 163 |
if ('true' == $this->retention_enabled) { |
| 164 |
return sprintf(__('Clean auto draft posts which are older than %s weeks', 'wp-optimize'), $this->retention_period); |
| 165 |
} else { |
| 166 |
return __('Clean all auto-draft posts', 'wp-optimize'); |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Return description |
| 173 |
* |
| 174 |
* @return string|void |
| 175 |
*/ |
| 176 |
public function get_auto_option_description() { |
| 177 |
return __('Remove auto-draft posts', 'wp-optimize'); |
| 178 |
} |
| 179 |
} |
| 180 |
|