function bb_delete_topic($topic_id, $new_status = 0)
{
    global $bbdb;
    $topic_id = (int) $topic_id;
    add_filter('get_topic_where', 'bb_no_where');
    if ($topic = get_topic($topic_id)) {
        $new_status = (int) $new_status;
        $old_status = (int) $topic->topic_status;
        if ($new_status == $old_status) {
            return;
        }
        $thread_args = array('per_page' => -1, 'order' => 'DESC');
        if (0 != $old_status && 0 == $new_status) {
            $thread_args['post_status'] = 'all';
        }
        $poster_ids = array();
        $posts = get_thread($topic_id, $thread_args);
        if ($posts && count($posts)) {
            foreach ($posts as $post) {
                _bb_delete_post($post->post_id, $new_status);
                $poster_ids[] = $post->poster_id;
            }
        }
        if (count($poster_ids)) {
            foreach (array_unique($poster_ids) as $id) {
                if ($user = bb_get_user($id)) {
                    $topics_replied_key = $bbdb->prefix . 'topics_replied';
                    bb_update_usermeta($user->ID, $topics_replied_key, $old_status ? $user->{$topics_replied_key} + 1 : $user->{$topics_replied_key} - 1);
                }
            }
        }
        if ($ids = $bbdb->get_col("SELECT user_id, meta_value FROM {$bbdb->usermeta} WHERE meta_key = 'favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0")) {
            foreach ($ids as $id) {
                bb_remove_user_favorite($id, $topic_id);
            }
        }
        switch ($new_status) {
            case 0:
                // Undeleting
                $bbdb->update($bbdb->topics, array('topic_status' => $new_status), compact('topic_id'));
                $topic_posts = (int) $bbdb->get_var($bbdb->prepare("SELECT COUNT(*) FROM {$bbdb->posts} WHERE topic_id = %d AND post_status = 0", $topic_id));
                $all_posts = (int) $bbdb->get_var($bbdb->prepare("SELECT COUNT(*) FROM {$bbdb->posts} WHERE topic_id = %d", $topic_id));
                bb_update_topicmeta($topic_id, 'deleted_posts', $all_posts - $topic_posts);
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id));
                $bbdb->update($bbdb->topics, compact('topic_posts'), compact('topic_id'));
                bb_topic_set_last_post($topic_id);
                bb_update_post_positions($topic_id);
                break;
            default:
                // Other statuses (like Delete and Bozo)
                bb_remove_topic_tags($topic_id);
                $bbdb->update($bbdb->topics, array('topic_status' => $new_status, 'tag_count' => 0), compact('topic_id'));
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id));
                break;
        }
        do_action('bb_delete_topic', $topic_id, $new_status, $old_status);
        wp_cache_delete($topic_id, 'bb_topic');
        wp_cache_delete($topic->topic_slug, 'bb_topic_slug');
        wp_cache_delete($topic_id, 'bb_thread');
        wp_cache_delete($topic->forum_id, 'bb_forum');
        wp_cache_flush('bb_forums');
        wp_cache_flush('bb_query');
        wp_cache_flush('bb_cache_posts_post_ids');
        return $topic_id;
    } else {
        return false;
    }
}
예제 #2
0
function bb_delete_post($post_id, $new_status = 0)
{
    global $bbdb, $topic, $bb_post;
    $post_id = (int) $post_id;
    $bb_post = bb_get_post($post_id);
    $new_status = (int) $new_status;
    $old_status = (int) $bb_post->post_status;
    add_filter('get_topic_where', 'bb_no_where');
    $topic = get_topic($bb_post->topic_id);
    $topic_id = (int) $topic->topic_id;
    if ($bb_post) {
        $uid = (int) $bb_post->poster_id;
        if ($new_status == $old_status) {
            return;
        }
        _bb_delete_post($post_id, $new_status);
        if (0 == $old_status) {
            bb_update_topicmeta($topic_id, 'deleted_posts', $topic->deleted_posts + 1);
            $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET posts = posts - 1 WHERE forum_id = %d", $topic->forum_id));
        } else {
            if (0 == $new_status) {
                bb_update_topicmeta($topic_id, 'deleted_posts', $topic->deleted_posts - 1);
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET posts = posts + 1 WHERE forum_id = %d", $topic->forum_id));
            }
        }
        $posts = (int) $bbdb->get_var($bbdb->prepare("SELECT COUNT(*) FROM {$bbdb->posts} WHERE topic_id = %d AND post_status = 0", $topic_id));
        $bbdb->update($bbdb->topics, array('topic_posts' => $posts), compact('topic_id'));
        if (0 == $posts) {
            if (0 == $topic->topic_status || 1 == $new_status) {
                bb_delete_topic($topic_id, $new_status);
            }
        } else {
            if (0 != $topic->topic_status) {
                $bbdb->update($bbdb->topics, array('topic_status' => 0), compact('topic_id'));
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics + 1 WHERE forum_id = %d", $topic->forum_id));
            }
            bb_topic_set_last_post($topic_id);
            bb_update_post_positions($topic_id);
            bb_update_topic_voices($topic_id);
        }
        $user = bb_get_user($uid);
        $user_posts = new BB_Query('post', array('post_author_id' => $user->ID, 'topic_id' => $topic_id));
        if ($new_status && !$user_posts->results) {
            $topics_replied_key = $bbdb->prefix . 'topics_replied';
            bb_update_usermeta($user->ID, $topics_replied_key, $user->{$topics_replied_key} - 1);
        }
        nxt_cache_delete($topic_id, 'bb_topic');
        nxt_cache_delete($topic_id, 'bb_thread');
        nxt_cache_flush('bb_forums');
        nxt_cache_flush('bb_query');
        nxt_cache_flush('bb_cache_posts_post_ids');
        do_action('bb_delete_post', $post_id, $new_status, $old_status);
        return $post_id;
    } else {
        return false;
    }
}