| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) 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_auto = true; |
| 10 |
|
| 11 |
public $available_for_saving = true; |
| 12 |
|
| 13 |
private $processed_trash_count; |
| 14 |
|
| 15 |
private $processed_akismet_count; |
| 16 |
|
| 17 |
private $found_trash_count; |
| 18 |
|
| 19 |
private $found_akismet_count; |
| 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 clicked comment type link. |
| 31 |
$type = isset($params['type']) && 'akismet' === $params['type'] ? 'akismet' : 'trash'; |
| 32 |
|
| 33 |
// get data requested for preview. |
| 34 |
// Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb` |
| 35 |
// phpcs:disable |
| 36 |
$sql = $this->wpdb->prepare( |
| 37 |
"SELECT * FROM `" . $this->wpdb->commentmeta . "`". |
| 38 |
" WHERE ". |
| 39 |
('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). |
| 40 |
('akismet' === $type ? "meta_key LIKE" : ""). |
| 41 |
" %s ORDER BY `comment_ID` LIMIT %d, %d;", |
| 42 |
array( |
| 43 |
('akismet' === $type ? '%akismet%' : ''), |
| 44 |
$params['offset'], |
| 45 |
$params['limit'], |
| 46 |
) |
| 47 |
); |
| 48 |
|
| 49 |
$posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 50 |
// phpcs:enable |
| 51 |
|
| 52 |
// fix empty revision titles. |
| 53 |
if (!empty($posts)) { |
| 54 |
foreach ($posts as $key => $post) { |
| 55 |
$posts[$key]['post_title'] = array( |
| 56 |
'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], |
| 57 |
'url' => get_edit_post_link($post['ID']), |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// get total count comments for optimization. |
| 63 |
// Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb` |
| 64 |
// phpcs:disable |
| 65 |
$sql = $this->wpdb->prepare( |
| 66 |
"SELECT COUNT(*) FROM `" . $this->wpdb->comments . "`". |
| 67 |
"WHERE ". |
| 68 |
('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). |
| 69 |
('akismet' === $type ? "meta_key LIKE " : ""). |
| 70 |
" %s;", |
| 71 |
array( |
| 72 |
('akismet' === $type ? '%akismet%' : ''), |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
$total = $this->wpdb->get_var($sql); |
| 77 |
// phpcs:enable |
| 78 |
|
| 79 |
return array( |
| 80 |
'id_key' => 'meta_id', |
| 81 |
'columns' => array( |
| 82 |
'meta_id' => __('ID', 'wp-optimize'), |
| 83 |
'comment_id' => __('Comment ID', 'wp-optimize'), |
| 84 |
'meta_key' => __('Meta Key', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This is not a query |
| 85 |
'meta_value' => __('Meta Value', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- This is not a query |
| 86 |
), |
| 87 |
'offset' => $params['offset'], |
| 88 |
'limit' => $params['limit'], |
| 89 |
'total' => $total, |
| 90 |
'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 91 |
'message' => $total > 0 ? '' : __('No orphaned comment metadata in your database', 'wp-optimize'), |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Do actions before optimize() function. |
| 97 |
*/ |
| 98 |
public function before_optimize() { |
| 99 |
$this->processed_trash_count = 0; |
| 100 |
$this->processed_akismet_count = 0; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Do actions after optimize() function. |
| 105 |
*/ |
| 106 |
public function after_optimize() { |
| 107 |
// translators: %s is a number of unused comment metadata item removed |
| 108 |
$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)); |
| 109 |
// translators: %s is a number of unused akismet comment metadata item removed |
| 110 |
$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)); |
| 111 |
|
| 112 |
if ($this->is_multisite_mode()) { |
| 113 |
// translators: %s is a number of sites |
| 114 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 115 |
$message .= $blogs_count_text; |
| 116 |
$message1 .= $blogs_count_text; |
| 117 |
} |
| 118 |
|
| 119 |
$this->logger->info($message); |
| 120 |
$this->register_output($message); |
| 121 |
|
| 122 |
$this->logger->info($message1); |
| 123 |
$this->register_output($message1); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* This needs reviewing when we review the whole cron-run set of options. |
| 128 |
*/ |
| 129 |
public function optimize() { |
| 130 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)"; |
| 131 |
|
| 132 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 133 |
if (isset($this->data['ids'])) { |
| 134 |
$clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')'; |
| 135 |
} |
| 136 |
|
| 137 |
$clean .= ";"; |
| 138 |
|
| 139 |
$commentstrash_meta = $this->query($clean); |
| 140 |
$this->processed_trash_count += $commentstrash_meta; |
| 141 |
|
| 142 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE meta_key LIKE '%akismet%'"; |
| 143 |
|
| 144 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 145 |
if (isset($this->data['ids'])) { |
| 146 |
$clean .= ' AND meta_id in ('.join(',', $this->data['ids']).')'; |
| 147 |
} |
| 148 |
|
| 149 |
$clean .= ";"; |
| 150 |
|
| 151 |
$commentstrash_meta2 = $this->query($clean); |
| 152 |
$this->processed_akismet_count += $commentstrash_meta2; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Do actions before get_info(). |
| 157 |
*/ |
| 158 |
public function before_get_info() { |
| 159 |
|
| 160 |
$this->found_trash_count = 0; |
| 161 |
$this->found_akismet_count = 0; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Do actions after get_info() function. |
| 167 |
*/ |
| 168 |
public function after_get_info() { |
| 169 |
|
| 170 |
if ($this->found_trash_count > 0) { |
| 171 |
// translators: %s is a number of orphaned comment metadata |
| 172 |
$message = sprintf(_n('%s orphaned comment metadata in your database', '%s orphaned comment metadata in your database', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)); |
| 173 |
} else { |
| 174 |
$message = __('No orphaned comment metadata in your database', 'wp-optimize'); |
| 175 |
} |
| 176 |
|
| 177 |
if ($this->found_akismet_count > 0) { |
| 178 |
// translators: %s is a number of unused Akismet comment metadata |
| 179 |
$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)); |
| 180 |
} else { |
| 181 |
$message1 = __('No Akismet comment meta rows in your database', 'wp-optimize'); |
| 182 |
} |
| 183 |
|
| 184 |
if ($this->is_multisite_mode()) { |
| 185 |
// translators: %s is a number of sites |
| 186 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 187 |
$message .= $blogs_count_text; |
| 188 |
$message1 .= $blogs_count_text; |
| 189 |
} |
| 190 |
|
| 191 |
if ($this->found_trash_count > 0) { |
| 192 |
$message = $this->get_preview_link($message, array('data-type' => 'trash')); |
| 193 |
} |
| 194 |
|
| 195 |
if ($this->found_akismet_count > 0) { |
| 196 |
$message1 = $this->get_preview_link($message1, array('data-type' => 'akismet')); |
| 197 |
} |
| 198 |
|
| 199 |
$this->register_output($message); |
| 200 |
$this->register_output($message1); |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get count of unoptimized items. |
| 206 |
*/ |
| 207 |
public function get_info() { |
| 208 |
|
| 209 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);"; |
| 210 |
$commentmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used |
| 211 |
$this->found_trash_count += $commentmeta; |
| 212 |
|
| 213 |
$sql = "SELECT COUNT(*) FROM `".$this->wpdb->commentmeta."` WHERE meta_key LIKE '%akismet%';"; |
| 214 |
$akismetmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used |
| 215 |
$this->found_akismet_count += $akismetmeta; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Return settings label |
| 221 |
* |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
public function settings_label() { |
| 225 |
return __('Clean comment metadata', 'wp-optimize'); |
| 226 |
} |
| 227 |
} |
| 228 |
|