/** * Recount forum topics * * @since 2.0.0 bbPress (r2613) * * @uses wpdb::query() To run our recount sql queries * @uses is_wp_error() To check if the executed query returned {@link WP_Error} * @uses bbp_get_forum_post_type() To get the forum post type * @uses get_posts() To get the forums * @uses bbp_update_forum_topic_count() To update the forum topic count * @return array An array of the status code and the message */ function bbp_admin_repair_forum_topic_count() { // Define variables $bbp_db = bbp_db(); $statement = __('Counting the number of topics in each forum… %s', 'bbpress'); $result = __('Failed!', 'bbpress'); $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' );"; if (is_wp_error($bbp_db->query($sql_delete))) { return array(1, sprintf($statement, $result)); } $forums = get_posts(array('post_type' => bbp_get_forum_post_type(), 'numberposts' => -1)); if (!empty($forums)) { foreach ($forums as $forum) { bbp_update_forum_topic_count($forum->ID); bbp_update_forum_topic_count_hidden($forum->ID); } } else { return array(2, sprintf($statement, $result)); } return array(0, sprintf($statement, __('Complete!', 'bbpress'))); }
/** * Updates the counts of a forum. * * This calls a few internal functions that all run manual queries against the * database to get their results. As such, this function can be costly to run * but is necessary to keep everything accurate. * * @since bbPress (r2908) * * @param mixed $args Supports these arguments: * - forum_id: Forum id * - last_topic_id: Last topic id * - last_reply_id: Last reply id * - last_active_id: Last active post id * - last_active_time: last active time * @uses bbp_update_forum_last_topic_id() To update the forum last topic id * @uses bbp_update_forum_last_reply_id() To update the forum last reply id * @uses bbp_update_forum_last_active_id() To update the last active post id * @uses get_post_field() To get the post date of the last active id * @uses bbp_update_forum_last_active_time() To update the last active time * @uses bbp_update_forum_subforum_count() To update the subforum count * @uses bbp_update_forum_topic_count() To update the forum topic count * @uses bbp_update_forum_reply_count() To update the forum reply count * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count */ function bbp_update_forum($args = '') { // Parse arguments against default values $r = bbp_parse_args($args, array('forum_id' => 0, 'post_parent' => 0, 'last_topic_id' => 0, 'last_reply_id' => 0, 'last_active_id' => 0, 'last_active_time' => 0, 'last_active_status' => bbp_get_public_status_id()), 'update_forum'); // Last topic and reply ID's bbp_update_forum_last_topic_id($r['forum_id'], $r['last_topic_id']); bbp_update_forum_last_reply_id($r['forum_id'], $r['last_reply_id']); // Active dance $r['last_active_id'] = bbp_update_forum_last_active_id($r['forum_id'], $r['last_active_id']); // If no active time was passed, get it from the last_active_id if (empty($r['last_active_time'])) { $r['last_active_time'] = get_post_field('post_date', $r['last_active_id']); } if (bbp_get_public_status_id() === $r['last_active_status']) { bbp_update_forum_last_active_time($r['forum_id'], $r['last_active_time']); } // Counts bbp_update_forum_subforum_count($r['forum_id']); bbp_update_forum_reply_count($r['forum_id']); bbp_update_forum_topic_count($r['forum_id']); bbp_update_forum_topic_count_hidden($r['forum_id']); // Update the parent forum if one was passed if (!empty($r['post_parent']) && is_numeric($r['post_parent'])) { bbp_update_forum(array('forum_id' => $r['post_parent'], 'post_parent' => get_post_field('post_parent', $r['post_parent']))); } }
/** * Fix counts on topic split * * When a topic is split, update the counts of source and destination topic * and their forums. * * @since bbPress (r2756) * * @param int $from_reply_id From reply id * @param int $source_topic_id Source topic id * @param int $destination_topic_id Destination topic id * @uses bbp_update_forum_topic_count() To update the forum topic counts * @uses bbp_update_forum_reply_count() To update the forum reply counts * @uses bbp_update_topic_reply_count() To update the topic reply counts * @uses bbp_update_topic_voice_count() To update the topic voice counts * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply * count * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id, * source topic id & destination topic id */ function bbp_split_topic_count($from_reply_id, $source_topic_id, $destination_topic_id) { // Forum Topic Counts bbp_update_forum_topic_count(bbp_get_topic_forum_id($destination_topic_id)); // Forum Reply Counts bbp_update_forum_reply_count(bbp_get_topic_forum_id($destination_topic_id)); // Topic Reply Counts bbp_update_topic_reply_count($source_topic_id); bbp_update_topic_reply_count($destination_topic_id); // Topic Hidden Reply Counts bbp_update_topic_reply_count_hidden($source_topic_id); bbp_update_topic_reply_count_hidden($destination_topic_id); // Topic Voice Counts bbp_update_topic_voice_count($source_topic_id); bbp_update_topic_voice_count($destination_topic_id); do_action('bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id); }
/** * Updates the counts of a forum. * * This calls a few internal functions that all run manual queries against the * database to get their results. As such, this function can be costly to run * but is necessary to keep everything accurate. * * @since bbPress (r2908) * * @param mixed $args Supports these arguments: * - forum_id: Forum id * - last_topic_id: Last topic id * - last_reply_id: Last reply id * - last_active_id: Last active post id * - last_active_time: last active time * @uses bbp_update_forum_last_topic_id() To update the forum last topic id * @uses bbp_update_forum_last_reply_id() To update the forum last reply id * @uses bbp_update_forum_last_active_id() To update the last active post id * @uses get_post_field() To get the post date of the last active id * @uses bbp_update_forum_last_active_time() To update the last active time * @uses bbp_update_forum_subforum_count() To update the subforum count * @uses bbp_update_forum_topic_count() To update the forum topic count * @uses bbp_update_forum_reply_count() To update the forum reply count * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count */ function bbp_update_forum($args = '') { $defaults = array('forum_id' => 0, 'post_parent' => 0, 'last_topic_id' => 0, 'last_reply_id' => 0, 'last_active_id' => 0, 'last_active_time' => 0, 'last_active_status' => bbp_get_public_status_id()); $r = bbp_parse_args($args, $defaults, 'update_forum'); extract($r); // Last topic and reply ID's bbp_update_forum_last_topic_id($forum_id, $last_topic_id); bbp_update_forum_last_reply_id($forum_id, $last_reply_id); // Active dance $last_active_id = bbp_update_forum_last_active_id($forum_id, $last_active_id); // If no active time was passed, get it from the last_active_id if (empty($last_active_time)) { $last_active_time = get_post_field('post_date', $last_active_id); } if (bbp_get_public_status_id() == $last_active_status) { bbp_update_forum_last_active_time($forum_id, $last_active_time); } // Counts bbp_update_forum_subforum_count($forum_id); bbp_update_forum_reply_count($forum_id); bbp_update_forum_topic_count($forum_id); bbp_update_forum_topic_count_hidden($forum_id); // Update the parent forum if one was passed if (!empty($post_parent) && is_numeric($post_parent)) { bbp_update_forum(array('forum_id' => $post_parent, 'post_parent' => get_post_field('post_parent', $post_parent))); } }
/** * Fix counts on reply move * * When a reply is moved, update the counts of source and destination topic * and their forums. * * @since bbPress (r4521) * * @param int $move_reply_id Move reply id * @param int $source_topic_id Source topic id * @param int $destination_topic_id Destination topic id * @uses bbp_update_forum_topic_count() To update the forum topic counts * @uses bbp_update_forum_reply_count() To update the forum reply counts * @uses bbp_update_topic_reply_count() To update the topic reply counts * @uses bbp_update_topic_voice_count() To update the topic voice counts * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply * count * @uses do_action() Calls 'bbp_move_reply_count' with the move reply id, * source topic id & destination topic id */ function bbp_move_reply_count($move_reply_id, $source_topic_id, $destination_topic_id) { // Forum topic counts bbp_update_forum_topic_count(bbp_get_topic_forum_id($destination_topic_id)); // Forum reply counts bbp_update_forum_reply_count(bbp_get_topic_forum_id($destination_topic_id)); // Topic reply counts bbp_update_topic_reply_count($source_topic_id); bbp_update_topic_reply_count($destination_topic_id); // Topic hidden reply counts bbp_update_topic_reply_count_hidden($source_topic_id); bbp_update_topic_reply_count_hidden($destination_topic_id); // Topic voice counts bbp_update_topic_voice_count($source_topic_id); bbp_update_topic_voice_count($destination_topic_id); do_action('bbp_move_reply_count', $move_reply_id, $source_topic_id, $destination_topic_id); }
/** * @covers ::bbp_admin_repair_forum_topic_count */ public function test_bbp_admin_repair_forum_topic_count() { $c = $this->factory->forum->create(array('forum_meta' => array('forum_type' => 'category', 'status' => 'open'))); $f = $this->factory->forum->create(array('post_parent' => $c, 'forum_meta' => array('forum_id' => $c, 'forum_type' => 'forum', 'status' => 'open'))); $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $count = bbp_get_forum_topic_count($f, true, true); $this->assertSame(1, $count); $t = $this->factory->topic->create_many(3, array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); bbp_update_forum_topic_count($c); bbp_update_forum_topic_count($f); // Category topic count. $count = bbp_get_forum_topic_count($c, false, true); $this->assertSame(0, $count); // Category total topic count. $count = bbp_get_forum_topic_count($c, true, true); $this->assertSame(4, $count); // Category topic count hidden. $count = bbp_get_forum_topic_count_hidden($c, true); $this->assertSame(0, $count); // Forum topic count. $count = bbp_get_forum_topic_count($f, false, true); $this->assertSame(4, $count); // Forum total topic count. $count = bbp_get_forum_topic_count($f, true, true); $this->assertSame(4, $count); // Forum topic count hidden. $count = bbp_get_forum_topic_count_hidden($f, true); $this->assertSame(0, $count); bbp_spam_topic($t[0]); bbp_unapprove_topic($t[2]); // Category topic count. $count = bbp_get_forum_topic_count($c, false, true); $this->assertSame(0, $count); // Category total topic count. $count = bbp_get_forum_topic_count($c, true, true); $this->assertSame(2, $count); // Category topic count hidden. $count = bbp_get_forum_topic_count_hidden($c, true); $this->assertSame(0, $count); // Forum topic count. $count = bbp_get_forum_topic_count($f, false, true); $this->assertSame(2, $count); // Forum total topic count. $count = bbp_get_forum_topic_count($f, true, true); $this->assertSame(2, $count); // Forum topic count hidden. $count = bbp_get_forum_topic_count_hidden($f, true); $this->assertSame(2, $count); // Delete the _bbp_total_topic_count meta key. $this->assertTrue(delete_post_meta_by_key('_bbp_topic_count_hidden')); // Delete the _bbp_total_topic_count meta key. $this->assertTrue(delete_post_meta_by_key('_bbp_total_topic_count')); // Delete the _bbp_topic_count meta key. $this->assertTrue(delete_post_meta_by_key('_bbp_topic_count')); // Category topic count. $count = bbp_get_forum_topic_count($c, false, true); $this->assertSame(0, $count); // Category total topic count. $count = bbp_get_forum_topic_count($c, true, true); $this->assertSame(0, $count); // Category topic count hidden. $count = bbp_get_forum_topic_count_hidden($c, true); $this->assertSame(0, $count); // Forum topic count. $count = bbp_get_forum_topic_count($f, false, true); $this->assertSame(0, $count); // Forum total topic count. $count = bbp_get_forum_topic_count($f, true, true); $this->assertSame(0, $count); // Forum topic count hidden. $count = bbp_get_forum_topic_count_hidden($f, true); $this->assertSame(0, $count); // Repair the forum topic count meta. bbp_admin_repair_forum_topic_count(); // Category topic count. $count = bbp_get_forum_topic_count($c, false, true); $this->assertSame(0, $count); // Category total topic count. $count = bbp_get_forum_topic_count($c, true, true); $this->assertSame(2, $count); // Category topic count hidden. $count = bbp_get_forum_topic_count_hidden($c, true); $this->assertSame(0, $count); // Forum topic count. $count = bbp_get_forum_topic_count($f, false, true); $this->assertSame(2, $count); // Forum total topic count. $count = bbp_get_forum_topic_count($f, true, true); $this->assertSame(2, $count); // Forum topic count hidden. $count = bbp_get_forum_topic_count_hidden($f, true); $this->assertSame(2, $count); }
/** * @covers ::bbp_forum_post_count * @covers ::bbp_get_forum_post_count */ public function test_bbp_get_forum_post_count() { $c = $this->factory->forum->create(array('forum_meta' => array('forum_type' => 'category'))); $f = $this->factory->forum->create(array('post_parent' => $c, 'forum_meta' => array('forum_id' => $c))); $t = $this->factory->topic->create(array('post_parent' => $f)); $int_value = 3; // Topic + Replies. $result = 4; $formatted_result = bbp_number_format($result); $this->factory->reply->create_many($int_value, array('post_parent' => $t)); bbp_update_forum_topic_count($c); bbp_update_forum_topic_count($f); bbp_update_forum_reply_count($c); bbp_update_forum_reply_count($f); // Forum output. $count = bbp_get_forum_post_count($f, true, false); $this->expectOutputString($formatted_result); bbp_forum_post_count($f); // Forum formatted string. $count = bbp_get_forum_post_count($f, true, false); $this->assertSame($formatted_result, $count); // Forum integer. $count = bbp_get_forum_post_count($f, true, true); $this->assertSame($result, $count); // Category post count. $count = bbp_get_forum_post_count($c, false, true); $this->assertSame(0, $count); // Category total post count. $count = bbp_get_forum_post_count($c, true, true); $this->assertSame($result, $count); }
/** * Updates the counts of a forum. * * This calls a few internal functions that all run manual queries against the * database to get their results. As such, this function can be costly to run * but is necessary to keep everything accurate. * * @since 2.0.0 bbPress (r2908) * * @param array $args Supports these arguments: * - forum_id: Forum id * - last_topic_id: Last topic id * - last_reply_id: Last reply id * - last_active_id: Last active post id * - last_active_time: last active time * @uses bbp_update_forum_last_topic_id() To update the forum last topic id * @uses bbp_update_forum_last_reply_id() To update the forum last reply id * @uses bbp_update_forum_last_active_id() To update the last active post id * @uses get_post_field() To get the post date of the last active id * @uses bbp_update_forum_last_active_time() To update the last active time * @uses bbp_update_forum_subforum_count() To update the subforum count * @uses bbp_update_forum_topic_count() To update the forum topic count * @uses bbp_update_forum_reply_count() To update the forum reply count * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count */ function bbp_update_forum($args = array()) { // Parse arguments against default values $r = bbp_parse_args($args, array('forum_id' => 0, 'post_parent' => 0, 'last_topic_id' => 0, 'last_reply_id' => 0, 'last_active_id' => 0, 'last_active_time' => 0, 'last_active_status' => bbp_get_public_status_id()), 'update_forum'); // Last topic and reply ID's bbp_update_forum_last_topic_id($r['forum_id'], $r['last_topic_id']); bbp_update_forum_last_reply_id($r['forum_id'], $r['last_reply_id']); // Active dance $r['last_active_id'] = bbp_update_forum_last_active_id($r['forum_id'], $r['last_active_id']); // If no active time was passed, get it from the last_active_id if (empty($r['last_active_time'])) { $r['last_active_time'] = get_post_field('post_date', $r['last_active_id']); } if (bbp_get_public_status_id() === $r['last_active_status']) { bbp_update_forum_last_active_time($r['forum_id'], $r['last_active_time']); } // Counts bbp_update_forum_subforum_count($r['forum_id']); // Only update topic count if we're deleting a topic, or in the dashboard. if (in_array(current_filter(), array('bbp_deleted_topic', 'save_post'), true)) { bbp_update_forum_reply_count($r['forum_id']); bbp_update_forum_topic_count($r['forum_id']); bbp_update_forum_topic_count_hidden($r['forum_id']); } // Update the parent forum if one was passed if (!empty($r['post_parent']) && is_numeric($r['post_parent'])) { bbp_update_forum(array('forum_id' => $r['post_parent'], 'post_parent' => get_post_field('post_parent', $r['post_parent']))); } }
/** * @covers ::bbp_update_forum_topic_count */ public function test_bbp_update_forum_topic_count() { // Create a top level forum f1 $f1 = $this->factory->forum->create(); bbp_normalize_forum($f1); $count = bbp_get_forum_topic_count($f1); $this->assertSame('0', $count); // Create 3 topics in f1 $t = $this->factory->topic->create_many(3, array('post_parent' => $f1)); bbp_update_forum_topic_count($f1); $count = bbp_get_forum_topic_count($f1); $this->assertSame('3', $count); // Create a new sub forum of f1 $f2 = $this->factory->forum->create(array('post_parent' => $f1)); // Create another sub forum of f1 $f3 = $this->factory->forum->create(array('post_parent' => $f1)); bbp_update_forum_topic_count($f1); bbp_update_forum_topic_count($f2); bbp_update_forum_topic_count($f3); $count = bbp_get_forum_topic_count($f1); $this->assertSame('3', $count); $count = bbp_get_forum_topic_count($f2); $this->assertSame('0', $count); $count = bbp_get_forum_topic_count($f3); $this->assertSame('0', $count); // Create some topics in forum f2 $this->factory->topic->create_many(4, array('post_parent' => $f2)); bbp_update_forum_topic_count($f1); bbp_update_forum_topic_count($f2); bbp_update_forum_topic_count($f3); $count = bbp_get_forum_topic_count($f1); $this->assertSame('7', $count); $count = bbp_get_forum_topic_count($f2); $this->assertSame('4', $count); $count = bbp_get_forum_topic_count($f3); $this->assertSame('0', $count); // Create some topics in forum f3 $this->factory->topic->create_many(5, array('post_parent' => $f3)); bbp_update_forum_topic_count($f1); bbp_update_forum_topic_count($f2); bbp_update_forum_topic_count($f3); $count = bbp_get_forum_topic_count($f1); $this->assertSame('12', $count); $count = bbp_get_forum_topic_count($f2); $this->assertSame('4', $count); $count = bbp_get_forum_topic_count($f3); $this->assertSame('5', $count); }
/** * @covers ::bbp_forum_post_count * @covers ::bbp_get_forum_post_count */ public function test_bbp_get_forum_post_count() { $f = $this->factory->forum->create(); $t = $this->factory->topic->create(array('post_parent' => $f)); $int_value = 3; // Topic + Replies $result = 4; $formatted_result = bbp_number_format($result); $this->factory->reply->create_many($int_value, array('post_parent' => $t)); bbp_update_forum_topic_count($f); bbp_update_forum_reply_count($f); // Output $count = bbp_get_forum_post_count($f, true, false); $this->expectOutputString($formatted_result); bbp_forum_post_count($f); // Formatted string $count = bbp_get_forum_post_count($f, true, false); $this->assertSame($formatted_result, $count); // Integer $count = bbp_get_forum_post_count($f, true, true); $this->assertSame($result, $count); }