bbp_toggle_topic_handler( string $action = '' )

Handles the front end opening/closing, spamming/unspamming, sticking/unsticking and trashing/untrashing/deleting of topics

Description

Parameters

$action

(string) (Optional) The requested action to compare this function to.

Default value: ''

Source

File: bp-forums/topics/functions.php

function bbp_toggle_topic_handler( $action = '' ) {

	// Bail if required GET actions aren't passed
	if ( empty( $_GET['topic_id'] ) )
		return;

	// Setup possible get actions
	$possible_actions = array(
		'bbp_toggle_topic_close',
		'bbp_toggle_topic_stick',
		'bbp_toggle_topic_spam',
		'bbp_toggle_topic_trash'
	);

	// Bail if actions aren't meant for this function
	if ( !in_array( $action, $possible_actions ) )
		return;

	$failure   = '';                         // Empty failure string
	$view_all  = false;                      // Assume not viewing all
	$topic_id  = (int) $_GET['topic_id'];    // What's the topic id?
	$success   = false;                      // Flag
	$post_data = array( 'ID' => $topic_id ); // Prelim array
	$redirect  = '';                         // Empty redirect URL

	// Make sure topic exists
	$topic = bbp_get_topic( $topic_id );
	if ( empty( $topic ) )
		return;

	// What is the user doing here?
	if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' === $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
		bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'buddyboss' ) );
		return;
	}

	// What action are we trying to perform?
	switch ( $action ) {

		// Toggle open/close
		case 'bbp_toggle_topic_close' :
			check_ajax_referer( 'close-topic_' . $topic_id );

			$is_open = bbp_is_topic_open( $topic_id );
			$success = true === $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
			$failure = true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the discussion.', 'buddyboss' ) : __( '<strong>ERROR</strong>: There was a problem opening the discussion.', 'buddyboss' );

			break;

		// Toggle sticky/super-sticky/unstick
		case 'bbp_toggle_topic_stick' :
			check_ajax_referer( 'stick-topic_' . $topic_id );

			$is_sticky = bbp_is_topic_sticky( $topic_id );
			$is_super  = false === $is_sticky && !empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false;
			$success   = true  === $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
			$failure   = true  === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the discussion.', 'buddyboss' ) : __( '<strong>ERROR</strong>: There was a problem sticking the discussion.', 'buddyboss' );

			break;

		// Toggle spam
		case 'bbp_toggle_topic_spam' :
			check_ajax_referer( 'spam-topic_' . $topic_id );

			$is_spam  = bbp_is_topic_spam( $topic_id );
			$success  = true === $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
			$failure  = true === $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the discussion as spam.', 'buddyboss' ) : __( '<strong>ERROR</strong>: There was a problem marking the discussion as spam.', 'buddyboss' );
			$view_all = !$is_spam;

			break;

		// Toggle trash
		case 'bbp_toggle_topic_trash' :

			$sub_action = !empty( $_GET['sub_action'] ) && in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;

			if ( empty( $sub_action ) )
				break;

			switch ( $sub_action ) {
				case 'trash':
					check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );

					$view_all = true;
					$success  = wp_trash_post( $topic_id );
					$failure  = __( '<strong>ERROR</strong>: There was a problem trashing the discussion.', 'buddyboss' );

					break;

				case 'untrash':
					check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );

					$success = wp_untrash_post( $topic_id );
					$failure = __( '<strong>ERROR</strong>: There was a problem untrashing the discussion.', 'buddyboss' );

					break;

				case 'delete':
					check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );

					$success = wp_delete_post( $topic_id );
					$failure = __( '<strong>ERROR</strong>: There was a problem deleting the discussion.', 'buddyboss' );

					break;
			}

			break;
	}

	// Do additional topic toggle actions
	do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );

	// No errors
	if ( false !== $success && !is_wp_error( $success ) ) {

		// Redirect back to the topic's forum
		if ( isset( $sub_action ) && ( 'delete' === $sub_action ) ) {
			$redirect = bbp_get_forum_permalink( $success->post_parent );

		// Redirect back to the topic
		} else {

			// Get the redirect detination
			$permalink = bbp_get_topic_permalink( $topic_id );
			$redirect  = bbp_add_view_all( $permalink, $view_all );
		}

		wp_safe_redirect( $redirect );

		// For good measure
		exit();

	// Handle errors
	} else {
		bbp_add_error( 'bbp_toggle_topic', $failure );
	}
}

Changelog

Changelog
Version Description
bbPress (r2727) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.