public function test_using_filter_adjusts_comment_count_without_an_additional_database_query() { global $wpdb; add_filter('pre_wp_update_comment_count_now', array($this, '_return_100')); $post_id = self::factory()->post->create(); self::factory()->comment->create_post_comments($post_id, 1); $this->assertSame('100', get_comments_number($post_id)); $num_queries = $wpdb->num_queries; $this->assertTrue(wp_update_comment_count_now($post_id)); // Only one query is made instead of two. $this->assertSame($num_queries + 1, $wpdb->num_queries); $this->assertSame('100', get_comments_number($post_id)); remove_filter('pre_wp_update_comment_count_now', array($this, '_return_100')); }
public function insert_duplicated_comment($comment, $dup_id, $original_cid) { global $wpdb, $iclTranslationManagement; $dup_comment_id = $this->duplicate_exists($dup_id, $original_cid); remove_action('wp_insert_comment', array($iclTranslationManagement, 'duplication_insert_comment'), 100); if ($dup_comment_id) { $comment['comment_ID'] = $dup_comment_id; wp_update_comment($comment); } else { $wpdb->insert($wpdb->comments, $comment); $dup_comment_id = $wpdb->insert_id; } add_action('wp_insert_comment', array($iclTranslationManagement, 'duplication_insert_comment'), 100); update_comment_meta($dup_comment_id, '_icl_duplicate_of', $original_cid); // comment meta $meta = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->commentmeta} WHERE comment_id=%d", $original_cid)); foreach ($meta as $meta_row) { $wpdb->insert($wpdb->commentmeta, array('comment_id' => $dup_comment_id, 'meta_key' => $meta_row->meta_key, 'meta_value' => $meta_row->meta_value), array('%d', '%s', '%s')); } wp_update_comment_count_now($dup_id); }
/** * Updates the comment count for post(s). * * When $do_deferred is false (is by default) and the comments have been set to * be deferred, the post_id will be added to a queue, which will be updated at a * later date and only updated once per post ID. * * If the comments have not be set up to be deferred, then the post will be * updated. When $do_deferred is set to true, then all previous deferred post * IDs will be updated along with the current $post_id. * * @since 2.1.0 * @see wp_update_comment_count_now() For what could cause a false return value * * @staticvar array $_deferred * * @param int $post_id Post ID * @param bool $do_deferred Whether to process previously deferred post comment counts * @return bool|void True on success, false on failure */ function wp_update_comment_count($post_id, $do_deferred = false) { static $_deferred = array(); if ($do_deferred) { $_deferred = array_unique($_deferred); foreach ($_deferred as $i => $_post_id) { wp_update_comment_count_now($_post_id); unset($_deferred[$i]); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ } } if (wp_defer_comment_counting()) { $_deferred[] = $post_id; return true; } elseif ($post_id) { return wp_update_comment_count_now($post_id); } }
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); }
/** * Proceed post comments * * @param int $postId * @param array $comments */ public function proceedPostComments($postId, $comments) { $addedCommentsCount = 0; foreach ($comments['data'] as $comment) { $commentId = WpAdaptor::addPostComment($postId, $comment); if ($commentId) { $addedCommentsCount++; } } if ($addedCommentsCount) { wp_update_comment_count_now($postId); echo 'Added ', $addedCommentsCount, ' comments for post ', $postId, "\n"; } if (isset($comments['paging']['next'])) { $queueData = array('postId' => $postId, 'page' => $comments['paging']['next']); $this->queue->publish(\Plugin\Queue::QUEUE_COMMENTS_CRAWL, $queueData); echo 'Published crawl comments page for post ', $postId, "\n"; } }
public static function move_comment($commentID = 0, $oldPostID = 0, $newPostID = 0) { global $wpdb, $post; //Return if not comment owner and if posts are the same if ($oldPostID == $newPostID || !AECCore::is_comment_owner($newPostID)) { return array('nochange' => true); } //Update the comment with the new post number $wpdb->update($wpdb->comments, array('comment_post_ID' => intval($newPostID)), array('comment_ID' => intval($commentID))); //Update the posts' comment count @wp_update_comment_count_now(intval($newPostID)); @wp_update_comment_count_now(intval($oldPostID)); $posts = get_posts("include={$oldPostID},{$newPostID}"); $response_arr = array(); foreach ($posts as $post) { if ($post->ID == $oldPostID) { $response_arr['old_post'] = array('new_id' => $newPostID, 'old_id' => $oldPostID, 'title' => esc_js(get_the_title($oldPostID)), 'permalink' => apply_filters('the_permalink', get_permalink($oldPostID)), 'comments' => intval($post->comment_count)); } else { $response_arr['new_post'] = array('new_id' => $newPostID, 'old_id' => $oldPostID, 'title' => esc_js(get_the_title($newPostID)), 'permalink' => apply_filters('the_permalink', get_permalink($newPostID)), 'comments' => intval($post->comment_count)); } //end if } //end foreeach return $response_arr; }