Skip to:
Content

bbPress.org


Ignore:
Timestamp:
04/12/2017 09:30:50 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.