Example #1
0
 /**
  * Generic function to test the topic counts on a deleted reply
  */
 public function test_bbp_topic_deleted_reply_counts()
 {
     $f = $this->factory->forum->create();
     $t = $this->factory->topic->create(array('post_parent' => $f, 'post_author' => bbp_get_current_user_id(), 'topic_meta' => array('forum_id' => $f)));
     $r1 = $this->factory->reply->create(array('post_parent' => $t, 'post_author' => bbp_get_current_user_id(), 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     $u = $this->factory->user->create();
     $count = bbp_update_topic_reply_count($t);
     $this->assertSame(1, $count);
     $count = bbp_update_topic_reply_count_hidden($t);
     $this->assertSame(0, $count);
     $count = bbp_update_topic_voice_count($t);
     $this->assertSame(1, $count);
     $r2 = $this->factory->reply->create(array('post_parent' => $t, 'post_author' => $u, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     $count = bbp_update_topic_reply_count($t);
     $this->assertSame(2, $count);
     $count = bbp_update_topic_reply_count_hidden($t);
     $this->assertSame(0, $count);
     $count = bbp_update_topic_voice_count($t);
     $this->assertSame(2, $count);
     // ToDo: Update this to use bbp_delete_reply().
     bbp_clean_post_cache($t);
     wp_delete_post($r2, true);
     $count = bbp_get_topic_reply_count($t, true);
     $this->assertSame(1, $count);
     $count = bbp_get_topic_reply_count_hidden($t, true);
     $this->assertSame(0, $count);
     $count = bbp_get_topic_voice_count($t, true);
     $this->assertSame(1, $count);
 }
Example #2
0
 /**
  * @covers ::bbp_clean_post_cache
  */
 public function test_bbp_clean_post_cache()
 {
     // Get the topic post type.
     $tpt = bbp_get_topic_post_type();
     // Set up a forum with 1 topic and 1 reply to that topic.
     $f = $this->factory->forum->create();
     $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f)));
     $r = $this->factory->topic->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Make sure we've cached some data.
     bbp_get_all_child_ids($f, $tpt);
     bbp_get_all_child_ids($t, $tpt);
     $this->assertEquals(array($t), wp_cache_get("bbp_parent_all_{$f}_type_{$tpt}_child_ids", 'bbpress_posts'));
     $this->assertEquals(array($r), wp_cache_get("bbp_parent_all_{$t}_type_{$tpt}_child_ids", 'bbpress_posts'));
     // Clean the cache.
     bbp_clean_post_cache($r);
     $this->assertEquals(false, wp_cache_get("bbp_parent_all_{$f}_type_{$tpt}_child_ids", 'bbpress_posts'));
     $this->assertEquals(false, wp_cache_get("bbp_parent_all_{$t}_type_{$tpt}_child_ids", 'bbpress_posts'));
 }
Example #3
0
/**
 * Mark the forum as hidden
 *
 * @since bbPress (r2996)
 *
 * @param int $forum_id Optional. Forum id
 * @uses update_post_meta() To update the forum private meta
 * @return bool False on failure, true on success
 */
function bbp_hide_forum($forum_id = 0, $current_visibility = '')
{
    $forum_id = bbp_get_forum_id($forum_id);
    do_action('bbp_hide_forum', $forum_id);
    // Only run queries if visibility is changing
    if (bbp_get_hidden_status_id() !== $current_visibility) {
        // Get private forums
        $private = bbp_get_private_forum_ids();
        // Find this forum in the array
        if (in_array($forum_id, $private)) {
            $offset = array_search($forum_id, $private);
            // Splice around it
            array_splice($private, $offset, 1);
            // Update private forums minus this one
            update_option('_bbp_private_forums', array_unique(array_filter(array_values($private))));
        }
        // Add to '_bbp_hidden_forums' site option
        $hidden = bbp_get_hidden_forum_ids();
        $hidden[] = $forum_id;
        update_option('_bbp_hidden_forums', array_unique(array_filter(array_values($hidden))));
        // Update forums visibility setting
        global $wpdb;
        $wpdb->update($wpdb->posts, array('post_status' => bbp_get_hidden_status_id()), array('ID' => $forum_id));
        wp_transition_post_status(bbp_get_hidden_status_id(), $current_visibility, get_post($forum_id));
        bbp_clean_post_cache($forum_id);
    }
    do_action('bbp_hid_forum', $forum_id);
    return $forum_id;
}
Example #4
0
/**
 * Will clean a post in the cache.
 *
 * Will call to clean the term object cache associated with the post ID.
 *
 * @since bbPress (r4040)
 *
 * @uses do_action() Calls 'bbp_clean_post_cache' on $id
 * @param object|int $_post The post object or ID to remove from the cache
 */
function bbp_clean_post_cache($_post = '')
{
    // Bail if no post
    $_post = get_post($_post);
    if (empty($_post)) {
        return;
    }
    wp_cache_delete($_post->ID, 'posts');
    wp_cache_delete($_post->ID, 'post_meta');
    clean_object_term_cache($_post->ID, $_post->post_type);
    do_action('bbp_clean_post_cache', $_post->ID, $_post);
    // Child query types to clean
    $post_types = array(bbp_get_topic_post_type(), bbp_get_forum_post_type(), bbp_get_reply_post_type());
    // Loop through query types and clean caches
    foreach ($post_types as $post_type) {
        wp_cache_delete('bbp_get_forum_' . $_post->ID . '_reply_id', 'bbpress');
        wp_cache_delete('bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress');
        wp_cache_delete('bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_count', 'bbpress');
        wp_cache_delete('bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress');
        wp_cache_delete('bbp_parent_all_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress');
    }
    // Invalidate parent caches
    if (!empty($_post->post_parent)) {
        bbp_clean_post_cache($_post->post_parent);
    }
}
Example #5
0
 /**
  * @covers ::bbp_admin_repair_forum_meta
  */
 public function test_bbp_admin_repair_forum_meta()
 {
     $f = $this->factory->forum->create();
     $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)));
     // Forums should NOT have a _bbp_forum_id meta key
     $this->assertEquals(array(), get_post_meta($f, '_bbp_forum_id', false));
     // Topics should have a _bbp_forum_id meta key
     $this->assertSame($f, bbp_get_topic_forum_id($t));
     // Replies should have a _bbp_forum_id meta key
     $this->assertSame($f, bbp_get_reply_forum_id($r));
     // Delete the topic and reply _bbp_forum_id meta key
     $this->assertTrue(delete_post_meta_by_key('_bbp_forum_id'));
     // Check the _bbp_forum_id meta key is deleted
     $this->assertEquals(array(), get_post_meta($f, '_bbp_forum_id', false));
     $this->assertEquals(array(), get_post_meta($t, '_bbp_forum_id', false));
     $this->assertEquals(array(), get_post_meta($r, '_bbp_forum_id', false));
     // Repair the forum meta
     bbp_admin_repair_forum_meta();
     bbp_clean_post_cache($f);
     bbp_clean_post_cache($t);
     bbp_clean_post_cache($r);
     // Forums should NOT have a _bbp_forum_id meta key
     $this->assertEquals(array(), get_post_meta($f, '_bbp_forum_id', false));
     // Topics should have a _bbp_forum_id meta key
     $this->assertEquals(array($f), get_post_meta($t, '_bbp_forum_id', false));
     $this->assertSame($f, bbp_get_topic_forum_id($t));
     // Replies should have a _bbp_forum_id meta key
     $this->assertEquals(array($f), get_post_meta($r, '_bbp_forum_id', false));
     $this->assertSame($f, bbp_get_reply_forum_id($r));
 }
Example #6
0
/**
 * Handle the moving of a topic from one forum to another. This includes walking
 * up the old and new branches and updating the counts.
 *
 * @since 2.0.0 bbPress (r2907)
 *
 * @param int $topic_id     The topic id.
 * @param int $old_forum_id Old forum id.
 * @param int $new_forum_id New forum id.
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_get_forum_id() To get the forum id
 * @uses bbp_clean_post_cache() To clean the old and new forum post caches
 * @uses bbp_update_topic_forum_id() To update the topic forum id
 * @uses wp_update_post() To update the topic post parent`
 * @uses bbp_get_stickies() To get the old forums sticky topics
 * @uses delete_post_meta() To delete the forum sticky meta
 * @uses update_post_meta() To update the old forum sticky meta
 * @uses bbp_stick_topic() To stick the topic in the new forum
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses bbp_get_all_child_ids() To get all the child ids
 * @uses bbp_update_reply_forum_id() To update the reply forum id
 * @uses get_post_ancestors() To get the topic's ancestors
 * @uses bbp_get_public_child_ids() To get all the public child ids
 * @uses get_post_field() To get the topic's post status
 * @uses bbp_get_public_status_id() To get the public status id
 * @uses bbp_decrease_forum_topic_count() To bump the forum topic count by -1
 * @uses bbp_bump_forum_reply_count() To bump the forum reply count
 * @uses bbp_increase_forum_topic_count() To bump the forum topic count by 1
 * @uses bbp_decrease_forum_topic_count_hidden() To bump the forum topic hidden count by -1
 * @uses bbp_increase_forum_topic_count_hidden() To bump the forum topic hidden count by 1
 * @uses bbp_is_forum() To check if the ancestor is a forum
 * @uses bbp_update_forum() To update the forum
 */
function bbp_move_topic_handler($topic_id, $old_forum_id, $new_forum_id)
{
    // Validate parameters
    $topic_id = bbp_get_topic_id($topic_id);
    $old_forum_id = bbp_get_forum_id($old_forum_id);
    $new_forum_id = bbp_get_forum_id($new_forum_id);
    // Clean old and new forum caches before proceeding, to ensure subsequent
    // calls to forum objects are using updated data.
    bbp_clean_post_cache($old_forum_id);
    bbp_clean_post_cache($new_forum_id);
    // Update topic forum's ID
    bbp_update_topic_forum_id($topic_id, $new_forum_id);
    // Update topic post parent with the new forum ID
    wp_update_post(array('ID' => $topic_id, 'post_parent' => $new_forum_id));
    /** Stickies **************************************************************/
    // Get forum stickies
    $old_stickies = bbp_get_stickies($old_forum_id);
    // Only proceed if stickies are found
    if (!empty($old_stickies)) {
        // Define local variables
        $updated_stickies = array();
        // Loop through stickies of forum and add misses to the updated array
        foreach ((array) $old_stickies as $sticky_topic_id) {
            if ($topic_id !== $sticky_topic_id) {
                $updated_stickies[] = $sticky_topic_id;
            }
        }
        // If stickies are different, update or delete them
        if ($updated_stickies !== $old_stickies) {
            // No more stickies so delete the meta
            if (empty($updated_stickies)) {
                delete_post_meta($old_forum_id, '_bbp_sticky_topics');
                // Still stickies so update the meta
            } else {
                update_post_meta($old_forum_id, '_bbp_sticky_topics', $updated_stickies);
            }
            // Topic was sticky, so restick in new forum
            bbp_stick_topic($topic_id);
        }
    }
    /** Topic Replies *********************************************************/
    // Get the topics replies
    $replies = bbp_get_all_child_ids($topic_id, bbp_get_reply_post_type());
    // Update the forum_id of all replies in the topic
    foreach ($replies as $reply_id) {
        bbp_update_reply_forum_id($reply_id, $new_forum_id);
    }
    /** Old forum_id **********************************************************/
    // Get topic ancestors
    $old_forum_ancestors = array_values(array_unique(array_merge(array($old_forum_id), (array) get_post_ancestors($old_forum_id))));
    // Get reply count.
    $public_reply_count = count(bbp_get_public_child_ids($topic_id, bbp_get_reply_post_type()));
    // Topic status.
    $topic_status = get_post_field('post_status', $topic_id);
    // Update old/new forum counts.
    if ($topic_status === bbp_get_public_status_id()) {
        // Update old forum counts.
        bbp_decrease_forum_topic_count($old_forum_id);
        bbp_bump_forum_reply_count($old_forum_id, -$public_reply_count);
        // Update new forum counts.
        bbp_increase_forum_topic_count($new_forum_id);
        bbp_bump_forum_reply_count($new_forum_id, $public_reply_count);
    } else {
        // Update old forum counts.
        bbp_decrease_forum_topic_count_hidden($old_forum_id);
        // Update new forum counts.
        bbp_increase_forum_topic_count_hidden($new_forum_id);
    }
    // Loop through ancestors and update them
    if (!empty($old_forum_ancestors)) {
        foreach ($old_forum_ancestors as $ancestor) {
            if (bbp_is_forum($ancestor)) {
                bbp_update_forum(array('forum_id' => $ancestor));
            }
        }
    }
    /** New forum_id **********************************************************/
    // Make sure we're not walking twice
    if (!in_array($new_forum_id, $old_forum_ancestors)) {
        // Get topic ancestors
        $new_forum_ancestors = array_values(array_unique(array_merge(array($new_forum_id), (array) get_post_ancestors($new_forum_id))));
        // Make sure we're not walking twice
        $new_forum_ancestors = array_diff($new_forum_ancestors, $old_forum_ancestors);
        // Loop through ancestors and update them
        if (!empty($new_forum_ancestors)) {
            foreach ($new_forum_ancestors as $ancestor) {
                if (bbp_is_forum($ancestor)) {
                    bbp_update_forum(array('forum_id' => $ancestor));
                }
            }
        }
    }
}
Example #7
0
/**
 * Will clean a post in the cache.
 *
 * Will call to clean the term object cache associated with the post ID.
 *
 * @since 2.1.0 bbPress (r4040)
 *
 * @uses do_action() Calls 'bbp_clean_post_cache' on $id
 * @since 2.6.0 bbPress (r6053) Introduced the `$post_id` parameter.
 *
 * @param int     $post_id The post id.
 * @param WP_Post $post    The WP_Post object.
 *
 * @uses get_post() To get the post object.
 * @uses bbp_get_forum_post_type() To get the forum post type.
 * @uses bbp_get_topic_post_type() To get the topic post type.
 * @uses bbp_get_reply_post_type() To get the reply post type.
 * @uses wp_cache_delete() To delete the cache item.
 * @uses clean_object_term_cache() To clean the term cache.
 * @uses bbp_clean_post_cache() Recursion.
 */
function bbp_clean_post_cache($post_id = null, $post = null)
{
    // Get the post object.
    if (null !== $post) {
        $post = get_post($post);
    } else {
        $post = get_post($post_id);
    }
    // Bail if no post
    if (empty($post)) {
        return;
    }
    // Child query types to clean
    $post_types = array(bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type());
    // Bail if not a bbPress post type
    if (!in_array($post->post_type, $post_types, true)) {
        return;
    }
    // Be sure we haven't recached the post data
    wp_cache_delete($post->ID, 'posts');
    wp_cache_delete($post->ID, 'post_meta');
    // Clean the term cache for the given post
    clean_object_term_cache($post->ID, $post->post_type);
    // Loop through query types and clean caches
    foreach ($post_types as $post_type) {
        wp_cache_delete('bbp_parent_all_' . $post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts');
    }
    /**
     * Fires immediately after the given post's cache is cleaned.
     *
     * @since 2.1.0
     *
     * @param int     $post_id Post ID.
     * @param WP_Post $post    Post object.
     */
    do_action('bbp_clean_post_cache', $post->ID, $post);
    // Invalidate parent caches
    if (!empty($post->post_parent)) {
        bbp_clean_post_cache($post->post_parent);
    }
}
Example #8
0
/**
 * Will clean a post in the cache.
 *
 * Will call to clean the term object cache associated with the post ID.
 *
 * @since bbPress (r4040)
 *
 * @uses do_action() Calls 'bbp_clean_post_cache' on $id
 * @param object|int $post The post object or ID to remove from the cache
 */
function bbp_clean_post_cache($post)
{
    global $wpdb;
    // Bail if no post
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    wp_cache_delete($post->ID, 'posts');
    wp_cache_delete($post->ID, 'post_meta');
    clean_object_term_cache($post->ID, $post->post_type);
    do_action('bbp_clean_post_cache', $post->ID, $post);
    // Child query types to clean
    $post_types = array(bbp_get_topic_post_type(), bbp_get_forum_post_type(), bbp_get_reply_post_type());
    // Loop through query types and clean caches
    foreach ($post_types as $post_type) {
        wp_cache_delete('bbp_get_forum_' . $post->ID . '_reply_id', 'bbpress');
        wp_cache_delete('bbp_parent_' . $post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress');
        wp_cache_delete('bbp_parent_' . $post->ID . '_type_' . $post_type . '_child_count', 'bbpress');
        wp_cache_delete('bbp_parent_public_' . $post->ID . '_type_' . $post_type . '_child_ids', 'bbpress');
        wp_cache_delete('bbp_parent_all_' . $post->ID . '_type_' . $post_type . '_child_ids', 'bbpress');
    }
    // Invalidate parent caches
    if ($parent = $wpdb->get_results($wpdb->prepare("SELECT ID, post_type FROM {$wpdb->posts} WHERE ID = %d", $post->post_parent))) {
        bbp_clean_post_cache($parent);
    }
}