| 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 comment_ID, comment_author, comment_content". |
| 35 |
" FROM `" . $this->wpdb->comments . "`". |
| 36 |
" WHERE comment_approved = '0'". |
| 37 |
$retention_subquery. |
| 38 |
" ORDER BY `comment_ID` LIMIT %d, %d", |
| 39 |
array( |
| 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 = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0' ".$retention_subquery.";"; |
| 59 |
|
| 60 |
$total = $this->wpdb->get_var($sql); |
| 61 |
|
| 62 |
return array( |
| 63 |
'id_key' => 'comment_ID', |
| 64 |
'columns' => array( |
| 65 |
'comment_ID' => __('ID', 'wp-optimize'), |
| 66 |
'comment_author' => __('Author', 'wp-optimize'), |
| 67 |
'comment_content' => __('Date', 'wp-optimize'), |
| 68 |
), |
| 69 |
'offset' => $params['offset'], |
| 70 |
'limit' => $params['limit'], |
| 71 |
'total' => $total, |
| 72 |
'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 73 |
'message' => $total > 0 ? '' : __('No unapproved comments found', 'wp-optimize'), |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Do actions after optimize() function. |
| 79 |
*/ |
| 80 |
public function after_optimize() { |
| 81 |
$message = sprintf(_n('%s unapproved comment deleted', '%s unapproved comments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 82 |
|
| 83 |
if ($this->is_multisite_mode()) { |
| 84 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 85 |
} |
| 86 |
|
| 87 |
$this->logger->info($message); |
| 88 |
$this->register_output($message); |
| 89 |
|
| 90 |
$awaiting_mod = wp_count_comments(); |
| 91 |
$awaiting_mod = $awaiting_mod->moderated; |
| 92 |
|
| 93 |
$this->register_meta('awaiting_mod', $awaiting_mod); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Unapproved-comments / unapproved |
| 98 |
* need to check what IDs were previously used before (dom ID, settings ID), |
| 99 |
* as there was a note about unapproved-comments in the source, which isn't in use now here. |
| 100 |
*/ |
| 101 |
public function optimize() { |
| 102 |
$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' "; |
| 103 |
|
| 104 |
if ('true' == $this->retention_enabled) { |
| 105 |
$clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 106 |
} |
| 107 |
|
| 108 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 109 |
if (isset($this->data['ids'])) { |
| 110 |
$clean .= ' AND c.comment_ID in ('.join(',', $this->data['ids']).')'; |
| 111 |
} |
| 112 |
|
| 113 |
$clean .= ';'; |
| 114 |
|
| 115 |
$comments = $this->query($clean); |
| 116 |
$this->processed_count += $comments; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Do actions after get_info() function. |
| 121 |
*/ |
| 122 |
public function after_get_info() { |
| 123 |
if ($this->found_count) { |
| 124 |
$message = sprintf(_n('%s unapproved comment found', '%s unapproved comments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 125 |
} else { |
| 126 |
$message = __('No unapproved comments found', 'wp-optimize'); |
| 127 |
} |
| 128 |
|
| 129 |
if ($this->is_multisite_mode()) { |
| 130 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 131 |
} |
| 132 |
|
| 133 |
// add preview link for output. |
| 134 |
if (0 != $this->found_count && null != $this->found_count) { |
| 135 |
$message = $this->get_preview_link($message); |
| 136 |
} |
| 137 |
|
| 138 |
$this->register_output($message); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get count of unoptimized items. |
| 143 |
*/ |
| 144 |
public function get_info() { |
| 145 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0'"; |
| 146 |
|
| 147 |
if ('true' == $this->retention_enabled) { |
| 148 |
$sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 149 |
} |
| 150 |
$sql .= ';'; |
| 151 |
|
| 152 |
$comments = $this->wpdb->get_var($sql); |
| 153 |
$this->found_count += $comments; |
| 154 |
} |
| 155 |
|
| 156 |
public function settings_label() { |
| 157 |
if ('true' == $this->retention_enabled) { |
| 158 |
return sprintf(__('Remove unapproved comments which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 159 |
} else { |
| 160 |
return __('Remove unapproved comments', 'wp-optimize'); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public function get_auto_option_description() { |
| 165 |
return __('Remove unapproved comments', 'wp-optimize'); |
| 166 |
} |
| 167 |
} |
| 168 |
|