/** * Decrease the total reply count of a topic by one. * * @since 2.6.0 bbPress (r6036) * * @param int $topic_id The topic id. * * @uses bbp_is_reply() To check if the passed topic id is a reply * @uses bbp_get_reply_topic_id() To get the replies topic id * @uses bbp_bump_topic_reply_count() To bump the topic reply count * * @return void */ function bbp_decrease_topic_reply_count($topic_id = 0) { // Bail early if no id is passed. if (empty($topic_id)) { return; } // If it's a reply, get the topic id. if (bbp_is_reply($topic_id)) { $topic_id = bbp_get_reply_topic_id($topic_id); } bbp_bump_topic_reply_count($topic_id, -1); }
/** * @covers ::bbp_bump_topic_reply_count */ public function test_bbp_bump_topic_reply_count() { $t = $this->factory->topic->create(); $count = bbp_get_topic_reply_count($t); $this->assertSame('0', $count); $count = bbp_bump_topic_reply_count($t); $this->assertSame(1, $count); $count = bbp_bump_topic_reply_count($t, 3); $count = bbp_get_topic_reply_count($t); $this->assertSame('4', $count); }
/** * Create bbPress replies with existing comments on a post * * @author javiarques * @param int $post_id ID of the post whose comments to import to topic * @param int $topic_id ID of the topic where comments will be added as replies */ function bbppt_import_comments($post_id, $topic_id) { $topic_forum = bbp_get_topic_forum_id($topic_id); /** getting post comments */ $post_comments = get_comments(array('post_id' => $post_id, 'order' => 'ASC')); $imported_comment_count = 0; if ($post_comments) { foreach ($post_comments as $post_comment) { if (!empty($post_comment->comment_type)) { continue; } /** Allow individual comments to be skipped with `bbppt_do_import_comment` filter * By default, skip comments that have already been imported */ if (!apply_filters('bbppt_do_import_comment', !get_comment_meta($post_comment->comment_ID, 'bbppt_imported', true), $post_comment)) { continue; } // If user is not registered if (empty($post_comment->user_id)) { // 1. Check if user exists by email if (!empty($post_comment->comment_author_email)) { $existing_user = get_user_by('email', $post_comment->comment_author_email); if ($existing_user) { $post_comment->user_id = $existing_user->ID; } } } // Reply data $reply_data = array('post_parent' => $topic_id, 'post_status' => bbp_get_public_status_id(), 'post_type' => bbp_get_reply_post_type(), 'post_author' => $post_comment->user_id, 'post_content' => apply_filters('bbppt_imported_comment_content', $post_comment->comment_content, $post_comment), 'post_date' => $post_comment->comment_date, 'post_date_gmt' => $post_comment->comment_date_gmt, 'post_modified' => $post_comment->comment_date, 'post_modified_gmt' => $post_comment->comment_date_gmt); // Reply meta $reply_meta = array('author_ip' => $post_comment->comment_author_IP, 'forum_id' => $topic_forum, 'topic_id' => $topic_id, 'imported_comment_id' => $post_comment->comment_ID); // If not registered user, add anonymous user information if (empty($post_comment->user_id)) { // Parse args $anonymous = array('anonymous_name' => $post_comment->comment_author, 'anonymous_email' => $post_comment->comment_author_email, 'anonymous_website' => $post_comment->comment_author_url); $reply_meta = wp_parse_args($reply_meta, $anonymous); } $reply_id = bbp_insert_reply($reply_data, $reply_meta); update_comment_meta($post_comment->comment_ID, 'bbppt_imported', true); do_action('bbppt_comment_imported', $post_comment, $post_id, $topic_id); $imported_comment_count++; } // Update the reply count meta value to reflect imported comments bbp_bump_topic_reply_count($topic_id, $imported_comment_count - 1); } }