hidden_forums' ) ) { return; } // Define local variables $forum_id = 0; $wp_query = bbp_get_wp_query(); // Check post type switch ( $wp_query->get( 'post_type' ) ) { // Forum case bbp_get_forum_post_type() : $forum_id = bbp_get_forum_id( $wp_query->post->ID ); break; // Topic case bbp_get_topic_post_type() : $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID ); break; // Reply case bbp_get_reply_post_type() : $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); break; } // If forum is explicitly hidden and user not capable, set 404 if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) { bbp_set_404( $wp_query ); } } /** * Check if it's a private forum or a topic or reply of a private forum and if * the user can't view it, then sets a 404 * * @since 2.0.0 bbPress (r2996) */ function bbp_forum_enforce_private() { // Bail if not viewing a single item or if user has caps if ( ! is_singular() || bbp_is_user_keymaster() || current_user_can( 'read_private_forums' ) ) { return; } // Define local variables $forum_id = 0; $wp_query = bbp_get_wp_query(); // Check post type switch ( $wp_query->get( 'post_type' ) ) { // Forum case bbp_get_forum_post_type() : $forum_id = bbp_get_forum_id( $wp_query->post->ID ); break; // Topic case bbp_get_topic_post_type() : $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID ); break; // Reply case bbp_get_reply_post_type() : $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); break; } // If forum is explicitly hidden and user not capable, set 404 if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) { bbp_set_404( $wp_query ); } } /** Permissions ***************************************************************/ /** * Redirect if unauthorized user is attempting to edit a forum * * @since 2.1.0 bbPress (r3607) */ function bbp_check_forum_edit() { // Bail if not editing a topic if ( ! bbp_is_forum_edit() ) { return; } // User cannot edit topic, so redirect back to reply if ( ! current_user_can( 'edit_forum', bbp_get_forum_id() ) ) { bbp_redirect( bbp_get_forum_permalink() ); } } /** * Delete all topics (and their replies) for a specific forum ID * * @since 2.1.0 bbPress (r3668) * * @param int $forum_id * @return If forum is not valid */ function bbp_delete_forum_topics( $forum_id = 0 ) { // Validate forum ID $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) ) { return; } // Forum is being permanently deleted, so its content has go too // Note that we get all post statuses here $topics = new WP_Query( array( 'fields' => 'id=>parent', 'post_type' => bbp_get_topic_post_type(), 'post_parent' => $forum_id, 'post_status' => array_keys( get_post_stati() ), 'posts_per_page' => -1, // Performance 'nopaging' => true, 'suppress_filters' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'ignore_sticky_posts' => true, 'no_found_rows' => true ) ); // Loop through and delete child topics. Topic replies will get deleted by // the bbp_delete_topic() action. if ( ! empty( $topics->posts ) ) { foreach ( $topics->posts as $topic ) { wp_delete_post( $topic->ID, true ); } // Reset the $post global wp_reset_postdata(); } // Cleanup unset( $topics ); } /** * Trash all topics inside a forum * * @since 2.1.0 bbPress (r3668) * * @param int $forum_id * @return If forum is not valid */ function bbp_trash_forum_topics( $forum_id = 0 ) { // Validate forum ID $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) ) { return; } // Allowed post statuses to pre-trash $post_stati = array( bbp_get_public_status_id(), bbp_get_closed_status_id(), bbp_get_pending_status_id() ); // Forum is being trashed, so its topics (and replies) are trashed too $topics = new WP_Query( array( 'fields' => 'id=>parent', 'post_type' => bbp_get_topic_post_type(), 'post_parent' => $forum_id, 'post_status' => $post_stati, 'posts_per_page' => -1, // Performance 'nopaging' => true, 'suppress_filters' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'ignore_sticky_posts' => true, 'no_found_rows' => true ) ); // Loop through and trash child topics. Topic replies will get trashed by // the bbp_trash_topic() action. if ( ! empty( $topics->posts ) ) { // Prevent debug notices $pre_trashed_topics = array(); // Loop through topics, trash them, and add them to array foreach ( $topics->posts as $topic ) { wp_trash_post( $topic->ID, true ); $pre_trashed_topics[] = $topic->ID; } // Set a post_meta entry of the topics that were trashed by this action. // This is so we can possibly untrash them, without untrashing topics // that were purposefully trashed before. update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics ); // Reset the $post global wp_reset_postdata(); } // Cleanup unset( $topics ); } /** * Untrash all topics inside a forum * * @since 2.1.0 bbPress (r3668) * * @param int $forum_id * @return If forum is not valid */ function bbp_untrash_forum_topics( $forum_id = 0 ) { // Validate forum ID $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) ) { return; } // Get the topics that were not previously trashed $pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true ); // There are topics to untrash if ( ! empty( $pre_trashed_topics ) ) { // Maybe reverse the trashed topics array if ( is_array( $pre_trashed_topics ) ) { $pre_trashed_topics = array_reverse( $pre_trashed_topics ); } // Loop through topics foreach ( (array) $pre_trashed_topics as $topic ) { wp_untrash_post( $topic ); } } } /** Before Delete/Trash/Untrash ***********************************************/ /** * Called before deleting a forum. * * This function is supplemental to the actual forum deletion which is * handled by WordPress core API functions. It is used to clean up after * a forum that is being deleted. * * @since 2.1.0 bbPress (r3668) */ function bbp_delete_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_delete_forum', $forum_id ); } /** * Called before trashing a forum * * This function is supplemental to the actual forum being trashed which is * handled by WordPress core API functions. It is used to clean up after * a forum that is being trashed. * * @since 2.1.0 bbPress (r3668) */ function bbp_trash_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_trash_forum', $forum_id ); } /** * Called before untrashing a forum * * @since 2.1.0 bbPress (r3668) */ function bbp_untrash_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_untrash_forum', $forum_id ); } /** After Delete/Trash/Untrash ************************************************/ /** * Called after deleting a forum * * Try not to use this action. All meta & taxonomy terms have already been * deleted, making them impossible to use. * * @since 2.1.0 bbPress (r3668) * @since 2.6.0 bbPress (r6526) Not recommend for usage */ function bbp_deleted_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_deleted_forum', $forum_id ); } /** * Called after trashing a forum * * @since 2.1.0 bbPress (r3668) */ function bbp_trashed_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_trashed_forum', $forum_id ); } /** * Called after untrashing a forum * * @since 2.1.0 bbPress (r3668) */ function bbp_untrashed_forum( $forum_id = 0 ) { $forum_id = bbp_get_forum_id( $forum_id ); if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { return false; } do_action( 'bbp_untrashed_forum', $forum_id ); }