Skip to:
Content

bbPress.org

Changeset 3382


Ignore:
Timestamp:
08/07/2011 02:07:20 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Add bbp_add_error() and bbp_has_error() functions to handle error adding and checking, and use through-out project. Rejig functions with early GET and POST checks to bail early rather than wrap routine in an if statement. Fixes issue where removing favorites and subscriptions from user profile pages would redirect incorrectly. Fixes issue where spamming and trashing topics and replies would not force view=all in some cases.

Location:
branches/plugin/bbp-includes
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-common-functions.php

    r3373 r3382  
    684684    // Filter variables and add errors if necessary
    685685    if ( !$bbp_anonymous_name  = apply_filters( 'bbp_pre_anonymous_post_author_name',  $bbp_anonymous_name  ) )
    686         $bbp->errors->add( 'bbp_anonymous_name',  __( '<strong>ERROR</strong>: Invalid author name submitted!',   'bbpress' ) );
     686        bbp_add_error( 'bbp_anonymous_name',  __( '<strong>ERROR</strong>: Invalid author name submitted!',   'bbpress' ) );
    687687
    688688    if ( !$bbp_anonymous_email = apply_filters( 'bbp_pre_anonymous_post_author_email', $bbp_anonymous_email ) )
    689         $bbp->errors->add( 'bbp_anonymous_email', __( '<strong>ERROR</strong>: Invalid email address submitted!', 'bbpress' ) );
     689        bbp_add_error( 'bbp_anonymous_email', __( '<strong>ERROR</strong>: Invalid email address submitted!', 'bbpress' ) );
    690690
    691691    // Website is optional
    692692    $bbp_anonymous_website = apply_filters( 'bbp_pre_anonymous_post_author_website', $bbp_anonymous_website );
    693693
    694     if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() )
     694    if ( !bbp_has_errors() )
    695695        $retval = compact( 'bbp_anonymous_name', 'bbp_anonymous_email', 'bbp_anonymous_website' );
    696696    else
     
    13461346}
    13471347
     1348/** Errors ********************************************************************/
     1349
     1350/**
     1351 * Adds an error message to later be output in the theme
     1352 *
     1353 * @since bbPress (r3381)
     1354 *
     1355 * @global bbPress $bbp
     1356 *
     1357 * @see WP_Error()
     1358 * @uses WP_Error::add();
     1359 *
     1360 * @param string $code Unique code for the error message
     1361 * @param string $message Translated error message
     1362 * @param string $data Any additional data passed with the error message
     1363 */
     1364function bbp_add_error( $code = '', $message = '', $data = '' ) {
     1365    global $bbp;
     1366
     1367    $bbp->errors->add( $code, $message, $data );
     1368}
     1369
     1370/**
     1371 * Check if error messages exist in queue
     1372 *
     1373 * @since bbPress (r3381)
     1374 *
     1375 * @global bbPress $bbp
     1376 *
     1377 * @see WP_Error()
     1378 *
     1379 * @uses is_wp_error()
     1380 * @usese WP_Error::get_error_codes()
     1381 */
     1382function bbp_has_errors() {
     1383    global $bbp;
     1384   
     1385    // Assume no errors
     1386    $has_errors = false;
     1387
     1388    // Check for errors
     1389    if ( $bbp->errors->get_error_codes() )
     1390        $has_errors = true;
     1391
     1392    // Filter return value
     1393    $has_errors = apply_filters( 'bbp_has_errors', $has_errors, $bbp->errors );
     1394   
     1395    return $has_errors;
     1396}
     1397
    13481398?>
  • branches/plugin/bbp-includes/bbp-common-template.php

    r3348 r3382  
    16371637
    16381638    // Bail if no notices or errors
    1639     if ( !isset( $bbp->errors ) || !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() )
     1639    if ( !isset( $bbp->errors ) || !bbp_has_errors() )
    16401640        return;
    16411641
  • branches/plugin/bbp-includes/bbp-reply-functions.php

    r3349 r3382  
    6666
    6767    // Update the topic
    68     if ( $topic_id = bbp_get_reply_topic_id( $reply_id ) )
     68    $topic_id = bbp_get_reply_topic_id( $reply_id );
     69    if ( !empty( $topic_id ) )
    6970        bbp_update_topic( $topic_id );
    7071
     
    108109function bbp_new_reply_handler() {
    109110
    110     // Only proceed if POST is a new reply
    111     if ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) && !empty( $_POST['action'] ) && ( 'bbp-new-reply' === $_POST['action'] ) ) {
    112         global $bbp;
    113 
    114         // Nonce check
    115         check_admin_referer( 'bbp-new-reply' );
    116 
    117         // Define local variable(s)
    118         $topic_id = $forum_id = $reply_author = $anonymous_data = 0;
    119         $reply_title = $reply_content = $terms = '';
    120 
    121         /** Reply Author ******************************************************/
    122 
    123         // User is anonymous
    124         if ( bbp_is_anonymous() ) {
    125 
    126             // Filter anonymous data
    127             $anonymous_data = bbp_filter_anonymous_post_data();
    128 
    129             // Anonymous data checks out, so set cookies, etc...
    130             if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
    131                 bbp_set_current_anonymous_user_data( $anonymous_data );
     111    // Bail if not a POST action
     112    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     113        return;
     114
     115    // Bail if action is not bbp-new-reply
     116    if ( empty( $_POST['action'] ) || ( 'bbp-new-reply' !== $_POST['action'] ) )
     117        return;
     118
     119    global $bbp;
     120
     121    // Nonce check
     122    check_admin_referer( 'bbp-new-reply' );
     123
     124    // Define local variable(s)
     125    $topic_id = $forum_id = $reply_author = $anonymous_data = 0;
     126    $reply_title = $reply_content = $terms = '';
     127
     128    /** Reply Author ******************************************************/
     129
     130    // User is anonymous
     131    if ( bbp_is_anonymous() ) {
     132
     133        // Filter anonymous data
     134        $anonymous_data = bbp_filter_anonymous_post_data();
     135
     136        // Anonymous data checks out, so set cookies, etc...
     137        if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
     138            bbp_set_current_anonymous_user_data( $anonymous_data );
     139        }
     140
     141    // User is logged in
     142    } else {
     143
     144        // User cannot create replies
     145        if ( !current_user_can( 'publish_replies' ) ) {
     146            bbp_add_error( 'bbp_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress' ) );
     147        }
     148
     149        // Reply author is current user
     150        $reply_author = bbp_get_current_user_id();
     151
     152    }
     153
     154    /** Topic ID **********************************************************/
     155
     156    // Handle Topic ID to append reply to
     157    if ( isset( $_POST['bbp_topic_id'] ) && ( !$topic_id = (int) $_POST['bbp_topic_id'] ) )
     158        bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID is missing.', 'bbpress' ) );
     159
     160    /** Forum ID **********************************************************/
     161
     162    // Handle Forum ID to adjust counts of
     163    if ( isset( $_POST['bbp_forum_id'] ) && ( !$forum_id = (int) $_POST['bbp_forum_id'] ) )
     164        bbp_add_error( 'bbp_reply_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
     165
     166    /** Unfiltered HTML ***************************************************/
     167
     168    // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
     169    if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $topic_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
     170        remove_filter( 'bbp_new_reply_pre_title',   'wp_filter_kses' );
     171        remove_filter( 'bbp_new_reply_pre_content', 'wp_filter_kses' );
     172    }
     173
     174    /** Reply Title *******************************************************/
     175
     176    if ( !empty( $_POST['bbp_reply_title'] ) )
     177        $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
     178
     179    // Filter and sanitize
     180    $reply_title = apply_filters( 'bbp_new_reply_pre_title', $reply_title );
     181
     182    // No reply title
     183    if ( empty( $reply_title ) )
     184        bbp_add_error( 'bbp_reply_title', __( '<strong>ERROR</strong>: Your reply needs a title.', 'bbpress' ) );
     185
     186    /** Reply Content *****************************************************/
     187
     188    if ( !empty( $_POST['bbp_reply_content'] ) )
     189        $reply_content = $_POST['bbp_reply_content'];
     190
     191    // Filter and sanitize
     192    $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_content );
     193
     194    // No reply content
     195    if ( empty( $reply_content ) )
     196        bbp_add_error( 'bbp_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
     197
     198    /** Reply Flooding ****************************************************/
     199
     200    if ( !bbp_check_for_flood( $anonymous_data, $reply_author ) )
     201        bbp_add_error( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
     202
     203    /** Reply Duplicate ***************************************************/
     204
     205    if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data ) ) )
     206        bbp_add_error( 'bbp_reply_duplicate', __( '<strong>ERROR</strong>: Duplicate reply detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
     207
     208    /** Topic Tags ********************************************************/
     209
     210    if ( !empty( $_POST['bbp_topic_tags'] ) )
     211        $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
     212
     213    /** Additional Actions (Before Save) **********************************/
     214
     215    do_action( 'bbp_new_reply_pre_extras' );
     216
     217    /** No Errors *********************************************************/
     218
     219    // Handle insertion into posts table
     220    if ( !bbp_has_errors() ) {
     221
     222        /** Create new reply **********************************************/
     223
     224        // Add the content of the form to $post as an array
     225        $reply_data = array(
     226            'post_author'  => $reply_author,
     227            'post_title'   => $reply_title,
     228            'post_content' => $reply_content,
     229            'post_parent'  => $topic_id,
     230            'post_status'  => 'publish',
     231            'post_type'    => bbp_get_reply_post_type()
     232        );
     233
     234        // Just in time manipulation of reply data before being created
     235        $reply_data = apply_filters( 'bbp_new_reply_pre_insert', $reply_data );
     236
     237        // Insert reply
     238        $reply_id = wp_insert_post( $reply_data );
     239
     240        /** No Errors *****************************************************/
     241
     242        // Check for missing reply_id or error
     243        if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
     244
     245            /** Topic Tags ************************************************/
     246
     247            // Just in time manipulation of reply terms before being edited
     248            $terms = apply_filters( 'bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id );
     249
     250            // Insert terms
     251            $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
     252
     253            // Term error
     254            if ( is_wp_error( $terms ) ) {
     255                bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
    132256            }
    133257
    134         // User is logged in
     258            /** Trash Check ***********************************************/
     259
     260            // If this reply starts as trash, add it to pre_trashed_replies
     261            // for the topic, so it is properly restored.
     262            if ( bbp_is_topic_trash( $topic_id ) || ( $reply_data['post_status'] == $bbp->trash_status_id ) ) {
     263
     264                // Trash the reply
     265                wp_trash_post( $reply_id );
     266
     267                // Get pre_trashed_replies for topic
     268                $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
     269
     270                // Add this reply to the end of the existing replies
     271                $pre_trashed_replies[] = $reply_id;
     272
     273                // Update the pre_trashed_reply post meta
     274                update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
     275            }
     276
     277            /** Spam Check ************************************************/
     278
     279            // If reply or topic are spam, officially spam this reply
     280            if ( bbp_is_topic_spam( $topic_id ) || ( $reply_data['post_status'] == $bbp->spam_status_id ) )
     281                add_post_meta( $reply_id, '_bbp_spam_meta_status', 'publish' );
     282
     283            /** Update counts, etc... *************************************/
     284
     285            do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author );
     286
     287            /** Redirect **************************************************/
     288
     289            // Redirect to
     290            $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
     291
     292            // Get the reply URL
     293            $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
     294
     295            // Allow to be filtered
     296            $reply_url = apply_filters( 'bbp_new_reply_redirect_to', $reply_url, $redirect_to );
     297
     298            /** Successful Save *******************************************/
     299
     300            // Redirect back to new reply
     301            wp_safe_redirect( $reply_url );
     302
     303            // For good measure
     304            exit();
     305
     306        /** Errors ********************************************************/
     307
    135308        } else {
    136 
    137             // User cannot create replies
    138             if ( !current_user_can( 'publish_replies' ) ) {
    139                 $bbp->errors->add( 'bbp_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress' ) );
    140             }
    141 
    142             // Reply author is current user
    143             $reply_author = bbp_get_current_user_id();
    144 
    145         }
    146 
    147         /** Topic ID **********************************************************/
    148 
    149         // Handle Topic ID to append reply to
    150         if ( isset( $_POST['bbp_topic_id'] ) && ( !$topic_id = (int) $_POST['bbp_topic_id'] ) )
    151             $bbp->errors->add( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID is missing.', 'bbpress' ) );
    152 
    153         /** Forum ID **********************************************************/
    154 
    155         // Handle Forum ID to adjust counts of
    156         if ( isset( $_POST['bbp_forum_id'] ) && ( !$forum_id = (int) $_POST['bbp_forum_id'] ) )
    157             $bbp->errors->add( 'bbp_reply_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
    158 
    159         /** Unfiltered HTML ***************************************************/
    160 
    161         // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
    162         if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $topic_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
    163             remove_filter( 'bbp_new_reply_pre_title',   'wp_filter_kses' );
    164             remove_filter( 'bbp_new_reply_pre_content', 'wp_filter_kses' );
    165         }
    166 
    167         /** Reply Title *******************************************************/
    168 
    169         if ( !empty( $_POST['bbp_reply_title'] ) )
    170             $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
    171 
    172         // Filter and sanitize
    173         $reply_title = apply_filters( 'bbp_new_reply_pre_title', $reply_title );
    174 
    175         // No reply title
    176         if ( empty( $reply_title ) )
    177             $bbp->errors->add( 'bbp_reply_title', __( '<strong>ERROR</strong>: Your reply needs a title.', 'bbpress' ) );
    178 
    179         /** Reply Content *****************************************************/
    180 
    181         if ( !empty( $_POST['bbp_reply_content'] ) )
    182             $reply_content = $_POST['bbp_reply_content'];
    183 
    184         // Filter and sanitize
    185         $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_content );
    186 
    187         // No reply content
    188         if ( empty( $reply_content ) )
    189             $bbp->errors->add( 'bbp_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
    190 
    191         /** Reply Flooding ****************************************************/
    192 
    193         if ( !bbp_check_for_flood( $anonymous_data, $reply_author ) )
    194             $bbp->errors->add( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
    195 
    196         /** Reply Duplicate ***************************************************/
    197 
    198         if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data ) ) )
    199             $bbp->errors->add( 'bbp_reply_duplicate', __( '<strong>ERROR</strong>: Duplicate reply detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
    200 
    201         /** Topic Tags ********************************************************/
    202 
    203         if ( !empty( $_POST['bbp_topic_tags'] ) )
    204             $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
    205 
    206         /** Additional Actions (Before Save) **********************************/
    207 
    208         do_action( 'bbp_new_reply_pre_extras' );
    209 
    210         /** No Errors *********************************************************/
    211 
    212         // Handle insertion into posts table
    213         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    214 
    215             /** Create new reply **********************************************/
    216 
    217             // Add the content of the form to $post as an array
    218             $reply_data = array(
    219                 'post_author'  => $reply_author,
    220                 'post_title'   => $reply_title,
    221                 'post_content' => $reply_content,
    222                 'post_parent'  => $topic_id,
    223                 'post_status'  => 'publish',
    224                 'post_type'    => bbp_get_reply_post_type()
    225             );
    226 
    227             // Just in time manipulation of reply data before being created
    228             $reply_data = apply_filters( 'bbp_new_reply_pre_insert', $reply_data );
    229 
    230             // Insert reply
    231             $reply_id = wp_insert_post( $reply_data );
    232 
    233             /** No Errors *****************************************************/
    234 
    235             // Check for missing reply_id or error
    236             if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
    237 
    238                 /** Topic Tags ************************************************/
    239 
    240                 // Just in time manipulation of reply terms before being edited
    241                 $terms = apply_filters( 'bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id );
    242 
    243                 // Insert terms
    244                 $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
    245 
    246                 // Term error
    247                 if ( is_wp_error( $terms ) )
    248                     $bbp->errors->add( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was some problem adding the tags to the topic.', 'bbpress' ) );
    249 
    250                 /** Trash Check ***********************************************/
    251 
    252                 // If this reply starts as trash, add it to pre_trashed_replies
    253                 // for the topic, so it is properly restored.
    254                 if ( bbp_is_topic_trash( $topic_id ) || ( $reply_data['post_status'] == $bbp->trash_status_id ) ) {
    255 
    256                     // Trash the reply
    257                     wp_trash_post( $reply_id );
    258 
    259                     // Get pre_trashed_replies for topic
    260                     $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
    261 
    262                     // Add this reply to the end of the existing replies
    263                     $pre_trashed_replies[] = $reply_id;
    264 
    265                     // Update the pre_trashed_reply post meta
    266                     update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
    267                 }
    268 
    269                 /** Spam Check ************************************************/
    270                
    271                 // If reply or topic are spam, officially spam this reply
    272                 if ( bbp_is_topic_spam( $topic_id ) || ( $reply_data['post_status'] == $bbp->spam_status_id ) )
    273                     add_post_meta( $reply_id, '_bbp_spam_meta_status', 'publish' );
    274 
    275                 /** Update counts, etc... *************************************/
    276 
    277                 do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author );
    278 
    279                 /** Redirect **************************************************/
    280 
    281                 // Redirect to
    282                 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
    283 
    284                 // Get the reply URL
    285                 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
    286 
    287                 // Allow to be filtered
    288                 $reply_url = apply_filters( 'bbp_new_reply_redirect_to', $reply_url, $redirect_to );
    289 
    290                 /** Successful Save *******************************************/
    291 
    292                 // Redirect back to new reply
    293                 wp_safe_redirect( $reply_url );
    294 
    295                 // For good measure
    296                 exit();
    297 
    298             /** Errors ********************************************************/
    299 
    300             } else {
    301                 $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
    302                 $bbp->errors->add( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
    303             }
     309            $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
     310            bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
    304311        }
    305312    }
     
    338345function bbp_edit_reply_handler() {
    339346
    340     // Only proceed if POST is an reply request
    341     if ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) && !empty( $_POST['action'] ) && ( 'bbp-edit-reply' === $_POST['action'] ) ) {
    342         global $bbp;
    343 
    344         // Define local variable(s)
    345         $reply = $reply_id = $topic_id = $forum_id = $anonymous_data = 0;
    346         $reply_title = $reply_content = $reply_edit_reason = $terms = '';
    347 
    348         /** Reply *************************************************************/
    349 
    350         // Reply id was not passed
    351         if ( empty( $_POST['bbp_reply_id'] ) )
    352             $bbp->errors->add( 'bbp_edit_reply_id', __( '<strong>ERROR</strong>: Reply ID not found.', 'bbpress' ) );
    353 
    354         // Reply id was passed
    355         elseif ( is_numeric( $_POST['bbp_reply_id'] ) )
    356             $reply_id = (int) $_POST['bbp_reply_id'];
    357 
    358         // Reply does not exist
    359         if ( !$reply = bbp_get_reply( $reply_id ) ) {
    360             $bbp->errors->add( 'bbp_edit_reply_not_found', __( '<strong>ERROR</strong>: The reply you want to edit was not found.', 'bbpress' ) );
    361 
    362         // Reply exists
     347    // Bail if not a POST action
     348    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     349        return;
     350
     351    // Bail if action is not bbp-edit-reply
     352    if ( empty( $_POST['action'] ) || ( 'bbp-edit-reply' !== $_POST['action'] ) )
     353        return;
     354
     355    // Define local variable(s)
     356    $reply = $reply_id = $topic_id = $forum_id = $anonymous_data = 0;
     357    $reply_title = $reply_content = $reply_edit_reason = $terms = '';
     358
     359    /** Reply *************************************************************/
     360
     361    // Reply id was not passed
     362    if ( empty( $_POST['bbp_reply_id'] ) ) {
     363        bbp_add_error( 'bbp_edit_reply_id', __( '<strong>ERROR</strong>: Reply ID not found.', 'bbpress' ) );
     364
     365    // Reply id was passed
     366    } elseif ( is_numeric( $_POST['bbp_reply_id'] ) ) {
     367        $reply_id = (int) $_POST['bbp_reply_id'];
     368        $reply    = bbp_get_reply( $reply_id );
     369    }
     370
     371    // Reply does not exist
     372    if ( empty( $reply ) ) {
     373        bbp_add_error( 'bbp_edit_reply_not_found', __( '<strong>ERROR</strong>: The reply you want to edit was not found.', 'bbpress' ) );
     374
     375    // Reply exists
     376    } else {
     377
     378        // Nonce check
     379        check_admin_referer( 'bbp-edit-reply_' . $reply_id );
     380
     381        // Check users ability to create new reply
     382        if ( !bbp_is_reply_anonymous( $reply_id ) ) {
     383
     384            // User cannot edit this reply
     385            if ( !current_user_can( 'edit_reply', $reply_id ) ) {
     386                bbp_add_error( 'bbp_edit_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', 'bbpress' ) );
     387            }
     388
     389        // It is an anonymous post
    363390        } else {
    364391
    365             // Nonce check
    366             check_admin_referer( 'bbp-edit-reply_' . $reply_id );
    367 
    368             // Check users ability to create new reply
    369             if ( !bbp_is_reply_anonymous( $reply_id ) ) {
    370 
    371                 // User cannot edit this reply
    372                 if ( !current_user_can( 'edit_reply', $reply_id ) ) {
    373                     $bbp->errors->add( 'bbp_edit_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', 'bbpress' ) );
    374                 }
    375 
    376             // It is an anonymous post
    377             } else {
    378 
    379                 // Filter anonymous data
    380                 $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
    381             }
     392            // Filter anonymous data
     393            $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
    382394        }
    383 
    384         // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
    385         if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $reply_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
    386             remove_filter( 'bbp_edit_reply_pre_title',   'wp_filter_kses' );
    387             remove_filter( 'bbp_edit_reply_pre_content', 'wp_filter_kses' );
     395    }
     396
     397    // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
     398    if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $reply_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
     399        remove_filter( 'bbp_edit_reply_pre_title',   'wp_filter_kses' );
     400        remove_filter( 'bbp_edit_reply_pre_content', 'wp_filter_kses' );
     401    }
     402
     403    /** Reply Topic *******************************************************/
     404
     405    $topic_id = bbp_get_reply_topic_id( $reply_id );
     406
     407    /** Topic Forum *******************************************************/
     408
     409    $forum_id = bbp_get_topic_forum_id( $topic_id );
     410
     411    // Forum exists
     412    if ( !empty( $forum_id ) && ( $forum_id !== bbp_get_reply_forum_id( $reply_id ) ) ) {
     413
     414        // Forum is a category
     415        if ( bbp_is_forum_category( $forum_id ) )
     416            bbp_add_error( 'bbp_edit_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics or replies can be created in it.', 'bbpress' ) );
     417
     418        // Forum is closed and user cannot access
     419        if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
     420            bbp_add_error( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics and replies.', 'bbpress' ) );
     421
     422        // Forum is private and user cannot access
     423        if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
     424            bbp_add_error( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
     425
     426        // Forum is hidden and user cannot access
     427        if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
     428            bbp_add_error( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
     429    }
     430
     431    /** Reply Title *******************************************************/
     432
     433    if ( !empty( $_POST['bbp_reply_title'] ) )
     434        $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
     435
     436    // Filter and sanitize
     437    $reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id );
     438
     439    /** Reply Content *****************************************************/
     440
     441    if ( !empty( $_POST['bbp_reply_content'] ) )
     442        $reply_content = $_POST['bbp_reply_content'];
     443
     444    // Filter and sanitize
     445    $reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id );
     446
     447    // No reply content
     448    if ( empty( $reply_content ) )
     449        bbp_add_error( 'bbp_edit_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
     450
     451    /** Topic Tags ********************************************************/
     452
     453    if ( !empty( $_POST['bbp_topic_tags'] ) )
     454        $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
     455
     456    /** Additional Actions (Before Save) **********************************/
     457
     458    do_action( 'bbp_edit_reply_pre_extras', $reply_id );
     459
     460    /** No Errors *********************************************************/
     461
     462    // Handle insertion into posts table
     463    if ( !bbp_has_errors() ) {
     464
     465        // Add the content of the form to $post as an array
     466        $reply_data = array(
     467            'ID'           => $reply_id,
     468            'post_title'   => $reply_title,
     469            'post_content' => $reply_content
     470        );
     471
     472        // Just in time manipulation of reply data before being edited
     473        $reply_data = apply_filters( 'bbp_edit_reply_pre_insert', $reply_data );
     474
     475        // Insert reply
     476        $reply_id = wp_update_post( $reply_data );
     477
     478        /** Topic Tags ************************************************/
     479
     480        // Just in time manipulation of reply terms before being edited
     481        $terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id );
     482
     483        // Insert terms
     484        $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
     485
     486        // Term error
     487        if ( is_wp_error( $terms ) ) {
     488            bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
    388489        }
    389490
    390         /** Reply Topic *******************************************************/
    391 
    392         $topic_id = bbp_get_reply_topic_id( $reply_id );
    393 
    394         /** Reply Forum *******************************************************/
    395 
    396         $forum_id = bbp_get_topic_forum_id( $topic_id );
    397 
    398         // Forum exists
    399         if ( !empty( $forum_id ) && ( $forum_id != $reply->post_parent ) ) {
    400 
    401             // Forum is a category
    402             if ( bbp_is_forum_category( $forum_id ) )
    403                 $bbp->errors->add( 'bbp_edit_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics or replies can be created in it.', 'bbpress' ) );
    404 
    405             // Forum is closed and user cannot access
    406             if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
    407                 $bbp->errors->add( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics and replies.', 'bbpress' ) );
    408 
    409             // Forum is private and user cannot access
    410             if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
    411                 $bbp->errors->add( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    412 
    413             // Forum is hidden and user cannot access
    414             if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
    415                 $bbp->errors->add( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
     491        /** Revisions *****************************************************/
     492
     493        // Revision Reason
     494        if ( !empty( $_POST['bbp_reply_edit_reason'] ) )
     495            $reply_edit_reason = esc_attr( strip_tags( $_POST['bbp_reply_edit_reason'] ) );
     496
     497        // Update revision log
     498        if ( !empty( $_POST['bbp_log_reply_edit'] ) && ( 1 == $_POST['bbp_log_reply_edit'] ) && ( $revision_id = wp_save_post_revision( $reply_id ) ) ) {
     499            bbp_update_reply_revision_log( array(
     500                'reply_id'    => $reply_id,
     501                'revision_id' => $revision_id,
     502                'author_id'   => bbp_get_current_user_id(),
     503                'reason'      => $reply_edit_reason
     504            ) );
    416505        }
    417506
    418         /** Reply Title *******************************************************/
    419 
    420         if ( !empty( $_POST['bbp_reply_title'] ) )
    421             $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
    422 
    423         // Filter and sanitize
    424         $reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id );
    425 
    426         /** Reply Content *****************************************************/
    427 
    428         if ( !empty( $_POST['bbp_reply_content'] ) )
    429             $reply_content = $_POST['bbp_reply_content'];
    430 
    431         // Filter and sanitize
    432         $reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id );
    433 
    434         // No reply content
    435         if ( empty( $reply_content ) )
    436             $bbp->errors->add( 'bbp_edit_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
    437 
    438         /** Topic Tags ********************************************************/
    439 
    440         if ( !empty( $_POST['bbp_topic_tags'] ) )
    441             $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
    442 
    443         /** Additional Actions (Before Save) **********************************/
    444 
    445         do_action( 'bbp_edit_reply_pre_extras', $reply_id );
    446 
    447         /** No Errors *********************************************************/
    448 
    449         // Handle insertion into posts table
    450         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    451 
    452             // Add the content of the form to $post as an array
    453             $reply_data = array(
    454                 'ID'           => $reply_id,
    455                 'post_title'   => $reply_title,
    456                 'post_content' => $reply_content
    457             );
    458 
    459             // Just in time manipulation of reply data before being edited
    460             $reply_data = apply_filters( 'bbp_edit_reply_pre_insert', $reply_data );
    461 
    462             // Insert reply
    463             $reply_id = wp_update_post( $reply_data );
    464 
    465             /** Topic Tags ************************************************/
    466 
    467             // Just in time manipulation of reply terms before being edited
    468             $terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id );
    469 
    470             // Insert terms
    471             $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
    472 
    473             // Term error
    474             if ( is_wp_error( $terms ) )
    475                 $bbp->errors->add( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was some problem adding the tags to the topic.', 'bbpress' ) );
    476 
    477             /** Revisions *****************************************************/
    478 
    479             // Revision Reason
    480             if ( !empty( $_POST['bbp_reply_edit_reason'] ) )
    481                 $reply_edit_reason = esc_attr( strip_tags( $_POST['bbp_reply_edit_reason'] ) );
    482 
    483             // Update revision log
    484             if ( !empty( $_POST['bbp_log_reply_edit'] ) && ( 1 == $_POST['bbp_log_reply_edit'] ) && ( $revision_id = wp_save_post_revision( $reply_id ) ) ) {
    485                 bbp_update_reply_revision_log( array(
    486                     'reply_id'    => $reply_id,
    487                     'revision_id' => $revision_id,
    488                     'author_id'   => bbp_get_current_user_id(),
    489                     'reason'      => $reply_edit_reason
    490                 ) );
    491             }
    492 
    493             /** No Errors *****************************************************/
    494 
    495             if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
    496 
    497                 // Update counts, etc...
    498                 do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply->post_author , true /* Is edit */ );
    499 
    500                 /** Additional Actions (After Save) ***************************/
    501 
    502                 do_action( 'bbp_edit_reply_post_extras', $reply_id );
    503 
    504                 /** Redirect **************************************************/
    505 
    506                 // Redirect to
    507                 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
    508 
    509                 // Get the reply URL
    510                 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
    511 
    512                 // Allow to be filtered
    513                 $reply_url = apply_filters( 'bbp_edit_reply_redirect_to', $reply_url, $redirect_to );
    514 
    515                 /** Successful Edit *******************************************/
    516 
    517                 // Redirect back to new reply
    518                 wp_safe_redirect( $reply_url );
    519 
    520                 // For good measure
    521                 exit();
    522 
    523             /** Errors ********************************************************/
    524 
    525             } else {
    526                 $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
    527                 $bbp->errors->add( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
    528             }
     507        /** No Errors *****************************************************/
     508
     509        if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
     510
     511            // Update counts, etc...
     512            do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply->post_author , true /* Is edit */ );
     513
     514            /** Additional Actions (After Save) ***************************/
     515
     516            do_action( 'bbp_edit_reply_post_extras', $reply_id );
     517
     518            /** Redirect **************************************************/
     519
     520            // Redirect to
     521            $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
     522
     523            // Get the reply URL
     524            $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
     525
     526            // Allow to be filtered
     527            $reply_url = apply_filters( 'bbp_edit_reply_redirect_to', $reply_url, $redirect_to );
     528
     529            /** Successful Edit *******************************************/
     530
     531            // Redirect back to new reply
     532            wp_safe_redirect( $reply_url );
     533
     534            // For good measure
     535            exit();
     536
     537        /** Errors ********************************************************/
     538
     539        } else {
     540            $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
     541            bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
    529542        }
    530543    }
     
    667680 */
    668681function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 0, $topic_id = 0, $refresh = true ) {
    669     global $bbp;
    670682
    671683    // Verify the reply ID
    672     if ( $reply_id = bbp_get_reply_id( $reply_id ) ) {
     684    $reply_id = bbp_get_reply_id( $reply_id );
     685
     686    // Reply was passed
     687    if ( !empty( $reply_id ) ) {
    673688
    674689        // Get the topic ID if none was passed
    675         if ( empty( $topic_id ) )
     690        if ( empty( $topic_id ) ) {
    676691            $topic_id = bbp_get_reply_topic_id( $reply_id );
     692        }
    677693
    678694        // Get the forum ID if none was passed
    679         if ( empty( $forum_id ) )
     695        if ( empty( $forum_id ) ) {
    680696            $forum_id = bbp_get_reply_forum_id( $reply_id );
     697        }
    681698    }
    682699
     
    909926function bbp_toggle_reply_handler() {
    910927
    911     // Only proceed if GET is a reply toggle action
    912     if ( 'GET' == strtoupper( $_SERVER['REQUEST_METHOD'] ) && !empty( $_GET['reply_id'] ) && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_toggle_reply_spam', 'bbp_toggle_reply_trash' ) ) ) {
    913         global $bbp;
    914 
    915         $action    = $_GET['action'];            // What action is taking place?
    916         $reply_id  = (int) $_GET['reply_id'];    // What's the reply id?
    917         $success   = false;                      // Flag
    918         $post_data = array( 'ID' => $reply_id ); // Prelim array
    919 
    920         // Make sure reply exists
    921         if ( !$reply = bbp_get_reply( $reply_id ) )
    922             return;
    923 
    924         // What is the user doing here?
    925         if ( !current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' == $action && !current_user_can( 'delete_reply', $reply->ID ) ) ) {
    926             $bbp->errors->add( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that!', 'bbpress' ) );
    927             return;
    928         }
    929 
    930         // What action are we trying to perform?
    931         switch ( $action ) {
    932 
    933             // Toggle spam
    934             case 'bbp_toggle_reply_spam' :
    935                 check_ajax_referer( 'spam-reply_' . $reply_id );
    936 
    937                 $is_spam = bbp_is_reply_spam( $reply_id );
    938                 $success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id );
    939                 $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam!', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam!', 'bbpress' );
    940 
     928    // Bail if not a GET action
     929    if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     930        return;
     931
     932    // Bail if required GET actions aren't passed
     933    if ( empty( $_GET['reply_id'] ) || empty( $_GET['action'] ) )
     934        return;
     935
     936    // Setup possible get actions
     937    $possible_actions = array(
     938        'bbp_toggle_reply_spam',
     939        'bbp_toggle_reply_trash'
     940    );
     941
     942    // Bail if actions aren't meant for this function
     943    if ( !in_array( $_GET['action'], $possible_actions ) )
     944        return;
     945
     946    $view_all  = false;                      // Assume not viewing all
     947    $action    = $_GET['action'];            // What action is taking place?
     948    $reply_id  = (int) $_GET['reply_id'];    // What's the reply id?
     949    $success   = false;                      // Flag
     950    $post_data = array( 'ID' => $reply_id ); // Prelim array
     951
     952    // Make sure reply exists
     953    if ( !$reply = bbp_get_reply( $reply_id ) )
     954        return;
     955
     956    // What is the user doing here?
     957    if ( !current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' == $action && !current_user_can( 'delete_reply', $reply->ID ) ) ) {
     958        bbp_add_error( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that!', 'bbpress' ) );
     959        return;
     960    }
     961
     962    // What action are we trying to perform?
     963    switch ( $action ) {
     964
     965        // Toggle spam
     966        case 'bbp_toggle_reply_spam' :
     967            check_ajax_referer( 'spam-reply_' . $reply_id );
     968
     969            $is_spam = bbp_is_reply_spam( $reply_id );
     970            $success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id );
     971            $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam!', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam!', 'bbpress' );
     972            $view_all = !$is_spam;
     973
     974            break;
     975
     976        // Toggle trash
     977        case 'bbp_toggle_reply_trash' :
     978
     979            $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
     980
     981            if ( empty( $sub_action ) )
    941982                break;
    942983
    943             // Toggle trash
    944             case 'bbp_toggle_reply_trash' :
    945 
    946                 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
    947 
    948                 if ( empty( $sub_action ) )
     984            switch ( $sub_action ) {
     985                case 'trash':
     986                    check_ajax_referer( 'trash-' . bbp_get_reply_post_type() . '_' . $reply_id );
     987
     988                    $view_all = true;
     989                    $success  = wp_trash_post( $reply_id );
     990                    $failure  = __( '<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress' );
     991
    949992                    break;
    950993
    951                 switch ( $sub_action ) {
    952                     case 'trash':
    953                         check_ajax_referer( 'trash-' . bbp_get_reply_post_type() . '_' . $reply_id );
    954 
    955                         $success = wp_trash_post( $reply_id );
    956                         $failure = __( '<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress' );
    957 
    958                         break;
    959 
    960                     case 'untrash':
    961                         check_ajax_referer( 'untrash-' . bbp_get_reply_post_type() . '_' . $reply_id );
    962 
    963                         $success = wp_untrash_post( $reply_id );
    964                         $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress' );
    965 
    966                         break;
    967 
    968                     case 'delete':
    969                         check_ajax_referer( 'delete-' . bbp_get_reply_post_type() . '_' . $reply_id );
    970 
    971                         $success = wp_delete_post( $reply_id );
    972                         $failure = __( '<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress' );
    973 
    974                         break;
    975                 }
    976 
    977                 break;
    978         }
    979 
    980         // Do additional reply toggle actions
    981         do_action( 'bbp_toggle_reply_handler', $success, $post_data, $action );
    982 
    983         // No errors
    984         if ( ( false != $success ) && !is_wp_error( $success ) ) {
    985 
    986             // Redirect back to the reply
    987             $redirect = bbp_get_reply_url( $reply_id );
    988             wp_redirect( $redirect );
    989 
    990             // For good measure
    991             exit();
    992 
    993         // Handle errors
    994         } else {
    995             $bbp->errors->add( 'bbp_toggle_reply', $failure );
    996         }
     994                case 'untrash':
     995                    check_ajax_referer( 'untrash-' . bbp_get_reply_post_type() . '_' . $reply_id );
     996
     997                    $success = wp_untrash_post( $reply_id );
     998                    $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress' );
     999
     1000                    break;
     1001
     1002                case 'delete':
     1003                    check_ajax_referer( 'delete-' . bbp_get_reply_post_type() . '_' . $reply_id );
     1004
     1005                    $success = wp_delete_post( $reply_id );
     1006                    $failure = __( '<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress' );
     1007
     1008                    break;
     1009            }
     1010
     1011            break;
     1012    }
     1013
     1014    // Do additional reply toggle actions
     1015    do_action( 'bbp_toggle_reply_handler', $success, $post_data, $action );
     1016
     1017    // No errors
     1018    if ( ( false != $success ) && !is_wp_error( $success ) ) {
     1019
     1020        // Redirect back to the reply
     1021        $redirect = bbp_get_reply_url( $reply_id );
     1022
     1023        // Add view all if needed
     1024        if ( !empty( $view_all ) )
     1025            $redirect = bbp_add_view_all( $redirect, true );
     1026
     1027        wp_redirect( $redirect );
     1028
     1029        // For good measure
     1030        exit();
     1031
     1032    // Handle errors
     1033    } else {
     1034        bbp_add_error( 'bbp_toggle_reply', $failure );
    9971035    }
    9981036}
  • branches/plugin/bbp-includes/bbp-reply-template.php

    r3361 r3382  
    14811481        $reply   = bbp_get_reply( bbp_get_reply_id( (int) $id ) );
    14821482
    1483         if ( empty( $reply ) || !current_user_can( 'delete_reply', $reply->ID ) )
     1483        if ( empty( $reply ) || !current_user_can( 'delete_reply', $reply->ID ) ) {
    14841484            return;
    1485 
    1486         if ( bbp_is_reply_trash( $reply->ID ) )
    1487             $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'untrash', 'reply_id' => $reply->ID ) ), 'untrash-' . $reply->post_type . '_' . $reply->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to restore that?', 'bbpress' ) ) . '\');">' . esc_html( $restore_text ) . '</a>';
    1488         elseif ( EMPTY_TRASH_DAYS )
    1489             $actions['trash']   = '<a title="' . esc_attr( __( 'Move this item to the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'trash', 'reply_id' => $reply->ID ) ), 'trash-' . $reply->post_type . '_' . $reply->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to trash that?', 'bbpress' ) ) . '\' );">' . esc_html( $trash_text ) . '</a>';
    1490 
    1491         if ( bbp_is_reply_trash( $reply->ID ) || !EMPTY_TRASH_DAYS )
     1485        }
     1486
     1487        if ( bbp_is_reply_trash( $reply->ID ) ) {
     1488            $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'untrash', 'reply_id' => $reply->ID ) ), 'untrash-' . $reply->post_type . '_' . $reply->ID ) ) . '">' . esc_html( $restore_text ) . '</a>';
     1489        } elseif ( EMPTY_TRASH_DAYS ) {
     1490            $actions['trash']   = '<a title="' . esc_attr( __( 'Move this item to the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'trash', 'reply_id' => $reply->ID ) ), 'trash-' . $reply->post_type . '_' . $reply->ID ) ) . '">' . esc_html( $trash_text ) . '</a>';
     1491        }
     1492
     1493        if ( bbp_is_reply_trash( $reply->ID ) || !EMPTY_TRASH_DAYS ) {
    14921494            $actions['delete']  = '<a title="' . esc_attr( __( 'Delete this item permanently', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'delete', 'reply_id' => $reply->ID ) ), 'delete-' . $reply->post_type . '_' . $reply->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );">' . esc_html( $delete_text ) . '</a>';
     1495        }
    14931496
    14941497        // Process the admin links
  • branches/plugin/bbp-includes/bbp-topic-functions.php

    r3350 r3382  
    7272
    7373    // Update the forum
    74     if ( $forum_id = bbp_get_topic_forum_id( $topic_id ) )
     74    $forum_id = bbp_get_topic_forum_id( $topic_id );
     75    if ( !empty( $forum_id ) )
    7576        bbp_update_forum( $forum_id );
    7677
     
    115116function bbp_new_topic_handler() {
    116117
    117     // Only proceed if POST is a new topic
    118     if ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) && !empty( $_POST['action'] ) && ( 'bbp-new-topic' === $_POST['action'] ) ) {
    119         global $bbp;
    120 
    121         // Nonce check
    122         check_admin_referer( 'bbp-new-topic' );
    123 
    124         // Define local variable(s)
    125         $view_all = false;
    126         $forum_id = $topic_author = $anonymous_data = 0;
    127         $topic_title = $topic_content = '';
    128         $terms = array( bbp_get_topic_tag_tax_id() => array() );
    129 
    130         /** Topic Author ******************************************************/
    131 
    132         // User is anonymous
    133         if ( bbp_is_anonymous() ) {
    134 
    135             // Filter anonymous data
    136             $anonymous_data = bbp_filter_anonymous_post_data();
    137 
    138             // Anonymous data checks out, so set cookies, etc...
    139             if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
    140                 bbp_set_current_anonymous_user_data( $anonymous_data );
     118    // Bail if not a POST action
     119    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     120        return;
     121
     122    // Bail if action is not bbp-new-topic
     123    if ( empty( $_POST['action'] ) || ( 'bbp-new-topic' !== $_POST['action'] ) )
     124        return;
     125
     126    global $bbp;
     127
     128    // Nonce check
     129    check_admin_referer( 'bbp-new-topic' );
     130
     131    // Define local variable(s)
     132    $view_all = false;
     133    $forum_id = $topic_author = $anonymous_data = 0;
     134    $topic_title = $topic_content = '';
     135    $terms = array( bbp_get_topic_tag_tax_id() => array() );
     136
     137    /** Topic Author ******************************************************/
     138
     139    // User is anonymous
     140    if ( bbp_is_anonymous() ) {
     141
     142        // Filter anonymous data
     143        $anonymous_data = bbp_filter_anonymous_post_data();
     144
     145        // Anonymous data checks out, so set cookies, etc...
     146        if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
     147            bbp_set_current_anonymous_user_data( $anonymous_data );
     148        }
     149
     150    // User is logged in
     151    } else {
     152
     153        // User cannot create topics
     154        if ( !current_user_can( 'publish_topics' ) ) {
     155            bbp_add_error( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress' ) );
     156        }
     157
     158        // Topic author is current user
     159        $topic_author = bbp_get_current_user_id();
     160
     161    }
     162
     163    // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
     164    if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) == $_POST['_bbp_unfiltered_html_topic'] ) {
     165        remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses' );
     166        remove_filter( 'bbp_new_topic_pre_content', 'wp_filter_kses' );
     167    }
     168
     169    /** Topic Title *******************************************************/
     170
     171    if ( !empty( $_POST['bbp_topic_title'] ) )
     172        $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
     173
     174    // Filter and sanitize
     175    $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
     176
     177    // No topic title
     178    if ( empty( $topic_title ) )
     179        bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
     180
     181    /** Topic Content *****************************************************/
     182
     183    if ( !empty( $_POST['bbp_topic_content'] ) )
     184        $topic_content = $_POST['bbp_topic_content'];
     185
     186    // Filter and sanitize
     187    $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
     188
     189    // No topic content
     190    if ( empty( $topic_content ) )
     191        bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
     192
     193    /** Topic Forum *******************************************************/
     194
     195    // Forum id was not passed
     196    if ( empty( $_POST['bbp_forum_id'] ) )
     197        bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
     198
     199    // Forum id was passed
     200    elseif ( is_numeric( $_POST['bbp_forum_id'] ) )
     201        $forum_id = (int) $_POST['bbp_forum_id'];
     202
     203    // Forum exists
     204    if ( !empty( $forum_id ) ) {
     205
     206        // Forum is a category
     207        if ( bbp_is_forum_category( $forum_id ) )
     208            bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
     209
     210        // Forum is closed and user cannot access
     211        if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
     212            bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
     213
     214        // Forum is private and user cannot access
     215        if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
     216            bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
     217
     218        // Forum is hidden and user cannot access
     219        if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
     220            bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
     221    }
     222
     223    /** Topic Flooding ****************************************************/
     224
     225    if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) )
     226        bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
     227
     228    /** Topic Duplicate ***************************************************/
     229
     230    if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) )
     231        bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
     232
     233    /** Topic Tags ********************************************************/
     234
     235    if ( !empty( $_POST['bbp_topic_tags'] ) ) {
     236
     237        // Escape tag input
     238        $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
     239
     240        // Explode by comma
     241        if ( strstr( $terms, ',' ) )
     242            $terms = explode( ',', $terms );
     243
     244        // Add topic tag ID as main key
     245        $terms = array( bbp_get_topic_tag_tax_id() => $terms );
     246    }
     247
     248    /** Additional Actions (Before Save) **********************************/
     249
     250    do_action( 'bbp_new_topic_pre_extras' );
     251
     252    /** No Errors *********************************************************/
     253
     254    if ( !bbp_has_errors() ) {
     255
     256        /** Create new topic **********************************************/
     257
     258        // Add the content of the form to $post as an array
     259        $topic_data = array(
     260            'post_author'  => $topic_author,
     261            'post_title'   => $topic_title,
     262            'post_content' => $topic_content,
     263            'post_parent'  => $forum_id,
     264            'tax_input'    => $terms,
     265            'post_status'  => 'publish',
     266            'post_type'    => bbp_get_topic_post_type()
     267        );
     268
     269        // Just in time manipulation of topic data before being created
     270        $topic_data = apply_filters( 'bbp_new_topic_pre_insert', $topic_data );
     271
     272        // Insert topic
     273        $topic_id = wp_insert_post( $topic_data );
     274
     275        /** No Errors *****************************************************/
     276
     277        if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
     278
     279            /** Stickies **************************************************/
     280
     281            if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
     282
     283                // What's the haps?
     284                switch ( $_POST['bbp_stick_topic'] ) {
     285
     286                    // Sticky in this forum
     287                    case 'stick'   :
     288                        bbp_stick_topic( $topic_id );
     289                        break;
     290
     291                    // Super sticky in all forums
     292                    case 'super'   :
     293                        bbp_stick_topic( $topic_id, true );
     294                        break;
     295
     296                    // We can avoid this as it is a new topic
     297                    case 'unstick' :
     298                    default        :
     299                        break;
     300                }
    141301            }
    142302
    143         // User is logged in
     303            /** Trash Check ***********************************************/
     304
     305            // If the forum is trash, or the topic_status is switched to
     306            // trash, trash it properly
     307            if ( ( get_post_field( 'post_status', $forum_id ) == $bbp->trash_status_id ) || ( $topic_data['post_status'] == $bbp->trash_status_id ) ) {
     308
     309                // Trash the reply
     310                wp_trash_post( $topic_id );
     311
     312                // Force view=all
     313                $view_all = true;
     314            }
     315
     316            /** Spam Check ************************************************/
     317
     318            // If reply or topic are spam, officially spam this reply
     319            if ( $topic_data['post_status'] == $bbp->spam_status_id ) {
     320                add_post_meta( $topic_id, '_bbp_spam_meta_status', 'publish' );
     321
     322                // Force view=all
     323                $view_all = true;
     324            }
     325
     326            /** Update counts, etc... *************************************/
     327
     328            do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
     329
     330            /** Redirect **************************************************/
     331
     332            // Redirect to
     333            $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
     334
     335            // Get the topic URL
     336            $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
     337
     338            // Add view all?
     339            if ( bbp_get_view_all() || ( current_user_can( 'moderate' ) && !empty( $view_all ) ) )
     340                $topic_url = bbp_add_view_all( $topic_url );
     341
     342            // Allow to be filtered
     343            $topic_url = apply_filters( 'bbp_new_topic_redirect_to', $topic_url, $redirect_to );
     344
     345            /** Successful Save *******************************************/
     346
     347            // Redirect back to new topic
     348            wp_safe_redirect( $topic_url );
     349
     350            // For good measure
     351            exit();
     352
     353        // Errors
    144354        } else {
    145 
    146             // User cannot create topics
    147             if ( !current_user_can( 'publish_topics' ) ) {
    148                 $bbp->errors->add( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress' ) );
    149             }
    150 
    151             // Topic author is current user
    152             $topic_author = bbp_get_current_user_id();
    153 
    154         }
    155 
    156         // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
    157         if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) == $_POST['_bbp_unfiltered_html_topic'] ) {
    158             remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses' );
    159             remove_filter( 'bbp_new_topic_pre_content', 'wp_filter_kses' );
    160         }
    161 
    162         /** Topic Title *******************************************************/
    163 
    164         if ( !empty( $_POST['bbp_topic_title'] ) )
    165             $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
    166 
    167         // Filter and sanitize
    168         $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
    169 
    170         // No topic title
    171         if ( empty( $topic_title ) )
    172             $bbp->errors->add( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
    173 
    174         /** Topic Content *****************************************************/
    175 
    176         if ( !empty( $_POST['bbp_topic_content'] ) )
    177             $topic_content = $_POST['bbp_topic_content'];
    178 
    179         // Filter and sanitize
    180         $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
    181 
    182         // No topic content
    183         if ( empty( $topic_content ) )
    184             $bbp->errors->add( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
    185 
    186         /** Topic Forum *******************************************************/
    187 
    188         // Forum id was not passed
    189         if ( empty( $_POST['bbp_forum_id'] ) )
    190             $bbp->errors->add( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
    191 
    192         // Forum id was passed
    193         elseif ( is_numeric( $_POST['bbp_forum_id'] ) )
    194             $forum_id = (int) $_POST['bbp_forum_id'];
    195 
    196         // Forum exists
    197         if ( !empty( $forum_id ) ) {
    198 
    199             // Forum is a category
    200             if ( bbp_is_forum_category( $forum_id ) )
    201                 $bbp->errors->add( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
    202 
    203             // Forum is closed and user cannot access
    204             if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
    205                 $bbp->errors->add( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
    206 
    207             // Forum is private and user cannot access
    208             if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
    209                 $bbp->errors->add( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    210 
    211             // Forum is hidden and user cannot access
    212             if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
    213                 $bbp->errors->add( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    214         }
    215 
    216         /** Topic Flooding ****************************************************/
    217 
    218         if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) )
    219             $bbp->errors->add( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
    220 
    221         /** Topic Duplicate ***************************************************/
    222 
    223         if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) )
    224             $bbp->errors->add( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
    225 
    226         /** Topic Tags ********************************************************/
    227 
    228         if ( !empty( $_POST['bbp_topic_tags'] ) ) {
    229 
    230             // Escape tag input
    231             $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
    232 
    233             // Explode by comma
    234             if ( strstr( $terms, ',' ) )
    235                 $terms = explode( ',', $terms );
    236 
    237             // Add topic tag ID as main key
    238             $terms = array( bbp_get_topic_tag_tax_id() => $terms );
    239         }
    240 
    241         /** Additional Actions (Before Save) **********************************/
    242 
    243         do_action( 'bbp_new_topic_pre_extras' );
    244 
    245         /** No Errors *********************************************************/
    246 
    247         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    248 
    249             /** Create new topic **********************************************/
    250 
    251             // Add the content of the form to $post as an array
    252             $topic_data = array(
    253                 'post_author'  => $topic_author,
    254                 'post_title'   => $topic_title,
    255                 'post_content' => $topic_content,
    256                 'post_parent'  => $forum_id,
    257                 'tax_input'    => $terms,
    258                 'post_status'  => 'publish',
    259                 'post_type'    => bbp_get_topic_post_type()
    260             );
    261 
    262             // Just in time manipulation of topic data before being created
    263             $topic_data = apply_filters( 'bbp_new_topic_pre_insert', $topic_data );
    264 
    265             // Insert topic
    266             $topic_id = wp_insert_post( $topic_data );
    267 
    268             /** No Errors *****************************************************/
    269 
    270             if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
    271 
    272                 /** Stickies **************************************************/
    273 
    274                 if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
    275 
    276                     // What's the haps?
    277                     switch ( $_POST['bbp_stick_topic'] ) {
    278 
    279                         // Sticky in this forum
    280                         case 'stick'   :
    281                             bbp_stick_topic( $topic_id );
    282                             break;
    283 
    284                         // Super sticky in all forums
    285                         case 'super'   :
    286                             bbp_stick_topic( $topic_id, true );
    287                             break;
    288 
    289                         // We can avoid this as it is a new topic
    290                         case 'unstick' :
    291                         default        :
    292                             break;
    293                     }
    294                 }
    295 
    296                 /** Trash Check ***********************************************/
    297 
    298                 // If the forum is trash, or the topic_status is switched to
    299                 // trash, trash it properly
    300                 if ( ( get_post_field( 'post_status', $forum_id ) == $bbp->trash_status_id ) || ( $topic_data['post_status'] == $bbp->trash_status_id ) ) {
    301 
    302                     // Trash the reply
    303                     wp_trash_post( $topic_id );
    304 
    305                     // Force view=all
    306                     $view_all = true;
    307                 }
    308 
    309                 /** Spam Check ************************************************/
    310                
    311                 // If reply or topic are spam, officially spam this reply
    312                 if ( $topic_data['post_status'] == $bbp->spam_status_id ) {
    313                     add_post_meta( $topic_id, '_bbp_spam_meta_status', 'publish' );
    314 
    315                     // Force view=all
    316                     $view_all = true;
    317                 }
    318 
    319                 /** Update counts, etc... *************************************/
    320 
    321                 do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
    322 
    323                 /** Redirect **************************************************/
    324 
    325                 // Redirect to
    326                 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
    327 
    328                 // Get the topic URL
    329                 $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
    330 
    331                 // Add view all?
    332                 if ( bbp_get_view_all() || ( current_user_can( 'moderate' ) && !empty( $view_all ) ) )
    333                     $topic_url = bbp_add_view_all( $topic_url );
    334 
    335                 // Allow to be filtered
    336                 $topic_url = apply_filters( 'bbp_new_topic_redirect_to', $topic_url, $redirect_to );
    337 
    338                 /** Successful Save *******************************************/
    339 
    340                 // Redirect back to new topic
    341                 wp_safe_redirect( $topic_url );
    342 
    343                 // For good measure
    344                 exit();
    345 
    346             // Errors
    347             } else {
    348                 $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
    349                 $bbp->errors->add( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, 'bbpress' ) );
    350             }
     355            $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
     356            bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, 'bbpress' ) );
    351357        }
    352358    }
     
    389395function bbp_edit_topic_handler() {
    390396
    391     // Only proceed if POST is an edit topic request
    392     if ( ( 'POST' === strtoupper( $_SERVER['REQUEST_METHOD'] ) ) && ( !empty( $_POST['action'] ) && ( 'bbp-edit-topic' === $_POST['action'] ) ) ) {
    393         global $bbp;
    394 
    395         // Define local variable(s)
    396         $view_all = false;
    397         $topic_id = $forum_id = $anonymous_data = 0;
    398         $topic_title = $topic_content = $topic_edit_reason = '';
    399         $terms = array( bbp_get_topic_tag_tax_id() => array() );
    400 
    401         /** Topic *************************************************************/
    402 
    403         // Topic id was not passed
    404         if ( empty( $_POST['bbp_topic_id'] ) )
    405             $bbp->errors->add( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
    406 
    407         // Topic id was passed
    408         elseif ( is_numeric( $_POST['bbp_topic_id'] ) )
    409             $topic_id = (int) $_POST['bbp_topic_id'];
    410 
    411         // Topic does not exist
    412         if ( !$topic = bbp_get_topic( $topic_id ) ) {
    413             $bbp->errors->add( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress' ) );
    414 
    415         // Topic exists
     397    // Bail if not a POST action
     398    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     399        return;
     400
     401    // Bail if action is not bbp-edit-topic
     402    if ( empty( $_POST['action'] ) || ( 'bbp-edit-topic' !== $_POST['action'] ) )
     403        return;
     404
     405    // Define local variable(s)
     406    $topic = $topic_id = $forum_id = $anonymous_data = 0;
     407    $topic_title = $topic_content = $topic_edit_reason = '';
     408    $terms = array( bbp_get_topic_tag_tax_id() => array() );
     409
     410    /** Topic *************************************************************/
     411
     412    // Topic id was not passed
     413    if ( empty( $_POST['bbp_topic_id'] ) ) {
     414        bbp_add_error( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
     415
     416    // Topic id was passed
     417    } elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) {
     418        $topic_id = (int) $_POST['bbp_topic_id'];
     419        $topic    = bbp_get_topic( $topic_id );
     420    }
     421
     422    // Topic does not exist
     423    if ( empty( $topic ) ) {
     424        bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress' ) );
     425
     426    // Topic exists
     427    } else {
     428
     429        // Nonce check
     430        check_admin_referer( 'bbp-edit-topic_' . $topic_id );
     431
     432        // Check users ability to create new topic
     433        if ( !bbp_is_topic_anonymous( $topic_id ) ) {
     434
     435            // User cannot edit this topic
     436            if ( !current_user_can( 'edit_topic', $topic_id ) ) {
     437                bbp_add_error( 'bbp_edit_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
     438            }
     439
     440        // It is an anonymous post
    416441        } else {
    417442
    418             // Nonce check
    419             check_admin_referer( 'bbp-edit-topic_' . $topic_id );
    420 
    421             // Check users ability to create new topic
    422             if ( !bbp_is_topic_anonymous( $topic_id ) ) {
    423 
    424                 // User cannot edit this topic
    425                 if ( !current_user_can( 'edit_topic', $topic_id ) ) {
    426                     $bbp->errors->add( 'bbp_edit_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
    427                 }
    428 
    429             // It is an anonymous post
    430             } else {
    431 
    432                 // Filter anonymous data
    433                 $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
     443            // Filter anonymous data
     444            $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
     445        }
     446    }
     447
     448    // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
     449    if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) == $_POST['_bbp_unfiltered_html_topic'] ) ) {
     450        remove_filter( 'bbp_edit_topic_pre_title',   'wp_filter_kses' );
     451        remove_filter( 'bbp_edit_topic_pre_content', 'wp_filter_kses' );
     452    }
     453
     454    /** Topic Forum *******************************************************/
     455
     456    // Forum id was not passed
     457    if ( empty( $_POST['bbp_forum_id'] ) ) {
     458        bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
     459
     460    // Forum id was passed
     461    } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
     462        $forum_id = (int) $_POST['bbp_forum_id'];
     463    }
     464
     465    // Current forum this topic is in
     466    $current_forum_id = bbp_get_topic_forum_id( $topic_id );
     467   
     468    // Forum exists
     469    if ( !empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) {
     470
     471        // Forum is a category
     472        if ( bbp_is_forum_category( $forum_id ) )
     473            bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
     474
     475        // Forum is closed and user cannot access
     476        if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
     477            bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
     478
     479        // Forum is private and user cannot access
     480        if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
     481            bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
     482
     483        // Forum is hidden and user cannot access
     484        if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
     485            bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
     486    }
     487
     488    /** Topic Title *******************************************************/
     489
     490    if ( !empty( $_POST['bbp_topic_title'] ) )
     491        $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
     492
     493    // Filter and sanitize
     494    $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
     495
     496    // No topic title
     497    if ( empty( $topic_title ) )
     498        bbp_add_error( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
     499
     500    /** Topic Content *****************************************************/
     501
     502    if ( !empty( $_POST['bbp_topic_content'] ) )
     503        $topic_content = $_POST['bbp_topic_content'];
     504
     505    // Filter and sanitize
     506    $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
     507
     508    // No topic content
     509    if ( empty( $topic_content ) )
     510        bbp_add_error( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
     511
     512    /** Topic Tags ********************************************************/
     513
     514    // Tags
     515    if ( !empty( $_POST['bbp_topic_tags'] ) ) {
     516
     517        // Escape tag input
     518        $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
     519
     520        // Explode by comma
     521        if ( strstr( $terms, ',' ) )
     522            $terms = explode( ',', $terms );
     523
     524        // Add topic tag ID as main key
     525        $terms = array( bbp_get_topic_tag_tax_id() => $terms );
     526    }
     527
     528    /** Additional Actions (Before Save) **********************************/
     529
     530    do_action( 'bbp_edit_topic_pre_extras', $topic_id );
     531
     532    /** No Errors *********************************************************/
     533
     534    if ( !bbp_has_errors() ) {
     535
     536        /** Stickies ******************************************************/
     537
     538        if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
     539
     540            // What's the dilly?
     541            switch ( $_POST['bbp_stick_topic'] ) {
     542
     543                // Sticky in forum
     544                case 'stick'   :
     545                    bbp_stick_topic( $topic_id );
     546                    break;
     547
     548                // Sticky in all forums
     549                case 'super'   :
     550                    bbp_stick_topic( $topic_id, true );
     551                    break;
     552
     553                // Normal
     554                case 'unstick' :
     555                default        :
     556                    bbp_unstick_topic( $topic_id );
     557                    break;
    434558            }
    435559        }
    436560
    437         // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
    438         if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) == $_POST['_bbp_unfiltered_html_topic'] ) ) {
    439             remove_filter( 'bbp_edit_topic_pre_title',   'wp_filter_kses' );
    440             remove_filter( 'bbp_edit_topic_pre_content', 'wp_filter_kses' );
     561        /** Update the topic **********************************************/
     562
     563        // Add the content of the form to $post as an array
     564        $topic_data = array(
     565            'ID'           => $topic_id,
     566            'post_title'   => $topic_title,
     567            'post_content' => $topic_content,
     568            'post_parent'  => $forum_id,
     569            'tax_input'    => $terms,
     570        );
     571
     572        // Just in time manipulation of topic data before being edited
     573        $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', $topic_data );
     574
     575        // Insert topic
     576        $topic_id = wp_update_post( $topic_data );
     577
     578        /** Revisions *****************************************************/
     579
     580        // Revision Reason
     581        if ( !empty( $_POST['bbp_topic_edit_reason'] ) )
     582            $topic_edit_reason = esc_attr( strip_tags( $_POST['bbp_topic_edit_reason'] ) );
     583
     584        // Update revision log
     585        if ( !empty( $_POST['bbp_log_topic_edit'] ) && ( 1 == $_POST['bbp_log_topic_edit'] ) && ( $revision_id = wp_save_post_revision( $topic_id ) ) ) {
     586            bbp_update_topic_revision_log( array(
     587                'topic_id'    => $topic_id,
     588                'revision_id' => $revision_id,
     589                'author_id'   => bbp_get_current_user_id(),
     590                'reason'      => $topic_edit_reason
     591            ) );
    441592        }
    442593
    443         /** Topic Forum *******************************************************/
    444 
    445         // Forum id was not passed
    446         if ( empty( $_POST['bbp_forum_id'] ) )
    447             $bbp->errors->add( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
    448 
    449         // Forum id was passed
    450         elseif ( is_numeric( $_POST['bbp_forum_id'] ) )
    451             $forum_id = (int) $_POST['bbp_forum_id'];
    452 
    453         // Forum exists
    454         if ( !empty( $forum_id ) && ( $forum_id != $topic->post_parent ) ) {
    455 
    456             // Forum is a category
    457             if ( bbp_is_forum_category( $forum_id ) )
    458                 $bbp->errors->add( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
    459 
    460             // Forum is closed and user cannot access
    461             if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
    462                 $bbp->errors->add( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
    463 
    464             // Forum is private and user cannot access
    465             if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
    466                 $bbp->errors->add( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    467 
    468             // Forum is hidden and user cannot access
    469             if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
    470                 $bbp->errors->add( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    471         }
    472 
    473         /** Topic Title *******************************************************/
    474 
    475         if ( !empty( $_POST['bbp_topic_title'] ) )
    476             $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
    477 
    478         // Filter and sanitize
    479         $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
    480 
    481         // No topic title
    482         if ( empty( $topic_title ) )
    483             $bbp->errors->add( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
    484 
    485         /** Topic Content *****************************************************/
    486 
    487         if ( !empty( $_POST['bbp_topic_content'] ) )
    488             $topic_content = $_POST['bbp_topic_content'];
    489 
    490         // Filter and sanitize
    491         $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
    492 
    493         // No topic content
    494         if ( empty( $topic_content ) )
    495             $bbp->errors->add( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
    496 
    497         /** Topic Tags ********************************************************/
    498 
    499         // Tags
    500         if ( !empty( $_POST['bbp_topic_tags'] ) ) {
    501 
    502             // Escape tag input
    503             $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
    504 
    505             // Explode by comma
    506             if ( strstr( $terms, ',' ) )
    507                 $terms = explode( ',', $terms );
    508 
    509             // Add topic tag ID as main key
    510             $terms = array( bbp_get_topic_tag_tax_id() => $terms );
    511         }
    512 
    513         /** Additional Actions (Before Save) **********************************/
    514 
    515         do_action( 'bbp_edit_topic_pre_extras', $topic_id );
    516 
    517         /** No Errors *********************************************************/
    518 
    519         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    520 
    521             /** Stickies ******************************************************/
    522 
    523             if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
    524 
    525                 // What's the dilly?
    526                 switch ( $_POST['bbp_stick_topic'] ) {
    527 
    528                     // Sticky in forum
    529                     case 'stick'   :
    530                         bbp_stick_topic( $topic_id );
    531                         break;
    532 
    533                     // Sticky in all forums
    534                     case 'super'   :
    535                         bbp_stick_topic( $topic_id, true );
    536                         break;
    537 
    538                     // Normal
    539                     case 'unstick' :
    540                     default        :
    541                         bbp_unstick_topic( $topic_id );
    542                         break;
    543                 }
    544             }
    545 
    546             /** Update the topic **********************************************/
    547 
    548             // Add the content of the form to $post as an array
    549             $topic_data = array(
    550                 'ID'           => $topic_id,
    551                 'post_title'   => $topic_title,
    552                 'post_content' => $topic_content,
    553                 'post_parent'  => $forum_id,
    554                 'tax_input'    => $terms,
    555             );
    556 
    557             // Just in time manipulation of topic data before being edited
    558             $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', $topic_data );
    559 
    560             // Insert topic
    561             $topic_id = wp_update_post( $topic_data );
    562 
    563             /** Revisions *****************************************************/
    564 
    565             // Revision Reason
    566             if ( !empty( $_POST['bbp_topic_edit_reason'] ) )
    567                 $topic_edit_reason = esc_attr( strip_tags( $_POST['bbp_topic_edit_reason'] ) );
    568 
    569             // Update revision log
    570             if ( !empty( $_POST['bbp_log_topic_edit'] ) && ( 1 == $_POST['bbp_log_topic_edit'] ) && ( $revision_id = wp_save_post_revision( $topic_id ) ) ) {
    571                 bbp_update_topic_revision_log( array(
    572                     'topic_id'    => $topic_id,
    573                     'revision_id' => $revision_id,
    574                     'author_id'   => bbp_get_current_user_id(),
    575                     'reason'      => $topic_edit_reason
    576                 ) );
    577             }
    578 
    579             /** No Errors *****************************************************/
    580 
    581             if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
    582 
    583                 // Update counts, etc...
    584                 do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic->post_author , true /* Is edit */ );
    585 
    586                 // If the new forum id is not equal to the old forum id, run the
    587                 // bbp_move_topic action and pass the topic's forum id as the
    588                 // first arg and topic id as the second to update counts.
    589                 if ( $forum_id != $topic->post_parent )
    590                     bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
    591 
    592                 /** Additional Actions (After Save) ***************************/
    593 
    594                 do_action( 'bbp_edit_topic_post_extras', $topic_id );
    595 
    596                 /** Redirect **************************************************/
    597 
    598                 // Redirect to
    599                 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
    600 
    601                 // View all?
    602                 $view_all = bbp_get_view_all();
    603 
    604                 // Get the topic URL
    605                 $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
    606 
    607                 // Add view all?
    608                 if ( !empty( $view_all ) )
    609                     $topic_url = bbp_add_view_all( $topic_url );
    610 
    611                 // Allow to be filtered
    612                 $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
    613 
    614                 /** Successful Edit *******************************************/
    615 
    616                 // Redirect back to new topic
    617                 wp_safe_redirect( $topic_url );
    618 
    619                 // For good measure
    620                 exit();
    621 
    622             /** Errors ********************************************************/
    623 
    624             } else {
    625                 $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
    626                 $bbp->errors->add( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress' ) );
    627             }
     594        /** No Errors *****************************************************/
     595
     596        if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
     597
     598            // Update counts, etc...
     599            do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic->post_author , true /* Is edit */ );
     600
     601            // If the new forum id is not equal to the old forum id, run the
     602            // bbp_move_topic action and pass the topic's forum id as the
     603            // first arg and topic id as the second to update counts.
     604            if ( $forum_id != $topic->post_parent )
     605                bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
     606
     607            /** Additional Actions (After Save) ***************************/
     608
     609            do_action( 'bbp_edit_topic_post_extras', $topic_id );
     610
     611            /** Redirect **************************************************/
     612
     613            // Redirect to
     614            $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
     615
     616            // View all?
     617            $view_all = bbp_get_view_all();
     618
     619            // Get the topic URL
     620            $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
     621
     622            // Add view all?
     623            if ( !empty( $view_all ) )
     624                $topic_url = bbp_add_view_all( $topic_url );
     625
     626            // Allow to be filtered
     627            $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
     628
     629            /** Successful Edit *******************************************/
     630
     631            // Redirect back to new topic
     632            wp_safe_redirect( $topic_url );
     633
     634            // For good measure
     635            exit();
     636
     637        /** Errors ********************************************************/
     638
     639        } else {
     640            $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
     641            bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress' ) );
    628642        }
    629643    }
     
    757771
    758772    // Validate topic_id
    759     if ( $topic_id = bbp_get_topic_id( $topic_id ) ) {
     773    $topic_id = bbp_get_topic_id( $topic_id );
     774
     775    // Topic was passed
     776    if ( !empty( $topic_id ) ) {
    760777
    761778        // Get the forum ID if none was passed
    762         if ( empty( $forum_id )  )
     779        if ( empty( $forum_id )  ) {
    763780            $forum_id = bbp_get_topic_forum_id( $topic_id );
     781        }
    764782
    765783        // Set the active_id based on topic_id/reply_id
     
    903921function bbp_merge_topic_handler() {
    904922
    905     // Only proceed if POST is an merge topic request
    906     if ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) && !empty( $_POST['action'] ) && ( 'bbp-merge-topic' === $_POST['action'] ) ) {
    907         global $bbp;
    908 
    909         // Define local variable(s)
    910         $source_topic_id = $destination_topic_id = 0;
    911         $source_topic = $destination_topic = 0;
    912         $subscribers = $favoriters = $replies = array();
    913 
    914         /** Source Topic ******************************************************/
    915 
    916         // Topic id
    917         if ( empty( $_POST['bbp_topic_id'] ) )
    918             $bbp->errors->add( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
    919         else
    920             $source_topic_id = (int) $_POST['bbp_topic_id'];
    921 
    922         // Nonce check
    923         check_admin_referer( 'bbp-merge-topic_' . $source_topic_id );
    924 
    925         // Source topic not found
    926         if ( !$source_topic = bbp_get_topic( $source_topic_id ) )
    927             $bbp->errors->add( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
    928 
    929         // Cannot edit source topic
    930         if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
    931             $bbp->errors->add( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
    932 
    933         /** Destination Topic *************************************************/
    934 
    935         // Topic id
    936         if ( empty( $_POST['bbp_destination_topic'] ) )
    937             $bbp->errors->add( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
    938         else
    939             $destination_topic_id = (int) $_POST['bbp_destination_topic'];
    940 
    941         // Destination topic not found
    942         if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
    943             $bbp->errors->add( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
    944 
    945         // Cannot edit destination topic
    946         if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
    947             $bbp->errors->add( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress' ) );
    948 
    949         /** No Errors *********************************************************/
    950 
    951         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    952 
    953             // Update counts, etc...
    954             do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
    955 
    956             /** Date Check ****************************************************/
    957 
    958             // Check if the destination topic is older than the source topic
    959             if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
    960 
    961                 // Set destination topic post_date to 1 second before source topic
    962                 $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
    963 
     923    // Bail if not a POST action
     924    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     925        return;
     926
     927    // Bail if action is not bbp-merge-topic
     928    if ( empty( $_POST['action'] ) || ( 'bbp-merge-topic' !== $_POST['action'] ) )
     929        return;
     930
     931    // Define local variable(s)
     932    $source_topic_id = $destination_topic_id = 0;
     933    $source_topic = $destination_topic = 0;
     934    $subscribers = $favoriters = $replies = array();
     935
     936    /** Source Topic ******************************************************/
     937
     938    // Topic id
     939    if ( empty( $_POST['bbp_topic_id'] ) )
     940        bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
     941    else
     942        $source_topic_id = (int) $_POST['bbp_topic_id'];
     943
     944    // Nonce check
     945    check_admin_referer( 'bbp-merge-topic_' . $source_topic_id );
     946
     947    // Source topic not found
     948    if ( !$source_topic = bbp_get_topic( $source_topic_id ) )
     949        bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
     950
     951    // Cannot edit source topic
     952    if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
     953        bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
     954
     955    /** Destination Topic *************************************************/
     956
     957    // Topic id
     958    if ( empty( $_POST['bbp_destination_topic'] ) )
     959        bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
     960    else
     961        $destination_topic_id = (int) $_POST['bbp_destination_topic'];
     962
     963    // Destination topic not found
     964    if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
     965        bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
     966
     967    // Cannot edit destination topic
     968    if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
     969        bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress' ) );
     970
     971    /** No Errors *********************************************************/
     972
     973    if ( !bbp_has_errors() ) {
     974
     975        // Update counts, etc...
     976        do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
     977
     978        /** Date Check ****************************************************/
     979
     980        // Check if the destination topic is older than the source topic
     981        if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
     982
     983            // Set destination topic post_date to 1 second before source topic
     984            $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
     985
     986            $postarr = array(
     987                'ID'            => $destination_topic_id,
     988                'post_date'     => $destination_post_date,
     989                'post_date_gmt' => get_gmt_from_date( $destination_post_date )
     990            );
     991
     992            // Update destination topic
     993            wp_update_post( $postarr );
     994        }
     995
     996        /** Subscriptions *************************************************/
     997
     998        // Get subscribers from source topic
     999        $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
     1000
     1001        // Remove the topic from everybody's subscriptions
     1002        if ( !empty( $subscribers ) ) {
     1003
     1004            // Loop through each user
     1005            foreach ( (array) $subscribers as $subscriber ) {
     1006
     1007                // Shift the subscriber if told to
     1008                if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( 1 == $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
     1009                    bbp_add_user_subscription( $subscriber, $destination_topic->ID );
     1010
     1011                // Remove old subscription
     1012                bbp_remove_user_subscription( $subscriber, $source_topic->ID );
     1013            }
     1014        }
     1015
     1016        /** Favorites *****************************************************/
     1017
     1018        // Get favoriters from source topic
     1019        $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
     1020
     1021        // Remove the topic from everybody's favorites
     1022        if ( !empty( $favoriters ) ) {
     1023
     1024            // Loop through each user
     1025            foreach ( (array) $favoriters as $favoriter ) {
     1026
     1027                // Shift the favoriter if told to
     1028                if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] )
     1029                    bbp_add_user_favorite( $favoriter, $destination_topic->ID );
     1030
     1031                // Remove old favorite
     1032                bbp_remove_user_favorite( $favoriter, $source_topic->ID );
     1033            }
     1034        }
     1035
     1036        /** Tags **********************************************************/
     1037
     1038        // Get the source topic tags
     1039        $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
     1040
     1041        // Tags to possibly merge
     1042        if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
     1043
     1044            // Shift the tags if told to
     1045            if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) )
     1046                wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
     1047
     1048            // Delete the tags from the source topic
     1049            wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
     1050        }
     1051
     1052        /** Source Topic **************************************************/
     1053
     1054        // Status
     1055        bbp_open_topic( $source_topic->ID );
     1056
     1057        // Sticky
     1058        bbp_unstick_topic( $source_topic->ID );
     1059
     1060        // Get the replies of the source topic
     1061        $replies = (array) get_posts( array(
     1062            'post_parent'    => $source_topic->ID,
     1063            'post_type'      => bbp_get_reply_post_type(),
     1064            'posts_per_page' => -1,
     1065            'order'          => 'ASC'
     1066        ) );
     1067
     1068        // Prepend the source topic to its replies array for processing
     1069        array_unshift( $replies, $source_topic );
     1070
     1071        if ( !empty( $replies ) ) {
     1072
     1073            /** Merge Replies *************************************************/
     1074
     1075            // Change the post_parent of each reply to the destination topic id
     1076            foreach ( $replies as $reply ) {
    9641077                $postarr = array(
    965                     'ID'            => $destination_topic_id,
    966                     'post_date'     => $destination_post_date,
    967                     'post_date_gmt' => get_gmt_from_date( $destination_post_date )
     1078                    'ID'          => $reply->ID,
     1079                    'post_title'  => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
     1080                    'post_name'   => false,
     1081                    'post_type'   => bbp_get_reply_post_type(),
     1082                    'post_parent' => $destination_topic->ID,
     1083                    'guid'        => ''
    9681084                );
    9691085
    970                 // Update destination topic
    9711086                wp_update_post( $postarr );
     1087
     1088                // Adjust reply meta values
     1089                bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
     1090                bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
     1091
     1092                // Do additional actions per merged reply
     1093                do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
    9721094            }
    973 
    974             /** Subscriptions *************************************************/
    975 
    976             // Remove the topic from everybody's subscriptions
    977             if ( $subscribers = bbp_get_topic_subscribers( $source_topic->ID ) ) {
    978 
    979                 // Loop through each user
    980                 foreach ( (array) $subscribers as $subscriber ) {
    981 
    982                     // Shift the subscriber if told to
    983                     if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( 1 == $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
    984                         bbp_add_user_subscription( $subscriber, $destination_topic->ID );
    985 
    986                     // Remove old subscription
    987                     bbp_remove_user_subscription( $subscriber, $source_topic->ID );
    988                 }
    989             }
    990 
    991             /** Favorites *****************************************************/
    992 
    993             // Remove the topic from everybody's favorites
    994             if ( $favoriters = bbp_get_topic_favoriters( $source_topic->ID ) ) {
    995 
    996                 // Loop through each user
    997                 foreach ( (array) $favoriters as $favoriter ) {
    998 
    999                     // Shift the favoriter if told to
    1000                     if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] )
    1001                         bbp_add_user_favorite( $favoriter, $destination_topic->ID );
    1002 
    1003                     // Remove old favorite
    1004                     bbp_remove_user_favorite( $favoriter, $source_topic->ID );
    1005                 }
    1006             }
    1007 
    1008             /** Tags **********************************************************/
    1009 
    1010             // Get the source topic tags
    1011             $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
    1012 
    1013             // Tags to possibly merge
    1014             if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
    1015 
    1016                 // Shift the tags if told to
    1017                 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) )
    1018                     wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
    1019 
    1020                 // Delete the tags from the source topic
    1021                 wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
    1022             }
    1023 
    1024             /** Source Topic **************************************************/
    1025 
    1026             // Status
    1027             bbp_open_topic( $source_topic->ID );
    1028 
    1029             // Sticky
    1030             bbp_unstick_topic( $source_topic->ID );
    1031 
    1032             // Get the replies of the source topic
    1033             $replies = (array) get_posts( array(
    1034                 'post_parent'    => $source_topic->ID,
    1035                 'post_type'      => bbp_get_reply_post_type(),
    1036                 'posts_per_page' => -1,
    1037                 'order'          => 'ASC'
    1038             ) );
    1039 
    1040             // Prepend the source topic to its replies array for processing
    1041             array_unshift( $replies, $source_topic );
    1042 
    1043             if ( !empty( $replies ) ) {
    1044 
    1045                 /** Merge Replies *************************************************/
    1046 
    1047                 // Change the post_parent of each reply to the destination topic id
    1048                 foreach ( $replies as $reply ) {
    1049                     $postarr = array(
    1050                         'ID'          => $reply->ID,
    1051                         'post_title'  => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
    1052                         'post_name'   => false,
    1053                         'post_type'   => bbp_get_reply_post_type(),
    1054                         'post_parent' => $destination_topic->ID,
    1055                         'guid'        => ''
    1056                     );
    1057 
    1058                     wp_update_post( $postarr );
    1059 
    1060                     // Adjust reply meta values
    1061                     bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
    1062                     bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
    1063 
    1064                     // Do additional actions per merged reply
    1065                     do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
    1066                 }
    1067             }
    1068 
    1069             /** Successful Merge *******************************************/
    1070 
    1071             // Send the post parent of the source topic as it has been shifted
    1072             // (possibly to a new forum) so we need to update the counts of the
    1073             // old forum as well as the new one
    1074             do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
    1075 
    1076             // Redirect back to new topic
    1077             wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
    1078 
    1079             // For good measure
    1080             exit();
    10811095        }
     1096
     1097        /** Successful Merge *******************************************/
     1098
     1099        // Send the post parent of the source topic as it has been shifted
     1100        // (possibly to a new forum) so we need to update the counts of the
     1101        // old forum as well as the new one
     1102        do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
     1103
     1104        // Redirect back to new topic
     1105        wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
     1106
     1107        // For good measure
     1108        exit();
    10821109    }
    10831110}
     
    11671194function bbp_split_topic_handler() {
    11681195
    1169     // Only proceed if POST is an split topic request
    1170     if ( ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) && !empty( $_POST['action'] ) && ( 'bbp-split-topic' === $_POST['action'] ) ) {
    1171         global $wpdb, $bbp;
    1172 
    1173         // Prevent debug notices
    1174         $from_reply_id = $destination_topic_id = 0;
    1175         $destination_topic_title = '';
    1176         $destination_topic = $from_reply = $source_topic = '';
    1177         $split_option = false;
    1178 
    1179         /** Split Reply *******************************************************/
    1180 
    1181         if ( empty( $_POST['bbp_reply_id'] ) )
    1182             $bbp->errors->add( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress' ) );
    1183         else
    1184             $from_reply_id = (int) $_POST['bbp_reply_id'];
    1185 
    1186         $from_reply = bbp_get_reply( $from_reply_id );
    1187 
    1188         // Reply exists
    1189         if ( empty( $from_reply ) )
    1190             $bbp->errors->add( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
    1191 
    1192         /** Topic to Split ****************************************************/
    1193 
    1194         // Get the topic being split
    1195         $source_topic = bbp_get_topic( $from_reply->post_parent );
    1196 
    1197         // No topic
    1198         if ( empty( $source_topic ) )
    1199             $bbp->errors->add( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
    1200 
    1201         // Nonce check
    1202         check_admin_referer( 'bbp-split-topic_' . $source_topic->ID );
    1203 
    1204         // Use cannot edit topic
    1205         if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
    1206             $bbp->errors->add( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
    1207 
    1208         /** How to Split ******************************************************/
    1209 
    1210         if ( !empty( $_POST['bbp_topic_split_option'] ) )
    1211             $split_option = (string) trim( $_POST['bbp_topic_split_option'] );
    1212 
    1213         // Invalid split option
    1214         if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) {
    1215             $bbp->errors->add( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
    1216 
    1217         // Valid Split Option
    1218         } else {
    1219 
    1220             // What kind of split
    1221             switch ( $split_option ) {
    1222 
    1223                 // Into an existing topic
    1224                 case 'existing' :
    1225 
    1226                     // Get destination topic id
    1227                     if ( empty( $_POST['bbp_destination_topic'] ) )
    1228                         $bbp->errors->add( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress' ) );
    1229                     else
    1230                         $destination_topic_id = (int) $_POST['bbp_destination_topic'];
    1231 
    1232                     // Get the destination topic
    1233                     $destination_topic = bbp_get_topic( $destination_topic_id );
    1234 
    1235                     // No destination topic
    1236                     if ( empty( $destination_topic ) )
    1237                         $bbp->errors->add( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress' ) );
    1238 
    1239                     // User cannot edit the destination topic
    1240                     if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
    1241                         $bbp->errors->add( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress' ) );
    1242 
    1243                     break;
    1244 
    1245                 // Split at reply into a new topic
    1246                 case 'reply' :
    1247                 default :
    1248 
    1249                     // User needs to be able to publish topics
    1250                     if ( current_user_can( 'publish_topics' ) ) {
    1251 
    1252                         // Use the new title that was passed
    1253                         if ( !empty( $_POST['bbp_topic_split_destination_title'] ) )
    1254                             $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) );
    1255 
    1256                         // Use the source topic title
    1257                         else
    1258                             $destination_topic_title = $source_topic->post_title;
    1259 
    1260                         // Setup the updated topic parameters
    1261                         $postarr = array(
    1262                             'ID'          => $from_reply->ID,
    1263                             'post_title'  => $destination_topic_title,
    1264                             'post_name'   => false,
    1265                             'post_type'   => bbp_get_topic_post_type(),
    1266                             'post_parent' => $source_topic->post_parent,
    1267                             'guid'        => ''
    1268                         );
    1269 
    1270                         // Update the topic
    1271                         $destination_topic_id = wp_update_post( $postarr );
    1272 
    1273                         // Make sure the new topic knows its a topic
    1274                         bbp_update_topic_topic_id( $from_reply->ID );
    1275 
    1276                         // Shouldn't happen
    1277                         if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || !$destination_topic = bbp_get_topic( $destination_topic_id ) )
    1278                             $bbp->errors->add( 'bbp_split_topic_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress' ) );
    1279 
    1280                     // User cannot publish posts
     1196    // Bail if not a POST action
     1197    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     1198        return;
     1199   
     1200    // Bail if action is not 'bbp-split-topic'
     1201    if ( empty( $_POST['action'] ) || ( 'bbp-split-topic' !== $_POST['action'] ) )
     1202        return;
     1203
     1204    global $wpdb, $bbp;
     1205
     1206    // Prevent debug notices
     1207    $from_reply_id = $destination_topic_id = 0;
     1208    $destination_topic_title = '';
     1209    $destination_topic = $from_reply = $source_topic = '';
     1210    $split_option = false;
     1211
     1212    /** Split Reply *******************************************************/
     1213
     1214    if ( empty( $_POST['bbp_reply_id'] ) )
     1215        bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress' ) );
     1216    else
     1217        $from_reply_id = (int) $_POST['bbp_reply_id'];
     1218
     1219    $from_reply = bbp_get_reply( $from_reply_id );
     1220
     1221    // Reply exists
     1222    if ( empty( $from_reply ) )
     1223        bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
     1224
     1225    /** Topic to Split ****************************************************/
     1226
     1227    // Get the topic being split
     1228    $source_topic = bbp_get_topic( $from_reply->post_parent );
     1229
     1230    // No topic
     1231    if ( empty( $source_topic ) )
     1232        bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
     1233
     1234    // Nonce check
     1235    check_admin_referer( 'bbp-split-topic_' . $source_topic->ID );
     1236
     1237    // Use cannot edit topic
     1238    if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
     1239        bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
     1240
     1241    /** How to Split ******************************************************/
     1242
     1243    if ( !empty( $_POST['bbp_topic_split_option'] ) )
     1244        $split_option = (string) trim( $_POST['bbp_topic_split_option'] );
     1245
     1246    // Invalid split option
     1247    if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) {
     1248        bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
     1249
     1250    // Valid Split Option
     1251    } else {
     1252
     1253        // What kind of split
     1254        switch ( $split_option ) {
     1255
     1256            // Into an existing topic
     1257            case 'existing' :
     1258
     1259                // Get destination topic id
     1260                if ( empty( $_POST['bbp_destination_topic'] ) )
     1261                    bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress' ) );
     1262                else
     1263                    $destination_topic_id = (int) $_POST['bbp_destination_topic'];
     1264
     1265                // Get the destination topic
     1266                $destination_topic = bbp_get_topic( $destination_topic_id );
     1267
     1268                // No destination topic
     1269                if ( empty( $destination_topic ) )
     1270                    bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress' ) );
     1271
     1272                // User cannot edit the destination topic
     1273                if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
     1274                    bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress' ) );
     1275
     1276                break;
     1277
     1278            // Split at reply into a new topic
     1279            case 'reply' :
     1280            default :
     1281
     1282                // User needs to be able to publish topics
     1283                if ( current_user_can( 'publish_topics' ) ) {
     1284
     1285                    // Use the new title that was passed
     1286                    if ( !empty( $_POST['bbp_topic_split_destination_title'] ) ) {
     1287                        $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) );
     1288
     1289                    // Use the source topic title
    12811290                    } else {
    1282                         $bbp->errors->add( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
     1291                        $destination_topic_title = $source_topic->post_title;
    12831292                    }
    12841293
    1285                     break;
     1294                    // Setup the updated topic parameters
     1295                    $postarr = array(
     1296                        'ID'          => $from_reply->ID,
     1297                        'post_title'  => $destination_topic_title,
     1298                        'post_name'   => false,
     1299                        'post_type'   => bbp_get_topic_post_type(),
     1300                        'post_parent' => $source_topic->post_parent,
     1301                        'guid'        => ''
     1302                    );
     1303
     1304                    // Update the topic
     1305                    $destination_topic_id = wp_update_post( $postarr );
     1306                    $destination_topic    = bbp_get_topic( $destination_topic_id );
     1307
     1308                    // Make sure the new topic knows its a topic
     1309                    bbp_update_topic_topic_id( $from_reply->ID );
     1310
     1311                    // Shouldn't happen
     1312                    if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
     1313                        bbp_add_error( 'bbp_split_topic_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress' ) );
     1314                    }
     1315
     1316                // User cannot publish posts
     1317                } else {
     1318                    bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
     1319                }
     1320
     1321                break;
     1322        }
     1323    }
     1324
     1325    /** No Errors - Do the Spit *******************************************/
     1326
     1327    if ( !bbp_has_errors() ) {
     1328
     1329        // Update counts, etc...
     1330        do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
     1331
     1332        /** Subscriptions *************************************************/
     1333
     1334        // Copy the subscribers
     1335        if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
     1336
     1337            // Get the subscribers
     1338            $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
     1339
     1340            if ( !empty( $subscribers ) ) {
     1341
     1342                // Add subscribers to new topic
     1343                foreach ( (array) $subscribers as $subscriber ) {
     1344                    bbp_add_user_subscription( $subscriber, $destination_topic->ID );
     1345                }
    12861346            }
    12871347        }
    12881348
    1289         /** No Errors - Do the Spit *******************************************/
    1290 
    1291         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    1292 
    1293             // Update counts, etc...
    1294             do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
    1295 
    1296             /** Subscriptions *************************************************/
    1297 
    1298             // Copy the subscribers
    1299             if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
    1300 
    1301                 // Get the subscribers
    1302                 if ( $subscribers = bbp_get_topic_subscribers( $source_topic->ID ) ) {
    1303 
    1304                     // Add subscribers to new topic
    1305                     foreach ( (array) $subscribers as $subscriber ) {
    1306                         bbp_add_user_subscription( $subscriber, $destination_topic->ID );
    1307                     }
     1349        /** Favorites *****************************************************/
     1350
     1351        // Copy the favoriters if told to
     1352        if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
     1353
     1354            // Get the favoriters
     1355            $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
     1356
     1357            if ( !empty( $favoriters ) ) {
     1358
     1359                // Add the favoriters to new topic
     1360                foreach ( (array) $favoriters as $favoriter ) {
     1361                    bbp_add_user_favorite( $favoriter, $destination_topic->ID );
    13081362                }
    13091363            }
    1310 
    1311             /** Favorites *****************************************************/
    1312 
    1313             // Copy the favoriters if told to
    1314             if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
    1315 
    1316                 // Get the favoriters
    1317                 if ( $favoriters = bbp_get_topic_favoriters( $source_topic->ID ) ) {
    1318 
    1319                     // Add the favoriters to new topic
    1320                     foreach ( (array) $favoriters as $favoriter ) {
    1321                         bbp_add_user_favorite( $favoriter, $destination_topic->ID );
    1322                     }
    1323                 }
     1364        }
     1365
     1366        /** Tags **********************************************************/
     1367
     1368        // Copy the tags if told to
     1369        if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) ) {
     1370
     1371            // Get the source topic tags
     1372            $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
     1373
     1374            if ( !empty( $source_topic_tags ) ) {
     1375                wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
    13241376            }
    1325 
    1326             /** Tags **********************************************************/
    1327 
    1328             // Copy the tags if told to
    1329             if ( !empty( $_POST['bbp_topic_tags'] ) && 1 == $_POST['bbp_topic_tags'] ) {
    1330 
    1331                 // Get the source topic tags
    1332                 if ( $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ) ) {
    1333                     wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
    1334                 }
     1377        }
     1378
     1379        /** Split Replies *************************************************/
     1380
     1381        // get_posts() is not used because it doesn't allow us to use '>='
     1382        // comparision without a filter.
     1383        $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
     1384
     1385        // Make sure there are replies to loop through
     1386        if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
     1387
     1388            // Change the post_parent of each reply to the destination topic id
     1389            foreach ( $replies as $reply ) {
     1390
     1391                // New reply data
     1392                $postarr = array(
     1393                    'ID'          => $reply->ID,
     1394                    'post_title'  => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
     1395                    'post_name'   => false, // will be automatically generated
     1396                    'post_parent' => $destination_topic->ID,
     1397                    'guid'        => ''
     1398                );
     1399
     1400                // Update the reply
     1401                wp_update_post( $postarr );
     1402
     1403                // Adjust reply meta values
     1404                bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
     1405                bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
     1406
     1407                // Do additional actions per split reply
     1408                do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
    13351409            }
    1336 
    1337             /** Split Replies *************************************************/
    1338 
    1339             // get_posts() is not used because it doesn't allow us to use '>='
    1340             // comparision without a filter.
    1341             $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
    1342 
    1343             // Make sure there are replies to loop through
    1344             if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
    1345 
    1346                 // Change the post_parent of each reply to the destination topic id
    1347                 foreach ( $replies as $reply ) {
    1348 
    1349                     // New reply data
    1350                     $postarr = array(
    1351                         'ID'          => $reply->ID,
    1352                         'post_title'  => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
    1353                         'post_name'   => false, // will be automatically generated
    1354                         'post_parent' => $destination_topic->ID,
    1355                         'guid'        => ''
    1356                     );
    1357 
    1358                     // Update the reply
    1359                     wp_update_post( $postarr );
    1360 
    1361                     // Adjust reply meta values
    1362                     bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
    1363                     bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
    1364 
    1365                     // Do additional actions per split reply
    1366                     do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
    1367                 }
    1368             }
    1369 
    1370             // It is a new topic and we need to set some default metas to make
    1371             // the topic display in bbp_has_topics() list
    1372             if ( 'reply' == $split_option ) {
    1373                 $last_reply_id = ( empty( $reply ) || empty( $reply->ID        ) ) ? 0  : $reply->ID;
    1374                 $freshness     = ( empty( $reply ) || empty( $reply->post_date ) ) ? '' : $reply->post_date;
    1375 
    1376                 bbp_update_topic_last_reply_id   ( $destination_topic->ID, $last_reply_id );
    1377                 bbp_update_topic_last_active_time( $destination_topic->ID, $freshness     );
    1378             }
    1379 
    1380             /** Successful Split **********************************************/
    1381 
    1382             // Update counts, etc...
    1383             do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
    1384 
    1385             // Redirect back to the topic
    1386             wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
    1387 
    1388             // For good measure
    1389             exit();
    13901410        }
     1411
     1412        // It is a new topic and we need to set some default metas to make
     1413        // the topic display in bbp_has_topics() list
     1414        if ( 'reply' == $split_option ) {
     1415            $last_reply_id = ( empty( $reply ) || empty( $reply->ID        ) ) ? 0  : $reply->ID;
     1416            $freshness     = ( empty( $reply ) || empty( $reply->post_date ) ) ? '' : $reply->post_date;
     1417
     1418            bbp_update_topic_last_reply_id   ( $destination_topic->ID, $last_reply_id );
     1419            bbp_update_topic_last_active_time( $destination_topic->ID, $freshness     );
     1420        }
     1421
     1422        /** Successful Split **********************************************/
     1423
     1424        // Update counts, etc...
     1425        do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
     1426
     1427        // Redirect back to the topic
     1428        wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
     1429
     1430        // For good measure
     1431        exit();
    13911432    }
    13921433}
     
    14551496function bbp_manage_topic_tag_handler() {
    14561497
    1457     // Are we managing a tag?
    1458     if ( ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) && !empty( $_POST['tag-id'] ) && !empty( $_POST['action'] ) && in_array( $_POST['action'], array( 'bbp-update-topic-tag', 'bbp-merge-topic-tag', 'bbp-delete-topic-tag' ) ) ) {
    1459 
    1460         global $bbp;
    1461 
    1462         // Setup vars
    1463         $action = $_POST['action'];
    1464         $tag_id = (int) $_POST['tag-id'];
    1465         $tag    = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
    1466 
    1467         // Tag does not exist
    1468         if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
    1469             $bbp->errors->add( '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() ) );
    1470             return;
    1471         }
    1472 
    1473         // What action are we trying to perform?
    1474         switch ( $action ) {
    1475 
    1476             // Update tag
    1477             case 'bbp-update-topic-tag' :
    1478 
    1479                 // Nonce check
    1480                 check_admin_referer( 'update-tag_' . $tag_id );
    1481 
    1482                 // Can user edit topic tags?
    1483                 if ( !current_user_can( 'edit_topic_tags' ) ) {
    1484                     $bbp->errors->add( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
    1485                     return;
    1486                 }
    1487 
    1488                 // No tag name was provided
    1489                 if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
    1490                     $bbp->errors->add( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
    1491                     return;
    1492                 }
    1493 
    1494                 // Attempt to update the tag
    1495                 $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
    1496                 $tag  = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
    1497 
    1498                 // Cannot update tag
    1499                 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
    1500                     $bbp->errors->add( '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() ) );
    1501                     return;
    1502                 }
    1503 
    1504                 // Redirect
    1505                 $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
    1506 
    1507                 // Update counts, etc...
    1508                 do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
    1509 
    1510                 break;
    1511 
    1512             // Merge two tags
    1513             case 'bbp-merge-topic-tag'  :
    1514 
    1515                 // Nonce check
    1516                 check_admin_referer( 'merge-tag_' . $tag_id );
    1517 
    1518                 // Can user edit topic tags?
    1519                 if ( !current_user_can( 'edit_topic_tags' ) ) {
    1520                     $bbp->errors->add( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
    1521                     return;
    1522                 }
    1523 
    1524                 // No tag name was provided
    1525                 if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
    1526                     $bbp->errors->add( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
    1527                     return;
    1528                 }
    1529 
    1530                 // If term does not exist, create it
    1531                 if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
    1532                     $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
    1533 
    1534                 // Problem inserting the new term
    1535                 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
    1536                     $bbp->errors->add( '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() ) );
    1537                     return;
    1538                 }
    1539 
    1540                 // Merging in to...
    1541                 $to_tag = $tag['term_id'];
    1542 
    1543                 // Attempting to merge a tag into itself
    1544                 if ( $tag_id == $to_tag ) {
    1545                     $bbp->errors->add( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
    1546                     return;
    1547                 }
    1548 
    1549                 // Delete the old term
    1550                 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
    1551 
    1552                 // Error merging the terms
    1553                 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
    1554                     $bbp->errors->add( '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() ) );
    1555                     return;
    1556                 }
    1557 
    1558                 // Redirect
    1559                 $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
    1560 
    1561                 // Update counts, etc...
    1562                 do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
    1563 
    1564                 break;
    1565 
    1566             // Delete tag
    1567             case 'bbp-delete-topic-tag' :
    1568 
    1569                 // Nonce check
    1570                 check_admin_referer( 'delete-tag_' . $tag_id );
    1571 
    1572                 // Can user delete topic tags?
    1573                 if ( !current_user_can( 'delete_topic_tags' ) ) {
    1574                     $bbp->errors->add( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
    1575                     return;
    1576                 }
    1577 
    1578                 // Attempt to delete term
    1579                 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
    1580 
    1581                 // Error deleting term
    1582                 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
    1583                     $bbp->errors->add( '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() ) );
    1584                     return;
    1585                 }
    1586 
    1587                 // We don't have any other place to go other than home! Or we may die because of the 404 disease
    1588                 $redirect = home_url();
    1589 
    1590                 // Update counts, etc...
    1591                 do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
    1592 
    1593                 break;
    1594         }
    1595 
    1596         /** Successful Moderation *********************************************/
    1597 
    1598         // Redirect back
    1599         $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
    1600         wp_safe_redirect( $redirect );
    1601 
    1602         // For good measure
    1603         exit();
    1604     }
     1498    // Bail if not a POST action
     1499    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     1500        return;
     1501   
     1502    // Bail if required POST actions aren't passed
     1503    if ( empty( $_POST['tag-id'] ) || empty( $_POST['action'] ) )
     1504        return;
     1505
     1506    // Setup possible get actions
     1507    $possible_actions = array(
     1508        'bbp-update-topic-tag',
     1509        'bbp-merge-topic-tag',
     1510        'bbp-delete-topic-tag'
     1511    );
     1512
     1513    // Bail if actions aren't meant for this function
     1514    if ( !in_array( $_POST['action'], $possible_actions ) )
     1515        return;
     1516
     1517    // Setup vars
     1518    $action = $_POST['action'];
     1519    $tag_id = (int) $_POST['tag-id'];
     1520    $tag    = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
     1521
     1522    // Tag does not exist
     1523    if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
     1524        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() ) );
     1525        return;
     1526    }
     1527
     1528    // What action are we trying to perform?
     1529    switch ( $action ) {
     1530
     1531        // Update tag
     1532        case 'bbp-update-topic-tag' :
     1533
     1534            // Nonce check
     1535            check_admin_referer( 'update-tag_' . $tag_id );
     1536
     1537            // Can user edit topic tags?
     1538            if ( !current_user_can( 'edit_topic_tags' ) ) {
     1539                bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
     1540                return;
     1541            }
     1542
     1543            // No tag name was provided
     1544            if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
     1545                bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
     1546                return;
     1547            }
     1548
     1549            // Attempt to update the tag
     1550            $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
     1551            $tag  = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
     1552
     1553            // Cannot update tag
     1554            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
     1555                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() ) );
     1556                return;
     1557            }
     1558
     1559            // Redirect
     1560            $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
     1561
     1562            // Update counts, etc...
     1563            do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
     1564
     1565            break;
     1566
     1567        // Merge two tags
     1568        case 'bbp-merge-topic-tag'  :
     1569
     1570            // Nonce check
     1571            check_admin_referer( 'merge-tag_' . $tag_id );
     1572
     1573            // Can user edit topic tags?
     1574            if ( !current_user_can( 'edit_topic_tags' ) ) {
     1575                bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
     1576                return;
     1577            }
     1578
     1579            // No tag name was provided
     1580            if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
     1581                bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
     1582                return;
     1583            }
     1584
     1585            // If term does not exist, create it
     1586            if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
     1587                $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
     1588
     1589            // Problem inserting the new term
     1590            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
     1591                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() ) );
     1592                return;
     1593            }
     1594
     1595            // Merging in to...
     1596            $to_tag = $tag['term_id'];
     1597
     1598            // Attempting to merge a tag into itself
     1599            if ( $tag_id == $to_tag ) {
     1600                bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
     1601                return;
     1602            }
     1603
     1604            // Delete the old term
     1605            $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
     1606
     1607            // Error merging the terms
     1608            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
     1609                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() ) );
     1610                return;
     1611            }
     1612
     1613            // Redirect
     1614            $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
     1615
     1616            // Update counts, etc...
     1617            do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
     1618
     1619            break;
     1620
     1621        // Delete tag
     1622        case 'bbp-delete-topic-tag' :
     1623
     1624            // Nonce check
     1625            check_admin_referer( 'delete-tag_' . $tag_id );
     1626
     1627            // Can user delete topic tags?
     1628            if ( !current_user_can( 'delete_topic_tags' ) ) {
     1629                bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
     1630                return;
     1631            }
     1632
     1633            // Attempt to delete term
     1634            $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
     1635
     1636            // Error deleting term
     1637            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
     1638                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() ) );
     1639                return;
     1640            }
     1641
     1642            // We don't have any other place to go other than home! Or we may die because of the 404 disease
     1643            $redirect = home_url();
     1644
     1645            // Update counts, etc...
     1646            do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
     1647
     1648            break;
     1649    }
     1650
     1651    /** Successful Moderation *********************************************/
     1652
     1653    // Redirect back
     1654    $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
     1655    wp_safe_redirect( $redirect );
     1656
     1657    // For good measure
     1658    exit();
    16051659}
    16061660
     
    16761730function bbp_toggle_topic_handler() {
    16771731
    1678     // Only proceed if GET is a topic toggle action
    1679     if ( ( 'GET' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) && !empty( $_GET['topic_id'] ) && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_toggle_topic_close', 'bbp_toggle_topic_stick', 'bbp_toggle_topic_spam', 'bbp_toggle_topic_trash' ) ) ) {
    1680         global $bbp;
    1681 
    1682         $action    = $_GET['action'];            // What action is taking place?
    1683         $topic_id  = (int) $_GET['topic_id'];    // What's the topic id?
    1684         $success   = false;                      // Flag
    1685         $post_data = array( 'ID' => $topic_id ); // Prelim array
    1686 
    1687         // Make sure topic exists
    1688         if ( !$topic = bbp_get_topic( $topic_id ) )
    1689             return;
    1690 
    1691         // What is the user doing here?
    1692         if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
    1693             $bbp->errors->add( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
    1694             return;
     1732    // Bail if not a GET action
     1733    if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     1734        return;
     1735
     1736    // Bail if required GET actions aren't passed
     1737    if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
     1738        return;
     1739
     1740    // Setup possible get actions
     1741    $possible_actions = array(
     1742        'bbp_toggle_topic_close',
     1743        'bbp_toggle_topic_stick',
     1744        'bbp_toggle_topic_spam',
     1745        'bbp_toggle_topic_trash'
     1746    );
     1747
     1748    // Bail if actions aren't meant for this function
     1749    if ( !in_array( $_GET['action'], $possible_actions ) )
     1750        return;
     1751
     1752    $view_all  = false;                      // Assume not viewing all
     1753    $action    = $_GET['action'];            // What action is taking place?
     1754    $topic_id  = (int) $_GET['topic_id'];    // What's the topic id?
     1755    $success   = false;                      // Flag
     1756    $post_data = array( 'ID' => $topic_id ); // Prelim array
     1757
     1758    // Make sure topic exists
     1759    if ( !$topic = bbp_get_topic( $topic_id ) )
     1760        return;
     1761
     1762    // What is the user doing here?
     1763    if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
     1764        bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
     1765        return;
     1766    }
     1767
     1768    // What action are we trying to perform?
     1769    switch ( $action ) {
     1770
     1771        // Toggle open/close
     1772        case 'bbp_toggle_topic_close' :
     1773            check_ajax_referer( 'close-topic_' . $topic_id );
     1774
     1775            $is_open = bbp_is_topic_open( $topic_id );
     1776            $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
     1777            $failure = $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
     1778
     1779            break;
     1780
     1781        // Toggle sticky/super-sticky/unstick
     1782        case 'bbp_toggle_topic_stick' :
     1783            check_ajax_referer( 'stick-topic_' . $topic_id );
     1784
     1785            $is_sticky = bbp_is_topic_sticky( $topic_id );
     1786            $is_super  = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
     1787            $success   = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
     1788            $failure   = $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
     1789
     1790            break;
     1791
     1792        // Toggle spam
     1793        case 'bbp_toggle_topic_spam' :
     1794            check_ajax_referer( 'spam-topic_' . $topic_id );
     1795
     1796            $is_spam  = bbp_is_topic_spam( $topic_id );
     1797            $success  = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
     1798            $failure  = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' );
     1799            $view_all = !$is_spam;
     1800
     1801            break;
     1802
     1803        // Toggle trash
     1804        case 'bbp_toggle_topic_trash' :
     1805
     1806            $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
     1807
     1808            if ( empty( $sub_action ) )
     1809                break;
     1810
     1811            switch ( $sub_action ) {
     1812                case 'trash':
     1813                    check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
     1814
     1815                    $view_all = true;
     1816                    $success  = wp_trash_post( $topic_id );
     1817                    $failure  = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
     1818
     1819                    break;
     1820
     1821                case 'untrash':
     1822                    check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
     1823
     1824                    $success = wp_untrash_post( $topic_id );
     1825                    $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
     1826
     1827                    break;
     1828
     1829                case 'delete':
     1830                    check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
     1831
     1832                    $success = wp_delete_post( $topic_id );
     1833                    $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
     1834
     1835                    break;
     1836            }
     1837
     1838            break;
     1839    }
     1840
     1841    // Do additional topic toggle actions
     1842    do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
     1843
     1844    // No errors
     1845    if ( false != $success && !is_wp_error( $success ) ) {
     1846
     1847        // Redirect back to the topic's forum
     1848        if ( isset( $sub_action ) && ( 'delete' == $sub_action ) ) {
     1849            $redirect = bbp_get_forum_permalink( $success->post_parent );
     1850
     1851        // Redirect back to the topic
     1852        } else {
     1853
     1854            // Get the redirect detination
     1855            $permalink = bbp_get_topic_permalink( $topic_id );
     1856            $redirect  = bbp_add_view_all( $permalink, $view_all );
    16951857        }
    16961858
    1697         // What action are we trying to perform?
    1698         switch ( $action ) {
    1699 
    1700             // Toggle open/close
    1701             case 'bbp_toggle_topic_close' :
    1702                 check_ajax_referer( 'close-topic_' . $topic_id );
    1703 
    1704                 $is_open = bbp_is_topic_open( $topic_id );
    1705                 $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
    1706                 $failure = $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
    1707 
    1708                 break;
    1709 
    1710             // Toggle sticky/super-sticky/unstick
    1711             case 'bbp_toggle_topic_stick' :
    1712                 check_ajax_referer( 'stick-topic_' . $topic_id );
    1713 
    1714                 $is_sticky = bbp_is_topic_sticky( $topic_id );
    1715                 $is_super  = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
    1716                 $success   = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
    1717                 $failure   = $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
    1718 
    1719                 break;
    1720 
    1721             // Toggle spam
    1722             case 'bbp_toggle_topic_spam' :
    1723                 check_ajax_referer( 'spam-topic_' . $topic_id );
    1724 
    1725                 $is_spam = bbp_is_topic_spam( $topic_id );
    1726                 $success = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
    1727                 $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' );
    1728 
    1729                 break;
    1730 
    1731             // Toggle trash
    1732             case 'bbp_toggle_topic_trash' :
    1733 
    1734                 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
    1735 
    1736                 if ( empty( $sub_action ) )
    1737                     break;
    1738 
    1739                 switch ( $sub_action ) {
    1740                     case 'trash':
    1741                         check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
    1742 
    1743                         $success = wp_trash_post( $topic_id );
    1744                         $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
    1745 
    1746                         break;
    1747 
    1748                     case 'untrash':
    1749                         check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
    1750 
    1751                         $success = wp_untrash_post( $topic_id );
    1752                         $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
    1753 
    1754                         break;
    1755 
    1756                     case 'delete':
    1757                         check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
    1758 
    1759                         $success = wp_delete_post( $topic_id );
    1760                         $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
    1761 
    1762                         break;
    1763                 }
    1764 
    1765                 break;
    1766         }
    1767 
    1768         // Do additional topic toggle actions
    1769         do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
    1770 
    1771         // No errors
    1772         if ( false != $success && !is_wp_error( $success ) ) {
    1773 
    1774             // Redirect back to the topic's forum
    1775             if ( isset( $sub_action ) && ( 'delete' == $sub_action ) )
    1776                 $redirect = bbp_get_forum_permalink( $success->post_parent );
    1777 
    1778             // Redirect back to the topic
    1779             else
    1780                 $redirect = bbp_add_view_all( bbp_get_topic_permalink( $topic_id ) );
    1781 
    1782             wp_redirect( $redirect );
    1783 
    1784             // For good measure
    1785             exit();
    1786 
    1787         // Handle errors
    1788         } else {
    1789             $bbp->errors->add( 'bbp_toggle_topic', $failure );
    1790         }
     1859        wp_redirect( $redirect );
     1860
     1861        // For good measure
     1862        exit();
     1863
     1864    // Handle errors
     1865    } else {
     1866        bbp_add_error( 'bbp_toggle_topic', $failure );
    17911867    }
    17921868}
     
    25922668
    25932669    // Loop through and restore pre trashed replies to this topic
    2594     if ( $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true ) ) {
    2595         foreach ( $pre_trashed_replies as $reply )
     2670    $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
     2671
     2672    if ( !empty( $pre_trashed_replies ) ) {
     2673        foreach ( $pre_trashed_replies as $reply ) {
    25962674            wp_untrash_post( $reply );
     2675        }
    25972676    }
    25982677}
  • branches/plugin/bbp-includes/bbp-topic-template.php

    r3361 r3382  
    20982098        $topic   = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
    20992099
    2100         if ( empty( $topic ) || !current_user_can( 'delete_topic', $topic->ID ) )
     2100        if ( empty( $topic ) || !current_user_can( 'delete_topic', $topic->ID ) ) {
    21012101            return;
    2102 
    2103         if ( bbp_is_topic_trash( $topic->ID ) )
    2104             $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'untrash', 'topic_id' => $topic->ID ) ), 'untrash-' . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to restore that?', 'bbpress' ) ) . '\');">' . esc_html( $restore_text ) . '</a>';
    2105         elseif ( EMPTY_TRASH_DAYS )
    2106             $actions['trash']   = '<a title="' . esc_attr( __( 'Move this item to the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'trash', 'topic_id' => $topic->ID ) ), 'trash-' . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to trash that?', 'bbpress' ) ) . '\' );">' . esc_html( $trash_text ) . '</a>';
    2107 
    2108         if ( bbp_is_topic_trash( $topic->ID ) || !EMPTY_TRASH_DAYS )
     2102        }
     2103
     2104        if ( bbp_is_topic_trash( $topic->ID ) ) {
     2105            $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'untrash', 'topic_id' => $topic->ID ) ), 'untrash-' . $topic->post_type . '_' . $topic->ID ) ) . '">' . esc_html( $restore_text ) . '</a>';
     2106        } elseif ( EMPTY_TRASH_DAYS ) {
     2107            $actions['trash']   = '<a title="' . esc_attr( __( 'Move this item to the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'trash', 'topic_id' => $topic->ID ) ), 'trash-' . $topic->post_type . '_' . $topic->ID ) ) . '">' . esc_html( $trash_text ) . '</a>';
     2108        }
     2109
     2110        if ( bbp_is_topic_trash( $topic->ID ) || !EMPTY_TRASH_DAYS ) {
    21092111            $actions['delete']  = '<a title="' . esc_attr( __( 'Delete this item permanently', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'delete', 'topic_id' => $topic->ID ) ), 'delete-' . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );">' . esc_html( $delete_text ) . '</a>';
     2112        }
    21102113
    21112114        // Process the admin links
     
    24832486        return;
    24842487
    2485     $bbp->errors->add( 'topic_notice', $notice_text, 'message' );
     2488    bbp_add_error( 'topic_notice', $notice_text, 'message' );
    24862489}
    24872490
  • branches/plugin/bbp-includes/bbp-user-functions.php

    r3357 r3382  
    353353function bbp_favorites_handler() {
    354354
    355     // Only proceed if GET is a favorite action
    356     if ( 'GET' == $_SERVER['REQUEST_METHOD'] && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_favorite_add', 'bbp_favorite_remove' ) ) && !empty( $_GET['topic_id'] ) ) {
    357 
    358         global $bbp;
    359 
    360         // What action is taking place?
    361         $action  = $_GET['action'];
    362 
    363         // Get user_id
    364         $user_id = bbp_get_user_id( 0, true, true );
    365 
    366         // Check current user's ability to edit the user
    367         if ( !current_user_can( 'edit_user', $user_id ) )
    368             $bbp->errors->add( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
    369 
    370         // Load favorite info
    371         if ( !$topic_id = intval( $_GET['topic_id'] ) )
    372             $bbp->errors->add( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
    373 
    374         $is_favorite    = bbp_is_user_favorite( $user_id, $topic_id );
    375         $success        = false;
    376 
    377         // Handle insertion into posts table
    378         if ( !empty( $topic_id ) && !empty( $user_id ) && ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) ) {
    379 
    380             if ( $is_favorite && 'bbp_favorite_remove' == $action )
    381                 $success = bbp_remove_user_favorite( $user_id, $topic_id );
    382             elseif ( !$is_favorite && 'bbp_favorite_add' == $action )
    383                 $success = bbp_add_user_favorite( $user_id, $topic_id );
    384 
    385             // Do additional favorites actions
    386             do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
    387 
    388             // Check for missing reply_id or error
    389             if ( true == $success ) {
    390 
    391                 // Redirect back to new reply
    392                 if ( bbp_is_favorites() )
    393                     $redirect = bbp_get_favorites_permalink( $user_id );
    394                 elseif ( is_singular( bbp_get_topic_post_type() ) )
    395                     $redirect = bbp_get_topic_permalink( $topic_id );
    396                 else
    397                     $redirect = get_permalink();
    398 
    399                 wp_redirect( $redirect );
    400 
    401                 // For good measure
    402                 exit();
    403 
    404             // Handle errors
    405             } else {
    406                 if ( $is_favorite && 'bbp_favorite_remove' == $action )
    407                     $bbp->errors->add( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
    408                 elseif ( !$is_favorite && 'bbp_favorite_add' == $action )
    409                     $bbp->errors->add( 'bbp_favorite_add',    __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
     355    if ( !bbp_is_favorites_active() )
     356        return false;
     357
     358    // Bail if not a GET action
     359    if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     360        return;
     361
     362    // Bail if required GET actions aren't passed
     363    if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
     364        return;
     365
     366    // Setup possible get actions
     367    $possible_actions = array(
     368        'bbp_favorite_add',
     369        'bbp_favorite_remove',
     370    );
     371
     372    // Bail if actions aren't meant for this function
     373    if ( !in_array( $_GET['action'], $possible_actions ) )
     374        return;
     375
     376    // What action is taking place?
     377    $action  = $_GET['action'];
     378
     379    // Get user_id
     380    $user_id = bbp_get_user_id( 0, true, true );
     381
     382    // Check current user's ability to edit the user
     383    if ( !current_user_can( 'edit_user', $user_id ) )
     384        bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     385
     386    // Load favorite info
     387    if ( !$topic_id = intval( $_GET['topic_id'] ) )
     388        bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
     389
     390    $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
     391    $success     = false;
     392
     393    // Handle insertion into posts table
     394    if ( !empty( $topic_id ) && !empty( $user_id ) && ( !bbp_has_errors() ) ) {
     395
     396        if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
     397            $success = bbp_remove_user_favorite( $user_id, $topic_id );
     398        } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
     399            $success = bbp_add_user_favorite( $user_id, $topic_id );
     400        }
     401
     402        // Do additional favorites actions
     403        do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
     404
     405        // Check for missing reply_id or error
     406        if ( true == $success ) {
     407
     408            // Redirect back to new reply
     409            if ( bbp_is_favorites() ) {
     410                $redirect = bbp_get_favorites_permalink( $user_id );
     411            } elseif ( bbp_is_single_user() ) {
     412                $redirect = bbp_get_user_profile_url();
     413            } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
     414                $redirect = bbp_get_topic_permalink( $topic_id );
     415            } elseif ( is_single() || is_page() ) {
     416                $redirect = get_permalink();
     417            }
     418
     419            wp_redirect( $redirect );
     420
     421            // For good measure
     422            exit();
     423
     424        // Handle errors
     425        } else {
     426            if ( $is_favorite && 'bbp_favorite_remove' == $action ) {
     427                bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) );
     428            } elseif ( !$is_favorite && 'bbp_favorite_add' == $action ) {
     429                bbp_add_error( 'bbp_favorite_add',    __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) );
    410430            }
    411431        }
     
    628648        return false;
    629649
    630     // Only proceed if GET is a favorite action
    631     if ( 'GET' == $_SERVER['REQUEST_METHOD'] && !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_subscribe', 'bbp_unsubscribe' ) ) && !empty( $_GET['topic_id'] ) ) {
    632 
    633         global $bbp;
    634 
    635         // What action is taking place?
    636         $action  = $_GET['action'];
    637 
    638         // Get user_id
    639         $user_id = bbp_get_user_id( 0, true, true );
    640 
    641         // Check current user's ability to edit the user
    642         if ( !current_user_can( 'edit_user', $user_id ) )
    643             $bbp->errors->add( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
    644 
    645         // Load subscription info
    646         if ( !$topic_id = intval( $_GET['topic_id'] ) )
    647             $bbp->errors->add( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
    648 
    649         if ( !is_wp_error( $bbp->errors ) || !$bbp->errors->get_error_codes() ) {
    650 
    651             $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
    652             $success         = false;
    653 
    654             if ( $is_subscription && 'bbp_unsubscribe' == $action )
    655                 $success = bbp_remove_user_subscription( $user_id, $topic_id );
    656             elseif ( !$is_subscription && 'bbp_subscribe' == $action )
    657                 $success = bbp_add_user_subscription( $user_id, $topic_id );
    658 
    659             // Do additional subscriptions actions
    660             do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
    661 
    662             // Check for missing reply_id or error
    663             if ( true == $success ) {
    664 
    665                 // Redirect back to new reply
    666                 if ( bbp_is_subscriptions() )
    667                     $redirect = bbp_get_subscriptions_permalink( $user_id );
    668                 elseif ( is_singular( bbp_get_topic_post_type() ) )
    669                     $redirect = bbp_get_topic_permalink( $topic_id );
    670                 else
    671                     $redirect = get_permalink();
    672 
    673                 wp_redirect( $redirect );
    674 
    675                 // For good measure
    676                 exit();
    677 
    678             // Handle errors
    679             } else {
    680                 if ( $is_subscription && 'bbp_unsubscribe' == $action )
    681                     $bbp->errors->add( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
    682                 elseif ( !$is_subscription && 'bbp_subscribe' == $action )
    683                     $bbp->errors->add( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
     650    // Bail if not a GET action
     651    if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     652        return;
     653
     654    // Bail if required GET actions aren't passed
     655    if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
     656        return;
     657
     658    // Setup possible get actions
     659    $possible_actions = array(
     660        'bbp_subscribe',
     661        'bbp_unsubscribe',
     662    );
     663
     664    // Bail if actions aren't meant for this function
     665    if ( !in_array( $_GET['action'], $possible_actions ) )
     666        return;
     667
     668    // What action is taking place?
     669    $action  = $_GET['action'];
     670
     671    // Get user_id
     672    $user_id = bbp_get_user_id( 0, true, true );
     673
     674    // Check current user's ability to edit the user
     675    if ( !current_user_can( 'edit_user', $user_id ) )
     676        bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     677
     678    // Load subscription info
     679    if ( !$topic_id = intval( $_GET['topic_id'] ) )
     680        bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
     681
     682    if ( !bbp_has_errors() ) {
     683
     684        $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
     685        $success         = false;
     686
     687        if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
     688            $success = bbp_remove_user_subscription( $user_id, $topic_id );
     689        } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
     690            $success = bbp_add_user_subscription( $user_id, $topic_id );
     691        }
     692
     693        // Do additional subscriptions actions
     694        do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
     695
     696        // Check for missing reply_id or error
     697        if ( true == $success ) {
     698
     699            // Redirect back to new reply
     700            if ( bbp_is_subscriptions() ) {
     701                $redirect = bbp_get_subscriptions_permalink( $user_id );
     702            } elseif( bbp_is_single_user() ) {
     703                $redirect = bbp_get_user_profile_url();
     704            } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
     705                $redirect = bbp_get_topic_permalink( $topic_id );
     706            } elseif ( is_single() || is_page() ) {
     707                $redirect = get_permalink();
     708            }
     709
     710            wp_redirect( $redirect );
     711
     712            // For good measure
     713            exit();
     714
     715        // Handle errors
     716        } else {
     717            if ( $is_subscription && 'bbp_unsubscribe' == $action ) {
     718                bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) );
     719            } elseif ( !$is_subscription && 'bbp_subscribe' == $action ) {
     720                bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) );
    684721            }
    685722        }
     
    720757function bbp_edit_user_handler() {
    721758
    722     if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && 'bbp-update-user' == $_POST['action'] ) {
    723 
    724         global $bbp, $wpdb;
    725 
    726         // Execute confirmed email change. See send_confirmation_on_profile_email().
    727         if ( is_multisite() && bbp_is_user_home() && isset( $_GET['newuseremail'] ) && $bbp->displayed_user->ID ) {
    728 
    729             $new_email = get_option( $bbp->displayed_user->ID . '_new_email' );
    730 
    731             if ( $new_email['hash'] == $_GET['newuseremail'] ) {
    732                 $user->ID         = $bbp->displayed_user->ID;
    733                 $user->user_email = esc_html( trim( $new_email['newemail'] ) );
    734 
    735                 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $bbp->displayed_user->user_login ) ) )
    736                     $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $bbp->displayed_user->user_login ) );
    737 
    738                 wp_update_user( get_object_vars( $user ) );
    739                 delete_option( $bbp->displayed_user->ID . '_new_email' );
    740 
    741                 wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
    742                 exit;
    743             }
    744 
    745         } elseif ( is_multisite() && bbp_is_user_home() && !empty( $_GET['dismiss'] ) && $bbp->displayed_user->ID . '_new_email' == $_GET['dismiss'] ) {
    746 
     759    // Bail if not a POST action
     760    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
     761        return;
     762
     763    // Bail if action is not 'bbp-update-user'
     764    if ( empty( $_POST['action'] ) || ( 'bbp-update-user' == $_POST['action'] ) )
     765        return;
     766
     767    global $bbp, $wpdb;
     768
     769    // Execute confirmed email change. See send_confirmation_on_profile_email().
     770    if ( is_multisite() && bbp_is_user_home() && isset( $_GET['newuseremail'] ) && $bbp->displayed_user->ID ) {
     771
     772        $new_email = get_option( $bbp->displayed_user->ID . '_new_email' );
     773
     774        if ( $new_email['hash'] == $_GET['newuseremail'] ) {
     775            $user->ID         = $bbp->displayed_user->ID;
     776            $user->user_email = esc_html( trim( $new_email['newemail'] ) );
     777
     778            if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $bbp->displayed_user->user_login ) ) )
     779                $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $bbp->displayed_user->user_login ) );
     780
     781            wp_update_user( get_object_vars( $user ) );
    747782            delete_option( $bbp->displayed_user->ID . '_new_email' );
     783
    748784            wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
    749785            exit;
    750 
    751786        }
    752787
    753         check_admin_referer( 'update-user_' . $bbp->displayed_user->ID );
    754 
    755         if ( !current_user_can( 'edit_user', $bbp->displayed_user->ID ) )
    756             wp_die( __( 'What are you doing here? You do not have the permission to edit this user.', 'bbpress' ) );
    757 
    758         if ( bbp_is_user_home() )
    759             do_action( 'personal_options_update', $bbp->displayed_user->ID );
    760         else
    761             do_action( 'edit_user_profile_update', $bbp->displayed_user->ID );
    762 
    763         if ( !is_multisite() ) {
    764             $bbp->errors = edit_user( $bbp->displayed_user->ID ); // Handles the trouble for us ;)
    765         } else {
    766             $user        = get_userdata( $bbp->displayed_user->ID );
    767 
    768             // Update the email address in signups, if present.
    769             if ( $user->user_login && isset( $_POST['email'] ) && is_email( $_POST['email'] ) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login ) ) )
    770                 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
    771 
    772             // WPMU must delete the user from the current blog if WP added him after editing.
    773             $delete_role = false;
    774             $blog_prefix = $wpdb->get_blog_prefix();
    775 
    776             if ( $bbp->displayed_user->ID != $bbp->displayed_user->ID ) {
    777                 $cap = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = '{$bbp->displayed_user->ID}' AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'" );
    778                 if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
    779                     $_POST['role'] = 'contributor';
    780                     $delete_role = true;
    781                 }
     788    } elseif ( is_multisite() && bbp_is_user_home() && !empty( $_GET['dismiss'] ) && $bbp->displayed_user->ID . '_new_email' == $_GET['dismiss'] ) {
     789
     790        delete_option( $bbp->displayed_user->ID . '_new_email' );
     791        wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
     792        exit;
     793
     794    }
     795
     796    check_admin_referer( 'update-user_' . $bbp->displayed_user->ID );
     797
     798    if ( !current_user_can( 'edit_user', $bbp->displayed_user->ID ) )
     799        wp_die( __( 'What are you doing here? You do not have the permission to edit this user.', 'bbpress' ) );
     800
     801    if ( bbp_is_user_home() )
     802        do_action( 'personal_options_update', $bbp->displayed_user->ID );
     803    else
     804        do_action( 'edit_user_profile_update', $bbp->displayed_user->ID );
     805
     806    if ( !is_multisite() ) {
     807        $bbp->errors = edit_user( $bbp->displayed_user->ID ); // Handles the trouble for us ;)
     808    } else {
     809        $user        = get_userdata( $bbp->displayed_user->ID );
     810
     811        // Update the email address in signups, if present.
     812        if ( $user->user_login && isset( $_POST['email'] ) && is_email( $_POST['email'] ) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login ) ) )
     813            $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
     814
     815        // WPMU must delete the user from the current blog if WP added him after editing.
     816        $delete_role = false;
     817        $blog_prefix = $wpdb->get_blog_prefix();
     818
     819        if ( $bbp->displayed_user->ID != $bbp->displayed_user->ID ) {
     820            $cap = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = '{$bbp->displayed_user->ID}' AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'" );
     821            if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
     822                $_POST['role'] = 'contributor';
     823                $delete_role = true;
    782824            }
    783 
    784             $bbp->errors = edit_user( $bbp->displayed_user->ID );
    785 
    786             if ( $delete_role ) // stops users being added to current blog when they are edited
    787                 delete_user_meta( $bbp->displayed_user->ID, $blog_prefix . 'capabilities' );
    788 
    789             if ( is_multisite() && is_network_admin() & !bbp_is_user_home() && current_user_can( 'manage_network_options' ) && !isset( $super_admins ) && empty( $_POST['super_admin'] ) == is_super_admin( $bbp->displayed_user->ID ) )
    790                 empty( $_POST['super_admin'] ) ? revoke_super_admin( $bbp->displayed_user->ID ) : grant_super_admin( $bbp->displayed_user->ID );
    791825        }
    792826
    793         if ( !is_wp_error( $bbp->errors ) ) {
    794             $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) );
    795 
    796             wp_redirect( $redirect );
    797             exit;
    798         }
     827        $bbp->errors = edit_user( $bbp->displayed_user->ID );
     828
     829        if ( $delete_role ) // stops users being added to current blog when they are edited
     830            delete_user_meta( $bbp->displayed_user->ID, $blog_prefix . 'capabilities' );
     831
     832        if ( is_multisite() && is_network_admin() & !bbp_is_user_home() && current_user_can( 'manage_network_options' ) && !isset( $super_admins ) && empty( $_POST['super_admin'] ) == is_super_admin( $bbp->displayed_user->ID ) )
     833            empty( $_POST['super_admin'] ) ? revoke_super_admin( $bbp->displayed_user->ID ) : grant_super_admin( $bbp->displayed_user->ID );
     834    }
     835
     836    if ( !bbp_has_errors() ) {
     837        $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) );
     838
     839        wp_redirect( $redirect );
     840        exit;
    799841    }
    800842}
  • branches/plugin/bbp-includes/bbp-user-template.php

    r3366 r3382  
    853853    // loggedout was passed
    854854    if ( !empty( $_GET['loggedout'] ) && ( true == $_GET['loggedout'] ) ) {
    855         $bbp->errors->add( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' );
     855        bbp_add_error( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' );
    856856
    857857    // registration is disabled
    858858    } elseif ( !empty( $_GET['registration'] ) && ( 'disabled' == $_GET['registration'] ) ) {
    859         $bbp->errors->add( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) );
     859        bbp_add_error( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) );
    860860
    861861    // Prompt user to check their email
     
    866866            // Email needs confirmation
    867867            case 'confirm' :
    868                 $bbp->errors->add( 'confirm',    __( 'Check your e-mail for the confirmation link.',     'bbpress' ), 'message' );
     868                bbp_add_error( 'confirm',    __( 'Check your e-mail for the confirmation link.',     'bbpress' ), 'message' );
    869869                break;
    870870
    871871            // User requested a new password
    872872            case 'newpass' :
    873                 $bbp->errors->add( 'newpass',    __( 'Check your e-mail for your new password.',         'bbpress' ), 'message' );
     873                bbp_add_error( 'newpass',    __( 'Check your e-mail for your new password.',         'bbpress' ), 'message' );
    874874                break;
    875875
    876876            // User is newly registered
    877877            case 'registered' :
    878                 $bbp->errors->add( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
     878                bbp_add_error( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
    879879                break;
    880880        }
Note: See TracChangeset for help on using the changeset viewer.