/** * Delete the comment corresponding to this container. * This will actually move the comment to the trash in newer versions of WP. * * @return bool|WP_error */ function trash_wrapped_object() { if (wp_trash_comment($this->container_id)) { return true; } else { return new WP_Error('trash_failed', sprintf(__('Can\'t move comment %d to the trash', 'broken-link-checker'), $this->container_id)); } }
/** * Trash a comment * * Example: wp comment trash 15 * * @param array $args} * @param array $assoc_args */ public function trash($args, $assoc_args) { $comment_id = WP_CLI::get_numeric_arg($args, 0, "Comment ID"); if (wp_trash_comment($comment_id)) { WP_CLI::success("Trashed comment {$comment_id}."); } else { WP_CLI::error("Failed trashing comment {$comment_id}"); } }
public function test_cache_is_cleared_when_comment_is_trashed() { $comment_1 = self::factory()->comment->create_and_get(array('comment_status' => 1, 'comment_date' => '1998-01-01 11:00:00', 'comment_date_gmt' => '1998-01-01 10:00:00')); $comment_2 = self::factory()->comment->create_and_get(array('comment_status' => 1, 'comment_date' => '2000-01-02 11:00:00', 'comment_date_gmt' => '2000-01-02 10:00:00')); get_lastcommentmodified(); $this->assertSame(strtotime('2000-01-02 10:00:00'), strtotime(wp_cache_get('lastcommentmodified:server', 'timeinfo'))); wp_trash_comment($comment_2->comment_ID); $this->assertFalse(wp_cache_get('lastcommentmodified:server', 'timeinfo')); $this->assertSame(strtotime('1998-01-01 10:00:00'), strtotime(get_lastcommentmodified())); $this->assertSame(strtotime('1998-01-01 10:00:00'), strtotime(wp_cache_get('lastcommentmodified:server', 'timeinfo'))); }
public function test_wp_untrash_comment() { wp_trash_comment($this->comment->comment_ID); $this->client->do_sync(); $this->assertEquals(0, $this->server_replica_storage->comment_count('approve')); $this->assertEquals(1, $this->server_replica_storage->comment_count('trash')); wp_untrash_comment($this->comment->comment_ID); $this->client->do_sync(); $this->assertEquals(1, $this->server_replica_storage->comment_count('approve')); $this->assertEquals(0, $this->server_replica_storage->comment_count('trash')); }
private function bulk_comments($doaction, $comment_ids) { global $wpdb; $approved = $unapproved = $spammed = $unspammed = $trashed = $untrashed = $deleted = 0; foreach ((array) $comment_ids as $comment_id) { // Check the permissions on each $_post_id = (int) $wpdb->get_var($wpdb->prepare("SELECT comment_post_ID FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id)); if (!current_user_can('edit_post', $_post_id)) { continue; } switch ($doaction) { case 'approve': wp_set_comment_status($comment_id, 'approve'); $approved++; break; case 'unapprove': wp_set_comment_status($comment_id, 'hold'); $unapproved++; break; case 'spam': case 'markspam': if (function_exists('wp_spam_coment')) { wp_spam_comment($comment_id); } else { wp_set_comment_status($comment_id, 'spam'); } $spammed++; break; case 'unspam': if (function_exists('wp_unspam_comment')) { wp_unspam_comment($comment_id); $unspammed++; } break; case 'trash': if (function_exists('wp_trash_comment')) { wp_trash_comment($comment_id); $trashed++; } break; case 'untrash': if (function_exists('wp_untrash_comment')) { wp_untrash_comment($comment_id); $untrashed++; } break; case 'delete': if (function_exists('wp_delete_comment')) { wp_delete_comment($comment_id); } else { wp_set_comment_status($comment_id, 'delete'); } $deleted++; break; } } $redirect_to = $this->referer; if (false === strpos($redirect_to, 'edit-comments.php')) { $redirect_to = 'edit-comments.php'; } if ($approved) { $redirect_to = add_query_arg('approved', $approved, $redirect_to); } if ($unapproved) { $redirect_to = add_query_arg('unapproved', $unapproved, $redirect_to); } if ($spammed) { $redirect_to = add_query_arg('spammed', $spammed, $redirect_to); } if ($unspammed) { $redirect_to = add_query_arg('unspammed', $unspammed, $redirect_to); } if ($trashed) { $redirect_to = add_query_arg('trashed', $trashed, $redirect_to); } if ($untrashed) { $redirect_to = add_query_arg('untrashed', $untrashed, $redirect_to); } if ($deleted) { $redirect_to = add_query_arg('deleted', $deleted, $redirect_to); } if ($trashed || $spammed) { $redirect_to = add_query_arg('ids', join(',', $comment_ids), $redirect_to); } if ($this->post_id > 0) { $redirect_to = add_query_arg('p', $this->post_id, $redirect_to); } if (isset($_REQUEST['apage'])) { $redirect_to = add_query_arg('apage', abs(intval($_REQUEST['apage'])), $redirect_to); } if (!empty($_REQUEST['mode'])) { $redirect_to = add_query_arg('mode', $_REQUEST['mode'], $redirect_to); } if (!empty($_REQUEST['comment_status'])) { $redirect_to = add_query_arg('comment_status', $_REQUEST['comment_status'], $redirect_to); } if (!empty($_REQUEST['s'])) { $redirect_to = add_query_arg('s', $_REQUEST['s'], $redirect_to); } $this->admin->redirect($redirect_to); }
$redir = wp_get_referer(); } elseif ('' != wp_get_original_referer() && !$noredir) { $redir = wp_get_original_referer(); } elseif (in_array($action, array('approvecomment', 'unapprovecomment'))) { $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); break; case 'untrashcomment': wp_untrash_comment($comment); $redir = add_query_arg(array('untrashed' => '1'), $redir); break; case 'spamcomment': wp_spam_comment($comment); $redir = add_query_arg(array('spammed' => '1', 'ids' => $comment_id), $redir); break; case 'unspamcomment': wp_unspam_comment($comment); $redir = add_query_arg(array('unspammed' => '1'), $redir); break; case 'approvecomment':
/** * Review Rating comment ajax actions. * * @since 1.0.0 * @package GeoDirectory_Review_Rating_Manager * * @param $request * @return bool */ function geodir_reviewrating_comment_action($request) { global $wpdb; $comment_ids = array(); if (isset($request['comment_ids']) && $request['comment_ids'] != '') { $comment_ids = explode(',', $request['comment_ids']); } if (!empty($comment_ids) && $request['comment_ids'] != '') { if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'geodir_review_action_nonce')) { return false; } foreach ($comment_ids as $comment_id) { if ($comment_id != '') { switch ($request['comment_action']) { case 'deletecomment': wp_delete_comment($comment_id); break; case 'trashcomment': wp_trash_comment($comment_id); break; case 'untrashcomment': wp_untrash_comment($comment_id); break; case 'spamcomment': wp_spam_comment($comment_id); break; case 'unspamcomment': wp_unspam_comment($comment_id); break; case 'approvecomment': wp_set_comment_status($comment_id, 'approve'); break; case 'unapprovecomment': wp_set_comment_status($comment_id, 'hold'); break; } } } if (isset($request['geodir_comment_search'])) { $geodir_commentsearch = $request['geodir_comment_search']; } if (isset($request['geodir_comment_posttype'])) { $post_type = $request['geodir_comment_posttype']; } $status = $request['subtab']; $orderby = 'comment_date_gmt'; $order = 'DESC'; if (isset($request['geodir_comment_sort'])) { if ($request['geodir_comment_sort'] == 'oldest') { $orderby = 'comment_date_gmt'; $order = 'ASC'; } } if (isset($request['paged']) && $request['paged'] != '') { $paged = $request['paged']; } else { $paged = 1; } $show_post = $request['show_post']; $defaults = array('paged' => $paged, 'show_post' => $show_post, 'orderby' => $orderby, 'order' => $order, 'post_type' => $post_type, 'comment_approved' => $status, 'user_id' => '', 'search' => $geodir_commentsearch); $comments = geodir_reviewrating_get_comments($defaults); geodir_reviewrating_show_comments($comments['comments']); } if (isset($request['gd_tab_head'])) { geodir_reviewrating_show_tab_head($request['gd_tab_head']); } exit; }
/** * Trashes or deletes a comment. * * The comment is moved to trash instead of permanently deleted unless trash is * disabled, item is already in the trash, or $force_delete is true. * * The post comment count will be updated if the comment was approved and has a * post ID available. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. * @return bool True on success, false on failure. */ function wp_delete_comment($comment_id, $force_delete = false) { global $wpdb; if (!($comment = get_comment($comment_id))) { return false; } if (!$force_delete && EMPTY_TRASH_DAYS && !in_array(wp_get_comment_status($comment), array('trash', 'spam'))) { return wp_trash_comment($comment_id); } /** * Fires immediately before a comment is deleted from the database. * * @since 1.2.0 * * @param int $comment_id The comment ID. */ do_action('delete_comment', $comment->comment_ID); // Move children up a level. $children = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent = %d", $comment->comment_ID)); if (!empty($children)) { $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID)); clean_comment_cache($children); } // Delete metadata $meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->commentmeta} WHERE comment_id = %d", $comment->comment_ID)); foreach ($meta_ids as $mid) { delete_metadata_by_mid('comment', $mid); } if (!$wpdb->delete($wpdb->comments, array('comment_ID' => $comment->comment_ID))) { return false; } /** * Fires immediately after a comment is deleted from the database. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action('deleted_comment', $comment->comment_ID); $post_id = $comment->comment_post_ID; if ($post_id && $comment->comment_approved == 1) { wp_update_comment_count($post_id); } clean_comment_cache($comment->comment_ID); /** This action is documented in wp-includes/comment-functions.php */ do_action('wp_set_comment_status', $comment->comment_ID, 'delete'); wp_transition_comment_status('delete', $comment->comment_approved, $comment); return true; }
/** * Delete a product review. * * @param WP_REST_Request $request Full details about the request * @return WP_Error|boolean */ public function delete_item($request) { $product_review_id = is_array($request['id']) ? $request['id']['id'] : $request['id']; $force = isset($request['force']) ? (bool) $request['force'] : false; $product_review = get_comment($product_review_id); if (empty($product_review_id) || empty($product_review->comment_ID) || empty($product_review->comment_post_ID)) { return new WP_Error('woocommerce_rest_product_review_invalid_id', __('Invalid product review ID.', 'woocommerce'), array('status' => 404)); } /** * Filter whether a product review is trashable. * * Return false to disable trash support for the product review. * * @param boolean $supports_trash Whether the object supports trashing. * @param WP_Post $product_review The object being considered for trashing support. */ $supports_trash = apply_filters('rest_product_review_trashable', EMPTY_TRASH_DAYS > 0, $product_review); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($product_review, $request); if ($force) { $result = wp_delete_comment($product_review_id, true); } else { if (!$supports_trash) { return new WP_Error('rest_trash_not_supported', __('The product review does not support trashing.', 'woocommerce'), array('status' => 501)); } if ('trash' === $product_review->comment_approved) { return new WP_Error('rest_already_trashed', __('The comment has already been trashed.', 'woocommerce'), array('status' => 410)); } $result = wp_trash_comment($product_review->comment_ID); } if (!$result) { return new WP_Error('rest_cannot_delete', __('The product review cannot be deleted.', 'woocommerce'), array('status' => 500)); } /** * Fires after a product review is deleted via the REST API. * * @param object $product_review The deleted item. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */ do_action('rest_delete_product_review', $product_review, $response, $request); return $response; }
function spc_manage_contest($contest_id = 0) { global $wpdb; switch ($_POST['submit_task']) { case 'Pubish': if (count($_POST['pid']) > 0) { foreach ($_POST['pid'] as $value) { wp_update_comment(array('comment_ID' => $value, 'comment_approved' => 1)); } } break; case 'UnPublish': if (count($_POST['pid']) > 0) { foreach ($_POST['pid'] as $value) { wp_update_comment(array('comment_ID' => $value, 'comment_approved' => 0)); } } break; case 'Delete': if (count($_POST['pid']) > 0) { foreach ($_POST['pid'] as $value) { wp_trash_comment($value); } } break; default: break; } $sql = "SELECT * FROM " . $wpdb->spc_contest . " WHERE " . $wpdb->spc_contest . ".contest_id <> 0 "; if ($contest_id) { $sql .= " AND " . $wpdb->spc_contest . ".contest_id = " . $contest_id; } $results = $wpdb->get_results($sql, ARRAY_A); //spc_dg($results); ?> <form name="listform" id="listform" action="admin.php?page=spc-manage-participant" method="POST"> <table class="widefat"> <thead> <tr> <td colspan="6"> <b>Action :</b> <input class="button" type="submit" name="submit_task" value="Approve"> <input class="button" type="submit" name="submit_task" value="UnApprove"> <input class="button" type="submit" name="submit_task" value="Delete"> </td> </tr> <tr> <th scope="col">Select</th> <th scope="col">PostID</th> <th scope="col">Desciption</th> <th scope="col">Start-End Date</th> <th scope="col">Upload End Date</th> <th scope="col">Contest Status</th> <th scope="col">Photo MaxUpload/user</th> <th scope="col">Action</th> <th scope="col"></th> </tr> </thead> <tbody id="the-list"> <?php $cnt = 0; foreach ($results as $val) { echo "<tr " . ($cnt % 2 == 0 ? ' class="alternate"' : '') . ">\n"; echo "<th scope=\"row\" style=\"text-align: center\"><input type=\"checkbox\" value=\"" . $val['contest_id'] . "\" name=\"pid[]\" id=\"cb" . $val['contest_id'] . "\"/></th>"; echo "<th scope=\"row\" style=\"text-align: center\">" . $val['contest_postid'] . "</th>\n"; echo "<th scope=\"row\">"; $temp = "<table border='0'>"; $temp .= "<tr><th colspan='2'>Title: " . $val['contest_title'] . "</th></tr>"; $temp .= "<tr><th valign='top'><p>" . $val['contest_description'] . "</p></th></tr>"; $temp .= "</table>"; echo $temp; echo "</th>"; echo "<th scope=\"row\">" . $val['contest_startdate'] . " until " . $val['contest_enddate'] . "</th>"; echo "<th scope=\"row\">" . $val['contest_uploadenddate'] . "</th>"; echo "<th scope=\"row\">" . $val['contest_status'] . "</th>"; echo "<th scope=\"row\">" . $val['contest_maxupload'] . "</th>"; echo "<th scope=\"row\"><button class=\"button\" onclick=\"location.href='./admin.php?page=spc-addedit&contestID=" . $val['contest_id'] . "';\">EDIT</button></th>"; echo "</tr>\n"; } ?> </tbody> </table> </form> <?php }
private function delete_comment($action) { $comment_id = intval($_REQUEST['c']); check_admin_referer('delete-comment_' . $comment_id); $noredir = isset($_REQUEST['noredir']); if (!($comment = get_comment($comment_id))) { $this->base->ks_die(__('Oops, no comment with this ID.') . sprintf(' <a href="%s">' . __('Go back') . '</a>!', 'edit-comments.php'), '', false); //exit; } if (!current_user_can('edit_post', $comment->comment_post_ID)) { $this->base->ks_die(__('You are not allowed to edit comments on this post.')); } $redir = $this->referer; if (empty($redir) || $noredir || false !== strpos($redir, 'comment.php')) { $redir = 'edit-comments.php'; } switch ($action) { case 'deletecomment': wp_delete_comment($comment_id); $redir = add_query_arg(array('deleted' => 1), $redir); break; case 'trashcomment': if (function_exists('wp_trash_comment')) { wp_trash_comment($comment_id); $redir = add_query_arg(array('trashed' => '1', 'ids' => $comment_id), $redir); } break; case 'untrashcomment': if (function_exists('wp_untrash_comment')) { wp_untrash_comment($comment_id); $redir = add_query_arg(array('untrashed' => '1'), $redir); } break; case 'spamcomment': if (function_exists('wp_spam_comment')) { wp_spam_comment($comment_id); } else { wp_set_comment_status($comment_id, 'spam'); } $redir = add_query_arg(array('spammed' => '1', 'ids' => $comment_id), $redir); break; case 'unspamcomment': if (function_exists('wp_spam_comment')) { wp_unspam_comment($comment_id); $redir = add_query_arg(array('unspammed' => '1'), $redir); } break; } $this->admin->redirect($redir); exit; }
public function test_wp_count_comments_cache() { $post_id = self::factory()->post->create(array('post_status' => 'publish')); $comment_id = self::factory()->comment->create(array('comment_approved' => '1', 'comment_post_ID' => $post_id)); $count1 = wp_count_comments($post_id); $this->assertEquals(1, $count1->approved); $this->assertEquals(0, $count1->moderated); $this->assertEquals(0, $count1->spam); $this->assertEquals(0, $count1->trash); $this->assertEquals(0, $count1->{'post-trashed'}); $this->assertEquals(1, $count1->total_comments); $this->assertEquals(1, $count1->all); $all_count1 = wp_count_comments(); $this->assertEquals(1, $all_count1->approved); $this->assertEquals(0, $all_count1->moderated); $this->assertEquals(0, $all_count1->spam); $this->assertEquals(0, $all_count1->trash); $this->assertEquals(0, $all_count1->{'post-trashed'}); $this->assertEquals(1, $all_count1->total_comments); $this->assertEquals(1, $all_count1->all); wp_spam_comment($comment_id); $count2 = wp_count_comments($post_id); $this->assertEquals(0, $count2->approved); $this->assertEquals(0, $count2->moderated); $this->assertEquals(1, $count2->spam); $this->assertEquals(0, $count2->trash); $this->assertEquals(0, $count2->{'post-trashed'}); $this->assertEquals(1, $count2->total_comments); $this->assertEquals(0, $count2->all); $all_count2 = wp_count_comments(); $this->assertEquals(0, $all_count2->approved); $this->assertEquals(0, $all_count2->moderated); $this->assertEquals(1, $all_count2->spam); $this->assertEquals(0, $all_count2->trash); $this->assertEquals(0, $all_count2->{'post-trashed'}); $this->assertEquals(1, $all_count2->total_comments); $this->assertEquals(0, $all_count2->all); wp_trash_comment($comment_id); $count3 = wp_count_comments($post_id); $this->assertEquals(0, $count3->approved); $this->assertEquals(0, $count3->moderated); $this->assertEquals(0, $count3->spam); $this->assertEquals(1, $count3->trash); $this->assertEquals(0, $count3->{'post-trashed'}); $this->assertEquals(0, $count3->total_comments); $this->assertEquals(0, $count3->all); $all_count3 = wp_count_comments(); $this->assertEquals(0, $all_count3->approved); $this->assertEquals(0, $all_count3->moderated); $this->assertEquals(0, $all_count3->spam); $this->assertEquals(1, $all_count3->trash); $this->assertEquals(0, $all_count3->{'post-trashed'}); $this->assertEquals(0, $all_count3->total_comments); $this->assertEquals(0, $all_count3->all); wp_untrash_comment($comment_id); $count4 = wp_count_comments($post_id); $this->assertEquals(0, $count4->approved); $this->assertEquals(0, $count4->moderated); $this->assertEquals(1, $count4->spam); $this->assertEquals(0, $count4->trash); $this->assertEquals(0, $count4->{'post-trashed'}); $this->assertEquals(1, $count4->total_comments); $this->assertEquals(0, $count4->all); $all_count4 = wp_count_comments(); $this->assertEquals(0, $all_count4->approved); $this->assertEquals(0, $all_count4->moderated); $this->assertEquals(1, $all_count4->spam); $this->assertEquals(0, $all_count4->trash); $this->assertEquals(0, $all_count4->{'post-trashed'}); $this->assertEquals(1, $all_count4->total_comments); $this->assertEquals(0, $all_count4->all); }
/** * @group bp_blogs_transition_activity_status * @group bp_blogs_remove_comment */ public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() { // save the current user and override logged-in user $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); $userdata = get_userdata($u); // create the blog post $post_id = $this->factory->post->create(array('post_status' => 'publish', 'post_type' => 'post', 'post_title' => 'First title')); // let's use activity comments instead of single "new_blog_comment" activity items add_filter('bp_disable_blogforum_comments', '__return_false'); $c1 = wp_new_comment(array('comment_post_ID' => $post_id, 'comment_author' => $userdata->user_nicename, 'comment_author_url' => 'http://buddypress.org', 'comment_author_email' => $userdata->user_email, 'comment_content' => 'this is a blog comment', 'comment_type' => '', 'comment_parent' => 0, 'user_id' => $u)); // save the corresponding activity comment ID $a1 = bp_activity_get_activity_id(array('type' => 'activity_comment', 'display_comments' => 'stream', 'meta_query' => array(array('key' => 'bp_blogs_post_comment_id', 'value' => $c1)))); // trash the parent comment. // corresponding activity comment should now be marked as spam // @see bp_blogs_transition_activity_status() wp_trash_comment($c1); // now permanently delete the comment wp_delete_comment($c1, true); // activity comment should no longer exist $a = bp_activity_get(array('in' => $a1, 'display_comments' => 'stream', 'spam' => 'all')); // this is a convoluted way of testing if the activity comment still exists $this->assertTrue(empty($a['activities'][0])); // reset $this->set_current_user($old_user); remove_filter('bp_disable_blogforum_comments', '__return_false'); }
/** * Removes comment ID and maybe updates post comment count. * * The post comment count will be updated if the comment was approved and has a * post ID available. * * @since 2.0.0 * @uses $wpdb * @uses do_action() Calls 'delete_comment' hook on comment ID * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param int $comment_id Comment ID * @return bool False if delete comment query failure, true on success. */ function wp_delete_comment($comment_id) { global $wpdb; if (!($comment = get_comment($comment_id))) { return false; } if (wp_get_comment_status($comment_id) != 'trash' && wp_get_comment_status($comment_id) != 'spam' && EMPTY_TRASH_DAYS > 0) { return wp_trash_comment($comment_id); } do_action('delete_comment', $comment_id); delete_comment_meta($comment_id, '_wp_trash_meta_status'); delete_comment_meta($comment_id, '_wp_trash_meta_time'); if (!$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment_id))) { return false; } // Move children up a level. $children = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent = %d", $comment_id)); if (!empty($children)) { $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id)); clean_comment_cache($children); } $post_id = $comment->comment_post_ID; if ($post_id && $comment->comment_approved == 1) { wp_update_comment_count($post_id); } clean_comment_cache($comment_id); do_action('wp_set_comment_status', $comment_id, 'delete'); wp_transition_comment_status('delete', $comment->comment_approved, $comment); return true; }
/** * Handles group's moderating actions about ideas * * @package WP Idea Stream * @subpackage buddypress/groups * * @since 2.0.0 * * @uses bp_is_group() to check a group is displayed * @uses bp_is_current_action() to check the group's current action * @uses wp_idea_stream_root_slug() to get the IdeaStream root slug * @uses wp_idea_stream_action_get_slug() to get the IdeaStream's action slug * @uses bp_action_variable() to get a specific action variable * @uses groups_get_current_group() to get the current group's object * @uses WP_Idea_Stream_Group->group_ideas_archive_url() to get the group's IdeaStream archive page * @uses check_admin_referer() to check the request was made on the site * @uses wp_idea_stream_user_can() to check user's capability * @uses WP_Idea_Stream_Group->remove_from_group() to remove one or more ideas from a group * @uses wp_get_referer() to get the url the user came from * @uses wp_spam_comment() to spam a comment made on an idea * @uses wp_trash_comment() to trash a comment made on an idea * @uses wp_idea_stream_add_message() to add a feedback to display to the user once redirected * @uses bp_core_redirect() to safely redirect the user */ public function group_actions() { if (!bp_is_group()) { return; } $group = groups_get_current_group(); // This part is to catch the group status before it might be updated if ('group-settings' == bp_get_group_current_admin_tab() && bp_is_item_admin()) { $this->group_update_ideas_stati = $group; if (!empty($_POST['group-status']) && in_array($_POST['group-status'], array('public', 'private', 'hidden'))) { $this->group_update_ideas_stati->new_status = $_POST['group-status']; } } // This part is for ideastream moderation actions. if (!(bp_is_current_action(wp_idea_stream_root_slug()) && wp_idea_stream_action_get_slug() == bp_action_variable(0) && bp_action_variable(1))) { return; } $feedback = array(); // Default to group's home $redirect = $this->group_ideas_archive_url($group, true); switch (bp_action_variable(1)) { case 'remove-idea': check_admin_referer('group-remove-idea'); if (!bp_action_variable(2)) { $feedback['type'] = 'error'; $feedback['content'] = __('Removing the idea failed.', 'wp-idea-stream'); break; } $idea_id = absint(bp_action_variable(2)); if (!wp_idea_stream_user_can('remove_group_ideas')) { $feedback['type'] = 'error'; $feedback['content'] = __('Removing the idea failed. You do not have the capability to remove ideas.', 'wp-idea-stream'); break; } if (false === $this->remove_from_group($idea_id, $group->id)) { $feedback['type'] = 'error'; $feedback['content'] = __('Removing the idea failed.', 'wp-idea-stream'); $redirect = wp_get_referer(); } else { $feedback['type'] = 'success'; $feedback['content'] = __('The idea was successfully removed.', 'wp-idea-stream'); } break; case 'spam-comment': check_admin_referer('group-spam-comment'); $redirect = wp_get_referer(); if (!bp_action_variable(2)) { $feedback['type'] = 'error'; $feedback['content'] = __('Spamming the comment failed.', 'wp-idea-stream'); break; } $comment_id = absint(bp_action_variable(2)); if (!wp_idea_stream_user_can('spam_group_idea_comments')) { $feedback['type'] = 'error'; $feedback['content'] = __('Spamming the comment failed. You do not have the capability to spam comments.', 'wp-idea-stream'); break; } if (false === wp_spam_comment($comment_id)) { $feedback['type'] = 'error'; $feedback['content'] = __('Spamming the comment failed.', 'wp-idea-stream'); } else { $feedback['type'] = 'success'; $feedback['content'] = __('The comment was successfully marked as spam.', 'wp-idea-stream'); } break; case 'trash-comment': check_admin_referer('group-trash-comment'); $redirect = wp_get_referer(); if (!bp_action_variable(2)) { $feedback['type'] = 'error'; $feedback['content'] = __('Deleting the comment failed.', 'wp-idea-stream'); break; } $comment_id = absint(bp_action_variable(2)); if (!wp_idea_stream_user_can('trash_group_idea_comments')) { $feedback['type'] = 'error'; $feedback['content'] = __('Deleting the comment failed. You do not have the capability to delete comments.', 'wp-idea-stream'); break; } if (false === wp_trash_comment($comment_id)) { $feedback['type'] = 'error'; $feedback['content'] = __('Deleting the comment failed.', 'wp-idea-stream'); } else { $feedback['type'] = 'success'; $feedback['content'] = __('The comment was successfully deleted.', 'wp-idea-stream'); } break; } if (!empty($feedback)) { wp_idea_stream_add_message($feedback); bp_core_redirect($redirect); } }
/** * Removes comment ID and maybe updates post comment count. * * The post comment count will be updated if the comment was approved and has a * post ID available. * * @since 2.0.0 * @uses $wpdb * @uses do_action() Calls 'delete_comment' hook on comment ID * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param int $comment_id Comment ID * @return bool False if delete comment query failure, true on success. */ function wp_delete_comment($comment_id) { global $wpdb; if (!($comment = get_comment($comment_id))) { return false; } if (wp_get_comment_status($comment_id) != 'trash' && wp_get_comment_status($comment_id) != 'spam' && EMPTY_TRASH_DAYS > 0) { return wp_trash_comment($comment_id); } do_action('delete_comment', $comment_id); // Move children up a level. $children = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent = %d", $comment_id)); if (!empty($children)) { $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id)); clean_comment_cache($children); } // Delete metadata $meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->commentmeta} WHERE comment_id = %d ", $comment_id)); if (!empty($meta_ids)) { do_action('delete_commentmeta', $meta_ids); $in_meta_ids = "'" . implode("', '", $meta_ids) . "'"; $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE meta_id IN ({$in_meta_ids})"); do_action('deleted_commentmeta', $meta_ids); } if (!$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->comments} WHERE comment_ID = %d LIMIT 1", $comment_id))) { return false; } do_action('deleted_comment', $comment_id); $post_id = $comment->comment_post_ID; if ($post_id && $comment->comment_approved == 1) { wp_update_comment_count($post_id); } clean_comment_cache($comment_id); do_action('wp_set_comment_status', $comment_id, 'delete'); wp_transition_comment_status('delete', $comment->comment_approved, $comment); return true; }
private function trash_comment($id) { return wp_trash_comment($id); }
/** * Set the comment_status of a given comment object when creating or updating a comment. * * @param string|int $new_status * @param object $comment * @return boolean $changed */ protected function handle_status_param($new_status, $comment) { $old_status = wp_get_comment_status($comment->comment_ID); if ($new_status === $old_status) { return false; } switch ($new_status) { case 'approved': case 'approve': case '1': $changed = wp_set_comment_status($comment->comment_ID, 'approve'); break; case 'hold': case '0': $changed = wp_set_comment_status($comment->comment_ID, 'hold'); break; case 'spam': $changed = wp_spam_comment($comment->comment_ID); break; case 'unspam': $changed = wp_unspam_comment($comment->comment_ID); break; case 'trash': $changed = wp_trash_comment($comment->comment_ID); break; case 'untrash': $changed = wp_untrash_comment($comment->comment_ID); break; default: $changed = false; break; } return $changed; }
switch ($action = $_POST['action']) { case 'delete-comment': // On success, die with time() instead of 1 if (!($comment = get_comment($id))) { die((string) time()); } if (!current_user_can('edit_post', $comment->comment_post_ID)) { die('-1'); } check_ajax_referer("delete-comment_{$id}"); $status = wp_get_comment_status($comment->comment_ID); if (isset($_POST['trash']) && 1 == $_POST['trash']) { if ('trash' == $status) { die((string) time()); } $r = wp_trash_comment($comment->comment_ID); } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) { if ('trash' != $status) { die((string) time()); } $r = wp_untrash_comment($comment->comment_ID); } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) { if ('spam' == $status) { die((string) time()); } $r = wp_spam_comment($comment->comment_ID); } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) { if ('spam' != $status) { die((string) time()); } $r = wp_unspam_comment($comment->comment_ID);
function bp_checkins_delete_place_comment($comment_id = false) { $message = false; if (empty($comment_id)) { return false; } if ($deleted = wp_trash_comment($comment_id)) { $message = array('message' => __('Comment has been successfully removed', 'bp-checkins'), 'type' => 'updated'); } else { $message = array('message' => __('Oops something went wrong, comment couldnot be removed', 'bp-checkins'), 'type' => 'error'); } return $message; }
function update_comment($path, $blog_id, $comment_id) { $comment = get_comment($comment_id); if (!$comment || is_wp_error($comment)) { return new WP_Error('unknown_comment', 'Unknown comment', 404); } if (!current_user_can('edit_comment', $comment->comment_ID)) { return new WP_Error('unauthorized', 'User cannot edit comment', 403); } $args = $this->query_args(); $input = $this->input(false); if (!is_array($input) || !$input) { return new WP_Error('invalid_input', 'Invalid request input', 400); } $update = array(); foreach ($input as $key => $value) { $update["comment_{$key}"] = $value; } $comment_status = wp_get_comment_status($comment->comment_ID); if ($comment_status !== $update['status'] && !current_user_can('moderate_comments')) { return new WP_Error('unauthorized', 'User cannot moderate comments', 403); } if (isset($update['comment_status'])) { switch ($update['comment_status']) { case 'approved': if ('approve' !== $comment_status) { wp_set_comment_status($comment->comment_ID, 'approve'); } break; case 'unapproved': if ('hold' !== $comment_status) { wp_set_comment_status($comment->comment_ID, 'hold'); } break; case 'spam': if ('spam' !== $comment_status) { wp_spam_comment($comment->comment_ID); } break; case 'unspam': if ('spam' === $comment_status) { wp_unspam_comment($comment->comment_ID); } break; case 'trash': if (!EMPTY_TRASH_DAYS) { return new WP_Error('trash_disabled', 'Cannot trash comment', 403); } if ('trash' !== $comment_status) { wp_trash_comment($comment_id); } break; case 'untrash': if ('trash' === $comment_status) { wp_untrash_comment($comment->comment_ID); } break; default: $update['comment_approved'] = 1; break; } unset($update['comment_status']); } if (!empty($update)) { $update['comment_ID'] = $comment->comment_ID; wp_update_comment(add_magic_quotes($update)); } $return = $this->get_comment($comment->comment_ID, $args['context']); if (!$return || is_wp_error($return)) { return $return; } do_action('wpcom_json_api_objects', 'comments'); return $return; }
public function test_untrash_should_invalidate_comment_cache() { global $wpdb; $c = self::factory()->comment->create(); wp_trash_comment($c); $comment = get_comment($c); $this->assertSame('trash', $comment->comment_approved); wp_untrash_comment($c); $comment = get_comment($c); $this->assertSame('1', $comment->comment_approved); }
function comment_bulk_action() { //Read form data $action = $_POST['action']; $commentIds = explode(',', $_POST['ids']); $information['success'] = 0; foreach ($commentIds as $commentId) { if ($commentId) { $information['success']++; if ('approve' === $action) { wp_set_comment_status($commentId, 'approve'); } else { if ('unapprove' === $action) { wp_set_comment_status($commentId, 'hold'); } else { if ('spam' === $action) { wp_spam_comment($commentId); } else { if ('unspam' === $action) { wp_unspam_comment($commentId); } else { if ('trash' === $action) { wp_trash_comment($commentId); } else { if ('restore' === $action) { wp_untrash_comment($commentId); } else { if ('delete' === $action) { wp_delete_comment($commentId, true); } else { $information['success']--; } } } } } } } } } MainWP_Helper::write($information); }
public function test_untrashed_comment_should_invalidate_query_cache() { global $wpdb; $c = self::factory()->comment->create(array('comment_post_ID' => self::$post_id, 'comment_approved' => '1')); wp_trash_comment($c); $q = new WP_Comment_Query(array('post_id' => self::$post_id, 'fields' => 'ids')); wp_untrash_comment($c); $num_queries = $wpdb->num_queries; $q = new WP_Comment_Query(array('post_id' => self::$post_id, 'fields' => 'ids')); $num_queries++; $this->assertSame($num_queries, $wpdb->num_queries); $this->assertEqualSets(array($c), $q->comments); }
/** * Trashes or deletes a comment. * * The comment is moved to trash instead of permanently deleted unless trash is * disabled, item is already in the trash, or $force_delete is true. * * The post comment count will be updated if the comment was approved and has a * post ID available. * * @since 2.0.0 * @uses $wpdb * @uses do_action() Calls 'delete_comment' hook on comment ID * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param int $comment_id Comment ID * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. * @return bool True on success, false on failure. */ function wp_delete_comment($comment_id, $force_delete = false) { global $wpdb; if (!($comment = get_comment($comment_id))) { return false; } if (!$force_delete && EMPTY_TRASH_DAYS && !in_array(wp_get_comment_status($comment_id), array('trash', 'spam'))) { return wp_trash_comment($comment_id); } do_action('delete_comment', $comment_id); // Move children up a level. $children = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent = %d", $comment_id)); if (!empty($children)) { $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id)); clean_comment_cache($children); } // Delete metadata $meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->commentmeta} WHERE comment_id = %d", $comment_id)); foreach ($meta_ids as $mid) { delete_metadata_by_mid('comment', $mid); } if (!$wpdb->delete($wpdb->comments, array('comment_ID' => $comment_id))) { return false; } do_action('deleted_comment', $comment_id); $post_id = $comment->comment_post_ID; if ($post_id && $comment->comment_approved == 1) { wp_update_comment_count($post_id); } clean_comment_cache($comment_id); do_action('wp_set_comment_status', $comment_id, 'delete'); wp_transition_comment_status('delete', $comment->comment_approved, $comment); return true; }
public function deletePost($postIdArray) { global $wpdb; $commentIdArray = $wpdb->get_col("SELECT comment_ID FROM {$wpdb->commentmeta} WHERE meta_key = 'duoshuo_post_id' AND meta_value IN ('" . implode("', '", $postIdArray) . "')"); if (count($commentIdArray)) { $commentIdArray = $wpdb->get_col("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_ID IN ('" . implode("', '", $commentIdArray) . "')"); foreach ($commentIdArray as $commentId) { wp_trash_comment($commentId); } } return array(); }
elseif ( '' != wp_get_original_referer() && ! $noredir ) $redir = wp_get_original_referer(); elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ) ) ) $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_id ); $redir = add_query_arg( array('deleted' => '1'), $redir ); break; case 'trashcomment' : wp_trash_comment($comment_id); $redir = add_query_arg( array('trashed' => '1', 'ids' => $comment_id), $redir ); break; case 'untrashcomment' : wp_untrash_comment($comment_id); $redir = add_query_arg( array('untrashed' => '1'), $redir ); break; case 'spamcomment' : wp_spam_comment($comment_id); $redir = add_query_arg( array('spammed' => '1', 'ids' => $comment_id), $redir ); break; case 'unspamcomment' : wp_unspam_comment($comment_id); $redir = add_query_arg( array('unspammed' => '1'), $redir ); break; case 'approvecomment' :
/** * Handle a few different variations of writing a comment. */ public static function write_comment($message, $method) { switch ($method) { case 'delete': // @todo comment delete // @todo capability check self::die_failure('delete_comment_not_supported', __('Deleting comments is not supported yet', 'o2')); case 'update': // Update an existing comment add_filter('map_meta_cap', array('o2_Write_API', 'restrict_comment_editing'), 10, 4); if (!current_user_can('edit_comment', $message->id)) { self::die_failure('cannot_edit_comment', __('You are not allowed to edit this comment', 'o2')); } remove_filter('map_meta_cap', array('o2_Write_API', 'restrict_comment_editing'), 10, 4); // Get current comment data $comment_status = wp_get_comment_status($message->id); // Assume that if comment_status is false, that the comment has been deleted. if (false == $comment_status) { self::die_failure('comment_already_deleted', __('Comment has already been deleted by another session.', 'o2')); } else { if ('trash' != $comment_status && $message->isTrashed) { if (!wp_trash_comment($message->id)) { self::die_failure('trash_comment_failed', __('Trashing that comment failed.', 'o2')); } do_action('o2_writeapi_comment_trashed', $message->id); } else { if ('trash' == $comment_status && !$message->isTrashed) { if (!wp_untrash_comment($message->id)) { self::die_failure('untrash_comment_failed', __('Untrashing that comment failed.', 'o2')); } do_action('o2_writeapi_comment_untrashed', $message->id); } else { // Load comment data, merge in new stuff, then save again $comment = get_comment($message->id); $comment->comment_content = addslashes($message->contentRaw); wp_update_comment((array) $comment); // Modifying trash status is bumped in o2:bump_trashed_comment based on trash actions. o2_Fragment::bump_comment_modified_time($message->id); do_action('o2_writeapi_comment_updated', $message->id); } } } // Reload the full, clean object and output it $comment = get_comment($message->id); self::die_success(o2_Fragment::get_fragment($comment)); case 'create': // Posting a new comment. We use the core-provided file to // avoid re-implementing a bunch of logic // Re-map some incoming data to match the expected _POST elements $remap = array('comment_post_ID' => $message->postID, 'comment' => addslashes($message->contentRaw), 'comment_parent' => $message->parentID); $_POST = array_merge($_POST, $remap); // Let the core comment handler take it from here // Have to suppress warnings from wp-comments-post because ABSPATH gets redefined global $wpdb, $user_ID; @(require_once ABSPATH . 'wp-comments-post.php'); // If we get here, it means the core actions weren't fired, so // something most likely went wrong self::die_failure('unknown_comment_error', __('Failed to post comment.', 'o2')); } }
/** * Ajax handler for deleting a comment. * * @since 3.1.0 */ function wp_ajax_delete_comment() { $id = isset($_POST['id']) ? (int) $_POST['id'] : 0; if (!($comment = get_comment($id))) { wp_die(time()); } if (!current_user_can('edit_comment', $comment->comment_ID)) { wp_die(-1); } check_ajax_referer("delete-comment_{$id}"); $status = wp_get_comment_status($comment); $delta = -1; if (isset($_POST['trash']) && 1 == $_POST['trash']) { if ('trash' == $status) { wp_die(time()); } $r = wp_trash_comment($comment); } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) { if ('trash' != $status) { wp_die(time()); } $r = wp_untrash_comment($comment); if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'trash') { // undo trash, not in trash $delta = 1; } } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) { if ('spam' == $status) { wp_die(time()); } $r = wp_spam_comment($comment); } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) { if ('spam' != $status) { wp_die(time()); } $r = wp_unspam_comment($comment); if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') { // undo spam, not in spam $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); }
/** * approve/unapprove/spam/unspam a comment via an admin-ajax.php endpoint */ public function ajax_comment_status() { $comment_id = absint($_GET['comment_id']); $direction = $_GET['direction']; if (!current_user_can('edit_comment', $comment_id)) { return wp_send_json_error(); } // END if if (!check_ajax_referer('bsocial-comment-status', 'bsocial-nonce')) { return wp_send_json_error(); } // END if $allowed_directions = array('approve', 'unapprove', 'spam', 'unspam', 'trash', 'untrash'); if (!in_array($direction, $allowed_directions)) { return wp_send_json_error(); } // END if if (!($comment = get_comment($comment_id))) { return wp_send_json_error(); } //end if $data = array(); switch ($direction) { case 'approve': $data = array('success' => wp_set_comment_status($comment->comment_ID, 'approve'), 'link' => $this->get_status_link($comment->comment_ID, 'approve'), 'state' => 'approved'); break; case 'unapprove': $data = array('success' => wp_set_comment_status($comment->comment_ID, 'hold'), 'link' => $this->get_status_link($comment->comment_ID, 'approve'), 'state' => 'unapproved'); break; case 'spam': $data = array('success' => wp_spam_comment($comment->comment_ID), 'link' => $this->get_status_link($comment->comment_ID, 'spam'), 'state' => 'spammed'); break; case 'unspam': $data = array('success' => wp_unspam_comment($comment->comment_ID), 'link' => $this->get_status_link($comment->comment_ID, 'spam'), 'state' => 'unspammed'); break; case 'trash': $data = array('success' => wp_trash_comment($comment->comment_ID), 'link' => $this->get_status_link($comment->comment_ID, 'trash'), 'state' => 'trashed'); break; case 'untrash': $data = array('success' => wp_untrash_comment($comment->comment_ID), 'link' => $this->get_status_link($comment->comment_ID, 'trash'), 'state' => 'untrashed'); break; } // END switch wp_send_json($data); die; }