BBP_Topics_Admin::toggle_topic()

Toggle topic


Description Description

Handles the admin-side opening/closing, sticking/unsticking and spamming/unspamming of topics


Source Source

File: includes/admin/topics.php

	public function toggle_topic() {

		// Bail if not a topic toggle action
		if ( ! bbp_is_get_request() || empty( $_GET['action'] ) || empty( $_GET['topic_id'] ) ) {
			return;
		}

		// Bail if not an allowed action
		$action = sanitize_key( $_GET['action'] );
		if ( empty( $action ) || ! in_array( $action, $this->get_allowed_action_toggles(), true ) ) {
			return;
		}

		// Bail if topic is missing
		$topic_id = bbp_get_topic_id( $_GET['topic_id'] );
		if ( ! bbp_get_topic( $topic_id ) ) {
			wp_die( esc_html__( 'The topic was not found.', 'bbpress' ) );
		}

		// What is the user doing here?
		if ( ! current_user_can( 'moderate', $topic_id ) ) {
			wp_die( esc_html__( 'You do not have permission to do that.', 'bbpress' ) );
		}

		// Defaults
		$post_data = array( 'ID' => $topic_id );
		$message   = '';
		$success   = false;

		switch ( $action ) {
			case 'bbp_toggle_topic_approve' :
				check_admin_referer( 'approve-topic_' . $topic_id );

				$is_approve = bbp_is_topic_public( $topic_id );
				$message    = ( true === $is_approve )
					? 'unapproved'
					: 'approved';
				$success    = ( true === $is_approve )
					? bbp_unapprove_topic( $topic_id )
					: bbp_approve_topic( $topic_id );

				break;

			case 'bbp_toggle_topic_close' :
				check_admin_referer( 'close-topic_' . $topic_id );

				$is_open = bbp_is_topic_open( $topic_id );
				$message = ( true === $is_open )
					? 'closed'
					: 'opened';
				$success = ( true === $is_open )
					? bbp_close_topic( $topic_id )
					: bbp_open_topic( $topic_id );

				break;

			case 'bbp_toggle_topic_stick' :
				check_admin_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;
				$message   = ( true  === $is_sticky )
					? 'unstuck'
					: 'stuck';
				$message   = ( true  === $is_super )
					? 'super_sticky'
					: $message;
				$success   = ( true  === $is_sticky )
					? bbp_unstick_topic( $topic_id )
					: bbp_stick_topic( $topic_id, $is_super );

				break;

			case 'bbp_toggle_topic_spam'  :
				check_admin_referer( 'spam-topic_' . $topic_id );

				$is_spam = bbp_is_topic_spam( $topic_id );
				$message = ( true === $is_spam )
					? 'unspammed'
					: 'spammed';
				$success = ( true === $is_spam )
					? bbp_unspam_topic( $topic_id )
					: bbp_spam_topic( $topic_id );

				break;
		}

		// Setup the message
		$retval = array(
			'bbp_topic_toggle_notice' => $message,
			'topic_id'                => $topic_id
		);

		// Prepare for failure
		if ( ( false === $success ) || is_wp_error( $success ) ) {
			$retval['failed'] = '1';
		}

		// Filter all message args
		$retval = apply_filters( 'bbp_toggle_topic_action_admin', $retval, $topic_id, $action );

		// Do additional topic toggle actions (admin side)
		do_action( 'bbp_toggle_topic_admin', $success, $post_data, $action, $retval );

		// Redirect back to the topic
		$redirect = add_query_arg( $retval, remove_query_arg( array( 'action', 'topic_id' ) ) );
		bbp_redirect( $redirect );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.