Exemple #1
0
 /**
  * @covers ::bbp_forum_query_last_reply_id
  */
 public function test_bbp_forum_query_last_reply_id()
 {
     $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)));
     $this->factory->reply->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Get the forums last reply id.
     $query_last_reply_f = bbp_forum_query_last_reply_id($f);
     $this->assertSame($query_last_reply_f, bbp_get_forum_last_reply_id($f));
     // Get the categories last reply id.
     $query_last_reply_c = bbp_forum_query_last_reply_id($c);
     $this->assertSame($query_last_reply_c, bbp_get_forum_last_reply_id($c));
     $this->factory->reply->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Get the forums last reply id.
     $query_last_reply_f = bbp_forum_query_last_reply_id($f);
     $this->assertSame($query_last_reply_f, bbp_get_forum_last_reply_id($f));
     // Get the categories last reply id.
     $query_last_reply_c = bbp_forum_query_last_reply_id($c);
     $this->assertSame($query_last_reply_c, bbp_get_forum_last_reply_id($c));
 }
/**
 * Update the forum last active post id
 *
 * @since bbPress (r2860)
 *
 * @param int $forum_id Optional. Forum id
 * @param int $active_id Optional. Active post id
 * @uses bbp_get_forum_id() To get the forum id
 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
 * @uses bbp_update_forum_last_active_id() To update the last active id of
 *                                          child forums
 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
 * @uses get_post_status() To make sure the reply is published
 * @uses update_post_meta() To update the forum's last active id meta
 * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last
 *                        active post id and forum id
 * @return bool True on success, false on failure
 */
function bbp_update_forum_last_active_id($forum_id = 0, $active_id = 0)
{
    $forum_id = bbp_get_forum_id($forum_id);
    // Define local variable(s)
    $children_last_active = 0;
    // Do some calculation if not manually set
    if (empty($active_id)) {
        // Loop through children and add together forum reply counts
        $children = bbp_forum_query_subforum_ids($forum_id);
        if (!empty($children)) {
            foreach ((array) $children as $child) {
                $children_last_active = bbp_update_forum_last_active_id($child, $active_id);
            }
        }
        // Don't count replies if the forum is a category
        $topic_ids = bbp_forum_query_topic_ids($forum_id);
        if (!empty($topic_ids)) {
            $active_id = bbp_forum_query_last_reply_id($forum_id, $topic_ids);
            $active_id = $active_id > max($topic_ids) ? $active_id : max($topic_ids);
            // Forum has no topics
        } else {
            $active_id = 0;
        }
    }
    // Cast as integer in case of empty or string
    $active_id = (int) $active_id;
    $children_last_active = (int) $children_last_active;
    // If child forums have higher id, use that instead
    if (!empty($children) && $children_last_active > $active_id) {
        $active_id = $children_last_active;
    }
    // Update only if published
    if (bbp_get_public_status_id() === get_post_status($active_id)) {
        update_post_meta($forum_id, '_bbp_last_active_id', (int) $active_id);
    }
    return (int) apply_filters('bbp_update_forum_last_active_id', (int) $active_id, $forum_id);
}