bbPress Close All Topics

Cristian Antohe
Last Updated: 22/03/16

With our bbPress forums retired, I wanted to close all topics without having to go in each one of them. While I couldn’t find something that did exactly that, I did found this really nice plugin https://github.com/thebrandonallen/bbp-auto-close-topics that automatically close bbPress topics after an admin-specified time period.

So I stripped it down of any UI since we don’t need that, removed the time specific code and we’re left with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Plugin Name: BBP Close All Topics
 * Plugin URI:  https://github.com/thebrandonallen/bbp-auto-close-topics
 * Description: bbPress plugin to close all topics. Inspired by https://github.com/thebrandonallen/bbp-auto-close-topics/blob/master/bbp-auto-close-topics.php
 * Author:      Cristian Antohe
 * Author URI:  http://www.cozmoslabs.com
 * Version:     0.1
 * License:     GPLv2 or later (license.txt)
 */
 
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
 
/**
 * Hook into 'the_posts' and check the topic status on single topic pages. If
 * the topic is not closed, set it to closed.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @param array $posts Array of post objects.
 * @param object $query WP_Query object.
 * @uses WP_Query::is_singular() To determine if we're on a single topic page.
 * @uses bbp_get_topic_post_type() To get the topic post type.
 * @uses bbp_get_closed_status_id() To get the topic status id.
 * @uses tba_bbp_auto_close_topics() To get auto-close option.
 * @uses tba_bbp_auto_close_age() To get the auto-close age.
 * @uses bbp_get_topic_id() To get the topic id.
 *
 * @return array $posts
 */
function cl_bbp_close_topics_the_posts( $posts, $query ) {
	if ( empty( $posts ) || ! $query->is_singular() ) {
		return $posts;
	}
	// Are we checking a topic?
	if ( $posts[0]->post_type !== bbp_get_topic_post_type() ) {
		return $posts;
	}
	// Are we already closed?
	if ( $posts[0]->post_status === bbp_get_closed_status_id() ) {
		return $posts;
	}
 
	$posts[0]->post_status = bbp_get_closed_status_id();
	return $posts;
}
 
/**
 * Hook into 'the_posts' and check the topic status on single topic pages. If
 * the topic is not closed, set it to closed.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @param string $status Status of supplied topic id.
 * @param int $topic_id The topic id.
 * @uses bbp_get_closed_status_id() To get the topic status id.
 * @uses bbp_get_topic_id() To get the topic id.
 * @uses bbp_is_topic() To verify we're checking a topic.
 * @uses tba_bbp_auto_close_topics() To get auto-close option.
 * @uses tba_bbp_auto_close_age() To get the auto-close age.
 *
 * @return string $status
 */
function cl_bbp_close_topics_topic_status( $status, $topic_id ) {
	// Bail if topic is already closed
	if ( $status === bbp_get_closed_status_id() ) {
		return $status;
	}
	// Validate topic id
	$topic_id = bbp_get_topic_id( $topic_id );
	// Are we checking a topic?
	if ( ! bbp_is_topic( $topic_id ) ) {
		return $status;
	}
 
	$status = bbp_get_closed_status_id();
 
	return $status;
}
 
/**
 * Only load our filters after bbPress has loaded.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @uses add_filter() To load our filters.
 */
function cl_bbp_auto_close_topics_loader() {
	add_filter( 'the_posts', 'cl_bbp_close_topics_the_posts', 10, 2 );
	add_filter( 'bbp_get_topic_status', 'cl_bbp_close_topics_topic_status', 10, 2 );
}
add_action( 'bbp_includes', 'cl_bbp_auto_close_topics_loader' );

You can install it as a plugin by creating a file or simply add the code to your functions.php file and you’re good to close all topics in bbPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.