| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_commentmeta extends WP_Optimization { |
| 6 |
|
| 7 |
public $ui_sort_order = 9000; |
| 8 |
|
| 9 |
public $available_for_saving = true; |
| 10 |
|
| 11 |
private $processed_trash_count; |
| 12 |
|
| 13 |
private $processed_akismet_count; |
| 14 |
|
| 15 |
private $found_trash_count; |
| 16 |
|
| 17 |
private $found_akismet_count; |
| 18 |
|
| 19 |
/** |
| 20 |
* Prepare data for preview widget. |
| 21 |
* |
| 22 |
* @param array $params |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function preview($params) { |
| 27 |
|
| 28 |
// get clicked comment type link. |
| 29 |
$type = isset($params['type']) && 'akismet' == $params['type'] ? 'akismet' : 'trash'; |
| 30 |
|
| 31 |
// get data requested for preview. |
| 32 |
$sql = $this->wpdb->prepare( |
| 33 |
"SELECT * FROM `" . $this->wpdb->commentmeta . "`". |
| 34 |
" WHERE ". |
| 35 |
('trash' == $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). |
| 36 |
('akismet' == $type ? "meta_key LIKE" : ""). |
| 37 |
" %s ORDER BY `comment_ID` LIMIT %d, %d;", |
| 38 |
array( |
| 39 |
('akismet' == $type ? '%akismet%' : ''), |
| 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'] = array( |
| 51 |
'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], |
| 52 |
'url' => get_edit_post_link($post['ID']), |
| 53 |
); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// get total count comments for optimization. |
| 58 |
$sql = $this->wpdb->prepare( |
| 59 |
"SELECT COUNT(*) FROM `" . $this->wpdb->comments . "`". |
| 60 |
"WHERE ". |
| 61 |
('trash' == $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). |
| 62 |
('akismet' == $type ? "meta_key LIKE " : ""). |
| 63 |
" %s;", |
| 64 |
array( |
| 65 |
('akismet' == $type ? '%akismet%' : ''), |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
$total = $this->wpdb->get_var($sql); |
| 70 |
|
| 71 |
return array( |
| 72 |
'id_key' => 'meta_id', |
| 73 |
'columns' => array( |
| 74 |
'meta_id' => __('ID', 'wp-optimize'), |
| 75 |
'comment_id' => __('Comment ID', 'wp-optimize'), |
| 76 |
'meta_key' => __('Meta Key', 'wp-optimize'), |
| 77 |
'meta_value' => __('Meta Value', 'wp-optimize'), |
| 78 |
), |
| 79 |
'offset' => $params['offset'], |
| 80 |
'limit' => $params['limit'], |
| 81 |
'total' => $total, |
| 82 |
'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 83 |
'message' => $total > 0 ? '' : __('No orphaned comment meta data in your database', 'wp-optimize'), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Do actions before optimize() function. |
| 89 |
*/ |
| 90 |
public function before_optimize() { |
| 91 |
$this->processed_trash_count = 0; |
| 92 |
$this->processed_akismet_count = 0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Do actions after optimize() function. |
| 97 |
*/ |
| 98 |
public function after_optimize() { |
| 99 |
$message = sprintf(_n('%s unused comment metadata item removed', '%s unused comment metadata items removed', $this->processed_trash_count, 'wp-optimize'), number_format_i18n($this->processed_trash_count)); |
| 100 |
$message1 = sprintf(_n('%s unused akismet comment metadata item removed', '%s unused akismet comment metadata items removed', $this->processed_akismet_count, 'wp-optimize'), number_format_i18n($this->processed_akismet_count)); |
| 101 |
|
| 102 |
if ($this->is_multisite_mode()) { |
| 103 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 104 |
$message .= $blogs_count_text; |
| 105 |
$message1 .= $blogs_count_text; |
| 106 |
} |
| 107 |
|
| 108 |
$this->logger->info($message); |
| 109 |
$this->register_output($message); |
| 110 |
|
| 111 |
$this->logger->info($message1); |
| 112 |
$this->register_output($message1); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* This needs reviewing when we review the whole cron-run set of options. |
| 117 |
*/ |
| 118 |
public function optimize() { |
| 119 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)"; |
| 120 |
|
| 121 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 122 |
if (isset($this->data['ids'])) { |
| 123 |
$clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')'; |
| 124 |
} |
| 125 |
|
| 126 |
$clean .= ";"; |
| 127 |
|
| 128 |
$commentstrash_meta = $this->query($clean); |
| 129 |
$this->processed_trash_count += $commentstrash_meta; |
| 130 |
|
| 131 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE meta_key LIKE '%akismet%'"; |
| 132 |
|
| 133 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 134 |
if (isset($this->data['ids'])) { |
| 135 |
$clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')'; |
| 136 |
} |
| 137 |
|
| 138 |
$clean .= ";"; |
| 139 |
|
| 140 |
$commentstrash_meta2 = $this->query($clean); |
| 141 |
$this->processed_akismet_count += $commentstrash_meta2; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Do actions before get_info(). |
| 146 |
*/ |
| 147 |
public function before_get_info() { |
| 148 |
|
| 149 |
$this->found_trash_count = 0; |
| 150 |
$this->found_akismet_count = 0; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Do actions after get_info() function. |
| 156 |
*/ |
| 157 |
public function after_get_info() { |
| 158 |
|
| 159 |
if ($this->found_trash_count > 0) { |
| 160 |
$message = sprintf(_n('%s orphaned comment meta data in your database', '%s orphaned comment meta data in your database', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)); |
| 161 |
} else { |
| 162 |
$message = __('No orphaned comment meta data in your database', 'wp-optimize'); |
| 163 |
} |
| 164 |
|
| 165 |
if ($this->found_akismet_count > 0) { |
| 166 |
$message1 = sprintf(_n('%s unused Akismet comment meta rows in your database', '%s unused Akismet meta rows in your database', $this->found_akismet_count, 'wp-optimize'), number_format_i18n($this->found_akismet_count)); |
| 167 |
} else { |
| 168 |
$message1 = __('No Akismet comment meta rows in your database', 'wp-optimize'); |
| 169 |
} |
| 170 |
|
| 171 |
if ($this->is_multisite_mode()) { |
| 172 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 173 |
$message .= $blogs_count_text; |
| 174 |
$message1 .= $blogs_count_text; |
| 175 |
} |
| 176 |
|
| 177 |
if ($this->found_trash_count > 0) { |
| 178 |
$message = $this->get_preview_link($message, array('data-type' => 'trash')); |
| 179 |
} |
| 180 |
|
| 181 |
if ($this->found_akismet_count > 0) { |
| 182 |
$message1 = $this->get_preview_link($message1, array('data-type' => 'akismet')); |
| 183 |
} |
| 184 |
|
| 185 |
$this->register_output($message); |
| 186 |
$this->register_output($message1); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Get count of unoptimized items. |
| 192 |
*/ |
| 193 |
public function get_info() { |
| 194 |
|
| 195 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);"; |
| 196 |
$commentmeta = $this->wpdb->get_var($sql); |
| 197 |
$this->found_trash_count += $commentmeta; |
| 198 |
|
| 199 |
$sql = "SELECT COUNT(*) FROM `".$this->wpdb->commentmeta."` WHERE meta_key LIKE '%akismet%';"; |
| 200 |
$akismetmeta = $this->wpdb->get_var($sql); |
| 201 |
$this->found_akismet_count += $akismetmeta; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
public function settings_label() { |
| 206 |
return __('Clean comment meta data', 'wp-optimize'); |
| 207 |
} |
| 208 |
|
| 209 |
public function get_auto_option_description() { |
| 210 |
return __('Clean comment meta data', 'wp-optimize'); |
| 211 |
} |
| 212 |
} |
| 213 |
|