Skip to:
Content

bbPress.org


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.