Skip to:
Content

bbPress.org

Changeset 6493


Ignore:
Timestamp:
06/06/2017 06:55:55 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Favorites/Subscriptions improvements:

  • Remove superfluous checks against current current user fav/sub status
  • Always return $success
  • Prefer bbp_get_ functions over generic intval() to validate $_GET ID's
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/engagements.php

    r6439 r6493  
    489489 *                    id and action
    490490 * @uses bbp_is_favorites() To check if it's the favorites page
    491  * @uses bbp_get_favorites_link() To get the favorites page link
    492491 * @uses bbp_get_topic_permalink() To get the topic permalink
    493492 * @uses bbp_redirect() To redirect to the url
     
    495494function bbp_favorites_handler( $action = '' ) {
    496495
     496    // Default
     497    $success = false;
     498
     499    // Bail if favorites not active
    497500    if ( ! bbp_is_favorites_active() ) {
    498         return false;
     501        return $success;
    499502    }
    500503
    501504    // Bail if no topic ID is passed
    502505    if ( empty( $_GET['topic_id'] ) ) {
    503         return;
     506        return $success;
    504507    }
    505508
     
    512515    // Bail if actions aren't meant for this function
    513516    if ( ! in_array( $action, $possible_actions, true ) ) {
    514         return;
     517        return $success;
    515518    }
    516519
    517520    // What action is taking place?
    518     $topic_id = intval( $_GET['topic_id'] );
     521    $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
    519522    $user_id  = bbp_get_user_id( 0, true, true );
    520523
     
    534537    // Bail if errors
    535538    if ( bbp_has_errors() ) {
    536         return;
     539        return $success;
    537540    }
    538541
    539542    /** No errors *************************************************************/
    540543
    541     $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
    542     $success     = false;
    543 
    544     if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {
     544    if ( 'bbp_favorite_remove' === $action ) {
    545545        $success = bbp_remove_user_favorite( $user_id, $topic_id );
    546     } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {
     546    } elseif ( 'bbp_favorite_add' === $action ) {
    547547        $success = bbp_add_user_favorite( $user_id, $topic_id );
    548548    }
     
    572572
    573573    // Fail! Handle errors
    574     } elseif ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {
     574    } elseif ( 'bbp_favorite_remove' === $action ) {
    575575        bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites.', 'bbpress' ) );
    576     } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {
     576    } elseif ( 'bbp_favorite_add' === $action ) {
    577577        bbp_add_error( 'bbp_favorite_add',    __( '<strong>ERROR</strong>: There was a problem favoriting that topic.', 'bbpress' ) );
    578578    }
     579
     580    return (bool) $success;
    579581}
    580582
     
    11631165function bbp_forum_subscriptions_handler( $action = '' ) {
    11641166
     1167    // Default
     1168    $success = false;
     1169
     1170    // Bail if subscriptions not active
    11651171    if ( ! bbp_is_subscriptions_active() ) {
    1166         return false;
     1172        return $success;
    11671173    }
    11681174
    11691175    // Bail if no forum ID is passed
    11701176    if ( empty( $_GET['forum_id'] ) ) {
    1171         return;
     1177        return $success;
    11721178    }
    11731179
     
    11801186    // Bail if actions aren't meant for this function
    11811187    if ( ! in_array( $action, $possible_actions, true ) ) {
    1182         return;
     1188        return $success;
    11831189    }
    11841190
    11851191    // Get required data
     1192    $forum_id = bbp_get_forum_id( $_GET['forum_id'] );
    11861193    $user_id  = bbp_get_user_id( 0, true, true );
    1187     $forum_id = intval( $_GET['forum_id'] );
    11881194
    11891195    // Check for empty forum
     
    12021208    // Bail if we have errors
    12031209    if ( bbp_has_errors() ) {
    1204         return;
     1210        return $success;
    12051211    }
    12061212
    12071213    /** No errors *************************************************************/
    12081214
    1209     $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
    1210     $success         = false;
    1211 
    1212     if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1215    if ( 'bbp_unsubscribe' === $action ) {
    12131216        $success = bbp_remove_user_subscription( $user_id, $forum_id );
    1214     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1217    } elseif ( 'bbp_subscribe' === $action ) {
    12151218        $success = bbp_add_user_subscription( $user_id, $forum_id );
    12161219    }
     
    12401243
    12411244    // Fail! Handle errors
    1242     } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1245    } elseif ( 'bbp_unsubscribe' === $action ) {
    12431246        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum.', 'bbpress' ) );
    1244     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1247    } elseif ( 'bbp_subscribe' === $action ) {
    12451248        bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum.', 'bbpress' ) );
    12461249    }
     1250
     1251    return (bool) $success;
    12471252}
    12481253
     
    12701275function bbp_subscriptions_handler( $action = '' ) {
    12711276
     1277    // Default
     1278    $success = false;
     1279
     1280    // Bail if subscriptions not active
    12721281    if ( ! bbp_is_subscriptions_active() ) {
    1273         return false;
     1282        return $success;
    12741283    }
    12751284
    12761285    // Bail if no topic ID is passed
    12771286    if ( empty( $_GET['topic_id'] ) ) {
    1278         return;
     1287        return $success;
    12791288    }
    12801289
     
    12871296    // Bail if actions aren't meant for this function
    12881297    if ( ! in_array( $action, $possible_actions, true ) ) {
    1289         return;
     1298        return $success;
    12901299    }
    12911300
    12921301    // Get required data
     1302    $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
    12931303    $user_id  = bbp_get_user_id( 0, true, true );
    1294     $topic_id = intval( $_GET['topic_id'] );
    12951304
    12961305    // Check for empty topic
     
    13091318    // Bail if we have errors
    13101319    if ( bbp_has_errors() ) {
    1311         return;
     1320        return $success;
    13121321    }
    13131322
    13141323    /** No errors *************************************************************/
    13151324
    1316     $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
    1317     $success         = false;
    1318 
    1319     if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1325    if ( 'bbp_unsubscribe' === $action ) {
    13201326        $success = bbp_remove_user_subscription( $user_id, $topic_id );
    1321     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1327    } elseif ( 'bbp_subscribe' === $action ) {
    13221328        $success = bbp_add_user_subscription( $user_id, $topic_id );
    13231329    }
     
    13471353
    13481354    // Fail! Handle errors
    1349     } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1355    } elseif ( 'bbp_unsubscribe' === $action ) {
    13501356        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic.', 'bbpress' ) );
    1351     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1357    } elseif ( 'bbp_subscribe' === $action ) {
    13521358        bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic.', 'bbpress' ) );
    13531359    }
    1354 }
     1360
     1361    return (bool) $success;
     1362}
Note: See TracChangeset for help on using the changeset viewer.