/**
  * @covers ::bbp_forum_last_active_id
  * @covers ::bbp_get_forum_last_active_id
  */
 public function test_bbp_get_forum_last_active_id_with_pending_reply()
 {
     $u = $this->factory->user->create_many(2);
     $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')));
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame(0, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame(0, $last_id);
     bbp_update_forum_last_active_id($f);
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame(0, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame(0, $last_id);
     $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f)));
     bbp_update_forum_last_active_id($f);
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame($t, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame($t, $last_id);
     $r1 = $this->factory->reply->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     bbp_update_forum_last_active_id($f);
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame($r1, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame($r1, $last_id);
     $r2 = $this->factory->reply->create(array('post_parent' => $t, 'post_author' => $u[1], 'post_status' => bbp_get_pending_status_id(), 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame($r1, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame($r1, $last_id);
     bbp_approve_reply($r2);
     // Get the forums last active id.
     $last_id = bbp_get_forum_last_active_id($f);
     $this->assertSame($r2, $last_id);
     // Get the categories last active id.
     $last_id = bbp_get_forum_last_active_id($c);
     $this->assertSame($r2, $last_id);
 }
Example #2
0
 /**
  * @covers ::bbp_update_forum_last_active_id
  */
 public function test_bbp_update_forum_last_active_id()
 {
     $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)));
     $r1 = $this->factory->reply->create(array('post_parent' => $t1, 'reply_meta' => array('forum_id' => $f1, 'topic_id' => $t1)));
     $id = bbp_update_forum_last_active_id($f1, $r1);
     $this->assertSame($r1, $id);
     $id = bbp_get_forum_last_active_id($f1);
     $this->assertSame($r1, $id);
     $t2 = $this->factory->topic->create(array('post_parent' => $f2, 'topic_meta' => array('forum_id' => $f2)));
     $r2 = $this->factory->reply->create(array('post_parent' => $t2, 'reply_meta' => array('forum_id' => $f2, 'topic_id' => $t2)));
     bbp_update_forum_last_active_id($f2);
     $id = bbp_get_forum_last_active_id($f2);
     $this->assertSame($r2, $id);
     bbp_update_forum_last_active_id($f1);
     $id = bbp_get_forum_last_active_id($f1);
     $this->assertSame($r2, $id);
 }
Example #3
0
/**
 * 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'])));
    }
}
/**
 * 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);
            }
        }
    }
}
Example #5
0
/**
 * 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)));
    }
}
Example #6
0
/**
 * 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'])));
    }
}
Example #7
0
/**
 * Walk up the ancestor tree from the current reply, and update all the counts
 *
 * @since 2.0.0 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);
            // Make every effort to get topic id
            // https://bbpress.trac.wordpress.org/ticket/2529
            if (empty($topic_id) && current_filter() === 'bbp_deleted_reply') {
                $topic_id = get_post_field('post_parent', $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));
                }
                // Update the topic last active time regardless of reply status.
                // See https://bbpress.trac.wordpress.org/ticket/2838
                bbp_update_topic_last_active_time($ancestor, $topic_last_active_time);
                // Counts
                bbp_update_topic_voice_count($ancestor);
                // Only update reply count if we're deleting a reply, or in the dashboard.
                if (in_array(current_filter(), array('bbp_deleted_reply', 'save_post'), true)) {
                    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
                // Only update reply count if we're deleting a reply, or in the dashboard.
                if (in_array(current_filter(), array('bbp_deleted_reply', 'save_post'), true)) {
                    bbp_update_forum_reply_count($ancestor);
                }
            }
        }
    }
}