post_type : ''; } public static function filter_open( $open, $post_id ) { return self::off_for( self::post_type_of( $post_id ) ) ? false : $open; } public static function filter_comments( $comments, $post_id ) { if ( Softvolt_Ai_Bodyguard_Modules::get( 'comments_hide_existing' ) && self::off_for( self::post_type_of( $post_id ) ) ) { return array(); } return $comments; } public static function filter_count( $count, $post_id ) { if ( Softvolt_Ai_Bodyguard_Modules::get( 'comments_hide_existing' ) && self::off_for( self::post_type_of( $post_id ) ) ) { return 0; } return $count; } /** * A comment posted straight to wp-comments-post.php. * * @param int $post_id Post. */ public static function refuse_post( $post_id ) { if ( self::off_for( self::post_type_of( $post_id ) ) ) { wp_die( esc_html__( 'Comments are closed.', 'ai-guardwall' ), esc_html__( 'Comments are closed', 'ai-guardwall' ), array( 'response' => 403 ) ); } } public static function refuse_rest( $prepared, $request ) { $post_id = isset( $prepared['comment_post_ID'] ) ? (int) $prepared['comment_post_ID'] : 0; if ( $post_id && self::off_for( self::post_type_of( $post_id ) ) ) { return new WP_Error( 'rest_comment_closed', __( 'Comments are closed.', 'ai-guardwall' ), array( 'status' => 403 ) ); } return $prepared; } /** * The Discussion box, the comment column and the comment settings disappear * from the editor of the types that have comments off. */ public static function remove_support() { foreach ( get_post_types( array(), 'names' ) as $type ) { if ( self::off_for( $type ) ) { if ( post_type_supports( $type, 'comments' ) ) { remove_post_type_support( $type, 'comments' ); } if ( post_type_supports( $type, 'trackbacks' ) ) { remove_post_type_support( $type, 'trackbacks' ); } } } } public static function admin_menu() { remove_menu_page( 'edit-comments.php' ); } public static function admin_redirect() { global $pagenow; if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) && ! wp_doing_ajax() ) { wp_safe_redirect( admin_url() ); exit; } } public static function admin_bar( $bar ) { $bar->remove_node( 'comments' ); } public static function dashboard() { remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); } public static function widgets() { unregister_widget( 'WP_Widget_Recent_Comments' ); } /** * The comment counts in the "At a glance" and Activity boxes, and the * keyboard-shortcuts option for comment moderation on the profile. * * @param string $hook Admin page. */ public static function admin_css( $hook ) { if ( 'index.php' === $hook ) { wp_add_inline_style( 'common', '#dashboard_right_now .comment-count,#dashboard_right_now .comment-mod-count,#latest-comments,#welcome-panel .welcome-comments{display:none!important}' ); } elseif ( in_array( $hook, array( 'profile.php', 'user-edit.php' ), true ) ) { wp_add_inline_style( 'common', '.user-comment-shortcuts-wrap{display:none!important}' ); } } public static function refuse_feed() { if ( is_comment_feed() ) { wp_die( esc_html__( 'Comments are closed.', 'ai-guardwall' ), esc_html__( 'Comments are closed', 'ai-guardwall' ), array( 'response' => 403 ) ); } } public static function headers( $headers ) { unset( $headers['X-Pingback'] ); return $headers; } public static function xmlrpc( $methods ) { foreach ( array( 'wp.newComment', 'wp.editComment', 'wp.getCommentCount', 'wp.getComment', 'wp.getComments', 'wp.deleteComment', 'wp.getCommentStatusList', 'pingback.ping', 'pingback.extensions.getPingbacks' ) as $m ) { unset( $methods[ $m ] ); } return $methods; } public static function rest_endpoints( $endpoints ) { foreach ( array_keys( $endpoints ) as $route ) { if ( 0 === strpos( $route, '/wp/v2/comments' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /* ------------------------------ delete ------------------------------ */ /** * How many comments a bulk delete would remove. * * @return array scope => count, plus per type. */ public static function counts() { global $wpdb; $out = array( 'all' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments}" ), 'spam' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = 'spam'" ), 'pending' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = '0'" ), 'trash' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved IN ('trash','post-trashed')" ), 'types' => array(), ); $rows = $wpdb->get_results( "SELECT p.post_type, COUNT(*) AS n FROM {$wpdb->comments} c JOIN {$wpdb->posts} p ON p.ID = c.comment_post_ID GROUP BY p.post_type", ARRAY_A ); foreach ( (array) $rows as $row ) { $out['types'][ $row['post_type'] ] = (int) $row['n']; } return $out; } /** * Up to 1000 comment IDs in the scope. * * @param string $scope all|spam|pending|trash|type. * @param string $type Post type (scope 'type'). */ private static function batch_ids( $scope, $type ) { global $wpdb; switch ( $scope ) { case 'spam': $found = $wpdb->get_col( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = 'spam' ORDER BY comment_ID LIMIT 1000" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'pending': $found = $wpdb->get_col( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = '0' ORDER BY comment_ID LIMIT 1000" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'trash': $found = $wpdb->get_col( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved IN ('trash','post-trashed') ORDER BY comment_ID LIMIT 1000" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'type': $found = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s) ORDER BY comment_ID LIMIT 1000", $type ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'all': $found = $wpdb->get_col( "SELECT comment_ID FROM {$wpdb->comments} ORDER BY comment_ID LIMIT 1000" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; default: $found = array(); } return array_map( 'intval', (array) $found ); } /** * How many comments are in the scope. * * @param string $scope all|spam|pending|trash|type. * @param string $type Post type (scope 'type'). */ private static function count_left( $scope, $type ) { global $wpdb; switch ( $scope ) { case 'spam': $found = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = 'spam'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'pending': $found = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = '0'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'trash': $found = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved IN ('trash','post-trashed')" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'type': $found = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $type ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; case 'all': $found = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Maintenance query on demand. break; default: $found = 0; } return (int) $found; } /** * Delete comments in batches (one call deletes for a few seconds). * The first call copies the two comment tables first. * * @param string $scope all|spam|pending|trash|type. * @param string $type Post type (scope 'type'). * @param bool $first First call of this run. * @return array|WP_Error deleted, left, backup */ public static function delete( $scope, $type = '', $first = false ) { global $wpdb; $valid = in_array( $scope, array( 'all', 'spam', 'pending', 'trash', 'type' ), true ); if ( ! $valid ) { return new WP_Error( 'svabg_comments', __( 'Choose which comments to delete.', 'ai-guardwall' ) ); } $backup = array(); if ( $first ) { foreach ( array( $wpdb->comments, $wpdb->commentmeta ) as $table ) { $copy = Softvolt_Ai_Bodyguard_Modules::backup_table( $table ); if ( is_wp_error( $copy ) ) { return $copy; } $backup[] = $copy; } } $deleted = 0; $until = microtime( true ) + 8; do { $ids = self::batch_ids( $scope, sanitize_key( $type ) ); if ( ! $ids ) { break; } $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $posts = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT comment_post_ID FROM {$wpdb->comments} WHERE comment_ID IN ( {$placeholders} )", $ids ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $placeholders holds %d placeholders only; the IDs are passed to prepare(). $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( {$placeholders} )", $ids ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $placeholders holds %d placeholders only; the IDs are passed to prepare(). $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_ID IN ( {$placeholders} )", $ids ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $placeholders holds %d placeholders only; the IDs are passed to prepare(). // Replies to deleted comments become top-level comments, as wp_delete_comment() does. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->comments} SET comment_parent = 0 WHERE comment_parent IN ( {$placeholders} )", $ids ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $placeholders holds %d placeholders only; the IDs are passed to prepare(). foreach ( $ids as $id ) { clean_comment_cache( $id ); } foreach ( $posts as $post_id ) { if ( $post_id ) { wp_update_comment_count_now( $post_id ); } } $deleted += count( $ids ); } while ( microtime( true ) < $until ); $left = self::count_left( $scope, sanitize_key( $type ) ); if ( $deleted ) { wp_cache_delete( 'comments-0', 'counts' ); delete_transient( 'wc_count_comments' ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'comments_deleted', sprintf( /* translators: 1: deleted, 2: scope */ __( '%1$d comments deleted (%2$s).', 'ai-guardwall' ), $deleted, 'type' === $scope ? $scope . ': ' . $type : $scope ) ); } return array( 'deleted' => $deleted, 'left' => $left, 'backup' => $backup, ); } }