Skip to content

Instantly share code, notes, and snippets.

@sareiodata
Last active March 22, 2016 07:55
Show Gist options
  • Save sareiodata/49949cea584f55326823 to your computer and use it in GitHub Desktop.
Save sareiodata/49949cea584f55326823 to your computer and use it in GitHub Desktop.
bbpress close all topics
/*
* Close all bbpress topics
*/
/**
* 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.
* @uses bbp_get_topic_last_active_time() To get the topic's last active time.
*
* @return array $posts
*/
function cl_bbp_auto_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.
* @uses bbp_get_topic_last_active_time() To get the topic's last active time.
*
* @return string $status
*/
function cl_bbp_auto_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_auto_close_topics_the_posts', 10, 2 );
add_filter( 'bbp_get_topic_status', 'cl_bbp_auto_close_topics_topic_status', 10, 2 );
}
add_action( 'bbp_includes', 'cl_bbp_auto_close_topics_loader' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment