bbp_edit_topic_tag_handler

Handles the front end tag management (renaming, merging, destroying).

Description

bbp_edit_topic_tag_handler( (string) $action = '' ); 

Parameters (1)

0. $action — Optional. (string) => ''
The requested action to compare this function to

Usage

  1. if ( !function_exists( 'bbp_edit_topic_tag_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The requested action to compare this function to 
  5. $action = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_edit_topic_tag_handler($action); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/topics/functions.php  
  1. function bbp_edit_topic_tag_handler( $action = '' ) { 
  2.  
  3. // Bail if required POST actions aren't passed 
  4. if ( empty( $_POST['tag-id'] ) ) 
  5. return; 
  6.  
  7. // Setup possible get actions 
  8. $possible_actions = array( 
  9. 'bbp-update-topic-tag',  
  10. 'bbp-merge-topic-tag',  
  11. 'bbp-delete-topic-tag' 
  12. ); 
  13.  
  14. // Bail if actions aren't meant for this function 
  15. if ( !in_array( $action, $possible_actions ) ) 
  16. return; 
  17.  
  18. // Setup vars 
  19. $tag_id = (int) $_POST['tag-id']; 
  20. $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() ); 
  21.  
  22. // Tag does not exist 
  23. if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 
  24. bbp_add_error( 'bbp_manage_topic_invalid_tag', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while getting the tag: %s', bbpress ), $tag->get_error_message() ) ); 
  25. return; 
  26.  
  27. // What action are we trying to perform? 
  28. switch ( $action ) { 
  29.  
  30. // Update tag 
  31. case 'bbp-update-topic-tag' : 
  32.  
  33. // Nonce check 
  34. if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) { 
  35. bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  36. return; 
  37.  
  38. // Can user edit topic tags? 
  39. if ( !current_user_can( 'edit_topic_tags' ) ) { 
  40. bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', bbpress ) ); 
  41. return; 
  42.  
  43. // No tag name was provided 
  44. if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) { 
  45. bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', bbpress ) ); 
  46. return; 
  47.  
  48. // Attempt to update the tag 
  49. $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : ''; 
  50. $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) ); 
  51.  
  52. // Cannot update tag 
  53. if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 
  54. 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', bbpress ), $tag->get_error_message() ) ); 
  55. return; 
  56.  
  57. // Redirect 
  58. $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() ); 
  59.  
  60. // Update counts, etc... 
  61. do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug ); 
  62.  
  63. break; 
  64.  
  65. // Merge two tags 
  66. case 'bbp-merge-topic-tag' : 
  67.  
  68. // Nonce check 
  69. if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) { 
  70. bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  71. return; 
  72.  
  73. // Can user edit topic tags? 
  74. if ( !current_user_can( 'edit_topic_tags' ) ) { 
  75. bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', bbpress ) ); 
  76. return; 
  77.  
  78. // No tag name was provided 
  79. if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) { 
  80. bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', bbpress ) ); 
  81. return; 
  82.  
  83. // If term does not exist, create it 
  84. if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) ) 
  85. $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() ); 
  86.  
  87. // Problem inserting the new term 
  88. if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 
  89. 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', bbpress ), $tag->get_error_message() ) ); 
  90. return; 
  91.  
  92. // Merging in to... 
  93. $to_tag = $tag['term_id']; 
  94.  
  95. // Attempting to merge a tag into itself 
  96. if ( $tag_id === $to_tag ) { 
  97. bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', bbpress ) ); 
  98. return; 
  99.  
  100. // Delete the old term 
  101. $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) ); 
  102.  
  103. // Error merging the terms 
  104. if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 
  105. 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', bbpress ), $tag->get_error_message() ) ); 
  106. return; 
  107.  
  108. // Redirect 
  109. $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() ); 
  110.  
  111. // Update counts, etc... 
  112. do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag ); 
  113.  
  114. break; 
  115.  
  116. // Delete tag 
  117. case 'bbp-delete-topic-tag' : 
  118.  
  119. // Nonce check 
  120. if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) { 
  121. bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  122. return; 
  123.  
  124. // Can user delete topic tags? 
  125. if ( !current_user_can( 'delete_topic_tags' ) ) { 
  126. bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', bbpress ) ); 
  127. return; 
  128.  
  129. // Attempt to delete term 
  130. $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() ); 
  131.  
  132. // Error deleting term 
  133. if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 
  134. 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', bbpress ), $tag->get_error_message() ) ); 
  135. return; 
  136.  
  137. // We don't have any other place to go other than home! Or we may die because of the 404 disease 
  138. $redirect = home_url(); 
  139.  
  140. // Update counts, etc... 
  141. do_action( 'bbp_delete_topic_tag', $tag_id, $tag ); 
  142.  
  143. break; 
  144.  
  145. /** Successful Moderation *************************************************/ 
  146.  
  147. // Redirect back 
  148. $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url(); 
  149. wp_safe_redirect( $redirect ); 
  150.  
  151. // For good measure 
  152. exit();