| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_unapproved extends WP_Optimization { |
| 6 |
|
| 7 |
public $available_for_auto = true; |
| 8 |
|
| 9 |
public $auto_default = false; |
| 10 |
|
| 11 |
public $setting_default = true; |
| 12 |
|
| 13 |
public $available_for_saving = true; |
| 14 |
|
| 15 |
public $ui_sort_order = 4000; |
| 16 |
|
| 17 |
/** |
| 18 |
* Prepare data for preview widget. |
| 19 |
* |
| 20 |
* @param array $params |
| 21 |
* |
| 22 |
* @return array |
| 23 |
*/ |
| 24 |
public function preview($params) { |
| 25 |
|
| 26 |
$retention_subquery = ''; |
| 27 |
|
| 28 |
if ('true' == $this->retention_enabled) { |
| 29 |
$retention_subquery = ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 30 |
} |
| 31 |
|
| 32 |
// get data requested for preview. |
| 33 |
$sql = $this->wpdb->prepare(" |
| 34 |
SELECT |
| 35 |
c.comment_ID, |
| 36 |
c.comment_author, |
| 37 |
c.comment_content, |
| 38 |
p.post_title, |
| 39 |
p.ID, |
| 40 |
c.comment_date". |
| 41 |
" FROM " . $this->wpdb->comments . " c" . |
| 42 |
" INNER JOIN " . $this->wpdb->posts . " p" . |
| 43 |
" ON c.comment_post_ID = p.ID" . |
| 44 |
" WHERE c.comment_approved = '0'". |
| 45 |
$retention_subquery. |
| 46 |
" ORDER BY `comment_ID` LIMIT %d, %d", |
| 47 |
array( |
| 48 |
$params['offset'], |
| 49 |
$params['limit'], |
| 50 |
) |
| 51 |
); |
| 52 |
|
| 53 |
$comments = $this->wpdb->get_results($sql, ARRAY_A); |
| 54 |
|
| 55 |
// fix empty revision titles. |
| 56 |
if (!empty($comments)) { |
| 57 |
foreach ($comments as $key => $comment) { |
| 58 |
$comments[$key]['post_title'] = array( |
| 59 |
'text' => '' == $comment['post_title'] ? '('.__('no title', 'wp-optimize').')' : $comment['post_title'], |
| 60 |
'url' => get_edit_post_link($comment['ID'], ''), |
| 61 |
); |
| 62 |
$args = array( |
| 63 |
'action' => 'editcomment', |
| 64 |
'c' => $comment['comment_ID'], |
| 65 |
); |
| 66 |
$comments[$key]['comment_content'] = array( |
| 67 |
'text' => $comment['comment_content'], |
| 68 |
'url' => add_query_arg($args, 'comment.php'), |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// get total count comments for optimization. |
| 74 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0' ".$retention_subquery.";"; |
| 75 |
|
| 76 |
$total = $this->wpdb->get_var($sql); |
| 77 |
|
| 78 |
return array( |
| 79 |
'id_key' => 'comment_ID', |
| 80 |
'columns' => array( |
| 81 |
'comment_ID' => __('ID', 'wp-optimize'), |
| 82 |
'comment_author' => __('Author', 'wp-optimize'), |
| 83 |
'comment_content' => __('Comment', 'wp-optimize'), |
| 84 |
'post_title' => __('Post', 'wp-optimize'), |
| 85 |
'comment_date' => __('Date', 'wp-optimize'), |
| 86 |
), |
| 87 |
'offset' => $params['offset'], |
| 88 |
'limit' => $params['limit'], |
| 89 |
'total' => $total, |
| 90 |
'data' => $this->htmlentities_array($comments, array('comment_ID')), |
| 91 |
'message' => $total > 0 ? '' : __('No unapproved comments found', 'wp-optimize'), |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Do actions after optimize() function. |
| 97 |
*/ |
| 98 |
public function after_optimize() { |
| 99 |
$message = sprintf(_n('%s unapproved comment deleted', '%s unapproved comments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 100 |
|
| 101 |
if ($this->is_multisite_mode()) { |
| 102 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 103 |
} |
| 104 |
|
| 105 |
$this->logger->info($message); |
| 106 |
$this->register_output($message); |
| 107 |
|
| 108 |
$awaiting_mod = wp_count_comments(); |
| 109 |
$awaiting_mod = $awaiting_mod->moderated; |
| 110 |
|
| 111 |
$this->register_meta('awaiting_mod', $awaiting_mod); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Unapproved-comments / unapproved |
| 116 |
* need to check what IDs were previously used before (dom ID, settings ID), |
| 117 |
* as there was a note about unapproved-comments in the source, which isn't in use now here. |
| 118 |
*/ |
| 119 |
public function optimize() { |
| 120 |
$clean = "DELETE c, cm FROM `" . $this->wpdb->comments . "` c LEFT JOIN `" . $this->wpdb->commentmeta . "` cm ON c.comment_ID = cm.comment_id WHERE comment_approved = '0' "; |
| 121 |
|
| 122 |
if ('true' == $this->retention_enabled) { |
| 123 |
$clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 124 |
} |
| 125 |
|
| 126 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 127 |
if (isset($this->data['ids'])) { |
| 128 |
$clean .= ' AND c.comment_ID in ('.join(',', $this->data['ids']).')'; |
| 129 |
} |
| 130 |
|
| 131 |
$clean .= ';'; |
| 132 |
|
| 133 |
$comments = $this->query($clean); |
| 134 |
$this->processed_count += $comments; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Do actions after get_info() function. |
| 139 |
*/ |
| 140 |
public function after_get_info() { |
| 141 |
if ($this->found_count) { |
| 142 |
$message = sprintf(_n('%s unapproved comment found', '%s unapproved comments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 143 |
} else { |
| 144 |
$message = __('No unapproved comments found', 'wp-optimize'); |
| 145 |
} |
| 146 |
|
| 147 |
if ($this->is_multisite_mode()) { |
| 148 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 149 |
} |
| 150 |
|
| 151 |
// add preview link for output. |
| 152 |
if (0 != $this->found_count && null != $this->found_count) { |
| 153 |
$message = $this->get_preview_link($message); |
| 154 |
} |
| 155 |
|
| 156 |
$this->register_output($message); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get count of unoptimized items. |
| 161 |
*/ |
| 162 |
public function get_info() { |
| 163 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0'"; |
| 164 |
|
| 165 |
if ('true' == $this->retention_enabled) { |
| 166 |
$sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 167 |
} |
| 168 |
$sql .= ';'; |
| 169 |
|
| 170 |
$comments = $this->wpdb->get_var($sql); |
| 171 |
$this->found_count += $comments; |
| 172 |
} |
| 173 |
|
| 174 |
public function settings_label() { |
| 175 |
if ('true' == $this->retention_enabled) { |
| 176 |
return sprintf(__('Remove unapproved comments which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 177 |
} else { |
| 178 |
return __('Remove unapproved comments', 'wp-optimize'); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public function get_auto_option_description() { |
| 183 |
return __('Remove unapproved comments', 'wp-optimize'); |
| 184 |
} |
| 185 |
} |
| 186 |
|