Skip to:
Content

bbPress.org

Changeset 6109


Ignore:
Timestamp:
11/01/2016 07:24:50 AM (4 years ago)
Author:
johnjamesjacoby
Message:

Favorites/Subscriptions/Moderators: Introduce metadata API for linking multiple users to multiple forums/topics.

Previous to this, connections were stored in usermeta. We knew this would not scale, but bbPress 1 had a friendlier database schema & we expected WordPress's taxonomy/relationship roadmap would be farther along by now.

By storing user ID's in postmeta instead, we gain an ability to query for connections from both directions without custom MySQL, while also leveraging persistent caching in a more sane way.

This commit includes several new helper functions for low-level relationship management, as well as modifications to existing functions to allow them to continue to work as they always have.

See: #2959.

Location:
trunk
Files:
4 edited

Legend:

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

    r6056 r6109  
    812812 */
    813813function bbp_add_moderator( $object_id = 0, $user_id = 0 ) {
    814     return add_post_meta( $object_id, '_bbp_moderator_id', $user_id );
     814    return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id' );
    815815}
    816816
     
    826826 */
    827827function bbp_remove_moderator( $object_id = 0, $user_id = 0 ) {
    828     return delete_post_meta( $object_id, '_bbp_moderator_id', $user_id );
     828    return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id' );
    829829}
    830830
     
    839839 */
    840840function bbp_get_moderator_ids( $object_id = 0 ) {
    841     return get_post_meta( $object_id, '_bbp_moderator_id', false );
     841    return bbp_get_users_for_object( $object_id, '_bbp_moderator_id' );
    842842}
    843843
  • trunk/src/includes/users/functions.php

    r6056 r6109  
    170170}
    171171
     172/** User Relationships ********************************************************/
     173
     174/**
     175 * Set a user id on an object
     176 *
     177 * @since 2.7 bbPress()
     178 *
     179 * @param int    $object_id The object id
     180 * @param int    $user_id   The user id
     181 * @param string $meta_key  The relationship key
     182 * @param string $meta_type The relationship type
     183 *
     184 * @uses bbp_is_object_of_user() To check if the term has already been set
     185 * @uses add_post_meta() To set the term on the object
     186 * @uses apply_filters() Calls 'bbp_add_user_to_object' with the object id, user
     187 *                        id, and taxonomy
     188 * @return bool Returns true if the user taxonomy term is added to the object,
     189 *               otherwise false
     190 */
     191function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     192    $retval = add_metadata( $meta_type, $object_id, $meta_key, $user_id, false );
     193
     194    return (bool) apply_filters( 'bbp_add_user_to_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
     195}
     196
     197/**
     198 * Remove a user id from an object
     199 *
     200 * @since 2.7 bbPress()
     201 *
     202 * @param int    $object_id The post id
     203 * @param int    $user_id   The user id
     204 * @param string $meta_key  The relationship key
     205 * @param string $meta_type The relationship type
     206 *
     207 * @uses bbp_is_object_of_user() To check if the term is set
     208 * @uses delete_post_meta() To remove the term from the object
     209 * @uses apply_filters() Calls 'bbp_remove_user_from_object' with the object
     210 *                        id, user id, and taxonomy
     211 * @return bool Returns true is the user taxonomy term is removed from the object,
     212 *               otherwise false
     213 */
     214function bbp_remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     215    $retval = delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false );
     216
     217    return (bool) apply_filters( 'bbp_remove_user_from_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
     218}
     219
     220/**
     221 * Get user taxonomy terms for an object
     222 *
     223 * @since 2.7 bbPress()
     224 *
     225 * @param int    $object_id The object id
     226 * @param string $meta_key  The key used to index this relationship
     227 * @param string $meta_type The type of meta to look in
     228 *
     229 * @uses get_post_meta() To get the user taxonomy terms
     230 * @uses apply_filters() Calls 'bbp_get_users_for_object' with the user
     231 *                        taxonomy terms, object id, and taxonomy
     232 * @return array Returns the user taxonomy terms of the object
     233 */
     234function bbp_get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     235    $meta   = get_metadata( $meta_type, $object_id, $meta_key, false );
     236    $retval = wp_parse_id_list( $meta );
     237
     238    return (array) apply_filters( 'bbp_get_users_for_object', (array) $retval, $object_id, $meta_key, $meta_type );
     239}
     240
     241/**
     242 * Check if the user id is set on an object
     243 *
     244 * @since 2.7 bbPress()
     245 *
     246 * @param int    $object_id The object id
     247 * @param int    $user_id   The user id
     248 * @param string $meta_key  The relationship key
     249 * @param string $meta_type The relationship type
     250 *
     251 * @uses get_post_meta() To check if the user id is set on the object
     252 * @uses apply_filters() Calls 'bbp_is_object_of_user' with the object id,
     253 *                        user id, and taxonomy
     254 * @return bool Returns true if the user id is set on the object for the
     255 *               taxonomy, otherwise false
     256 */
     257function bbp_is_object_of_user( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     258    $user_ids = bbp_get_users_for_object( $object_id, $meta_key, $meta_type );
     259    $retval   = is_numeric( array_search( $user_id, $user_ids, true ) );
     260
     261    return (bool) apply_filters( 'bbp_is_object_of_user', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
     262}
     263
    172264/** Favorites *****************************************************************/
    173265
     
    185277function bbp_get_topic_favoriters( $topic_id = 0 ) {
    186278    $topic_id = bbp_get_topic_id( $topic_id );
    187     if ( empty( $topic_id ) ) {
    188         return;
    189     }
    190 
    191     $bbp_db = bbp_db();
    192     $key    = $bbp_db->prefix . '_bbp_favorites';
    193     $users  = wp_cache_get( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );
    194     if ( false === $users ) {
    195         $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    196         wp_cache_set( 'bbp_get_topic_favoriters_' . $topic_id, $users, 'bbpress_users' );
    197     }
    198 
    199     return apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
     279    $users    = bbp_get_users_for_object( $topic_id, '_bbp_favorite' );
     280
     281    return (array) apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
    200282}
    201283
     
    213295 */
    214296function bbp_get_user_favorites( $user_id = 0 ) {
    215     $user_id = bbp_get_user_id( $user_id );
    216     if ( empty( $user_id ) ) {
    217         return false;
    218     }
    219 
    220     // If user has favorites, load them
     297    $user_id   = bbp_get_user_id( $user_id );
    221298    $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    222     if ( ! empty( $favorites ) ) {
    223         $query = bbp_has_topics( array( 'post__in' => $favorites ) );
    224     } else {
    225         $query = false;
    226     }
     299    $query     = ! empty( $favorites )
     300        ? bbp_has_topics( array( 'post__in' => $favorites ) )
     301        : false;
    227302
    228303    return apply_filters( 'bbp_get_user_favorites', $query, $user_id, $favorites );
     
    230305
    231306/**
    232  * Get a user's favorite topics' ids
     307 * Get a user's favorite topic ids
    233308 *
    234309 * @since 2.0.0 bbPress (r2652)
     
    236311 * @param int $user_id Optional. User id
    237312 * @uses bbp_get_user_id() To get the user id
    238  * @uses get_user_option() To get the user favorites
     313 * @uses bbp_get_topic_post_type() To get the topic post type
    239314 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    240315 *                        the favorites and user id
     
    242317 */
    243318function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    244     $user_id = bbp_get_user_id( $user_id );
    245     if ( empty( $user_id ) ) {
    246         return false;
    247     }
    248 
    249     $favorites = get_user_option( '_bbp_favorites', $user_id );
    250     $favorites = array_filter( wp_parse_id_list( $favorites ) );
    251 
    252     return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
     319    $user_id   = bbp_get_user_id( $user_id );
     320    $favorites = new WP_Query( array(
     321        'fields'     => 'ids',
     322        'post_type'  => bbp_get_topic_post_type(),
     323        'meta_query' => array( array(
     324            'key'     => '_bbp_favorite',
     325            'value'   => $user_id,
     326            'compare' => 'NUMERIC'
     327        ) )
     328    ) );
     329
     330    return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
    253331}
    254332
     
    264342 * @uses bbp_get_topic() To get the topic
    265343 * @uses bbp_get_topic_id() To get the topic id
     344 * @uses bbp_is_object_of_user() To check if the user has a favorite
    266345 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,
    267346 *                        topic id and favorites
     
    269348 */
    270349function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
    271 
    272     $user_id = bbp_get_user_id( $user_id, true, true );
    273     if ( empty( $user_id ) ) {
    274         return false;
    275     }
    276 
    277350    $retval    = false;
     351    $user_id   = bbp_get_user_id( $user_id, true, true );
    278352    $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    279353
     
    296370        // Is topic_id in the user's favorites
    297371        if ( ! empty( $topic_id ) ) {
    298             $retval = in_array( $topic_id, $favorites );
     372            $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
    299373        }
    300374    }
     
    310384 * @param int $user_id Optional. User id
    311385 * @param int $topic_id Optional. Topic id
    312  * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    313  * @uses update_user_option() To update the user favorites
     386 * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    314387 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
    315388 * @return bool Always true
    316389 */
    317390function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
     391
     392    // Bail if not enough info
    318393    if ( empty( $user_id ) || empty( $topic_id ) ) {
    319394        return false;
    320395    }
    321396
     397    // Bail if to topic
    322398    $topic = bbp_get_topic( $topic_id );
    323399    if ( empty( $topic ) ) {
     
    325401    }
    326402
    327     $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    328     if ( ! in_array( $topic_id, $favorites ) ) {
    329         $favorites[] = $topic_id;
    330         $favorites   = implode( ',', wp_parse_id_list( array_filter( $favorites ) ) );
    331         update_user_option( $user_id, '_bbp_favorites', $favorites );
    332 
    333         // Purge cache
    334         wp_cache_delete( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );
     403    // Bail if already a favorite
     404    if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {
     405        return false;
     406    }
     407
     408    // Bail if add fails
     409    if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
     410        return false;
    335411    }
    336412
     
    347423 * @param int $user_id Optional. User id
    348424 * @param int $topic_id Optional. Topic id
    349  * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    350  * @uses update_user_option() To update the user favorites
    351  * @uses delete_user_option() To delete the user favorites meta
     425 * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    352426 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
    353427 * @return bool True if the topic was removed from user's favorites, otherwise
     
    355429 */
    356430function bbp_remove_user_favorite( $user_id, $topic_id ) {
     431
     432    // Bail if not enough info
    357433    if ( empty( $user_id ) || empty( $topic_id ) ) {
    358434        return false;
    359435    }
    360436
    361     $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
    362     if ( empty( $favorites ) ) {
    363         return false;
    364     }
    365 
    366     $pos = array_search( $topic_id, $favorites );
    367     if ( is_numeric( $pos ) ) {
    368         array_splice( $favorites, $pos, 1 );
    369         $favorites = array_filter( $favorites );
    370 
    371         if ( ! empty( $favorites ) ) {
    372             $favorites = implode( ',', wp_parse_id_list( $favorites ) );
    373             update_user_option( $user_id, '_bbp_favorites', $favorites );
    374         } else {
    375             delete_user_option( $user_id, '_bbp_favorites' );
    376         }
    377 
    378         // Purge cache
    379         wp_cache_delete( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );
     437    // Bail if not already a favorite
     438    if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {
     439        return false;
     440    }
     441
     442    // Bail if remove fails
     443    if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
     444        return false;
    380445    }
    381446
     
    426491
    427492    // What action is taking place?
    428     $topic_id    = intval( $_GET['topic_id'] );
    429     $user_id     = bbp_get_user_id( 0, true, true );
     493    $topic_id = intval( $_GET['topic_id'] );
     494    $user_id  = bbp_get_user_id( 0, true, true );
    430495
    431496    // Check for empty topic
     
    495560 *
    496561 * @param int $forum_id Optional. forum id
    497  * @uses wpdb::get_col() To execute our query and get the column back
     562 * @uses bbp_get_users_for_object() To get the forum subscribers
    498563 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
    499564 * @return array|bool Results if the forum has any subscribers, otherwise false
     
    501566function bbp_get_forum_subscribers( $forum_id = 0 ) {
    502567    $forum_id = bbp_get_forum_id( $forum_id );
    503     if ( empty( $forum_id ) ) {
    504         return;
    505     }
    506 
    507     $bbp_db = bbp_db();
    508     $key    = $bbp_db->prefix . '_bbp_forum_subscriptions';
    509     $users  = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
    510     if ( false === $users ) {
    511         $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
    512         wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
    513     }
    514 
    515     return apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
     568    $users    = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
     569
     570    return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
    516571}
    517572
     
    522577 *
    523578 * @param int $topic_id Optional. Topic id
    524  * @uses wpdb::get_col() To execute our query and get the column back
     579 * @uses bbp_get_user_terms_by_object() To get the topic subscribers
    525580 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    526581 * @return array|bool Results if the topic has any subscribers, otherwise false
     
    528583function bbp_get_topic_subscribers( $topic_id = 0 ) {
    529584    $topic_id = bbp_get_topic_id( $topic_id );
    530     if ( empty( $topic_id ) ) {
    531         return;
    532     }
    533 
    534     $bbp_db = bbp_db();
    535     $key    = $bbp_db->prefix . '_bbp_subscriptions';
    536     $users  = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    537     if ( false === $users ) {
    538         $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    539         wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress_users' );
    540     }
    541 
    542     return apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
     585    $users    = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
     586
     587    return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    543588}
    544589
     
    575620
    576621    // Default to the displayed user
    577     $user_id = bbp_get_user_id( $user_id );
    578     if ( empty( $user_id ) ) {
    579         return false;
    580     }
     622    $user_id       = bbp_get_user_id( $user_id );
     623    $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    581624
    582625    // If user has subscriptions, load them
    583     $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    584626    if ( ! empty( $subscriptions ) ) {
    585627        $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
     
    606648
    607649    // Default to the displayed user
    608     $user_id = bbp_get_user_id( $user_id );
    609     if ( empty( $user_id ) ) {
    610         return false;
    611     }
     650    $user_id       = bbp_get_user_id( $user_id );
     651    $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
    612652
    613653    // If user has subscriptions, load them
    614     $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
    615654    if ( ! empty( $subscriptions ) ) {
    616655        $query = bbp_has_forums( array( 'post__in' => $subscriptions ) );
     
    629668 * @param int $user_id Optional. User id
    630669 * @uses bbp_get_user_id() To get the user id
    631  * @uses get_user_option() To get the user's subscriptions
     670 * @uses bbp_get_forum_post_type() To get the forum post type
    632671 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
    633672 *                        the subscriptions and user id
     
    635674 */
    636675function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
    637     $user_id = bbp_get_user_id( $user_id );
    638     if ( empty( $user_id ) ) {
    639         return false;
    640     }
    641 
    642     $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
    643     $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
    644 
    645     return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
     676    $user_id       = bbp_get_user_id( $user_id );
     677    $subscriptions = new WP_Query( array(
     678        'fields'     => 'ids',
     679        'post_type'  => bbp_get_forum_post_type(),
     680        'meta_query' => array( array(
     681            'key'     => '_bbp_subscription',
     682            'value'   => $user_id,
     683            'compare' => 'NUMERIC'
     684        ) )
     685    ) );
     686
     687    return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions->posts, $user_id );
    646688}
    647689
     
    653695 * @param int $user_id Optional. User id
    654696 * @uses bbp_get_user_id() To get the user id
    655  * @uses get_user_option() To get the user's subscriptions
     697 * @uses bbp_get_topic_post_type() To get the topic post type
    656698 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    657699 *                        the subscriptions and user id
     
    659701 */
    660702function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
    661     $user_id = bbp_get_user_id( $user_id );
    662     if ( empty( $user_id ) ) {
    663         return false;
    664     }
    665 
    666     $subscriptions = get_user_option( '_bbp_subscriptions', $user_id );
    667     $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
    668 
    669     return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
     703    $user_id       = bbp_get_user_id( $user_id );
     704    $subscriptions = new WP_Query( array(
     705        'fields'     => 'ids',
     706        'post_type'  => bbp_get_topic_post_type(),
     707        'meta_query' => array( array(
     708            'key'     => '_bbp_subscription',
     709            'value'   => $user_id,
     710            'compare' => 'NUMERIC'
     711        ) )
     712    ) );
     713
     714    return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions->posts, $user_id );
    670715}
    671716
     
    733778 * @param array $subscribed_ids Optional. Array of forum ID's to check
    734779 * @uses bbp_get_user_id() To get the user id
    735  * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
    736780 * @uses bbp_get_forum() To get the forum
    737781 * @uses bbp_get_forum_id() To get the forum id
     782 * @uses bbp_is_object_of_user() To check if the user has a subscription
    738783 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    739784 *                        forum id and subsriptions
     
    771816            }
    772817
    773             // Is forum_id in the user's favorites
     818            // Is forum_id in the user's subscriptions
    774819            if ( ! empty( $forum_id ) ) {
    775                 $retval = in_array( $forum_id, $subscribed_ids );
     820                $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' );
    776821            }
    777822        }
     
    790835 * @param array $subscribed_ids Optional. Array of topic ID's to check
    791836 * @uses bbp_get_user_id() To get the user id
    792  * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    793837 * @uses bbp_get_topic() To get the topic
    794838 * @uses bbp_get_topic_id() To get the topic id
     839 * @uses bbp_is_object_of_user() To check if the user is subscribed
    795840 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    796841 *                        topic id and subsriptions
     
    828873            }
    829874
    830             // Is topic_id in the user's favorites
     875            // Is topic_id in the user's subscriptions
    831876            if ( ! empty( $topic_id ) ) {
    832                 $retval = in_array( $topic_id, $subscribed_ids );
     877                $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' );
    833878            }
    834879        }
     
    839884
    840885/**
    841  * Add a topic to user's subscriptions
     886 * Add a user subscription
    842887 *
    843888 * @since 2.5.0 bbPress (r5156)
     
    846891 * @param int $object_id Optional. Topic id
    847892 * @uses get_post() To get the post object
    848  * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
    849  * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
    850  * @uses bbp_get_forum_post_type() To get the forum post type
    851  * @uses bbp_get_topic_post_type() To get the topic post type
    852  * @uses update_user_option() To update the user's subscriptions
    853  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
     893 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
    854894 * @return bool Always true
    855895 */
    856896function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     897
     898    // Bail if not enough info
    857899    if ( empty( $user_id ) || empty( $object_id ) ) {
    858900        return false;
     
    865907    }
    866908
    867     switch( $post_type ) {
    868 
    869         // Forum
    870         case bbp_get_forum_post_type() :
    871             bbp_add_user_forum_subscription( $user_id, $object_id );
    872             break;
    873 
    874         // Topic
    875         case bbp_get_topic_post_type() :
    876         default :
    877             bbp_add_user_topic_subscription( $user_id, $object_id );
    878             break;
     909    // Bail if already subscribed
     910    if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {
     911        return false;
     912    }
     913
     914    // Bail if add fails
     915    if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) {
     916        return false;
    879917    }
    880918
     
    891929 * @param int $user_id Optional. User id
    892930 * @param int $forum_id Optional. forum id
    893  * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
    894931 * @uses bbp_get_forum() To get the forum
    895  * @uses update_user_option() To update the user's subscriptions
     932 * @uses bbp_add_user_subscription() To add the user subscription
    896933 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
    897934 * @return bool Always true
    898935 */
    899936function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     937
     938    // Bail if not enough info
    900939    if ( empty( $user_id ) || empty( $forum_id ) ) {
    901940        return false;
    902941    }
    903942
     943    // Bail if no forum
    904944    $forum = bbp_get_forum( $forum_id );
    905945    if ( empty( $forum ) ) {
     
    907947    }
    908948
    909     $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
    910     if ( ! in_array( $forum_id, $subscriptions ) ) {
    911         $subscriptions[] = $forum_id;
    912         $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
    913         update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
    914 
    915         wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     949    // Bail if already subscribed
     950    if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) {
     951        return false;
     952    }
     953
     954    // Bail if add fails
     955    if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) {
     956        return false;
    916957    }
    917958
     
    928969 * @param int $user_id Optional. User id
    929970 * @param int $topic_id Optional. Topic id
    930  * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    931971 * @uses bbp_get_topic() To get the topic
    932  * @uses update_user_option() To update the user's subscriptions
     972 * @uses bbp_add_user_subscription() To add the subscription
    933973 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    934974 * @return bool Always true
    935975 */
    936976function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
     977
     978    // Bail if not enough info
    937979    if ( empty( $user_id ) || empty( $topic_id ) ) {
    938980        return false;
    939981    }
    940982
     983    // Bail if no topic
    941984    $topic = bbp_get_topic( $topic_id );
    942985    if ( empty( $topic ) ) {
     
    944987    }
    945988
    946     $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
    947     if ( ! in_array( $topic_id, $subscriptions ) ) {
    948         $subscriptions[] = $topic_id;
    949         $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
    950         update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    951 
    952         wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
     989    // Bail if already subscribed
     990    if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) {
     991        return false;
     992    }
     993
     994    // Bail if add fails
     995    if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) {
     996        return false;
    953997    }
    954998
     
    9591003
    9601004/**
    961  * Remove a topic from user's subscriptions
     1005 * Remove a user subscription
    9621006 *
    9631007 * @since 2.0.0 bbPress (r2668)
     
    9661010 * @param int $object_id Optional. Topic id
    9671011 * @uses get_post() To get the post object
    968  * @uses bbp_get_forum_post_type() To get the forum post type
    969  * @uses bbp_get_topic_post_type() To get the topic post type
    970  * @uses bbp_remove_user_forum_subscription() To remove the user's subscription
    971  * @uses bbp_remove_user_topic_subscription() To remove the user's subscription
     1012 * @uses bbp_is_user_subscribed() To check if the user is already subscribed
    9721013 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    9731014 *                    topic id
     
    9761017 */
    9771018function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
     1019
     1020    // Bail if not enough info
    9781021    if ( empty( $user_id ) || empty( $object_id ) ) {
    9791022        return false;
    9801023    }
    9811024
     1025    // Get post type
    9821026    $post_type = get_post_type( $object_id );
    9831027    if ( empty( $post_type ) ) {
     
    9851029    }
    9861030
    987     switch( $post_type ) {
    988 
    989         // Forum
    990         case bbp_get_forum_post_type() :
    991             bbp_remove_user_forum_subscription( $user_id, $object_id );
    992             break;
    993 
    994         // Topic
    995         case bbp_get_topic_post_type() :
    996         default :
    997             bbp_remove_user_topic_subscription( $user_id, $object_id );
    998             break;
     1031    // Bail if not subscribed
     1032    if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {
     1033        return false;
     1034    }
     1035
     1036    // Bail if remove fails
     1037    if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) {
     1038        return false;
    9991039    }
    10001040
     
    10111051 * @param int $user_id Optional. User id
    10121052 * @param int $forum_id Optional. forum id
    1013  * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
    1014  * @uses update_user_option() To update the user's subscriptions
    1015  * @uses delete_user_option() To delete the user's subscriptions meta
     1053 * @uses bbp_remove_user_subscription() To remove the subscription
    10161054 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    10171055 *                    forum id
     
    10191057 *               otherwise false
    10201058 */
    1021 function bbp_remove_user_forum_subscription( $user_id, $forum_id ) {
     1059function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     1060
     1061    // Bail if not enough info
    10221062    if ( empty( $user_id ) || empty( $forum_id ) ) {
    10231063        return false;
    10241064    }
    10251065
    1026     $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
    1027     if ( empty( $subscriptions ) ) {
    1028         return false;
    1029     }
    1030 
    1031     $pos = array_search( $forum_id, $subscriptions );
    1032     if ( false === $pos ) {
    1033         return false;
    1034     }
    1035 
    1036     array_splice( $subscriptions, $pos, 1 );
    1037     $subscriptions = array_filter( $subscriptions );
    1038 
    1039     if ( ! empty( $subscriptions ) ) {
    1040         $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
    1041         update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
    1042     } else {
    1043         delete_user_option( $user_id, '_bbp_forum_subscriptions' );
    1044     }
    1045 
    1046     wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     1066    // Bail if remove fails
     1067    if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) {
     1068        return false;
     1069    }
    10471070
    10481071    do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
     
    10581081 * @param int $user_id Optional. User id
    10591082 * @param int $topic_id Optional. Topic id
    1060  * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    1061  * @uses update_user_option() To update the user's subscriptions
    1062  * @uses delete_user_option() To delete the user's subscriptions meta
     1083 * @uses bbp_remove_user_subscription() To remove the subscription
    10631084 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    10641085 *                    topic id
     
    10661087 *               otherwise false
    10671088 */
    1068 function bbp_remove_user_topic_subscription( $user_id, $topic_id ) {
     1089function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
     1090
     1091    // Bail if not enough info
    10691092    if ( empty( $user_id ) || empty( $topic_id ) ) {
    10701093        return false;
    10711094    }
    10721095
    1073     $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
    1074     if ( empty( $subscriptions ) ) {
    1075         return false;
    1076     }
    1077 
    1078     $pos = array_search( $topic_id, $subscriptions );
    1079     if ( false === $pos ) {
    1080         return false;
    1081     }
    1082 
    1083     array_splice( $subscriptions, $pos, 1 );
    1084     $subscriptions = array_filter( $subscriptions );
    1085 
    1086     if ( ! empty( $subscriptions ) ) {
    1087         $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
    1088         update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    1089     } else {
    1090         delete_user_option( $user_id, '_bbp_subscriptions' );
    1091     }
    1092 
    1093     wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
     1096    // Bail if remove fails
     1097    if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) {
     1098        return false;
     1099    }
    10941100
    10951101    do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
  • trunk/tests/phpunit/testcases/users/functions/favorites.php

    r5928 r6109  
    120120
    121121        // Add topic favorites.
    122         update_user_option( $u, '_bbp_favorites', $t[0] );
     122        add_post_meta( $t[0], '_bbp_favorite', $u, false );
    123123
    124124        // Add user favorite.
     
    145145
    146146        // Add topic favorites.
    147         update_user_option( $u, '_bbp_favorites', implode( ',', $t ) );
     147        foreach ( $t as $topic ) {
     148            add_post_meta( $topic, '_bbp_favorite', $u );
     149        }
    148150
    149151        // Remove user favorite.
  • trunk/tests/phpunit/testcases/users/functions/functions.php

    r5755 r6109  
    8787
    8888    /**
     89     * @covers ::bbp_add_user_to_object
     90     */
     91    public function test_bbp_add_user_to_object() {
     92        $u = $this->factory->user->create_many( 3 );
     93        $t = $this->factory->topic->create();
     94
     95        // Add object terms.
     96        foreach ( $u as $k => $v ) {
     97            bbp_add_user_to_object( $t, $v, '_bbp_moderator' );
     98        }
     99
     100        $r = get_metadata( 'post', $t, '_bbp_moderator', false );
     101
     102        $this->assertCount( 3, $r );
     103    }
     104
     105    /**
     106     * @covers ::bbp_remove_user_from_object
     107     */
     108    public function test_bbp_remove_user_from_object() {
     109        $u = $this->factory->user->create();
     110        $t = $this->factory->topic->create();
     111
     112        // Add object terms.
     113        add_metadata( 'post', $t, '_bbp_moderator', $u, false );
     114
     115        $r = get_metadata( 'post', $t, '_bbp_moderator', false );
     116
     117        $this->assertCount( 1, $r );
     118
     119        $r = bbp_remove_user_from_object( $t, $u, '_bbp_moderator' );
     120
     121        $this->assertTrue( $r );
     122
     123        $r = get_metadata( 'post', $t, '_bbp_moderator', false );
     124
     125        $this->assertCount( 0, $r );
     126    }
     127
     128    /**
     129     * @covers ::bbp_is_object_of_user
     130     */
     131    public function test_bbp_is_object_of_user() {
     132        $u = $this->factory->user->create();
     133        $t = $this->factory->topic->create();
     134
     135        $r = bbp_is_object_of_user( $t, $u, '_bbp_moderator' );
     136
     137        $this->assertFalse( $r );
     138
     139        // Add user id.
     140        add_metadata( 'post', $t, '_bbp_moderator', $u, false );
     141
     142        $r = bbp_is_object_of_user( $t, $u, '_bbp_moderator' );
     143
     144        $this->assertTrue( $r );
     145    }
     146
     147    /**
    89148     * @covers ::bbp_edit_user_handler
    90149     * @todo   Implement test_bbp_edit_user_handler().
Note: See TracChangeset for help on using the changeset viewer.