| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_spam 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 = 3500; |
| 16 |
|
| 17 |
protected $dom_id = 'clean-comments'; |
| 18 |
|
| 19 |
protected $setting_id = 'spams'; |
| 20 |
|
| 21 |
protected $auto_id = 'spams'; |
| 22 |
|
| 23 |
private $processed_spam_count; |
| 24 |
|
| 25 |
private $processed_trash_count; |
| 26 |
|
| 27 |
private $found_spam_count; |
| 28 |
|
| 29 |
private $found_trash_count; |
| 30 |
|
| 31 |
/** |
| 32 |
* Prepare data for preview widget. |
| 33 |
* |
| 34 |
* @param array $params |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function preview($params) { |
| 39 |
|
| 40 |
// get clicked comment type link. |
| 41 |
$type = isset($params['type']) && 'spam' == $params['type'] ? 'spam' : 'trash'; |
| 42 |
|
| 43 |
$retention_subquery = ''; |
| 44 |
|
| 45 |
if ('true' == $this->retention_enabled) { |
| 46 |
$retention_subquery = ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 47 |
} |
| 48 |
|
| 49 |
// get data requested for preview. |
| 50 |
$sql = $this->wpdb->prepare( |
| 51 |
"SELECT comment_ID, comment_author, SUBSTR(comment_content, 1, 128) AS comment_content FROM". |
| 52 |
" `" . $this->wpdb->comments . "`". |
| 53 |
" WHERE comment_approved = '{$type}'". |
| 54 |
$retention_subquery. |
| 55 |
" ORDER BY `comment_ID` LIMIT %d, %d;", |
| 56 |
array( |
| 57 |
$params['offset'], |
| 58 |
$params['limit'], |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
$posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 63 |
|
| 64 |
// fix empty revision titles. |
| 65 |
if (!empty($posts)) { |
| 66 |
foreach ($posts as $key => $post) { |
| 67 |
$posts[$key]['post_title'] = array( |
| 68 |
'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], |
| 69 |
'url' => get_edit_post_link($post['ID']), |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// get total count comments for optimization. |
| 75 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '{$type}' ".$retention_subquery.";"; |
| 76 |
|
| 77 |
$total = $this->wpdb->get_var($sql); |
| 78 |
|
| 79 |
return array( |
| 80 |
'id_key' => 'comment_ID', |
| 81 |
'columns' => array( |
| 82 |
'comment_ID' => __('ID', 'wp-optimize'), |
| 83 |
'comment_author' => __('Author', 'wp-optimize'), |
| 84 |
'comment_content' => __('Comment', 'wp-optimize'), |
| 85 |
), |
| 86 |
'offset' => $params['offset'], |
| 87 |
'limit' => $params['limit'], |
| 88 |
'total' => $total, |
| 89 |
'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 90 |
'message' => $total > 0 ? '' : __('No spam or trashed comments found', 'wp-optimize'), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Do actions before optimize() function. |
| 96 |
*/ |
| 97 |
public function before_optimize() { |
| 98 |
$this->processed_spam_count = 0; |
| 99 |
$this->processed_trash_count = 0; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Do actions after optimize() function. |
| 104 |
*/ |
| 105 |
public function after_optimize() { |
| 106 |
$message = sprintf(_n('%s spam comment deleted', '%s spam comments deleted', $this->processed_spam_count, 'wp-optimize'), number_format_i18n($this->processed_spam_count)); |
| 107 |
$message1 = sprintf(_n('%s comment removed from Trash', '%s comments removed from Trash', $this->processed_trash_count, 'wp-optimize'), number_format_i18n($this->processed_trash_count)); |
| 108 |
|
| 109 |
|
| 110 |
if ($this->is_multisite_mode()) { |
| 111 |
$blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 112 |
|
| 113 |
$message .= ' '.$blogs_count_text; |
| 114 |
$message1 .= ' '.$blogs_count_text; |
| 115 |
} |
| 116 |
|
| 117 |
$this->logger->info($message); |
| 118 |
$this->logger->info($message1); |
| 119 |
|
| 120 |
$this->register_output($message); |
| 121 |
$this->register_output($message1); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Do optimization. |
| 126 |
*/ |
| 127 |
public function optimize() { |
| 128 |
// remove spam comments. |
| 129 |
|
| 130 |
$this->processed_spam_count += $this->get_count_comments('spam'); |
| 131 |
$this->delete_comments_by_type('spam'); |
| 132 |
|
| 133 |
$this->processed_trash_count += $this->get_count_comments('trash'); |
| 134 |
$this->delete_comments_by_type('trash'); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Delete comments by $type along with comments meta from database. |
| 139 |
* |
| 140 |
* @param string $type comment type. |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function delete_comments_by_type($type) { |
| 144 |
$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_approved = '{$type}'"; |
| 145 |
|
| 146 |
if ('true' == $this->retention_enabled) { |
| 147 |
$clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 148 |
} |
| 149 |
|
| 150 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 151 |
if (isset($this->data['ids'])) { |
| 152 |
$clean .= ' AND c.comment_ID in ('.join(',', $this->data['ids']).')'; |
| 153 |
} |
| 154 |
|
| 155 |
$clean .= ';'; |
| 156 |
return $this->query($clean); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Do actions before get_info() function. |
| 161 |
*/ |
| 162 |
public function before_get_info() { |
| 163 |
$this->found_spam_count = 0; |
| 164 |
$this->found_trash_count = 0; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Do actions after get_info() function. |
| 169 |
*/ |
| 170 |
public function after_get_info() { |
| 171 |
|
| 172 |
if ($this->found_spam_count > 0) { |
| 173 |
$message = sprintf(_n('%s spam comment found', '%s spam comments found', $this->found_spam_count, 'wp-optimize'), number_format_i18n($this->found_spam_count)); |
| 174 |
|
| 175 |
// if current version is not premium and Preview feature not supported then |
| 176 |
// add to message Review link to comments page |
| 177 |
if (!WP_Optimize::is_premium()) { |
| 178 |
$message .= ' | <a id="wp-optimize-edit-comments-spam" href="'.admin_url('edit-comments.php?comment_status=spam').'">'.' '.__('Review', 'wp-optimize').'</a>'; |
| 179 |
} |
| 180 |
|
| 181 |
} else { |
| 182 |
$message = __('No spam comments found', 'wp-optimize'); |
| 183 |
} |
| 184 |
|
| 185 |
if ($this->found_trash_count > 0) { |
| 186 |
$message1 = sprintf(_n('%s trashed comment found', '%s trashed comments found', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)); |
| 187 |
|
| 188 |
// if current version is not premium and Preview feature not supported then |
| 189 |
// add to message Review link to comments page |
| 190 |
if (!WP_Optimize::is_premium()) { |
| 191 |
$message .= ' | <a id="wp-optimize-edit-comments-trash" href="'.admin_url('edit-comments.php?comment_status=trash').'">'.' '.__('Review', 'wp-optimize').'</a>'; |
| 192 |
} |
| 193 |
} else { |
| 194 |
$message1 = __('No trashed comments found', 'wp-optimize'); |
| 195 |
} |
| 196 |
|
| 197 |
if ($this->is_multisite_mode()) { |
| 198 |
$blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 199 |
|
| 200 |
$message .= ' '.$blogs_count_text; |
| 201 |
$message1 .= ' '.$blogs_count_text; |
| 202 |
} |
| 203 |
|
| 204 |
// add preview link to message. |
| 205 |
if ($this->found_spam_count > 0) { |
| 206 |
$message = $this->get_preview_link($message, array('data-type' => 'spam')); |
| 207 |
} |
| 208 |
|
| 209 |
// add preview link to message. |
| 210 |
if ($this->found_trash_count > 0) { |
| 211 |
$message = $this->get_preview_link($message1, array('data-type' => 'trash')); |
| 212 |
} |
| 213 |
|
| 214 |
$this->register_output($message); |
| 215 |
$this->register_output($message1); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Count records those can be optimized. |
| 220 |
*/ |
| 221 |
public function get_info() { |
| 222 |
$this->found_spam_count += $this->get_count_comments('spam'); |
| 223 |
$this->found_trash_count += $this->get_count_comments('trash'); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Returns count comments by $type. |
| 228 |
* |
| 229 |
* @param string $type comment type. |
| 230 |
* @return mixed |
| 231 |
*/ |
| 232 |
public function get_count_comments($type) { |
| 233 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '{$type}'"; |
| 234 |
if ('true' == $this->retention_enabled) { |
| 235 |
$sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 236 |
} |
| 237 |
$sql .= ';'; |
| 238 |
|
| 239 |
return $this->wpdb->get_var($sql); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Do actions after get_info() function. |
| 244 |
*/ |
| 245 |
public function settings_label() { |
| 246 |
|
| 247 |
if ('true' == $this->retention_enabled) { |
| 248 |
return sprintf(__('Remove spam and trashed comments which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 249 |
} else { |
| 250 |
return __('Remove spam and trashed comments', 'wp-optimize'); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public function get_auto_option_description() { |
| 255 |
return __('Remove spam and trashed comments', 'wp-optimize'); |
| 256 |
} |
| 257 |
} |
| 258 |
|