Skip to:
Content

bbPress.org

Changeset 6142


Ignore:
Timestamp:
12/08/2016 05:00:47 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Moderation: First pass at improved topic toggle actions:

  • Allow custom toggles, or replacement of existing toggles with new procedures
  • Introduce a handful of actions & filters to enable the above
  • Separate functionality into smaller, more manageable parts.

See #3032. (Forums @todo)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/topics/functions.php

    r6141 r6142  
    20632063}
    20642064
     2065/**
     2066 * Return array of available topic toggle actions
     2067 *
     2068 * @since 2.6.0 bbPress (r6133)
     2069 *
     2070 * @param int $topic_id   Optional. Topic id.
     2071 *
     2072 * @return array
     2073 */
     2074function bbp_get_topic_toggles( $topic_id = 0 ) {
     2075    return apply_filters( 'bbp_get_toggle_topic_actions', array(
     2076        'bbp_toggle_topic_close',
     2077        'bbp_toggle_topic_stick',
     2078        'bbp_toggle_topic_spam',
     2079        'bbp_toggle_topic_trash',
     2080        'bbp_toggle_topic_approve'
     2081    ), $topic_id );
     2082}
     2083
    20652084/** Stickies ******************************************************************/
    20662085
     
    21392158    }
    21402159
    2141     // Setup possible get actions
    2142     $possible_actions = array(
    2143         'bbp_toggle_topic_close',
    2144         'bbp_toggle_topic_stick',
    2145         'bbp_toggle_topic_spam',
    2146         'bbp_toggle_topic_trash',
    2147         'bbp_toggle_topic_approve'
    2148     );
     2160    // What's the topic id?
     2161    $topic_id = bbp_get_topic_id( (int) $_GET['topic_id'] );
     2162
     2163    // Get possible topic-handler toggles
     2164    $toggles = bbp_get_topic_toggles( $topic_id );
    21492165
    21502166    // Bail if actions aren't meant for this function
    2151     if ( ! in_array( $action, $possible_actions ) ) {
     2167    if ( ! in_array( $action, $toggles, true ) ) {
    21522168        return;
    21532169    }
    2154 
    2155     $failure   = '';                         // Empty failure string
    2156     $view_all  = false;                      // Assume not viewing all
    2157     $topic_id  = (int) $_GET['topic_id'];    // What's the topic id?
    2158     $success   = false;                      // Flag
    2159     $post_data = array( 'ID' => $topic_id ); // Prelim array
    2160     $redirect  = '';                         // Empty redirect URL
    21612170
    21622171    // Make sure topic exists
    21632172    $topic = bbp_get_topic( $topic_id );
    21642173    if ( empty( $topic ) ) {
     2174        bbp_add_error( 'bbp_toggle_topic_missing', __( '<strong>ERROR:</strong> This topic could not be found or no longer exists.', 'bbpress' ) );
    21652175        return;
    21662176    }
    21672177
    21682178    // What is the user doing here?
    2169     if ( ! current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' === $action && ! current_user_can( 'delete_topic', $topic->ID ) ) ) {
     2179    if ( ! current_user_can( 'edit_topic', $topic_id ) || ( 'bbp_toggle_topic_trash' === $action && ! current_user_can( 'delete_topic', $topic_id ) ) ) {
    21702180        bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have permission to do that.', 'bbpress' ) );
    21712181        return;
    21722182    }
    21732183
     2184    // Sub-action?
     2185    $sub_action = ! empty( $_GET['sub_action'] )
     2186        ? sanitize_key( $_GET['sub_action'] )
     2187        : false;
     2188
     2189    // Preliminary array
     2190    $post_data = array( 'ID' => $topic_id );
     2191
     2192    // Do the topic toggling
     2193    $retval = bbp_toggle_topic( array(
     2194        'id'         => $topic_id,
     2195        'action'     => $action,
     2196        'sub_action' => $sub_action,
     2197        'data'       => $post_data
     2198    ) );
     2199
     2200    // Do additional topic toggle actions
     2201    do_action( 'bbp_toggle_topic_handler', $retval['status'], $post_data, $action );
     2202
     2203    // No errors
     2204    if ( ( false !== $retval['status'] ) && ! is_wp_error( $retval['status'] ) ) {
     2205        bbp_redirect( $retval['redirect_to'] );
     2206
     2207    // Handle errors
     2208    } else {
     2209        bbp_add_error( 'bbp_toggle_topic', $retval['message'] );
     2210    }
     2211}
     2212
     2213/**
     2214 * Do the actual topic toggling
     2215 *
     2216 * This function is used by `bbp_toggle_topic_handler()` to do the actual heavy
     2217 * lifting when it comes to toggling topic. It only really makes sense to call
     2218 * within that context, so if you need to call this function directly, make sure
     2219 * you're also doing what the handler does too.
     2220 *
     2221 * @since 2.6.0
     2222 * @access private
     2223 *
     2224 * @param array $args
     2225 */
     2226function bbp_toggle_topic( $args = array() ) {
     2227
     2228    // Parse the arguments
     2229    $r = bbp_parse_args( $args, array(
     2230        'id'         => 0,
     2231        'action'     => '',
     2232        'sub_action' => '',
     2233        'data'       => array()
     2234    ) );
     2235
     2236    // Build the nonce suffix
     2237    $nonce_suffix = bbp_get_topic_post_type() . '_' . (int) $r['id'];
     2238
     2239    // Default return values
     2240    $retval = array(
     2241        'status'      => 0,
     2242        'message'     => '',
     2243        'redirect_to' => bbp_get_topic_permalink( $r['id'], bbp_get_redirect_to() ),
     2244        'view_all'    => false
     2245    );
     2246
    21742247    // What action are we trying to perform?
    2175     switch ( $action ) {
     2248    switch ( $r['action'] ) {
    21762249
    21772250        // Toggle approve/unapprove
    21782251        case 'bbp_toggle_topic_approve' :
    2179             check_ajax_referer( 'approve-topic_' . $topic_id );
    2180 
    2181             $is_pending = bbp_is_topic_pending( $topic_id );
    2182             $success    = true === $is_pending ? bbp_approve_topic( $topic_id ) : bbp_unapprove_topic( $topic_id );
    2183             $failure    = true === $is_pending ? __( '<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress' );
     2252            check_ajax_referer( "approve-{$nonce_suffix}" );
     2253
     2254            $is_pending         = bbp_is_topic_pending( $r['id'] );
     2255            $retval['status']   = true === $is_pending ? bbp_approve_topic( $r['id'] ) : bbp_unapprove_topic( $r['id'] );
     2256            $retval['message']  = true === $is_pending ? __( '<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress' );
     2257            $retval['view_all'] = ! $is_pending;
    21842258
    21852259            break;
     
    21872261        // Toggle open/close
    21882262        case 'bbp_toggle_topic_close' :
    2189             check_ajax_referer( 'close-topic_' . $topic_id );
    2190 
    2191             $is_open = bbp_is_topic_open( $topic_id );
    2192             $success = true === $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
    2193             $failure = true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
     2263            check_ajax_referer( "close-{$nonce_suffix}" );
     2264
     2265            $is_open           = bbp_is_topic_open( $r['id'] );
     2266            $retval['status']  = true === $is_open ? bbp_close_topic( $r['id'] ) : bbp_open_topic( $r['id'] );
     2267            $retval['message'] = true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
    21942268
    21952269            break;
     
    21972271        // Toggle sticky/super-sticky/unstick
    21982272        case 'bbp_toggle_topic_stick' :
    2199             check_ajax_referer( 'stick-topic_' . $topic_id );
    2200 
    2201             $is_sticky = bbp_is_topic_sticky( $topic_id );
    2202             $is_super  = false === $is_sticky && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false;
    2203             $success   = true  === $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
    2204             $failure  = true  === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
     2273            check_ajax_referer( "stick-{$nonce_suffix}" );
     2274
     2275            $is_sticky         = bbp_is_topic_sticky( $r['id'] );
     2276            $is_super          = false === $is_sticky && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false;
     2277            $retval['status']  = true  === $is_sticky ? bbp_unstick_topic( $r['id'] ) : bbp_stick_topic( $r['id'], $is_super );
     2278            $retval['message'] = true  === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
    22052279
    22062280            break;
     
    22082282        // Toggle spam
    22092283        case 'bbp_toggle_topic_spam' :
    2210             check_ajax_referer( 'spam-topic_' . $topic_id );
    2211 
    2212             $is_spam  = bbp_is_topic_spam( $topic_id );
    2213             $success  = true === $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
    2214             $failure  = true === $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' );
    2215             $view_all = ! $is_spam;
     2284            check_ajax_referer( "spam-{$nonce_suffix}" );
     2285
     2286            $is_spam            = bbp_is_topic_spam( $r['id'] );
     2287            $retval['status']   = true === $is_spam ? bbp_unspam_topic( $r['id'] ) : bbp_spam_topic( $r['id'] );
     2288            $retval['message']  = true === $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' );
     2289            $retval['view_all'] = ! $is_spam;
    22162290
    22172291            break;
     
    22202294        case 'bbp_toggle_topic_trash' :
    22212295
    2222             $sub_action = ! empty( $_GET['sub_action'] ) && in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
    2223 
    2224             if ( empty( $sub_action ) ) {
    2225                 break;
    2226             }
    2227 
    2228             switch ( $sub_action ) {
     2296            switch ( $r['sub_action'] ) {
    22292297                case 'trash':
    2230                     check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
     2298                    check_ajax_referer( "trash-{$nonce_suffix}" );
    22312299
    22322300                    $view_all = true;
    2233                     $success  = wp_trash_post( $topic_id );
    2234                     $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
     2301                    $retval['status']  = wp_trash_post( $r['id'] );
     2302                    $retval['message'] = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
    22352303
    22362304                    break;
    22372305
    22382306                case 'untrash':
    2239                     check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
    2240 
    2241                     $success = wp_untrash_post( $topic_id );
    2242                     $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
     2307                    check_ajax_referer( "untrash-{$nonce_suffix}" );
     2308
     2309                    $retval['status']  = wp_untrash_post( $r['id'] );
     2310                    $retval['message'] = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
    22432311
    22442312                    break;
    22452313
    22462314                case 'delete':
    2247                     check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
    2248 
    2249                     $success = wp_delete_post( $topic_id );
    2250                     $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
     2315                    check_ajax_referer( "delete-{$nonce_suffix}" );
     2316
     2317                    $retval['status']  = wp_delete_post( $r['id'] );
     2318                    $retval['message'] = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
    22512319
    22522320                    break;
     
    22562324    }
    22572325
    2258     // Do additional topic toggle actions
    2259     do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
    2260 
    2261     // No errors
    2262     if ( false !== $success && ! is_wp_error( $success ) ) {
    2263 
    2264         // Redirect back to the topic's forum
    2265         if ( isset( $sub_action ) && ( 'delete' === $sub_action ) ) {
    2266             $redirect = bbp_get_forum_permalink( $success->post_parent );
    2267 
    2268         // Redirect back to the topic
    2269         } else {
    2270 
    2271             // Get the redirect detination
    2272             $permalink = bbp_get_topic_permalink( $topic_id );
    2273             $redirect  = bbp_add_view_all( $permalink, $view_all );
    2274         }
    2275 
    2276         bbp_redirect( $redirect );
    2277 
    2278     // Handle errors
    2279     } else {
    2280         bbp_add_error( 'bbp_toggle_topic', $failure );
    2281     }
     2326    // Maybe redirect back to the topic's forum
     2327    if ( isset( $r['sub_action'] ) && ( 'delete' === $r['sub_action'] ) ) {
     2328        $retval['redirect_to'] = bbp_get_forum_permalink( $retval['status']->post_parent );
     2329    }
     2330
     2331    // Add view all if needed
     2332    if ( ! empty( $retval['view_all'] ) ) {
     2333        $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true );
     2334    }
     2335
     2336    // Filter & return
     2337    return apply_filters( 'bbp_toggle_topic', $retval, $r, $args );
    22822338}
    22832339
Note: See TracChangeset for help on using the changeset viewer.