bbp_edit_topic_tag_handler( string $action = '' )
Handles the front end tag management (renaming, merging, destroying)
Description
Parameters
- $action
-
(Optional) The requested action to compare this function to
Default value: ''
Source
File: bp-forums/topics/functions.php
function bbp_edit_topic_tag_handler( $action = '' ) { // Bail if required POST actions aren't passed if ( empty( $_POST['tag-id'] ) ) return; // Setup possible get actions $possible_actions = array( 'bbp-update-topic-tag', 'bbp-merge-topic-tag', 'bbp-delete-topic-tag' ); // Bail if actions aren't meant for this function if ( !in_array( $action, $possible_actions ) ) return; // Setup vars $tag_id = (int) $_POST['tag-id']; $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() ); // Tag does not exist if ( is_wp_error( $tag ) && $tag->get_error_message() ) { bbp_add_error( 'bbp_manage_topic_invalid_tag', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while getting the tag: %s', 'buddyboss' ), $tag->get_error_message() ) ); return; } // What action are we trying to perform? switch ( $action ) { // Update tag case 'bbp-update-topic-tag' : // Nonce check if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) { bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) ); return; } // Can user edit topic tags? if ( !current_user_can( 'edit_topic_tags' ) ) { bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the discussion tags.', 'buddyboss' ) ); return; } // No tag name was provided if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) { bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'buddyboss' ) ); return; } // Attempt to update the tag $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : ''; $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) ); // Cannot update tag if ( is_wp_error( $tag ) && $tag->get_error_message() ) { bbp_add_error( 'bbp_manage_topic_tag_update_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while updating the tag: %s', 'buddyboss' ), $tag->get_error_message() ) ); return; } // Redirect $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() ); // Update counts, etc... do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug ); break; // Merge two tags case 'bbp-merge-topic-tag' : // Nonce check if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) { bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) ); return; } // Can user edit topic tags? if ( !current_user_can( 'edit_topic_tags' ) ) { bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the discussion tags.', 'buddyboss' ) ); return; } // No tag name was provided if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) { bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'buddyboss' ) ); return; } // If term does not exist, create it if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) ) $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() ); // Problem inserting the new term if ( is_wp_error( $tag ) && $tag->get_error_message() ) { bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'buddyboss' ), $tag->get_error_message() ) ); return; } // Merging in to... $to_tag = $tag['term_id']; // Attempting to merge a tag into itself if ( $tag_id === $to_tag ) { bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'buddyboss' ) ); return; } // Delete the old term $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) ); // Error merging the terms if ( is_wp_error( $tag ) && $tag->get_error_message() ) { bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'buddyboss' ), $tag->get_error_message() ) ); return; } // Redirect $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() ); // Update counts, etc... do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag ); break; // Delete tag case 'bbp-delete-topic-tag' : // Nonce check if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) { bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) ); return; } // Can user delete topic tags? if ( !current_user_can( 'delete_topic_tags' ) ) { bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the discussion tags.', 'buddyboss' ) ); return; } // Attempt to delete term $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() ); // Error deleting term if ( is_wp_error( $tag ) && $tag->get_error_message() ) { bbp_add_error( 'bbp_manage_topic_tag_delete_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while deleting the tag: %s', 'buddyboss' ), $tag->get_error_message() ) ); return; } // We don't have any other place to go other than home! Or we may die because of the 404 disease $redirect = home_url(); // Update counts, etc... do_action( 'bbp_delete_topic_tag', $tag_id, $tag ); break; } /** Successful Moderation *************************************************/ // Redirect back $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url(); wp_safe_redirect( $redirect ); // For good measure exit(); }
Changelog
Version | Description |
---|---|
bbPress (r2768) | 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.