wp_delete_comment example

$allowed_length = (int) $content_length['length'] - 10;

    $comments = $wpdb->get_results(
        "SELECT `comment_ID` FROM `{$wpdb->comments}` WHERE `comment_date_gmt` > '2015-04-26' AND LENGTH( `comment_content` ) >= {$allowed_length} AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
    );

    foreach ( $comments as $comment ) {
        wp_delete_comment( $comment->comment_ID, true );
    }
}

/** * Executes changes made in WordPress 4.3.1. * * @ignore * @since 4.3.1 */
function upgrade_431() {
    // Fix incorrect cron entries for term splitting.

function wp_trash_comment( $comment_id ) {
    if ( ! EMPTY_TRASH_DAYS ) {
        return wp_delete_comment( $comment_id, true );
    }

    $comment = get_comment( $comment_id );
    if ( ! $comment ) {
        return false;
    }

    /** * Fires immediately before a comment is sent to the Trash. * * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment to be trashed. */
if ( ! get_comment( $comment_id ) ) {
            return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
        }

        if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
            return new IXR_Error( 403, __( 'Sorry, you are not allowed to delete this comment.' ) );
        }

        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
        do_action( 'xmlrpc_call', 'wp.deleteComment', $args$this );

        $status = wp_delete_comment( $comment_id );

        if ( $status ) {
            /** * Fires after a comment has been successfully deleted via XML-RPC. * * @since 3.4.0 * * @param int $comment_id ID of the deleted comment. * @param array $args An array of arguments to delete the comment. */
            do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_id$args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
$unspammed++;
                break;
            case 'trash':
                wp_trash_comment( $comment_id );
                $trashed++;
                break;
            case 'untrash':
                wp_untrash_comment( $comment_id );
                $untrashed++;
                break;
            case 'delete':
                wp_delete_comment( $comment_id );
                $deleted++;
                break;
        }
    }

    if ( ! in_array( $doaction, array( 'approve', 'unapprove', 'spam', 'unspam', 'trash', 'delete' ), true ) ) {
        $screen = get_current_screen()->id;

        /** This action is documented in wp-admin/edit.php */
        $redirect_to = apply_filters( "handle_bulk_actions-{$screen}", $redirect_to$doaction$comment_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores     }

    
if ( 'spam' !== $status ) {
            wp_die( time() );
        }

        $r = wp_unspam_comment( $comment );

        // Undo spam, not in spam.         if ( ! isset( $_POST['comment_status'] ) || 'spam' !== $_POST['comment_status'] ) {
            $delta = 1;
        }
    } elseif ( isset( $_POST['delete'] ) && 1 == $_POST['delete'] ) {
        $r = wp_delete_comment( $comment );
    } else {
        wp_die( -1 );
    }

    if ( $r ) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts.         _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
    }

    wp_die( 0 );
}

$redir = wp_get_original_referer();
        } elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ), true ) ) {
            $redir = admin_url( 'edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
        } else {
            $redir = admin_url( 'edit-comments.php' );
        }

        $redir = remove_query_arg( array( 'spammed', 'unspammed', 'trashed', 'untrashed', 'deleted', 'ids', 'approved', 'unapproved' )$redir );

        switch ( $action ) {
            case 'deletecomment':
                wp_delete_comment( $comment );
                $redir = add_query_arg( array( 'deleted' => '1' )$redir );
                break;
            case 'trashcomment':
                wp_trash_comment( $comment );
                $redir = add_query_arg(
                    array(
                        'trashed' => '1',
                        'ids'     => $comment_id,
                    ),
                    $redir
                );
                

        $supports_trash = apply_filters( 'rest_comment_trashable', ( EMPTY_TRASH_DAYS > 0 )$comment );

        $request->set_param( 'context', 'edit' );

        if ( $force ) {
            $previous = $this->prepare_item_for_response( $comment$request );
            $result   = wp_delete_comment( $comment->comment_ID, true );
            $response = new WP_REST_Response();
            $response->set_data(
                array(
                    'deleted'  => true,
                    'previous' => $previous->get_data(),
                )
            );
        } else {
            // If this type doesn't support trashing, error out.             if ( ! $supports_trash ) {
                return new WP_Error(
                    
$comment_id = (int) $comment['comment_id'];
        if ( ! $comment_id ) {
            continue;
        }

        $del_comment = get_comment( $comment_id );

        if ( ! $del_comment || 'trash' !== $del_comment->comment_approved ) {
            delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
            delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
        } else {
            wp_delete_comment( $del_comment );
        }
    }
}

/** * Retrieves metadata from a file. * * Searches for metadata in the first 8 KB of a file, such as a plugin or theme. * Each piece of metadata must be on its own line. Fields can not span multiple * lines, the value will get cut at the end of the first line. * * If the file data is not within that first 8 KB, then the author should correct * their plugin file and move the data headers to the top. * * @link https://codex.wordpress.org/File_Header * * @since 2.9.0 * * @param string $file Absolute path to the file. * @param array $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`. * @param string $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}. * Default empty string. * @return string[] Array of file header values keyed by header name. */
    foreach ( $revision_ids as $revision_id ) {
        wp_delete_post_revision( $revision_id );
    }

    // Point all attachments to this post up one level.     $wpdb->update( $wpdb->posts, $parent_data$parent_where + array( 'post_type' => 'attachment' ) );

    wp_defer_comment_counting( true );

    $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) );
    foreach ( $comment_ids as $comment_id ) {
        wp_delete_comment( $comment_id, true );
    }

    wp_defer_comment_counting( false );

    $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) );
    foreach ( $post_meta_ids as $mid ) {
        delete_metadata_by_mid( 'post', $mid );
    }

    /** * Fires immediately before a post is deleted from the database. * * @since 1.2.0 * @since 5.5.0 Added the `$post` parameter. * * @param int $postid Post ID. * @param WP_Post $post Post object. */
Home | Imprint | This part of the site doesn't use cookies.