| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The page Settings. |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined('ABSPATH') ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
class WbcrCmp_DeleteCommentsPage extends Wbcr_FactoryPages401_ImpressiveThemplate { |
| 16 |
|
| 17 |
/** |
| 18 |
* The id of the page in the admin menu. |
| 19 |
* |
| 20 |
* Mainly used to navigate between pages. |
| 21 |
* @see FactoryPages401_AdminPage |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $id = "delete_comments"; |
| 27 |
public $type = "page"; |
| 28 |
public $page_parent_page = "comments"; |
| 29 |
public $page_menu_dashicon = 'dashicons-testimonial'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param Wbcr_Factory400_Plugin $plugin |
| 33 |
*/ |
| 34 |
public function __construct(Wbcr_Factory400_Plugin $plugin) |
| 35 |
{ |
| 36 |
$this->menu_title = __('Comments cleaner', 'comments-plus'); |
| 37 |
|
| 38 |
parent::__construct($plugin); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* We register notifications for some actions |
| 43 |
* |
| 44 |
* @see libs\factory\pages\themplates\FactoryPages401_ImpressiveThemplate |
| 45 |
* @param $notices |
| 46 |
* @param Wbcr_Factory400_Plugin $plugin |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public function getActionNotices($notices) |
| 50 |
{ |
| 51 |
|
| 52 |
$notices[] = array( |
| 53 |
'conditions' => array( |
| 54 |
'wbcr_cmp_clear_comments' => 1 |
| 55 |
), |
| 56 |
'type' => 'success', |
| 57 |
'message' => __('All comments have been deleted.', 'comments-plus') |
| 58 |
); |
| 59 |
|
| 60 |
$notices[] = array( |
| 61 |
'conditions' => array( |
| 62 |
'wbcr_cmp_clear_comments_error' => 1, |
| 63 |
'wbcr_cmp_code' => 'interal_error' |
| 64 |
), |
| 65 |
'type' => 'danger', |
| 66 |
'message' => __('An error occurred while trying to delete comments. Internal error occured. Please try again later.', 'comments-plus') |
| 67 |
); |
| 68 |
|
| 69 |
return $notices; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Prints the content of the page |
| 74 |
* |
| 75 |
* @see libs\factory\pages\themplates\FactoryPages401_ImpressiveThemplate |
| 76 |
*/ |
| 77 |
public function showPageContent() |
| 78 |
{ |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$stat_data = $wpdb->get_results("SELECT count(*) as total_comments, |
| 82 |
SUM(comment_type='order_note') as order_notes_count, |
| 83 |
SUM(comment_approved='spam') as spamcount, |
| 84 |
SUM(comment_approved='0') as unpcount, |
| 85 |
SUM(comment_approved='trash') as trashcount |
| 86 |
FROM {$wpdb->prefix}comments"); |
| 87 |
|
| 88 |
$stat_data_by_post_type = $wpdb->get_results("SELECT |
| 89 |
SUM(comment_count) as type_comments_count, post_type |
| 90 |
FROM $wpdb->posts |
| 91 |
GROUP BY post_type"); |
| 92 |
|
| 93 |
$types = get_post_types(array('public' => true), 'objects'); |
| 94 |
|
| 95 |
$post_types = array(); |
| 96 |
foreach((array)$types as $type_name => $type) { |
| 97 |
$comments_count = 0; |
| 98 |
if( !empty($stat_data_by_post_type) ) { |
| 99 |
foreach((array)$stat_data_by_post_type as $post_type_stat_value) { |
| 100 |
if( $post_type_stat_value->post_type == $type_name ) { |
| 101 |
$comments_count = $post_type_stat_value->type_comments_count; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$post_types[$type_name] = array('label' => $type->label, 'comments_count' => $comments_count); |
| 107 |
} |
| 108 |
|
| 109 |
?> |
| 110 |
<script> |
| 111 |
/** |
| 112 |
* Select all types by one click. |
| 113 |
*/ |
| 114 |
jQuery(document).ready(function($) { |
| 115 |
var allTypesCheckbox = $('#wbcr-cmp-all-types-checkbox'); |
| 116 |
allTypesCheckbox.click(function() { |
| 117 |
$('.wbcr-cmp-post-type-checkbox').prop("checked", $(this).prop("checked")); |
| 118 |
}); |
| 119 |
|
| 120 |
$('.wbcr-cmp-post-type-checkbox').click(function() { |
| 121 |
if( !$(this).prop("checked") ) { |
| 122 |
allTypesCheckbox.prop("checked", false); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
$('.wbcr-cmp-delete-comments-button').click(function() { |
| 127 |
var confrimDelete = confirm('<?php _e('Are you sure you want to delete comments from the database without restoring?', 'comments-plus'); ?>'); |
| 128 |
|
| 129 |
if( !confrimDelete ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
$(this).submit(); |
| 134 |
}); |
| 135 |
}); |
| 136 |
</script> |
| 137 |
|
| 138 |
<div class="wbcr-factory-page-group-header" style="margin-top:0;"> |
| 139 |
<strong><?php _e('Comments clearing tools', 'comments-plus') ?></strong> |
| 140 |
|
| 141 |
<p> |
| 142 |
<?php _e('These functions can be useful for global disabling comments or bulk cleaning spam comments.', 'comments-plus') ?> |
| 143 |
</p> |
| 144 |
</div> |
| 145 |
|
| 146 |
<form method="post" action="<?= $this->getActionUrl('delete-all-comments') ?>" style="padding: 20px;"> |
| 147 |
<h5><?php _e('Remove all comments', 'comments-plus'); ?></h5> |
| 148 |
|
| 149 |
<p><?php _e('You can delete all comments in your database with one click.', 'comments-plus'); ?></p> |
| 150 |
|
| 151 |
<p><strong><?php _e('Choose post types', 'comments-plus'); ?></strong> |
| 152 |
|
| 153 |
<div style="height:150px; width:400px; padding:10px 10px 0; background: #fff; border:1px solid #ccc; overflow-y: scroll; overflow-x:hidden;"> |
| 154 |
<p> |
| 155 |
<label> |
| 156 |
<input type="checkbox" id="wbcr-cmp-all-types-checkbox" name="wbcr_cmp_post_type[]" value="all" checked/> <?php _e('Select all', 'comments-plus'); ?> |
| 157 |
</label> |
| 158 |
</p> |
| 159 |
<?php foreach((array)$post_types as $key => $type): ?> |
| 160 |
<p> |
| 161 |
<label> |
| 162 |
<input type="checkbox" class="wbcr-cmp-post-type-checkbox" name="wbcr_cmp_post_type[]" value="<?= esc_attr($key) ?>" checked/> <?= $type['label'] ?> |
| 163 |
(<?= $type['comments_count'] ?>) |
| 164 |
</label> |
| 165 |
</p> |
| 166 |
<?php endforeach; ?> |
| 167 |
</div> |
| 168 |
|
| 169 |
<?php if( class_exists('WooCommerce') ): ?> |
| 170 |
<p style="margin:15px 0 0"> |
| 171 |
<label> |
| 172 |
<input type="checkbox" name="wbcr_cmp_delete_order_notes" value="1"/> <?php printf(__('Delete Woocommerce order notices? (%d)', 'comments-plus'), $stat_data[0]->order_notes_count); ?> |
| 173 |
</label> |
| 174 |
</p> |
| 175 |
<?php endif; ?> |
| 176 |
<p style="margin-top:15px;"> |
| 177 |
<input type="submit" name="wbcr_cmp_delete_all" class="button button-default wbcr-cmp-delete-comments-button" value="<?php printf(__('Delete (%d)', 'comments-plus'), $stat_data[0]->total_comments); ?>"> |
| 178 |
</p> |
| 179 |
<?php wp_nonce_field($this->getResultId() . '_delete_all_comments') ?> |
| 180 |
</form> |
| 181 |
|
| 182 |
<div style="padding: 20px;"> |
| 183 |
<hr/> |
| 184 |
<h5><?php _e('Remove spam comments', 'comments-plus'); ?></h5> |
| 185 |
|
| 186 |
<p><?php _e('You can remove only spam comments from the database with one click.', 'comments-plus'); ?></p> |
| 187 |
<a href="<?= wp_nonce_url($this->getActionUrl('delete-spam-comments'), $this->getResultId() . '_delete_spam_comments') ?>" class="button button-default wbcr-cmp-delete-comments-button"> |
| 188 |
<?php printf(__('Delete (%d)', 'comments-plus'), $stat_data[0]->spamcount); ?> |
| 189 |
</a> |
| 190 |
<hr/> |
| 191 |
<h5><?php _e('Remove unapproved comments', 'comments-plus'); ?></h5> |
| 192 |
|
| 193 |
<p><?php _e('You can remove only unapproved comments from the database with one click.', 'comments-plus'); ?></p> |
| 194 |
<a href="<?= wp_nonce_url($this->getActionUrl('delete-unaproved-comments'), $this->getResultId() . '_delete_unaproved_comments') ?>" class="button button-default wbcr-cmp-delete-comments-button"> |
| 195 |
<?php printf(__('Delete (%d)', 'comments-plus'), $stat_data[0]->unpcount); ?> |
| 196 |
</a> |
| 197 |
<hr/> |
| 198 |
<h5><?php _e('Remove trashed comments', 'comments-plus'); ?></h5> |
| 199 |
|
| 200 |
<p><?php _e('You can remove only trashed comments from the database with one click.', 'comments-plus'); ?></p> |
| 201 |
<a href="<?= wp_nonce_url($this->getActionUrl('delete-trash-comments'), $this->getResultId() . '_delete_trash_comments') ?>" class="button button-default wbcr-cmp-delete-comments-button"> |
| 202 |
<?php printf(__('Delete (%d)', 'comments-plus'), $stat_data[0]->trashcount); ?> |
| 203 |
</a> |
| 204 |
</div> |
| 205 |
<?php |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* This action deletes all comments from the database without restoring. |
| 210 |
*/ |
| 211 |
public function deleteAllCommentsAction() |
| 212 |
{ |
| 213 |
global $wpdb; |
| 214 |
check_admin_referer($this->getResultId() . '_delete_all_comments'); |
| 215 |
|
| 216 |
if( isset($_POST['wbcr_cmp_delete_all']) ) { |
| 217 |
$post_types = $this->request->post('wbcr_cmp_post_type', array(), true); |
| 218 |
$delete_order_notes = $this->request->post('wbcr_cmp_delete_order_notes', false, 'intval'); |
| 219 |
|
| 220 |
if( empty($post_types) || in_array('all', $post_types) ) { |
| 221 |
if( $wpdb->query("TRUNCATE $wpdb->commentmeta") != false ) { |
| 222 |
$delete_all_sql = "TRUNCATE $wpdb->comments"; |
| 223 |
|
| 224 |
if( class_exists('WooCommerce') ) { |
| 225 |
if( !$delete_order_notes ) { |
| 226 |
$delete_all_sql = "DELETE FROM $wpdb->comments WHERE comment_type != 'order_note'"; |
| 227 |
} |
| 228 |
} |
| 229 |
if( $wpdb->query($delete_all_sql) != false ) { |
| 230 |
$wpdb->query("UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0"); |
| 231 |
$wpdb->query("OPTIMIZE TABLE $wpdb->commentmeta"); |
| 232 |
$wpdb->query("OPTIMIZE TABLE $wpdb->comments"); |
| 233 |
|
| 234 |
$this->redirectToAction('index', array( |
| 235 |
'wbcr_cmp_clear_comments' => '1' |
| 236 |
)); |
| 237 |
} else { |
| 238 |
$this->redirectToAction('index', array( |
| 239 |
'wbcr_cmp_clear_comments_error' => '1', |
| 240 |
'wbcr_cmp_code' => 'interal_error', |
| 241 |
)); |
| 242 |
} |
| 243 |
} else { |
| 244 |
$this->redirectToAction('index', array( |
| 245 |
'wbcr_cmp_clear_comments_error' => '1', |
| 246 |
'wbcr_cmp_code' => 'interal_error', |
| 247 |
)); |
| 248 |
} |
| 249 |
} else { |
| 250 |
|
| 251 |
// Loop through post_types and remove comments/meta and set posts comment_count to 0 |
| 252 |
foreach($post_types as $delete_post_type) { |
| 253 |
$wpdb->query("DELETE cmeta FROM $wpdb->commentmeta cmeta INNER JOIN $wpdb->comments comments ON cmeta.comment_id=comments.comment_ID INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'"); |
| 254 |
|
| 255 |
$delete_certain_sql = "DELETE comments FROM $wpdb->comments comments INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'"; |
| 256 |
|
| 257 |
if( class_exists('WooCommerce') ) { |
| 258 |
if( !$delete_order_notes ) { |
| 259 |
$delete_certain_sql .= " and comment_type != 'order_note'"; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
$wpdb->query($delete_certain_sql); |
| 264 |
$wpdb->query("UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0 AND post_type = '$delete_post_type'"); |
| 265 |
} |
| 266 |
|
| 267 |
$wpdb->query("OPTIMIZE TABLE $wpdb->commentmeta"); |
| 268 |
$wpdb->query("OPTIMIZE TABLE $wpdb->comments"); |
| 269 |
|
| 270 |
$this->redirectToAction('index', array( |
| 271 |
'wbcr_cmp_clear_comments' => '1' |
| 272 |
)); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$this->redirectToAction('index'); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* The basic function of deleting comments. |
| 281 |
* |
| 282 |
* @param int|string $type |
| 283 |
*/ |
| 284 |
public function deleteComments($type = 0) |
| 285 |
{ |
| 286 |
global $wpdb; |
| 287 |
|
| 288 |
if( in_array($type, array('spam', 'trash', 0)) ) { |
| 289 |
$wpdb->query("DELETE cmeta |
| 290 |
FROM $wpdb->commentmeta cmeta |
| 291 |
INNER JOIN $wpdb->comments comments ON cmeta.comment_id=comments.comment_ID |
| 292 |
WHERE comment_approved='{$type}'"); |
| 293 |
|
| 294 |
if( $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved='{$type}'") ) { |
| 295 |
$wpdb->query("OPTIMIZE TABLE $wpdb->comments"); |
| 296 |
$wpdb->query("OPTIMIZE TABLE $wpdb->commentmeta"); |
| 297 |
|
| 298 |
$this->redirectToAction('index', array( |
| 299 |
'wbcr_cmp_clear_comments' => '1' |
| 300 |
)); |
| 301 |
} else { |
| 302 |
$this->redirectToAction('index', array( |
| 303 |
'wbcr_cmp_clear_comments_error' => '1', |
| 304 |
'wbcr_cmp_code' => 'interal_error', |
| 305 |
)); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* This action deletes spam comments |
| 312 |
*/ |
| 313 |
public function deleteSpamCommentsAction() |
| 314 |
{ |
| 315 |
check_admin_referer($this->getResultId() . '_delete_spam_comments'); |
| 316 |
|
| 317 |
$this->deleteComments('spam'); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* This action deletes unaproved comments |
| 322 |
*/ |
| 323 |
public function deleteUnaprovedCommentsAction() |
| 324 |
{ |
| 325 |
check_admin_referer($this->getResultId() . '_delete_unaproved_comments'); |
| 326 |
|
| 327 |
$this->deleteComments(); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* This action deletes trash comments |
| 332 |
*/ |
| 333 |
public function deleteTrashCommentsAction() |
| 334 |
{ |
| 335 |
check_admin_referer($this->getResultId() . '_delete_trash_comments'); |
| 336 |
|
| 337 |
$this->deleteComments('trash'); |
| 338 |
} |
| 339 |
} |