| 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 |
* Do actions before optimize() function. |
| 33 |
*/ |
| 34 |
public function before_optimize() { |
| 35 |
$this->processed_spam_count = 0; |
| 36 |
$this->processed_trash_count = 0; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Do actions after optimize() function. |
| 41 |
*/ |
| 42 |
public function after_optimize() { |
| 43 |
$message = sprintf(_n('%s spam comment deleted', '%s spam comments deleted', $this->processed_spam_count, 'wp-optimize'), number_format_i18n($this->processed_spam_count)); |
| 44 |
$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)); |
| 45 |
|
| 46 |
|
| 47 |
if ($this->is_multisite_mode()) { |
| 48 |
$blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 49 |
|
| 50 |
$message .= ' '.$blogs_count_text; |
| 51 |
$message1 .= ' '.$blogs_count_text; |
| 52 |
} |
| 53 |
|
| 54 |
$this->logger->info($message); |
| 55 |
$this->logger->info($message1); |
| 56 |
|
| 57 |
$this->register_output($message); |
| 58 |
$this->register_output($message1); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Do optimization. |
| 63 |
*/ |
| 64 |
public function optimize() { |
| 65 |
// remove spam comments. |
| 66 |
|
| 67 |
$this->processed_spam_count += $this->get_count_comments('spam'); |
| 68 |
$this->delete_comments_by_type('spam'); |
| 69 |
|
| 70 |
$this->processed_trash_count += $this->get_count_comments('trash'); |
| 71 |
$this->delete_comments_by_type('trash'); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Delete comments by $type along with comments meta from database. |
| 76 |
* |
| 77 |
* @param string $type comment type. |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function delete_comments_by_type($type) { |
| 81 |
$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}'"; |
| 82 |
|
| 83 |
if ('true' == $this->retention_enabled) { |
| 84 |
$clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 85 |
} |
| 86 |
|
| 87 |
$clean .= ';'; |
| 88 |
return $this->query($clean); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Do actions before get_info() function. |
| 93 |
*/ |
| 94 |
public function before_get_info() { |
| 95 |
$this->found_spam_count = 0; |
| 96 |
$this->found_trash_count = 0; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Do actions after get_info() function. |
| 101 |
*/ |
| 102 |
public function after_get_info() { |
| 103 |
|
| 104 |
if ($this->found_spam_count > 0) { |
| 105 |
$message = sprintf(_n('%s spam comment found', '%s spam comments found', $this->found_spam_count, 'wp-optimize'), number_format_i18n($this->found_spam_count)).' | <a id="wp-optimize-edit-comments-spam" href="'.admin_url('edit-comments.php?comment_status=spam').'">'.' '.__('Review', 'wp-optimize').'</a>'; |
| 106 |
} else { |
| 107 |
$message = __('No spam comments found', 'wp-optimize'); |
| 108 |
} |
| 109 |
|
| 110 |
if ($this->found_trash_count > 0) { |
| 111 |
$message1 = sprintf(_n('%s trashed comment found', '%s trashed comments found', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)).' | <a id="wp-optimize-edit-comments-trash" href="'.admin_url('edit-comments.php?comment_status=trash').'">'.' '.__('Review', 'wp-optimize').'</a>'; |
| 112 |
} else { |
| 113 |
$message1 = __('No trashed comments found', 'wp-optimize'); |
| 114 |
} |
| 115 |
|
| 116 |
if ($this->is_multisite_mode()) { |
| 117 |
$blogs_count_text = sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 118 |
|
| 119 |
$message .= ' '.$blogs_count_text; |
| 120 |
$message1 .= ' '.$blogs_count_text; |
| 121 |
} |
| 122 |
|
| 123 |
$this->register_output($message); |
| 124 |
$this->register_output($message1); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Count records those can be optimized. |
| 129 |
*/ |
| 130 |
public function get_info() { |
| 131 |
$this->found_spam_count += $this->get_count_comments('spam'); |
| 132 |
$this->found_trash_count += $this->get_count_comments('trash'); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns count comments by $type. |
| 137 |
* |
| 138 |
* @param string $type comment type. |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function get_count_comments($type) { |
| 142 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '{$type}'"; |
| 143 |
if ('true' == $this->retention_enabled) { |
| 144 |
$sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 145 |
} |
| 146 |
$sql .= ';'; |
| 147 |
|
| 148 |
return $this->wpdb->get_var($sql); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Do actions after get_info() function. |
| 153 |
*/ |
| 154 |
public function settings_label() { |
| 155 |
|
| 156 |
if ('true' == $this->retention_enabled) { |
| 157 |
return sprintf(__('Remove spam and trashed comments which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 158 |
} else { |
| 159 |
return __('Remove spam and trashed comments', 'wp-optimize'); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function get_auto_option_description() { |
| 164 |
return __('Remove spam and trashed comments', 'wp-optimize'); |
| 165 |
} |
| 166 |
} |
| 167 |
|