Skip to:
Content

bbPress.org

Changeset 6397


Ignore:
Timestamp:
04/12/2017 09:30:50 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Admin: Update row action toggle methods to be a bit more flexible.

This should improve support for custom toggle actions in wp-admin.

Location:
trunk/src/includes/admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/forums.php

    r6272 r6397  
    396396    public function toggle_forum() {
    397397
    398         // Only proceed if GET is a forum toggle action
    399         if ( bbp_is_get_request() && ! empty( $_GET['forum_id'] ) && ! empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_toggle_forum_close' ) ) ) {
    400             $action    = $_GET['action'];            // What action is taking place?
    401             $forum_id  = (int) $_GET['forum_id'];    // What's the forum id?
    402             $success   = false;                      // Flag
    403             $post_data = array( 'ID' => $forum_id ); // Prelim array
    404             $forum     = bbp_get_forum( $forum_id );
    405 
    406             // Bail if forum is missing
    407             if ( empty( $forum ) ) {
    408                 wp_die( __( 'The forum was not found.', 'bbpress' ) );
    409             }
    410 
    411             // What is the user doing here?
    412             if ( ! current_user_can( 'keep_gate', $forum->ID ) ) {
    413                 wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
    414             }
    415 
    416             switch ( $action ) {
    417                 case 'bbp_toggle_forum_close' :
    418                     check_admin_referer( 'close-forum_' . $forum_id );
    419 
    420                     $is_open = bbp_is_forum_open( $forum_id );
    421                     $message = ( true === $is_open )
    422                         ? 'closed'
    423                         : 'opened';
    424                     $success = ( true === $is_open )
    425                         ? bbp_close_forum( $forum_id )
    426                         : bbp_open_forum( $forum_id );
    427 
    428                     break;
    429             }
    430 
    431             $message = array( 'bbp_forum_toggle_notice' => $message, 'forum_id' => $forum->ID );
    432 
    433             if ( false === $success || is_wp_error( $success ) ) {
    434                 $message['failed'] = '1';
    435             }
    436 
    437             // Do additional forum toggle actions (admin side)
    438             do_action( 'bbp_toggle_forum_admin', $success, $post_data, $action, $message );
    439 
    440             // Redirect back to the forum
    441             $redirect = add_query_arg( $message, remove_query_arg( array( 'action', 'forum_id' ) ) );
    442             bbp_redirect( $redirect );
    443         }
     398        // Bail if not a forum toggle action
     399        if ( ! bbp_is_get_request() || empty( $_GET['action'] ) || empty( $_GET['forum_id'] ) ) {
     400            return;
     401        }
     402
     403        // Bail if not an allowed action
     404        $action = sanitize_key( $_GET['action'] );
     405        if ( empty( $action ) || ! in_array( $action, $this->get_allowed_action_toggles(), true ) ) {
     406            return;
     407        }
     408
     409        // Bail if forum is missing
     410        $forum_id = bbp_get_forum_id( $_GET['forum_id'] );
     411        if ( ! bbp_get_forum( $forum_id ) ) {
     412            wp_die( __( 'The forum was not found.', 'bbpress' ) );
     413        }
     414
     415        // What is the user doing here?
     416        if ( ! current_user_can( 'keep_gate', $forum_id ) ) {
     417            wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
     418        }
     419
     420        // Defaults
     421        $post_data = array( 'ID' => $forum_id );
     422        $message   = '';
     423        $success   = false;
     424
     425        switch ( $action ) {
     426            case 'bbp_toggle_forum_close' :
     427                check_admin_referer( 'close-forum_' . $forum_id );
     428
     429                $is_open = bbp_is_forum_open( $forum_id );
     430                $message = ( true === $is_open )
     431                    ? 'closed'
     432                    : 'opened';
     433                $success = ( true === $is_open )
     434                    ? bbp_close_forum( $forum_id )
     435                    : bbp_open_forum( $forum_id );
     436
     437                break;
     438        }
     439
     440        // Setup the message
     441        $retval = array(
     442            'bbp_forum_toggle_notice' => $message,
     443            'forum_id'                => $forum_id
     444        );
     445
     446        // Prepare for failure
     447        if ( false === $success || is_wp_error( $success ) ) {
     448            $retval['failed'] = '1';
     449        }
     450
     451        // Filter all message args
     452        $retval = apply_filters( 'bbp_toggle_forum_action_admin', $retval, $forum_id, $action );
     453
     454        // Do additional forum toggle actions (admin side)
     455        do_action( 'bbp_toggle_forum_admin', $success, $post_data, $action, $retval );
     456
     457        // Redirect back to the forum
     458        $redirect = add_query_arg( $retval, remove_query_arg( array( 'action', 'forum_id' ) ) );
     459        bbp_redirect( $redirect );
    444460    }
    445461
     
    460476    public function toggle_forum_notice() {
    461477
    462         // Only proceed if GET is a forum toggle action
    463         if ( bbp_is_get_request() && ! empty( $_GET['bbp_forum_toggle_notice'] ) && in_array( $_GET['bbp_forum_toggle_notice'], array( 'opened', 'closed' ) ) && ! empty( $_GET['forum_id'] ) ) {
    464             $notice     = $_GET['bbp_forum_toggle_notice'];         // Which notice?
    465             $forum_id   = (int) $_GET['forum_id'];                  // What's the forum id?
    466             $is_failure = ! empty( $_GET['failed'] ) ? true : false; // Was that a failure?
    467 
    468             // Bail if no forum_id or notice
    469             if ( empty( $notice ) || empty( $forum_id ) ) {
    470                 return;
    471             }
    472 
    473             // Bail if forum is missing
    474             $forum = bbp_get_forum( $forum_id );
    475             if ( empty( $forum ) ) {
    476                 return;
    477             }
    478 
    479             $forum_title = bbp_get_forum_title( $forum->ID );
    480 
    481             switch ( $notice ) {
    482                 case 'opened' :
    483                     $message = ( $is_failure === true )
    484                         ? sprintf( __( 'There was a problem opening the forum "%1$s".', 'bbpress' ), $forum_title )
    485                         : sprintf( __( 'Forum "%1$s" successfully opened.',             'bbpress' ), $forum_title );
    486                     break;
    487 
    488                 case 'closed' :
    489                     $message = ( $is_failure === true )
    490                         ? sprintf( __( 'There was a problem closing the forum "%1$s".', 'bbpress' ), $forum_title )
    491                         : sprintf( __( 'Forum "%1$s" successfully closed.',             'bbpress' ), $forum_title );
    492                     break;
    493             }
    494 
    495             // Do additional forum toggle notice filters (admin side)
    496             $message = apply_filters( 'bbp_toggle_forum_notice_admin', $message, $forum->ID, $notice, $is_failure );
    497 
    498             ?>
    499 
    500             <div id="message" class="<?php echo $is_failure === true ? 'error' : 'updated'; ?> fade">
    501                 <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
    502             </div>
    503 
    504             <?php
    505         }
     478        // Bail if missing topic toggle action
     479        if ( ! bbp_is_get_request() || empty( $_GET['forum_id'] ) || empty( $_GET['bbp_forum_toggle_notice'] ) ) {
     480            return;
     481        }
     482
     483        // Bail if not an allowed notice
     484        $notice = sanitize_key( $_GET['bbp_forum_toggle_notice'] );
     485        if ( empty( $notice ) || ! in_array( $notice, $this->get_allowed_notice_toggles(), true ) ) {
     486            return;
     487        }
     488
     489        // Bail if no forum_id or notice
     490        $forum_id = bbp_get_forum_id( $_GET['forum_id'] );
     491        if ( empty( $forum_id ) ) {
     492            return;
     493        }
     494
     495        // Bail if forum is missing
     496        if ( ! bbp_get_forum( $forum_id ) ) {
     497            return;
     498        }
     499
     500        // Use the title in the responses
     501        $forum_title = bbp_get_forum_title( $forum_id );
     502        $is_failure  = ! empty( $_GET['failed'] );
     503        $message     = '';
     504
     505        switch ( $notice ) {
     506            case 'opened' :
     507                $message = ( $is_failure === true )
     508                    ? sprintf( __( 'There was a problem opening the forum "%1$s".', 'bbpress' ), $forum_title )
     509                    : sprintf( __( 'Forum "%1$s" successfully opened.',             'bbpress' ), $forum_title );
     510                break;
     511
     512            case 'closed' :
     513                $message = ( $is_failure === true )
     514                    ? sprintf( __( 'There was a problem closing the forum "%1$s".', 'bbpress' ), $forum_title )
     515                    : sprintf( __( 'Forum "%1$s" successfully closed.',             'bbpress' ), $forum_title );
     516                break;
     517        }
     518
     519        // Do additional forum toggle notice filters (admin side)
     520        $message = apply_filters( 'bbp_toggle_forum_notice_admin', $message, $forum_id, $notice, $is_failure );
     521        $class   = ( $is_failure === true )
     522            ? 'error'
     523            : 'updated';
     524
     525        ?>
     526
     527        <div id="message" class="<?php echo esc_html( $class ); ?> fade">
     528            <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
     529        </div>
     530
     531        <?php
     532    }
     533
     534    /**
     535     * Returns an array of notice toggles
     536     *
     537     * @since 2.6.0 bbPress (r6396)
     538     *
     539     * @return array
     540     */
     541    private function get_allowed_notice_toggles() {
     542        return apply_filters( 'bbp_admin_forums_allowed_notice_toggles', array(
     543            'opened',
     544            'closed'
     545        ) );
     546    }
     547
     548    /**
     549     * Returns an array of notice toggles
     550     *
     551     * @since 2.6.0 bbPress (r6396)
     552     *
     553     * @return array
     554     */
     555    private function get_allowed_action_toggles() {
     556        return apply_filters( 'bbp_admin_forums_allowed_action_toggles', array(
     557            'bbp_toggle_forum_close'
     558        ) );
    506559    }
    507560
     
    625678
    626679            // Show the 'close' and 'open' link on published, private, hidden and closed posts only
    627             if ( in_array( $forum->post_status, array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id(), bbp_get_closed_status_id() ) ) ) {
     680            if ( in_array( $forum->post_status, array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id(), bbp_get_closed_status_id() ), true ) ) {
    628681                $close_uri = wp_nonce_url( add_query_arg( array( 'forum_id' => $forum->ID, 'action' => 'bbp_toggle_forum_close' ), remove_query_arg( array( 'bbp_forum_toggle_notice', 'forum_id', 'failed', 'super' ) ) ), 'close-forum_' . $forum->ID );
    629682                if ( bbp_is_forum_open( $forum->ID ) ) {
  • trunk/src/includes/admin/replies.php

    r6384 r6397  
    473473    public function toggle_reply() {
    474474
    475         // Only proceed if GET is a reply toggle action
    476         if ( bbp_is_get_request() && ! empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_toggle_reply_spam', 'bbp_toggle_reply_approve' ) ) && ! empty( $_GET['reply_id'] ) ) {
    477             $action    = $_GET['action'];            // What action is taking place?
    478             $reply_id  = (int) $_GET['reply_id'];    // What's the reply id?
    479             $success   = false;                      // Flag
    480             $post_data = array( 'ID' => $reply_id ); // Prelim array
    481 
    482             // Get reply and die if empty
    483             $reply = bbp_get_reply( $reply_id );
    484             if ( empty( $reply ) ) {
    485                 wp_die( __( 'The reply was not found.', 'bbpress' ) );
    486             }
    487 
    488             // What is the user doing here?
    489             if ( ! current_user_can( 'moderate', $reply->ID ) ) {
    490                 wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
    491             }
    492 
    493             switch ( $action ) {
    494                 case 'bbp_toggle_reply_approve' :
    495                     check_admin_referer( 'approve-reply_' . $reply_id );
    496 
    497                     $is_approve = bbp_is_reply_pending( $reply_id );
    498                     $message    = $is_approve ? 'approved' : 'unapproved';
    499                     $success    = $is_approve ? bbp_approve_reply( $reply_id ) : bbp_unapprove_reply( $reply_id );
    500 
    501                     break;
    502 
    503                 case 'bbp_toggle_reply_spam' :
    504                     check_admin_referer( 'spam-reply_' . $reply_id );
    505 
    506                     $is_spam = bbp_is_reply_spam( $reply_id );
    507                     $message = $is_spam ? 'unspammed' : 'spammed';
    508                     $success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id );
    509 
    510                     break;
    511             }
    512 
    513             $message = array( 'bbp_reply_toggle_notice' => $message, 'reply_id' => $reply->ID );
    514 
    515             if ( false === $success || is_wp_error( $success ) ) {
    516                 $message['failed'] = '1';
    517             }
    518 
    519             // Do additional reply toggle actions (admin side)
    520             do_action( 'bbp_toggle_reply_admin', $success, $post_data, $action, $message );
    521 
    522             // Redirect back to the reply
    523             $redirect = add_query_arg( $message, remove_query_arg( array( 'action', 'reply_id' ) ) );
    524             bbp_redirect( $redirect );
    525         }
     475        // Bail if not a topic toggle action
     476        if ( ! bbp_is_get_request() || empty( $_GET['action'] ) || empty( $_GET['reply_id'] ) ) {
     477            return;
     478        }
     479
     480        // Bail if not an allowed action
     481        $action = sanitize_key( $_GET['action'] );
     482        if ( empty( $action ) || ! in_array( $action, $this->get_allowed_action_toggles(), true ) ) {
     483            return;
     484        }
     485
     486        // Get reply and die if empty
     487        $reply_id = bbp_get_reply_id( $_GET['reply_id'] );
     488        if ( ! bbp_get_reply( $reply_id ) ) {
     489            wp_die( __( 'The reply was not found.', 'bbpress' ) );
     490        }
     491
     492        // What is the user doing here?
     493        if ( ! current_user_can( 'moderate', $reply_id ) ) {
     494            wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
     495        }
     496
     497        // Defaults
     498        $post_data = array( 'ID' => $reply_id );
     499        $message   = '';
     500        $success   = false;
     501
     502        switch ( $action ) {
     503            case 'bbp_toggle_reply_approve' :
     504                check_admin_referer( 'approve-reply_' . $reply_id );
     505
     506                $is_approve = bbp_is_reply_pending( $reply_id );
     507                $message    = ( true === $is_approve )
     508                    ? 'approved'
     509                    : 'unapproved';
     510                $success    = ( true === $is_approve )
     511                    ? bbp_approve_reply( $reply_id )
     512                    : bbp_unapprove_reply( $reply_id );
     513
     514                break;
     515
     516            case 'bbp_toggle_reply_spam' :
     517                check_admin_referer( 'spam-reply_' . $reply_id );
     518
     519                $is_spam = bbp_is_reply_spam( $reply_id );
     520                $message = ( true === $is_spam )
     521                    ? 'unspammed'
     522                    : 'spammed';
     523                $success = ( true === $is_spam )
     524                    ? bbp_unspam_reply( $reply_id )
     525                    : bbp_spam_reply( $reply_id );
     526
     527                break;
     528        }
     529
     530        // Setup the message
     531        $retval = array(
     532            'bbp_reply_toggle_notice' => $message,
     533            'reply_id'                => $reply_id
     534        );
     535
     536        // Prepare for failure
     537        if ( false === $success || is_wp_error( $success ) ) {
     538            $retval['failed'] = '1';
     539        }
     540
     541        // Filter all message args
     542        $retval = apply_filters( 'bbp_toggle_reply_action_admin', $retval, $reply_id, $action );
     543
     544        // Do additional reply toggle actions (admin side)
     545        do_action( 'bbp_toggle_reply_admin', $success, $post_data, $action, $retval );
     546
     547        // Redirect back to the reply
     548        $redirect = add_query_arg( $retval, remove_query_arg( array( 'action', 'reply_id' ) ) );
     549        bbp_redirect( $redirect );
    526550    }
    527551
     
    542566    public function toggle_reply_notice() {
    543567
    544         // Only proceed if GET is a reply toggle action
    545         if ( bbp_is_get_request() && ! empty( $_GET['bbp_reply_toggle_notice'] ) && in_array( $_GET['bbp_reply_toggle_notice'], array( 'spammed', 'unspammed', 'approved', 'unapproved' ) ) && ! empty( $_GET['reply_id'] ) ) {
    546             $notice     = $_GET['bbp_reply_toggle_notice'];         // Which notice?
    547             $reply_id   = (int) $_GET['reply_id'];                  // What's the reply id?
    548             $is_failure = ! empty( $_GET['failed'] ) ? true : false; // Was that a failure?
    549 
    550             // Empty? No reply?
    551             if ( empty( $notice ) || empty( $reply_id ) ) {
    552                 return;
    553             }
    554 
    555             // Get reply and bail if empty
    556             $reply = bbp_get_reply( $reply_id );
    557             if ( empty( $reply ) ) {
    558                 return;
    559             }
    560 
    561             $reply_title = bbp_get_reply_title( $reply->ID );
    562 
    563             switch ( $notice ) {
    564                 case 'spammed' :
    565                     $message = ( $is_failure === true )
    566                         ? sprintf( __( 'There was a problem marking the reply "%1$s" as spam.', 'bbpress' ), $reply_title )
    567                         : sprintf( __( 'Reply "%1$s" successfully marked as spam.',             'bbpress' ), $reply_title );
    568                     break;
    569 
    570                 case 'unspammed' :
    571                     $message = ( $is_failure === true )
    572                         ? sprintf( __( 'There was a problem unmarking the reply "%1$s" as spam.', 'bbpress' ), $reply_title )
    573                         : sprintf( __( 'Reply "%1$s" successfully unmarked as spam.',             'bbpress' ), $reply_title );
    574                     break;
    575 
    576                 case 'approved' :
    577                     $message = ( $is_failure === true )
    578                         ? sprintf( __( 'There was a problem approving the reply "%1$s".', 'bbpress' ), $reply_title )
    579                         : sprintf( __( 'Reply "%1$s" successfully approved.',             'bbpress' ), $reply_title );
    580                     break;
    581 
    582                 case 'unapproved' :
    583                     $message = ( $is_failure === true )
    584                         ? sprintf( __( 'There was a problem unapproving the reply "%1$s".', 'bbpress' ), $reply_title )
    585                         : sprintf( __( 'Reply "%1$s" successfully unapproved.',             'bbpress' ), $reply_title );
    586                     break;
    587             }
    588 
    589             // Do additional reply toggle notice filters (admin side)
    590             $message = apply_filters( 'bbp_toggle_reply_notice_admin', $message, $reply->ID, $notice, $is_failure );
    591 
    592             ?>
    593 
    594             <div id="message" class="<?php echo $is_failure === true ? 'error' : 'updated'; ?> fade">
    595                 <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
    596             </div>
    597 
    598             <?php
    599         }
     568        // Bail if missing reply toggle action
     569        if ( ! bbp_is_get_request() || empty( $_GET['reply_id'] ) || empty( $_GET['bbp_reply_toggle_notice'] ) ) {
     570            return;
     571        }
     572
     573        // Bail if not an allowed notice
     574        $notice = sanitize_key( $_GET['bbp_reply_toggle_notice'] );
     575        if ( empty( $notice ) || ! in_array( $notice, $this->get_allowed_notice_toggles(), true ) ) {
     576            return;
     577        }
     578
     579        // Bail if no reply_id or notice
     580        $reply_id = bbp_get_reply_id( $_GET['reply_id'] );
     581        if ( empty( $reply_id ) ) {
     582            return;
     583        }
     584
     585        // Bail if reply is missing
     586        if ( ! bbp_get_reply( $reply_id ) ) {
     587            return;
     588        }
     589
     590        // Use the title in the responses
     591        $reply_title = bbp_get_reply_title( $reply_id );
     592        $is_failure  = ! empty( $_GET['failed'] );
     593        $message     = '';
     594
     595        switch ( $notice ) {
     596            case 'spammed' :
     597                $message = ( $is_failure === true )
     598                    ? sprintf( __( 'There was a problem marking the reply "%1$s" as spam.', 'bbpress' ), $reply_title )
     599                    : sprintf( __( 'Reply "%1$s" successfully marked as spam.',             'bbpress' ), $reply_title );
     600                break;
     601
     602            case 'unspammed' :
     603                $message = ( $is_failure === true )
     604                    ? sprintf( __( 'There was a problem unmarking the reply "%1$s" as spam.', 'bbpress' ), $reply_title )
     605                    : sprintf( __( 'Reply "%1$s" successfully unmarked as spam.',             'bbpress' ), $reply_title );
     606                break;
     607
     608            case 'approved' :
     609                $message = ( $is_failure === true )
     610                    ? sprintf( __( 'There was a problem approving the reply "%1$s".', 'bbpress' ), $reply_title )
     611                    : sprintf( __( 'Reply "%1$s" successfully approved.',             'bbpress' ), $reply_title );
     612                break;
     613
     614            case 'unapproved' :
     615                $message = ( $is_failure === true )
     616                    ? sprintf( __( 'There was a problem unapproving the reply "%1$s".', 'bbpress' ), $reply_title )
     617                    : sprintf( __( 'Reply "%1$s" successfully unapproved.',             'bbpress' ), $reply_title );
     618                break;
     619        }
     620
     621        // Do additional reply toggle notice filters (admin side)
     622        $message = apply_filters( 'bbp_toggle_reply_notice_admin', $message, $reply_id, $notice, $is_failure );
     623        $class   = ( $is_failure === true )
     624            ? 'error'
     625            : 'updated';
     626
     627        ?>
     628
     629        <div id="message" class="<?php echo esc_html( $class ); ?> fade">
     630            <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
     631        </div>
     632
     633        <?php
     634    }
     635
     636    /**
     637     * Returns an array of notice toggles
     638     *
     639     * @since 2.6.0 bbPress (r6396)
     640     *
     641     * @return array
     642     */
     643    private function get_allowed_notice_toggles() {
     644        return apply_filters( 'bbp_admin_replies_allowed_notice_toggles', array(
     645            'spammed',
     646            'unspammed',
     647            'approved',
     648            'unapproved'
     649        ) );
     650    }
     651
     652    /**
     653     * Returns an array of notice toggles
     654     *
     655     * @since 2.6.0 bbPress (r6396)
     656     *
     657     * @return array
     658     */
     659    private function get_allowed_action_toggles() {
     660        return apply_filters( 'bbp_admin_replies_allowed_action_toggles', array(
     661            'bbp_toggle_reply_spam',
     662            'bbp_toggle_reply_approve'
     663        ) );
    600664    }
    601665
  • trunk/src/includes/admin/topics.php

    r6384 r6397  
    603603    public function toggle_topic() {
    604604
    605         // Only proceed if GET is a topic toggle action
    606         if ( bbp_is_get_request() && ! empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'bbp_toggle_topic_close', 'bbp_toggle_topic_stick', 'bbp_toggle_topic_spam', 'bbp_toggle_topic_approve' ) ) && ! empty( $_GET['topic_id'] ) ) {
    607             $action    = $_GET['action'];            // What action is taking place?
    608             $topic_id  = (int) $_GET['topic_id'];    // What's the topic id?
    609             $success   = false;                      // Flag
    610             $post_data = array( 'ID' => $topic_id ); // Prelim array
    611             $topic     = bbp_get_topic( $topic_id ); // Verify the topic id
    612 
    613             // Bail if topic is missing
    614             if ( empty( $topic ) ) {
    615                 wp_die( __( 'The topic was not found.', 'bbpress' ) );
    616             }
    617 
    618             // What is the user doing here?
    619             if ( ! current_user_can( 'moderate', $topic->ID ) ) {
    620                 wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
    621             }
    622 
    623             switch ( $action ) {
    624                 case 'bbp_toggle_topic_approve' :
    625                     check_admin_referer( 'approve-topic_' . $topic_id );
    626 
    627                     $is_approve = bbp_is_topic_pending( $topic_id );
    628                     $message    = ( true === $is_approve )
    629                         ? 'approved'
    630                         : 'unapproved';
    631                     $success    = ( true === $is_approve )
    632                         ? bbp_approve_topic( $topic_id )
    633                         : bbp_unapprove_topic( $topic_id );
    634 
    635                     break;
    636 
    637                 case 'bbp_toggle_topic_close' :
    638                     check_admin_referer( 'close-topic_' . $topic_id );
    639 
    640                     $is_open = bbp_is_topic_open( $topic_id );
    641                     $message = ( true === $is_open )
    642                         ? 'closed'
    643                         : 'opened';
    644                     $success = ( true === $is_open )
    645                         ? bbp_close_topic( $topic_id )
    646                         : bbp_open_topic( $topic_id );
    647 
    648                     break;
    649 
    650                 case 'bbp_toggle_topic_stick' :
    651                     check_admin_referer( 'stick-topic_' . $topic_id );
    652 
    653                     $is_sticky = bbp_is_topic_sticky( $topic_id );
    654                     $is_super  = ( false === $is_sticky ) && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] )
    655                         ? true
    656                         : false;
    657                     $message   = ( true  === $is_sticky )
    658                         ? 'unstuck'
    659                         : 'stuck';
    660                     $message   = ( true  === $is_super )
    661                         ? 'super_sticky'
    662                         : $message;
    663                     $success   = ( true  === $is_sticky )
    664                         ? bbp_unstick_topic( $topic_id )
    665                         : bbp_stick_topic( $topic_id, $is_super );
    666 
    667                     break;
    668 
    669                 case 'bbp_toggle_topic_spam'  :
    670                     check_admin_referer( 'spam-topic_' . $topic_id );
    671 
    672                     $is_spam = bbp_is_topic_spam( $topic_id );
    673                     $message = ( true === $is_spam )
    674                         ? 'unspammed'
    675                         : 'spammed';
    676                     $success = ( true === $is_spam )
    677                         ? bbp_unspam_topic( $topic_id )
    678                         : bbp_spam_topic( $topic_id );
    679 
    680                     break;
    681             }
    682 
    683             $message = array( 'bbp_topic_toggle_notice' => $message, 'topic_id' => $topic->ID );
    684 
    685             if ( false === $success || is_wp_error( $success ) ) {
    686                 $message['failed'] = '1';
    687             }
    688 
    689             // Do additional topic toggle actions (admin side)
    690             do_action( 'bbp_toggle_topic_admin', $success, $post_data, $action, $message );
    691 
    692             // Redirect back to the topic
    693             $redirect = add_query_arg( $message, remove_query_arg( array( 'action', 'topic_id' ) ) );
    694             bbp_redirect( $redirect );
    695         }
     605        // Bail if not a topic toggle action
     606        if ( ! bbp_is_get_request() || empty( $_GET['action'] ) || empty( $_GET['topic_id'] ) ) {
     607            return;
     608        }
     609
     610        // Bail if not an allowed action
     611        $action = sanitize_key( $_GET['action'] );
     612        if ( empty( $action ) || ! in_array( $action, $this->get_allowed_action_toggles(), true ) ) {
     613            return;
     614        }
     615
     616        // Bail if topic is missing
     617        $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
     618        if ( ! bbp_get_topic( $topic_id ) ) {
     619            wp_die( __( 'The topic was not found.', 'bbpress' ) );
     620        }
     621
     622        // What is the user doing here?
     623        if ( ! current_user_can( 'moderate', $topic_id ) ) {
     624            wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
     625        }
     626
     627        // Defaults
     628        $post_data = array( 'ID' => $topic_id );
     629        $message   = '';
     630        $success   = false;
     631
     632        switch ( $action ) {
     633            case 'bbp_toggle_topic_approve' :
     634                check_admin_referer( 'approve-topic_' . $topic_id );
     635
     636                $is_approve = bbp_is_topic_pending( $topic_id );
     637                $message    = ( true === $is_approve )
     638                    ? 'approved'
     639                    : 'unapproved';
     640                $success    = ( true === $is_approve )
     641                    ? bbp_approve_topic( $topic_id )
     642                    : bbp_unapprove_topic( $topic_id );
     643
     644                break;
     645
     646            case 'bbp_toggle_topic_close' :
     647                check_admin_referer( 'close-topic_' . $topic_id );
     648
     649                $is_open = bbp_is_topic_open( $topic_id );
     650                $message = ( true === $is_open )
     651                    ? 'closed'
     652                    : 'opened';
     653                $success = ( true === $is_open )
     654                    ? bbp_close_topic( $topic_id )
     655                    : bbp_open_topic( $topic_id );
     656
     657                break;
     658
     659            case 'bbp_toggle_topic_stick' :
     660                check_admin_referer( 'stick-topic_' . $topic_id );
     661
     662                $is_sticky = bbp_is_topic_sticky( $topic_id );
     663                $is_super  = ( false === $is_sticky ) && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] )
     664                    ? true
     665                    : false;
     666                $message   = ( true  === $is_sticky )
     667                    ? 'unstuck'
     668                    : 'stuck';
     669                $message   = ( true  === $is_super )
     670                    ? 'super_sticky'
     671                    : $message;
     672                $success   = ( true  === $is_sticky )
     673                    ? bbp_unstick_topic( $topic_id )
     674                    : bbp_stick_topic( $topic_id, $is_super );
     675
     676                break;
     677
     678            case 'bbp_toggle_topic_spam'  :
     679                check_admin_referer( 'spam-topic_' . $topic_id );
     680
     681                $is_spam = bbp_is_topic_spam( $topic_id );
     682                $message = ( true === $is_spam )
     683                    ? 'unspammed'
     684                    : 'spammed';
     685                $success = ( true === $is_spam )
     686                    ? bbp_unspam_topic( $topic_id )
     687                    : bbp_spam_topic( $topic_id );
     688
     689                break;
     690        }
     691
     692        // Setup the message
     693        $retval = array(
     694            'bbp_topic_toggle_notice' => $message,
     695            'topic_id'                => $topic_id
     696        );
     697
     698        // Prepare for failure
     699        if ( ( false === $success ) || is_wp_error( $success ) ) {
     700            $retval['failed'] = '1';
     701        }
     702
     703        // Filter all message args
     704        $retval = apply_filters( 'bbp_toggle_topic_action_admin', $retval, $topic_id, $action );
     705
     706        // Do additional topic toggle actions (admin side)
     707        do_action( 'bbp_toggle_topic_admin', $success, $post_data, $action, $retval );
     708
     709        // Redirect back to the topic
     710        $redirect = add_query_arg( $retval, remove_query_arg( array( 'action', 'topic_id' ) ) );
     711        bbp_redirect( $redirect );
    696712    }
    697713
     
    712728    public function toggle_topic_notice() {
    713729
    714         // Only proceed if GET is a topic toggle action
    715         if ( bbp_is_get_request() && ! empty( $_GET['bbp_topic_toggle_notice'] ) && in_array( $_GET['bbp_topic_toggle_notice'], array( 'opened', 'closed', 'super_sticky', 'stuck', 'unstuck', 'spammed', 'unspammed', 'approved', 'unapproved' ) ) && ! empty( $_GET['topic_id'] ) ) {
    716             $notice     = $_GET['bbp_topic_toggle_notice'];         // Which notice?
    717             $topic_id   = (int) $_GET['topic_id'];                  // What's the topic id?
    718             $is_failure = ! empty( $_GET['failed'] ) ? true : false; // Was that a failure?
    719 
    720             // Bais if no topic_id or notice
    721             if ( empty( $notice ) || empty( $topic_id ) ) {
    722                 return;
    723             }
    724 
    725             // Bail if topic is missing
    726             $topic = bbp_get_topic( $topic_id );
    727             if ( empty( $topic ) ) {
    728                 return;
    729             }
    730 
    731             $topic_title = bbp_get_topic_title( $topic->ID );
    732 
    733             switch ( $notice ) {
    734                 case 'opened'    :
    735                     $message = ( $is_failure === true )
    736                         ? sprintf( __( 'There was a problem opening the topic "%1$s".', 'bbpress' ), $topic_title )
    737                         : sprintf( __( 'Topic "%1$s" successfully opened.',             'bbpress' ), $topic_title );
    738                     break;
    739 
    740                 case 'closed'    :
    741                     $message = ( $is_failure === true )
    742                         ? sprintf( __( 'There was a problem closing the topic "%1$s".', 'bbpress' ), $topic_title )
    743                         : sprintf( __( 'Topic "%1$s" successfully closed.',             'bbpress' ), $topic_title );
    744                     break;
    745 
    746                 case 'super_sticky' :
    747                     $message = ( $is_failure === true )
    748                         ? sprintf( __( 'There was a problem sticking the topic "%1$s" to front.', 'bbpress' ), $topic_title )
    749                         : sprintf( __( 'Topic "%1$s" successfully stuck to front.',               'bbpress' ), $topic_title );
    750                     break;
    751 
    752                 case 'stuck'   :
    753                     $message = ( $is_failure === true )
    754                         ? sprintf( __( 'There was a problem sticking the topic "%1$s".', 'bbpress' ), $topic_title )
    755                         : sprintf( __( 'Topic "%1$s" successfully stuck.',               'bbpress' ), $topic_title );
    756                     break;
    757 
    758                 case 'unstuck' :
    759                     $message = ( $is_failure === true )
    760                         ? sprintf( __( 'There was a problem unsticking the topic "%1$s".', 'bbpress' ), $topic_title )
    761                         : sprintf( __( 'Topic "%1$s" successfully unstuck.',               'bbpress' ), $topic_title );
    762                     break;
    763 
    764                 case 'spammed'   :
    765                     $message = ( $is_failure === true )
    766                         ? sprintf( __( 'There was a problem marking the topic "%1$s" as spam.', 'bbpress' ), $topic_title )
    767                         : sprintf( __( 'Topic "%1$s" successfully marked as spam.',             'bbpress' ), $topic_title );
    768                     break;
    769 
    770                 case 'unspammed' :
    771                     $message = ( $is_failure === true )
    772                         ? sprintf( __( 'There was a problem unmarking the topic "%1$s" as spam.', 'bbpress' ), $topic_title )
    773                         : sprintf( __( 'Topic "%1$s" successfully unmarked as spam.',             'bbpress' ), $topic_title );
    774                     break;
    775 
    776                 case 'approved'   :
    777                     $message = ( $is_failure === true )
    778                         ? sprintf( __( 'There was a problem approving the topic "%1$s".', 'bbpress' ), $topic_title )
    779                         : sprintf( __( 'Topic "%1$s" successfully approved.',             'bbpress' ), $topic_title );
    780                     break;
    781 
    782                 case 'unapproved' :
    783                     $message = ( $is_failure === true )
    784                         ? sprintf( __( 'There was a problem unapproving the topic "%1$s".', 'bbpress' ), $topic_title )
    785                         : sprintf( __( 'Topic "%1$s" successfully unapproved.',             'bbpress' ), $topic_title );
    786                     break;
    787             }
    788 
    789             // Do additional topic toggle notice filters (admin side)
    790             $message = apply_filters( 'bbp_toggle_topic_notice_admin', $message, $topic->ID, $notice, $is_failure );
    791 
    792             ?>
    793 
    794             <div id="message" class="<?php echo ( $is_failure === true ) ? 'error' : 'updated'; ?> fade">
    795                 <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
    796             </div>
    797 
    798             <?php
    799         }
     730        // Bail if missing topic toggle action
     731        if ( ! bbp_is_get_request() || empty( $_GET['topic_id'] ) || empty( $_GET['bbp_topic_toggle_notice'] ) ) {
     732            return;
     733        }
     734
     735        // Bail if not an allowed notice
     736        $notice = sanitize_key( $_GET['bbp_topic_toggle_notice'] );
     737        if ( empty( $notice ) || ! in_array( $notice, $this->get_allowed_notice_toggles(), true ) ) {
     738            return;
     739        }
     740
     741        // Bail if no topic_id or notice
     742        $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
     743        if (  empty( $topic_id ) ) {
     744            return;
     745        }
     746
     747        // Bail if topic is missing
     748        if ( ! bbp_get_topic( $topic_id ) ) {
     749            return;
     750        }
     751
     752        // Use the title in the responses
     753        $topic_title = bbp_get_topic_title( $topic_id );
     754        $is_failure  = ! empty( $_GET['failed'] );
     755        $message     = '';
     756
     757        // Which notice?
     758        switch ( $notice ) {
     759            case 'opened'    :
     760                $message = ( $is_failure === true )
     761                    ? sprintf( __( 'There was a problem opening the topic "%1$s".', 'bbpress' ), $topic_title )
     762                    : sprintf( __( 'Topic "%1$s" successfully opened.',             'bbpress' ), $topic_title );
     763                break;
     764
     765            case 'closed'    :
     766                $message = ( $is_failure === true )
     767                    ? sprintf( __( 'There was a problem closing the topic "%1$s".', 'bbpress' ), $topic_title )
     768                    : sprintf( __( 'Topic "%1$s" successfully closed.',             'bbpress' ), $topic_title );
     769                break;
     770
     771            case 'super_sticky' :
     772                $message = ( $is_failure === true )
     773                    ? sprintf( __( 'There was a problem sticking the topic "%1$s" to front.', 'bbpress' ), $topic_title )
     774                    : sprintf( __( 'Topic "%1$s" successfully stuck to front.',               'bbpress' ), $topic_title );
     775                break;
     776
     777            case 'stuck'   :
     778                $message = ( $is_failure === true )
     779                    ? sprintf( __( 'There was a problem sticking the topic "%1$s".', 'bbpress' ), $topic_title )
     780                    : sprintf( __( 'Topic "%1$s" successfully stuck.',               'bbpress' ), $topic_title );
     781                break;
     782
     783            case 'unstuck' :
     784                $message = ( $is_failure === true )
     785                    ? sprintf( __( 'There was a problem unsticking the topic "%1$s".', 'bbpress' ), $topic_title )
     786                    : sprintf( __( 'Topic "%1$s" successfully unstuck.',               'bbpress' ), $topic_title );
     787                break;
     788
     789            case 'spammed'   :
     790                $message = ( $is_failure === true )
     791                    ? sprintf( __( 'There was a problem marking the topic "%1$s" as spam.', 'bbpress' ), $topic_title )
     792                    : sprintf( __( 'Topic "%1$s" successfully marked as spam.',             'bbpress' ), $topic_title );
     793                break;
     794
     795            case 'unspammed' :
     796                $message = ( $is_failure === true )
     797                    ? sprintf( __( 'There was a problem unmarking the topic "%1$s" as spam.', 'bbpress' ), $topic_title )
     798                    : sprintf( __( 'Topic "%1$s" successfully unmarked as spam.',             'bbpress' ), $topic_title );
     799                break;
     800
     801            case 'approved'   :
     802                $message = ( $is_failure === true )
     803                    ? sprintf( __( 'There was a problem approving the topic "%1$s".', 'bbpress' ), $topic_title )
     804                    : sprintf( __( 'Topic "%1$s" successfully approved.',             'bbpress' ), $topic_title );
     805                break;
     806
     807            case 'unapproved' :
     808                $message = ( $is_failure === true )
     809                    ? sprintf( __( 'There was a problem unapproving the topic "%1$s".', 'bbpress' ), $topic_title )
     810                    : sprintf( __( 'Topic "%1$s" successfully unapproved.',             'bbpress' ), $topic_title );
     811                break;
     812        }
     813
     814        // Do additional topic toggle notice filters (admin side)
     815        $message = apply_filters( 'bbp_toggle_topic_notice_admin', $message, $topic_id, $notice, $is_failure );
     816        $class   = ( $is_failure === true )
     817            ? 'error'
     818            : 'updated';
     819
     820        ?>
     821
     822        <div id="message" class="<?php echo esc_html( $class ); ?> fade">
     823            <p style="line-height: 150%"><?php echo esc_html( $message ); ?></p>
     824        </div>
     825
     826        <?php
     827    }
     828
     829    /**
     830     * Returns an array of notice toggles
     831     *
     832     * @since 2.6.0 bbPress (r6396)
     833     *
     834     * @return array
     835     */
     836    private function get_allowed_notice_toggles() {
     837        return apply_filters( 'bbp_admin_topics_allowed_notice_toggles', array(
     838            'opened',
     839            'closed',
     840            'super_sticky',
     841            'stuck',
     842            'unstuck',
     843            'spammed',
     844            'unspammed',
     845            'approved',
     846            'unapproved'
     847        ) );
     848    }
     849
     850    /**
     851     * Returns an array of notice toggles
     852     *
     853     * @since 2.6.0 bbPress (r6396)
     854     *
     855     * @return array
     856     */
     857    private function get_allowed_action_toggles() {
     858        return apply_filters( 'bbp_admin_topics_allowed_action_toggles', array(
     859            'bbp_toggle_topic_close',
     860            'bbp_toggle_topic_stick',
     861            'bbp_toggle_topic_spam',
     862            'bbp_toggle_topic_approve'
     863        ) );
    800864    }
    801865
Note: See TracChangeset for help on using the changeset viewer.