Skip to content

Commit

Permalink
Add a tools page to allow admins to delete comments in bulk. Fixes #37.
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Hyder authored and solarissmoke committed Dec 2, 2016
1 parent c57c221 commit 6791ce7
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
25 changes: 24 additions & 1 deletion disable-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ public function init_wploaded_filters(){
if( is_admin() ) {
if( $this->networkactive ) {
add_action( 'network_admin_menu', array( $this, 'settings_menu' ) );
add_action( 'network_admin_menu', array( $this, 'tools_menu' ) );
add_filter( 'network_admin_plugin_action_links', array( $this, 'plugin_actions_links'), 10, 2 );
}
else {
add_action( 'admin_menu', array( $this, 'settings_menu' ) );
add_action( 'admin_menu', array( $this, 'tools_menu' ) );
add_filter( 'plugin_action_links', array( $this, 'plugin_actions_links'), 10, 2 );
if( is_multisite() ) // We're on a multisite setup, but the plugin isn't network activated.
register_deactivation_hook( __FILE__, array( $this, 'single_site_deactivate' ) );
Expand Down Expand Up @@ -299,6 +301,14 @@ private function settings_page_url() {
return add_query_arg( 'page', 'disable_comments_settings', $base );
}

/**
* Return context-aware tools page URL
*/
private function tools_page_url() {
$base = $this->networkactive ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
return add_query_arg( 'page', 'disable_comments_tools', $base );
}

public function setup_notice(){
if( strpos( get_current_screen()->id, 'settings_page_disable_comments_settings' ) === 0 )
return;
Expand Down Expand Up @@ -369,7 +379,8 @@ public function plugin_actions_links( $links, $file ) {
if( $file == $plugin && current_user_can('manage_options') ) {
array_unshift(
$links,
sprintf( '<a href="%s">%s</a>', esc_attr( $this->settings_page_url() ), __( 'Settings' ) )
sprintf( '<a href="%s">%s</a>', esc_attr( $this->settings_page_url() ), __( 'Settings' ) ),
sprintf( '<a href="%s">%s</a>', esc_attr( $this->tools_page_url() ), __( 'Tools' ) )
);
}

Expand All @@ -388,6 +399,18 @@ public function settings_page() {
include dirname( __FILE__ ) . '/includes/settings-page.php';
}

public function tools_menu() {
$title = __( 'Delete Comments', 'disable-comments' );
if( $this->networkactive )
add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'disable_comments_tools', array( $this, 'tools_page' ) );
else
add_submenu_page( 'tools.php', $title, $title, 'manage_options', 'disable_comments_tools', array( $this, 'tools_page' ) );
}

public function tools_page() {
include dirname( __FILE__ ) . '/includes/tools-page.php';
}

private function enter_permanent_mode() {
$types = $this->get_disabled_post_types();
if( empty( $types ) )
Expand Down
4 changes: 2 additions & 2 deletions includes/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</ul>
<?php if( $this->networkactive ) :?>
<p class="indent" id="extratypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
<br /><label>Custom post types: <input type="text" name="extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
<br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
<?php endif; ?>
<p class="indent"><?php _e( 'Disabling comments will also disable trackbacks and pingbacks. All comment-related fields will also be hidden from the edit/quick-edit screens of the affected posts. These settings cannot be overridden for individual posts.', 'disable-comments') ?></p>
</li>
Expand All @@ -86,7 +86,7 @@
<?php endif; ?>

<?php wp_nonce_field( 'disable-comments-admin' ); ?>
<p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes') ?>"></p>
<p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes', 'disable-comments') ?>"></p>
</form>
</div>
<script>
Expand Down
122 changes: 122 additions & 0 deletions includes/tools-page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
if( !defined( 'ABSPATH' ) ) {
exit;
}
?>
<div class="wrap">
<h1><?php _e( 'Delete Comments', 'disable-comments') ?></h1>
<?php

global $wpdb;
$comments_count = $wpdb->get_var("SELECT count(comment_id) from $wpdb->comments");
if ( $comments_count <= 0 ) { ?>
<p><strong><?php _e( 'No comments available for deletion.', 'disable-comments') ?></strong></p>
</div>
<?php
return;
}

$typeargs = array( 'public' => true );
if( $this->networkactive ) {
$typeargs['_builtin'] = true; // stick to known types for network
}
$types = get_post_types( $typeargs, 'objects' );
foreach( array_keys( $types ) as $type ) {
if( ! in_array( $type, $this->modified_types ) && ! post_type_supports( $type, 'comments' ) ) // the type doesn't support comments anyway
unset( $types[$type] );
}

if ( isset( $_POST['delete'] ) && isset( $_POST['delete_mode'] ) ) {
check_admin_referer( 'delete-comments-admin' );

if( $_POST['delete_mode'] == 'delete_everywhere' ) {
if ( $wpdb->query( "TRUNCATE $wpdb->commentmeta" ) != FALSE ){
if ( $wpdb->query( "TRUNCATE $wpdb->comments" ) != FALSE ){
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0" );
$wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
$wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );
echo "<p style='color:green'><strong>" . __( 'All comments have been deleted.', 'disable-comments' ) . "</strong></p>";
} else {
echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . "</strong></p>";
}
} else {
echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . "</strong></p>";
}
} else {
$delete_post_types = empty( $_POST['delete_types'] ) ? array() : (array) $_POST['delete_types'];
$delete_post_types = array_intersect( $delete_post_types, array_keys( $types ) );

// Extra custom post types
if( $this->networkactive && !empty( $_POST['delete_extra_post_types'] ) ) {
$delete_extra_post_types = array_filter( array_map( 'sanitize_key', explode( ',', $_POST['delete_extra_post_types'] ) ) );
$delete_extra_post_types = array_diff( $delete_extra_post_types, array_keys( $types ) ); // Make sure we don't double up builtins
$delete_post_types = array_merge($delete_post_types, $delete_extra_post_types);
}

if ( ! empty( $delete_post_types ) ) {
// Loop through post_types and remove comments/meta and set posts comment_count to 0
foreach ( $delete_post_types as $delete_post_type ) {
$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'" );
$wpdb->query( "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'" );
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0 AND post_type = '$delete_post_type'" );
echo "<p style='color:green'><strong>" . sprintf( __( 'All comments have been deleted for %ss.', 'disable-comments' ), $delete_post_type ) . "</strong></p>";
}

$wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
$wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );

echo "<h4 style='color:green'><strong>" . __( 'Comment Deletion Complete', 'disable-comments') . "</strong></h4>";
}

}

$comments_count = $wpdb->get_var("SELECT count(comment_id) from $wpdb->comments");
if ( $comments_count <= 0 ) { ?>
<p><strong><?php _e( 'No comments available for deletion.', 'disable-comments') ?></strong></p>
</div>
<?php
return;
}

}
?>
<form action="" method="post" id="delete-comments">
<ul>
<li><label for="delete_everywhere"><input type="radio" id="delete_everywhere" name="delete_mode" value="delete_everywhere" <?php checked( $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'Everywhere', 'disable-comments') ?></strong>: <?php _e( 'Delete all comments in WordPress.', 'disable-comments') ?></label>
<p class="indent"><?php printf( __( '%s: This function and will affect your entire site. Use it only if you want to delete comments <em>everywhere</em>.', 'disable-comments' ), '<strong style="color: #900">' . __('Warning', 'disable-comments') . '</strong>' ); ?></p>
</li>
<li><label for="selected_delete_types"><input type="radio" id="selected_delete_types" name="delete_mode" value="selected_delete_types" <?php checked( ! $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'For certain post types', 'disable-comments') ?></strong>:</label>
<p></p>
<ul class="indent" id="listofdeletetypes">
<?php foreach( $types as $k => $v ) echo "<li><label for='post-type-$k'><input type='checkbox' name='delete_types[]' value='$k' ". checked( in_array( $k, $this->options['disabled_post_types'] ), true, false ) ." id='post-type-$k'> {$v->labels->name}</label></li>";?>
</ul>
<?php if( $this->networkactive ) :?>
<p class="indent" id="extradeletetypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
<br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="delete_extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
<?php endif; ?>
<p class="indent"><?php printf( __( '%s: Deleting comments will remove existing comment entries in the database and cannot be reverted without a database backup.', 'disable-comments'), '<strong style="color: #900">' . __('Warning', 'disable-comments') . '</strong>' ); ?></p>
</li>
</ul>

<?php wp_nonce_field( 'delete-comments-admin' ); ?>
<h4><?php _e( 'Total Comments:', 'disable-comments' ); ?> <?php echo $comments_count; ?></h4>
<p class="submit"><input class="button-primary" type="submit" name="delete" value="<?php _e( 'Delete Comments', 'disable-comments') ?>"></p>
</form>
</div>
<script>
jQuery(document).ready(function($){
function delete_comments_uihelper(){
var toggle_bits = $("#listofdeletetypes, #extradeletetypes");
if( $("#delete_everywhere").is(":checked") )
toggle_bits.css("color", "#888").find(":input").attr("disabled", true );
else
toggle_bits.css("color", "#000").find(":input").attr("disabled", false );
}

$("#delete-comments :input").change(function(){
delete_comments_uihelper();
});

delete_comments_uihelper();
});
</script>
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Requires at least: 4.0
Tested up to: 4.6
Stable tag: trunk

Allows administrators to globally disable comments on their site. Comments can be disabled according to post type. Multisite friendly.
Allows administrators to globally disable comments on their site. Comments can be disabled according to post type. Multisite friendly. Provides tool to delete all comments or according to post type.

== Description ==

Expand Down Expand Up @@ -44,6 +44,10 @@ Go to the edit page for the post you want to disable comments on. Scroll down to

You can also bulk-edit the comment status of multiple posts from the [posts screen](https://codex.wordpress.org/Posts_Screen).

= I want to delete comments from my database. What do I do? =

Go to the settings page for the disable comments plugin and utlize the Delete Comments tool to delete all comments or according to the specified post types from your database.

== Details ==

The plugin provides the option to **completely disable the commenting feature in WordPress**. When this option is selected, the following changes are made:
Expand All @@ -56,7 +60,7 @@ The plugin provides the option to **completely disable the commenting feature in
* The X-Pingback HTTP header is removed from all pages;
* Outgoing pingbacks are disabled.

**Please delete any existing comments on your site before applying this setting, otherwise (depending on your theme) those comments may still be displayed to visitors.**
**Please delete any existing comments on your site before applying this setting, otherwise (depending on your theme) those comments may still be displayed to visitors. You can now use the Delete Comments tool to delete any existing comments on your site.**

== Advanced Configuration ==

Expand Down

0 comments on commit 6791ce7

Please sign in to comment.