/** * @ticket 33154 */ function test_editor_can_edit_orphan_comments() { global $wpdb; // Become an editor $this->_setRole('editor'); // Get a comment $comments = get_comments(array('post_id' => $this->_comment_post->ID)); $comment = array_pop($comments); // Manually update the comment_post_ID, because wp_update_comment() will prevent it. $wpdb->query("UPDATE {$wpdb->comments} SET comment_post_ID=0 WHERE comment_ID={$comment->comment_ID}"); clean_comment_cache($comment->comment_ID); // Set up a default request $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce('replyto-comment'); $_POST['comment_ID'] = $comment->comment_ID; $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; // Make the request try { $this->_handleAjax('edit-comment'); } catch (WPAjaxDieContinueException $e) { unset($e); } // Get the response $xml = simplexml_load_string($this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA); // Check the meta data $this->assertEquals(-1, (string) $xml->response[0]->edit_comment['position']); $this->assertEquals($comment->comment_ID, (string) $xml->response[0]->edit_comment['id']); $this->assertEquals('edit-comment_' . $comment->comment_ID, (string) $xml->response['action']); // Check the payload $this->assertNotEmpty((string) $xml->response[0]->edit_comment[0]->response_data); // And supplemental is empty $this->assertEmpty((string) $xml->response[0]->edit_comment[0]->supplemental); }
public static function wpSetUpBeforeClass($factory) { global $wpdb; // Ensure that there is a comment with ID 1. $comment_1 = WP_Comment::get_instance(1); if (!$comment_1) { $wpdb->insert($wpdb->comments, array('comment_ID' => 1)); clean_comment_cache(1); } self::$comment_id = self::factory()->comment->create(); }
public static function comment_edited($commentID = 0, $postID = 0) { //Clear the comment cache if (function_exists('clean_comment_cache')) { clean_comment_cache($commentID); } //For WP Cache and WP Super Cache if (function_exists('wp_cache_post_change')) { @wp_cache_post_change($postID); } //Get out if user is admin or post owner if (AECCore::is_comment_owner($postID)) { return; } //Increment the number of edited comments AECCore::increment_edit_count(); }
/** * @ticket 16894 */ public function test_update_comment_meta_cache_true() { global $wpdb; $p = $this->factory->post->create(array('post_status' => 'publish')); $comment_ids = $this->factory->comment->create_post_comments($p, 3); foreach ($comment_ids as $cid) { update_comment_meta($cid, 'foo', 'bar'); } // Clear comment cache, just in case. clean_comment_cache($comment_ids); $q = new WP_Comment_Query(array('post_ID' => $p, 'update_comment_meta_cache' => true)); $num_queries = $wpdb->num_queries; foreach ($comment_ids as $cid) { get_comment_meta($cid, 'foo', 'bar'); } $this->assertSame($num_queries, $wpdb->num_queries); }
function akismet_delete_old() { global $wpdb; $now_gmt = current_time('mysql', 1); $comment_ids = $wpdb->get_col("SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB('{$now_gmt}', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'"); if (empty($comment_ids)) { return; } $comma_comment_ids = implode(', ', array_map('intval', $comment_ids)); do_action('delete_comment', $comment_ids); $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( {$comma_comment_ids} )"); $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( {$comma_comment_ids} )"); clean_comment_cache($comment_ids); $n = mt_rand(1, 5000); if (apply_filters('akismet_optimize_table', $n == 11)) { // lucky number $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}"); } }
function download_comment_bodies() { global $wpdb; $cookie = $this->get_session(); if (is_wp_error($cookie)) { return $cookie; } // Load previous state (if any) $this->usermap = (array) get_option('ljapi_usermap'); $maxid = get_option('ljapi_maxid') ? (int) get_option('ljapi_maxid') : 1; $highest_id = (int) get_option('ljapi_highest_comment_id'); $loop = 0; while ($maxid > $highest_id && $loop < 5) { // We do 5 loops per call to avoid memory limits $loop++; // Get a batch of comments, using the highest_id we've already got as a starting point $results = wp_remote_get($this->comments_url . '?get=comment_body&startid=' . ($highest_id + 1), array('cookies' => array($cookie), 'timeout' => 20)); if (is_wp_error($results)) { return new WP_Error('comment_bodies', __('Failed to retrieve comment bodies from LiveJournal. Please try again soon.')); } $results = wp_remote_retrieve_body($results); // Parse out each comment and insert directly preg_match_all('|<comment id=\'(\\d+)\'.*</comment>|iUs', $results, $matches); for ($c = 0; $c < count($matches[0]); $c++) { // Keep track of highest id seen if ($matches[1][$c] > $highest_id) { $highest_id = $matches[1][$c]; update_option('ljapi_highest_comment_id', $highest_id); } $comment = $matches[0][$c]; // Filter out any captured, deleted comments (nothing useful to import) $comment = preg_replace('|<comment id=\'\\d+\' jitemid=\'\\d+\' posterid=\'\\d+\' state=\'D\'[^/]*/>|is', '', $comment); // Parse this comment into an array and insert $comment = $this->parse_comment($comment); $comment = wp_filter_comment($comment); $id = wp_insert_comment($comment); // Clear cache clean_comment_cache($id); } // Clear cache to preseve memory wp_cache_flush(); } // endwhile - all comments downloaded and ready for bulk processing // Counter just used to show progress to user update_option('ljapi_comment_batch', (int) get_option('ljapi_comment_batch') + 1); return true; }
private function duplicate_comments($master_post_id, $translated_id) { global $sitepress; remove_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10); $comments_on_master = get_comments(array('post_id' => $master_post_id)); $comments_on_translation = get_comments(array('post_id' => $translated_id, 'status' => 'any')); add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2); foreach ($comments_on_translation as $comment) { wp_delete_comment($comment->comment_ID, true); clean_comment_cache($comment->comment_ID); } $iclTranslationManagement = wpml_load_core_tm(); foreach ($comments_on_master as $comment) { $iclTranslationManagement->duplication_insert_comment($comment->comment_ID); clean_comment_cache($comment->comment_ID); } wp_update_comment_count_now($master_post_id); wp_update_comment_count_now($translated_id); }
public static function delete_old_comments() { global $wpdb; while ($comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", defined('AKISMET_DELETE_LIMIT') ? AKISMET_DELETE_LIMIT : 10000))) { if (empty($comment_ids)) { return; } $wpdb->queries = array(); do_action('delete_comment', $comment_ids); $comma_comment_ids = implode(', ', array_map('intval', $comment_ids)); $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( {$comma_comment_ids} )"); $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( {$comma_comment_ids} )"); clean_comment_cache($comment_ids); } if (apply_filters('akismet_optimize_table', mt_rand(1, 5000) == 11, $wpdb->comments)) { // lucky number $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}"); } }
/** * @ticket 34138 */ public function test_comment_objects_should_be_filled_from_cache() { global $wpdb; $comments = self::factory()->comment->create_many(3, array('comment_post_ID' => self::$post_id)); clean_comment_cache($comments); $num_queries = $wpdb->num_queries; $q = new WP_Comment_Query(array('post_id' => self::$post_id, 'no_found_rows' => true, 'update_comment_post_cache' => false, 'update_comment_meta_cache' => false)); // 2 queries should have been fired: one for IDs, one to prime comment caches. $num_queries += 2; $found = wp_list_pluck($q->comments, 'comment_ID'); $this->assertEqualSets($comments, $found); $this->assertSame($num_queries, $wpdb->num_queries); }
/** * Ask not for whom the bell tolls, * it tolls for thee, Spam... * This routine is intended to be called by the scheduled event. * * @author Brian Layman <*****@*****.**> * @since 1.4 */ public function apply_cleanout() { // error_log( "Apply Cleanout Executing" ); // This routine is originally lifted from Akismet. Adjusted to limit the initial deletes. global $wpdb; $now_gmt = current_time('mysql', 1); $sql = "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB('{$now_gmt}', INTERVAL " . $this->spam_days . " DAY) > comment_date_gmt AND comment_approved = 'spam' limit 0," . $this->spam_delete_limit; // error_log( $sql ); $comment_ids = $wpdb->get_col($sql); if (empty($comment_ids)) { return; } $comma_comment_ids = implode(', ', array_map('intval', $comment_ids)); do_action('delete_comment', $comment_ids); // error_log( "Deleted comments: " . count( $comment_ids ) ); if (count($comment_ids) >= $this->spam_delete_limit) { $this->schedule_single_deletion_event(); } $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( {$comma_comment_ids} )"); // Note these have passed throught intval $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( {$comma_comment_ids} )"); // Note these have passed throught intval clean_comment_cache($comment_ids); }
/** * Restore comments for a post from the trash. * * @since 2.9.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. * @return true|void */ function wp_untrash_post_comments($post = null) { global $wpdb; $post = get_post($post); if (empty($post)) { return; } $post_id = $post->ID; $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); if (empty($statuses)) { return true; } /** * Fires before comments are restored for a post from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action('untrash_post_comments', $post_id); // Restore each comment to its original status. $group_by_status = array(); foreach ($statuses as $comment_id => $comment_status) { $group_by_status[$comment_status][] = $comment_id; } foreach ($group_by_status as $status => $comments) { // Sanity check. This shouldn't happen. if ('post-trashed' == $status) { $status = '0'; } $comments_in = implode(', ', array_map('intval', $comments)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} SET comment_approved = %s WHERE comment_ID IN ({$comments_in})", $status)); } clean_comment_cache(array_keys($statuses)); delete_post_meta($post_id, '_wp_trash_meta_comments_status'); /** * Fires after comments are restored for a post from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action('untrashed_post_comments', $post_id); }
/** * wp_update_comment() - Parses and updates an existing comment in the database * * {@internal Missing Long Description}} * * @since 2.0.0 * @uses $wpdb * * @param array $commentarr Contains information on the comment * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ((array) $comment as $key => $value) { $comment[$key] = $wpdb->escape($value); } // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract($commentarr, EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); $wpdb->query("UPDATE {$wpdb->comments} SET\n\t\t\tcomment_content = '{$comment_content}',\n\t\t\tcomment_author = '{$comment_author}',\n\t\t\tcomment_author_email = '{$comment_author_email}',\n\t\t\tcomment_approved = '{$comment_approved}',\n\t\t\tcomment_author_url = '{$comment_author_url}',\n\t\t\tcomment_date = '{$comment_date}',\n\t\t\tcomment_date_gmt = '{$comment_date_gmt}'\n\t\tWHERE comment_ID = {$comment_ID}"); $rval = $wpdb->rows_affected; clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); return $rval; }
/** * Hook to wp_set_comment_status action and correctly update the status if necessary * * @param $comment_id (int) The id of the comment */ public function wp_set_comment_status($comment_id) { // Make sure we are working with a comment we have data for if (!isset($this->current_comment->status) || $this->current_comment->comment_ID != $comment_id) { return; } // END if $comment_status = $this->current_comment->status; // Set $this->current_comment back to NULL now that we're finished $this->current_comment = NULL; global $wpdb; // Update comment status with the CORRECT value for this comment $wpdb->update($wpdb->comments, array('comment_approved' => $comment_status), array('comment_ID' => $comment_id)); // We've changed stuff so the cache needs to be cleared again clean_comment_cache($comment_id); // The rest of this is aping default WP behavior to make sure things fire correctly based on the status change do_action('wp_set_comment_status', $comment_id, $comment_status); $comment = get_comment($comment_id); $comment_old = clone get_comment($comment_id); wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment); wp_update_comment_count($comment->comment_post_ID); }
/** * Create a new comment with message "This comment has been permanently deleted." * and put it in place of deleted comment. * * @param $comment_ID */ public function delete_comment_override($comment_ID) { $children = get_comments(array('status' => 'approve', 'parent' => $comment_ID)); if (!empty($children)) { $old_comment = get_comment($comment_ID); $comment_to_add = $old_comment; unset($comment_to_add->comment_ID); $comment_to_add->comment_approved = 1; $comment_to_add->comment_content = __('This comment was deleted.', 'o2'); $new_comment_id = wp_insert_comment((array) $comment_to_add); $comment_created = get_comment_meta($comment_ID, 'o2_comment_created', true); if (empty($comment_created)) { $comment = get_comment($comment_ID); $comment_created = strtotime($comment->comment_date_gmt); } update_comment_meta($new_comment_id, 'o2_comment_created', $comment_created); o2_Fragment::bump_comment_modified_time($new_comment_id); update_comment_meta($new_comment_id, 'o2_comment_prev_deleted', $comment_ID); $clean_comment_ids = array(); foreach ($children as $child) { $clean_comment_ids[] = $child->comment_ID; $child->comment_parent = $new_comment_id; wp_update_comment((array) $child); } $clean_comment_ids[] = $new_comment_id; clean_comment_cache($clean_comment_ids); } }
public static function delete_old_comments() { global $wpdb; /** * Determines how many comments will be deleted in each batch. * * @param int The default, as defined by AKISMET_DELETE_LIMIT. */ $delete_limit = apply_filters('akismet_delete_comment_limit', defined('AKISMET_DELETE_LIMIT') ? AKISMET_DELETE_LIMIT : 10000); $delete_limit = max(1, intval($delete_limit)); /** * Determines how many days a comment will be left in the Spam queue before being deleted. * * @param int The default number of days. */ $delete_interval = apply_filters('akismet_delete_comment_interval', 15); $delete_interval = max(1, intval($delete_interval)); while ($comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL %d DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", $delete_interval, $delete_limit))) { if (empty($comment_ids)) { return; } $wpdb->queries = array(); foreach ($comment_ids as $comment_id) { do_action('delete_comment', $comment_id); } $comma_comment_ids = implode(', ', array_map('intval', $comment_ids)); $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( {$comma_comment_ids} )"); $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( {$comma_comment_ids} )"); clean_comment_cache($comment_ids); } if (apply_filters('akismet_optimize_table', mt_rand(1, 5000) == 11, $wpdb->comments)) { // lucky number $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}"); } }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ((array) $comment as $key => $value) { $comment[$key] = $wpdb->escape($value); } // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract(stripslashes_deep($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} SET\n\t\t\tcomment_content = %s,\n\t\t\tcomment_author = %s,\n\t\t\tcomment_author_email = %s,\n\t\t\tcomment_approved = %s,\n\t\t\tcomment_author_url = %s,\n\t\t\tcomment_date = %s,\n\t\t\tcomment_date_gmt = %s\n\t\tWHERE comment_ID = %d", $comment_content, $comment_author, $comment_author_email, $comment_approved, $comment_author_url, $comment_date, $comment_date_gmt, $comment_ID)); $rval = $wpdb->rows_affected; clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment_approved, $comment->comment_approved, $comment); return $rval; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); if (empty($comment)) { return 0; } // Make sure that the comment post ID is valid (if specified). if (isset($commentarr['comment_post_ID']) && !get_post($commentarr['comment_post_ID'])) { return 0; } // Escape data pulled from DB. $comment = wp_slash($comment); $old_status = $comment['comment_approved']; // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. $data = wp_unslash($commentarr); /** * Filter the comment content before it is updated in the database. * * @since 1.5.0 * * @param string $comment_content The comment data. */ $data['comment_content'] = apply_filters('comment_save_pre', $data['comment_content']); $data['comment_date_gmt'] = get_gmt_from_date($data['comment_date']); if (!isset($data['comment_approved'])) { $data['comment_approved'] = 1; } elseif ('hold' == $data['comment_approved']) { $data['comment_approved'] = 0; } elseif ('approve' == $data['comment_approved']) { $data['comment_approved'] = 1; } $comment_ID = $data['comment_ID']; $comment_post_ID = $data['comment_post_ID']; $keys = array('comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id'); $data = wp_array_slice_assoc($data, $keys); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); /** * Fires immediately after a comment is updated in the database. * * The hook also fires immediately before comment status transition hooks are fired. * * @since 1.2.0 * * @param int $comment_ID The comment ID. */ do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); if (empty($comment)) { return 0; } // Escape data pulled from DB. $comment = wp_slash($comment); $old_status = $comment['comment_approved']; // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract(wp_unslash($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent'); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
/** * Restore comments for a post from the trash * * @since 2.9.0 * @uses do_action() on 'untrash_post_comments' before trashing * @uses do_action() on 'untrashed_post_comments' after trashing * * @param int $post Post ID or object. * @return mixed False on failure */ function wp_untrash_post_comments($post = null) { global $wpdb; $post = get_post($post); if (empty($post)) { return; } $post_id = $post->ID; $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); if (empty($statuses)) { return true; } do_action('untrash_post_comments', $post_id); // Restore each comment to its original status $group_by_status = array(); foreach ($statuses as $comment_id => $comment_status) { $group_by_status[$comment_status][] = $comment_id; } foreach ($group_by_status as $status => $comments) { // Sanity check. This shouldn't happen. if ('post-trashed' == $status) { $status = '0'; } $comments_in = implode("', '", $comments); $wpdb->query("UPDATE {$wpdb->comments} SET comment_approved = '{$status}' WHERE comment_ID IN ('" . $comments_in . "')"); } clean_comment_cache(array_keys($statuses)); delete_post_meta($post_id, '_wp_trash_meta_comments_status'); do_action('untrashed_post_comments', $post_id); }
public function saveContent($content) { global $wpdb; if ($content == $this->comment->comment_content) { return true; } else { $this->comment->comment_content = $content; $rval = $wpdb->update($wpdb->comments, (array) $this->comment, array('comment_ID' => $this->getId())); clean_comment_cache($this->getId()); wp_update_comment_count($this->getThreadId()); return $rval; } }