/** * Recount forum replies * * @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_reply_count() To update the forum reply count * @return array An array of the status code and the message */ function bbp_admin_repair_forum_reply_count() { // Define variables $bbp_db = bbp_db(); $statement = __('Counting the number of replies in each forum… %s', 'bbpress'); $result = __('Failed!', 'bbpress'); // Post type $fpt = bbp_get_forum_post_type(); // Delete the meta keys _bbp_reply_count and _bbp_total_reply_count for each forum $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`\n\t\t\t\t\t\tLEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`\n\t\t\t\t\t\tWHERE `posts`.`post_type` = '{$fpt}'\n\t\t\t\t\t\tAND `postmeta`.`meta_key` = '_bbp_reply_count'\n\t\t\t\t\t\tOR `postmeta`.`meta_key` = '_bbp_total_reply_count'"; if (is_wp_error($bbp_db->query($sql_delete))) { return array(1, sprintf($statement, $result)); } // Recalculate the metas key _bbp_reply_count and _bbp_total_reply_count for each forum $forums = get_posts(array('post_type' => bbp_get_forum_post_type(), 'numberposts' => -1)); if (!empty($forums)) { foreach ($forums as $forum) { bbp_update_forum_reply_count($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); }
/** * 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); }
/** * Recount forum replies * * @since 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_reply_count() To update the forum reply count * @return array An array of the status code and the message */ function bbp_admin_repair_forum_reply_count() { global $wpdb; $statement = __('Counting the number of replies in each forum… %s', 'bbpress'); $result = __('Failed!', 'bbpress'); $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' );"; if (is_wp_error($wpdb->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_reply_count($forum->ID); } } else { return array(2, sprintf($statement, $result)); } return array(0, sprintf($statement, __('Complete!', 'bbpress'))); }
/** * @covers ::bbp_admin_repair_forum_reply_count */ public function test_bbp_admin_repair_forum_reply_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))); $r = $this->factory->reply->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t))); $count = bbp_get_forum_reply_count($f, true, true); $this->assertSame(1, $count); $r = $this->factory->reply->create_many(3, array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t))); bbp_update_forum_reply_count($c); bbp_update_forum_reply_count($f); // Category reply count. $count = bbp_get_forum_reply_count($c, false, true); $this->assertSame(0, $count); // Category total reply count. $count = bbp_get_forum_reply_count($c, true, true); $this->assertSame(4, $count); // Forum reply count. $count = bbp_get_forum_reply_count($f, false, true); $this->assertSame(4, $count); // Forum total reply count. $count = bbp_get_forum_reply_count($f, true, true); $this->assertSame(4, $count); bbp_spam_reply($r[0]); bbp_unapprove_reply($r[2]); // Category reply count. $count = bbp_get_forum_reply_count($c, false, true); $this->assertSame(0, $count); // Category total reply count. $count = bbp_get_forum_reply_count($c, true, true); $this->assertSame(2, $count); // Forum reply count. $count = bbp_get_forum_reply_count($f, false, true); $this->assertSame(2, $count); // Forum total reply count. $count = bbp_get_forum_reply_count($f, true, true); $this->assertSame(2, $count); // Delete the _bbp_total_reply_count meta key. $this->assertTrue(delete_post_meta_by_key('_bbp_total_reply_count')); // Delete the _bbp_reply_count meta key. $this->assertTrue(delete_post_meta_by_key('_bbp_reply_count')); // Category reply count. $count = bbp_get_forum_reply_count($c, false, true); $this->assertSame(0, $count); // Category total reply count. $count = bbp_get_forum_reply_count($c, true, true); $this->assertSame(0, $count); // Forum reply count. $count = bbp_get_forum_reply_count($f, false, true); $this->assertSame(0, $count); // Forum total reply count. $count = bbp_get_forum_reply_count($f, true, true); $this->assertSame(0, $count); // Repair the forum reply count meta. bbp_admin_repair_forum_reply_count(); // Category reply count. $count = bbp_get_forum_reply_count($c, false, true); $this->assertSame(0, $count); // Category total reply count. $count = bbp_get_forum_reply_count($c, true, true); $this->assertSame(2, $count); // Forum reply count. $count = bbp_get_forum_reply_count($f, false, true); $this->assertSame(2, $count); // Forum total reply count. $count = bbp_get_forum_reply_count($f, true, true); $this->assertSame(2, $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 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))); } }
/** * @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); }
/** * Walk up the ancestor tree from the current reply, and update all the counts * * @since bbPress (r2884) * * @param int $reply_id Optional. Reply id * @param string $last_active_time Optional. Last active time * @param int $forum_id Optional. Forum id * @param int $topic_id Optional. Topic id * @param bool $refresh If set to true, unsets all the previous parameters. * Defaults to true * @uses bbp_get_reply_id() To get the reply id * @uses bbp_get_reply_topic_id() To get the reply topic id * @uses bbp_get_reply_forum_id() To get the reply forum id * @uses get_post_ancestors() To get the ancestors of the reply * @uses bbp_is_reply() To check if the ancestor is a reply * @uses bbp_is_topic() To check if the ancestor is a topic * @uses bbp_update_topic_last_reply_id() To update the topic last reply id * @uses bbp_update_topic_last_active_id() To update the topic last active id * @uses bbp_get_topic_last_active_id() To get the topic last active id * @uses get_post_field() To get the post date of the last active id * @uses bbp_update_topic_last_active_time() To update the last active topic meta * @uses bbp_update_topic_voice_count() To update the topic voice count * @uses bbp_update_topic_reply_count() To update the topic reply count * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply * count * @uses bbp_is_forum() To check if the ancestor is a forum * @uses bbp_update_forum_last_topic_id() To update the last topic id forum meta * @uses bbp_update_forum_last_reply_id() To update the last reply id forum meta * @uses bbp_update_forum_last_active_id() To update the forum last active id * @uses bbp_get_forum_last_active_id() To get the forum last active id * @uses bbp_update_forum_last_active_time() To update the forum last active time * @uses bbp_update_forum_reply_count() To update the forum reply count */ function bbp_update_reply_walker($reply_id, $last_active_time = '', $forum_id = 0, $topic_id = 0, $refresh = true) { // Verify the reply ID $reply_id = bbp_get_reply_id($reply_id); // Reply was passed if (!empty($reply_id)) { // Get the topic ID if none was passed if (empty($topic_id)) { $topic_id = bbp_get_reply_topic_id($reply_id); } // Get the forum ID if none was passed if (empty($forum_id)) { $forum_id = bbp_get_reply_forum_id($reply_id); } } // Set the active_id based on topic_id/reply_id $active_id = empty($reply_id) ? $topic_id : $reply_id; // Setup ancestors array to walk up $ancestors = array_values(array_unique(array_merge(array($topic_id, $forum_id), (array) get_post_ancestors($topic_id)))); // If we want a full refresh, unset any of the possibly passed variables if (true == $refresh) { $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0; } // Walk up ancestors if (!empty($ancestors)) { foreach ($ancestors as $ancestor) { // Reply meta relating to most recent reply if (bbp_is_reply($ancestor)) { // @todo - hierarchical replies // Topic meta relating to most recent reply } elseif (bbp_is_topic($ancestor)) { // Last reply and active ID's bbp_update_topic_last_reply_id($ancestor, $reply_id); bbp_update_topic_last_active_id($ancestor, $active_id); // Get the last active time if none was passed $topic_last_active_time = $last_active_time; if (empty($last_active_time)) { $topic_last_active_time = get_post_field('post_date', bbp_get_topic_last_active_id($ancestor)); } // Only update if reply is published if (bbp_is_reply_published($reply_id)) { bbp_update_topic_last_active_time($ancestor, $topic_last_active_time); } // Counts bbp_update_topic_voice_count($ancestor); bbp_update_topic_reply_count($ancestor); bbp_update_topic_reply_count_hidden($ancestor); // Forum meta relating to most recent topic } elseif (bbp_is_forum($ancestor)) { // Last topic and reply ID's bbp_update_forum_last_topic_id($ancestor, $topic_id); bbp_update_forum_last_reply_id($ancestor, $reply_id); // Last Active bbp_update_forum_last_active_id($ancestor, $active_id); // Get the last active time if none was passed $forum_last_active_time = $last_active_time; if (empty($last_active_time)) { $forum_last_active_time = get_post_field('post_date', bbp_get_forum_last_active_id($ancestor)); } // Only update if reply is published if (bbp_is_reply_published($reply_id)) { bbp_update_forum_last_active_time($ancestor, $forum_last_active_time); } // Counts bbp_update_forum_reply_count($ancestor); } } } }
/** * 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_reply_count */ public function test_bbp_update_forum_reply_count() { $f1 = $this->factory->forum->create(); $f2 = $this->factory->forum->create(array('post_parent' => $f1)); $t1 = $this->factory->topic->create(array('post_parent' => $f1, 'topic_meta' => array('forum_id' => $f1))); $t2 = $this->factory->topic->create(array('post_parent' => $f2, 'topic_meta' => array('forum_id' => $f2))); $count = bbp_get_forum_reply_count($f1, false, true); $this->assertSame(0, $count); $count = bbp_update_forum_reply_count($f1); $this->assertSame(0, $count); $this->factory->reply->create_many(3, array('post_parent' => $t1, 'reply_meta' => array('forum_id' => $f1, 'topic_id' => $t1))); $count = bbp_update_forum_reply_count($f1); $this->assertSame(3, $count); $this->factory->reply->create_many(3, array('post_parent' => $t2, 'reply_meta' => array('forum_id' => $f2, 'topic_id' => $t2))); $count = bbp_update_forum_reply_count($f1); $this->assertSame(6, $count); $count = bbp_update_forum_reply_count($f2); $this->assertSame(3, $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); }