Skip to:
Content

bbPress.org

Ticket #2959: 2959.1.diff

File 2959.1.diff, 30.5 KB (added by jmdodd, 5 years ago)

Creates meta functions and corresponding unit test coverage; updates moderators, subscriptions, and favorites.

  • src/includes/users/capabilities.php

     
    811811 * @return @mixed
    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_object_user_term( $object_id, $user_id, '_bbp_moderator_id' );
    815815}
    816816
    817817/**
     
    825825 * @return mixed
    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_object_user_term( $object_id, $user_id, '_bbp_moderator_id' );
    829829}
    830830
    831831/**
     
    838838 * @return mixed
    839839 */
    840840function bbp_get_moderator_ids( $object_id = 0 ) {
    841         return get_post_meta( $object_id, '_bbp_moderator_id', false );
     841        return bbp_get_user_terms_by_object( $object_id, '_bbp_moderator_id' );
    842842}
    843843
    844844/**
     
    865865                        'include' => bbp_get_moderator_ids( $object_id ),
    866866                ) );
    867867        }
     868        error_log( print_r( $users, true ) );
    868869
    869870        return apply_filters( 'bbp_get_moderators', $users, $object_id );
    870871}
  • src/includes/users/functions.php

     
    169169        return apply_filters( 'bbp_current_author_ua', $retval );
    170170}
    171171
     172/** User Taxonomy Terms *******************************************************/
     173
     174/**
     175 * Check if the user term is set 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 $taxonomy The object user taxonomy
     182 * @uses get_post_meta() To check if the user term is set on the object
     183 * @uses apply_filters() Calls 'bbp_object_has_user_term' with the object id,
     184 *                        user id, and taxonomy
     185 * @return bool Returns true if the user term is set on the object for the
     186 *               taxonomy, otherwise false
     187 */
     188function bbp_object_has_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     189        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     190                return;
     191        }
     192
     193        $user_ids = get_post_meta( $object_id, $taxonomy, false );
     194        $retval = (bool) in_array( $user_id, $user_ids );
     195        return apply_filters( 'bbp_object_has_user_term', $retval, $object_id, $user_id, $taxonomy );
     196}
     197
     198/**
     199 * Set a user term on an object
     200 *
     201 * @since 2.7 bbPress ()
     202 *
     203 * @param int $object_id The object id
     204 * @param int $user_id The user id
     205 * @param string $taxonomy The user term taxonomy
     206 * @uses bbp_object_has_user_term() To check if the term has already been set
     207 * @uses add_post_meta() To set the term on the object
     208 * @uses apply_filters() Calls 'bbp_add_object_user_term' with the object id, user
     209 *                        id, and taxonomy
     210 * @return bool Returns true if the user taxonomy term is added to the object,
     211 *               otherwise false
     212 */
     213function bbp_add_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     214        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     215                return;
     216        }
     217
     218        if ( bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) {
     219                $retval = false;
     220        } else {
     221                $retval = (bool) add_post_meta( $object_id, $taxonomy, $user_id, false );
     222        }
     223        return apply_filters( 'bbp_add_object_user_term', $retval, $object_id, $user_id, $taxonomy );
     224}
     225
     226/**
     227 * Remove a user term from an object
     228 *
     229 * @since 2.7 bbPress ()
     230 *
     231 * @param int $object_id The post id
     232 * @param int $user_id The user id
     233 * @param string $taxonomy The user term taxonomy
     234 * @uses bbp_object_has_user_term() To check if the term is set
     235 * @uses delete_post_meta() To remove the term from the object
     236 * @uses apply_filters() Calls 'bbp_remove_object_user_term' with the object
     237 *                        id, user id, and taxonomy
     238 * @return bool Returns true is the user taxonomy term is removed from the object,
     239 *               otherwise false
     240 */
     241function bbp_remove_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     242        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     243                return;
     244        }
     245
     246        if ( ! bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) {
     247                $retval = false;
     248        } else {
     249                $retval = delete_post_meta( $object_id, $taxonomy, $user_id );
     250        }
     251        return apply_filters( 'bbp_remove_object_user_term', $retval, $object_id, $user_id, $taxonomy );
     252}
     253
     254/**
     255 * Get objects for a user taxonomy term
     256 *
     257 * @since 2.7 bbPress ()
     258 *
     259 * @param int $user_id The user id
     260 * @param string $taxonomy The user term taxonomy
     261 * @param string $object_type Optional. The object type
     262 * @uses post_type_exists() To validate the object type
     263 * @uses get_posts() To get the objects with a meta query
     264 * @uses apply_filters() Calls 'bbp_get_objects_by_user_term' with the objects,
     265 *                        user_id, taxonomy, and object type
     266 * @return array Returns the objects with the user taxonomy term
     267 */
     268function bbp_get_objects_by_user_term( $user_id = 0, $taxonomy = '', $object_type = null ) {
     269        if ( empty( $user_id ) || empty( $taxonomy ) || $object_type && ! post_type_exists( $object_type ) ) {
     270                return;
     271        }
     272
     273        $args = array(
     274                'meta_key'    => $taxonomy,
     275                'meta_type'   => 'NUMERIC',
     276                'meta_value'  => $user_id,
     277                'numberposts' => -1
     278        );
     279        if ( $object_type ) {
     280                $args['post_type'] = $object_type;
     281        }
     282        $objects = get_posts( $args );
     283        return apply_filters( 'bbp_get_objects_by_user_term', $objects, $user_id, $taxonomy, $object_type );
     284}
     285
     286/**
     287 * Get user taxonomy terms for an object
     288 *
     289 * @since 2.7 bbPress ()
     290 *
     291 * @param int $object_id The object id
     292 * @param string $taxonomy The user term taxonomy
     293 * @uses get_post_meta() To get the user taxonomy terms
     294 * @uses apply_filters() Calls 'bbp_get_user_terms_by_object' with the user
     295 *                        taxonomy terms, object id, and taxonomy
     296 * @return array Returns the user taxonomy terms of the object
     297 */
     298function bbp_get_user_terms_by_object( $object_id = 0, $taxonomy = '' ) {
     299        if ( empty( $object_id ) || empty( $taxonomy ) ) {
     300                return;
     301        }
     302
     303        $retval = wp_parse_id_list( get_post_meta( $object_id, $taxonomy, false ) );
     304        return apply_filters( 'bbp_get_user_terms_by_object', $retval, $object_id, $taxonomy );
     305}
     306
    172307/** Favorites *****************************************************************/
    173308
    174309/**
     
    188323                return;
    189324        }
    190325
    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         }
     326        $users = bbp_get_user_terms_by_object( $topic_id, '_bbp_favorite' );
    198327
    199328        return apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
    200329}
     
    235364 *
    236365 * @param int $user_id Optional. User id
    237366 * @uses bbp_get_user_id() To get the user id
    238  * @uses get_user_option() To get the user favorites
     367 * @uses bbp_get_objects_by_user_term() To get the user favorites
     368 * @uses bbp_get_topic_post_type() To get the topic post type
    239369 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    240370 *                        the favorites and user id
    241371 * @return array|bool Results if user has favorites, otherwise false
     
    246376                return false;
    247377        }
    248378
    249         $favorites = get_user_option( '_bbp_favorites', $user_id );
    250         $favorites = array_filter( wp_parse_id_list( $favorites ) );
     379        $favorites = bbp_get_objects_by_user_term( $user_id, '_bbp_favorite', bbp_get_topic_post_type() );
     380        $favorites_ids = array_map( function ( $o ) { return (int) $o->ID; }, $favorites );
    251381
    252         return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
     382        return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites_ids, $user_id );
    253383}
    254384
    255385/**
     
    263393 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    264394 * @uses bbp_get_topic() To get the topic
    265395 * @uses bbp_get_topic_id() To get the topic id
     396 * @uses bbp_object_has_user_term() To check if the user has a favorite
    266397 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,
    267398 *                        topic id and favorites
    268399 * @return bool True if the topic is in user's favorites, otherwise false
    269400 */
    270401function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
    271 
    272402        $user_id = bbp_get_user_id( $user_id, true, true );
    273403        if ( empty( $user_id ) ) {
    274404                return false;
    275405        }
    276406
    277         $retval    = false;
     407        $retval = false;
    278408        $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    279409
    280410        if ( ! empty( $favorites ) ) {
     
    295425
    296426                // Is topic_id in the user's favorites
    297427                if ( ! empty( $topic_id ) ) {
    298                         $retval = in_array( $topic_id, $favorites );
     428                        $retval = bbp_object_has_user_term( $topic_id, $user_id, '_bbp_favorite' );
    299429                }
    300430        }
    301431
     
    309439 *
    310440 * @param int $user_id Optional. User id
    311441 * @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
     442 * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    314443 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
    315444 * @return bool Always true
    316445 */
     
    324453                return false;
    325454        }
    326455
    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' );
     456        if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {
     457                bbp_add_object_user_term( $topic_id, $user_id, '_bbp_favorite' );
    335458        }
    336459
    337460        do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
     
    346469 *
    347470 * @param int $user_id Optional. User id
    348471 * @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
     472 * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    352473 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
    353474 * @return bool True if the topic was removed from user's favorites, otherwise
    354475 *               false
     
    358479                return false;
    359480        }
    360481
    361         $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
    362         if ( empty( $favorites ) ) {
     482        if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {
    363483                return false;
    364484        }
    365485
    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' );
     486        if ( ! bbp_remove_object_user_term( $topic_id, $user_id, '_bbp_favorite' ) ) {
     487                return false;
    380488        }
    381489
    382490        do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
     
    494602 * @since 2.5.0 bbPress (r5156)
    495603 *
    496604 * @param int $forum_id Optional. forum id
    497  * @uses wpdb::get_col() To execute our query and get the column back
     605 * @uses bbp_get_user_terms_by_object() To get the forum subscribers
    498606 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
    499607 * @return array|bool Results if the forum has any subscribers, otherwise false
    500608 */
     
    504612                return;
    505613        }
    506614
    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         }
     615        $users = bbp_get_user_terms_by_object( $forum_id, '_bbp_subscription' );
    514616
    515617        return apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
    516618}
     
    521623 * @since 2.0.0 bbPress (r2668)
    522624 *
    523625 * @param int $topic_id Optional. Topic id
    524  * @uses wpdb::get_col() To execute our query and get the column back
     626 * @uses bbp_get_user_terms_by_objet() To get the topic subscribers
    525627 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    526628 * @return array|bool Results if the topic has any subscribers, otherwise false
    527629 */
     
    531633                return;
    532634        }
    533635
    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         }
     636        $users = bbp_get_user_terms_by_object( $topic_id, '_bbp_subscription' );
    541637
    542638        return apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    543639}
     
    628724 *
    629725 * @param int $user_id Optional. User id
    630726 * @uses bbp_get_user_id() To get the user id
    631  * @uses get_user_option() To get the user's subscriptions
     727 * @uses bbp_get_objects_by_user_term() To get the user's subscriptions
     728 * @uses bbp_get_forum_post_type() To get the forum post type
    632729 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
    633730 *                        the subscriptions and user id
    634731 * @return array|bool Results if user has subscriptions, otherwise false
     
    639736                return false;
    640737        }
    641738
    642         $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
    643         $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     739        $subscriptions = bbp_get_objects_by_user_term( $user_id, '_bbp_subscription', bbp_get_forum_post_type() );
     740        $subscriptions = array_map( function ( $o ) { return $o->ID; }, $subscriptions);
    644741
    645742        return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
    646743}
     
    652749 *
    653750 * @param int $user_id Optional. User id
    654751 * @uses bbp_get_user_id() To get the user id
    655  * @uses get_user_option() To get the user's subscriptions
     752 * @uses bbp_get_objects_by_user_term() To get the user's subscriptions
     753 * @uses bbp_get_topic_post_type() To get the topic post type
    656754 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    657755 *                        the subscriptions and user id
    658756 * @return array|bool Results if user has subscriptions, otherwise false
     
    663761                return false;
    664762        }
    665763
    666         $subscriptions = get_user_option( '_bbp_subscriptions', $user_id );
    667         $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     764        $subscriptions = bbp_get_objects_by_user_term( $user_id, '_bbp_subscription', bbp_get_topic_post_type() );
     765        $subscriptions = array_map( function ( $o ) { return $o->ID; }, $subscriptions);
    668766
    669767        return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id );
    670768}
     
    706804
    707805                                // Forum
    708806                                case bbp_get_forum_post_type() :
    709                                         $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    710                                         $retval         = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );
     807                                        $subscribed_ids = bbp_get_user_subscribed_forum_ids();
     808                                        $retval = bbp_object_has_user_term( $object_id, $user_id, '_bbp_subscription' );
    711809                                        break;
    712810
    713811                                // Topic (default)
    714812                                case bbp_get_topic_post_type() :
    715813                                default :
    716                                         $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    717                                         $retval         = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );
     814                                        $subscribed_ids = bbp_get_user_subscribed_topic_ids();
     815                                        $retval = bbp_object_has_user_term( $object_id, $user_id, '_bbp_subscription' );
    718816                                        break;
    719817                        }
    720818                }
     
    732830 * @param int $forum_id Optional. Topic id
    733831 * @param array $subscribed_ids Optional. Array of forum ID's to check
    734832 * @uses bbp_get_user_id() To get the user id
    735  * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
    736833 * @uses bbp_get_forum() To get the forum
    737834 * @uses bbp_get_forum_id() To get the forum id
     835 * @uses bbp_object_has_user_term() To check if the user has a subscription
    738836 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    739837 *                        forum id and subsriptions
    740838 * @return bool True if the forum is in user's subscriptions, otherwise false
     
    770868                                $forum_id = get_the_ID();
    771869                        }
    772870
    773                         // Is forum_id in the user's favorites
     871                        // Is forum_id in the user's subscriptions
    774872                        if ( ! empty( $forum_id ) ) {
    775                                 $retval = in_array( $forum_id, $subscribed_ids );
     873                                $retval = bbp_object_has_user_term( $forum_id, $user_id, '_bbp_subscription' );
    776874                        }
    777875                }
    778876        }
     
    789887 * @param int $topic_id Optional. Topic id
    790888 * @param array $subscribed_ids Optional. Array of topic ID's to check
    791889 * @uses bbp_get_user_id() To get the user id
    792  * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    793890 * @uses bbp_get_topic() To get the topic
    794891 * @uses bbp_get_topic_id() To get the topic id
     892 * @uses bbp_object_has_user_term() To check if the user is subscribed
    795893 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    796894 *                        topic id and subsriptions
    797895 * @return bool True if the topic is in user's subscriptions, otherwise false
     
    827925                                $topic_id = get_the_ID();
    828926                        }
    829927
    830                         // Is topic_id in the user's favorites
     928                        // Is topic_id in the user's subscriptions
    831929                        if ( ! empty( $topic_id ) ) {
    832                                 $retval = in_array( $topic_id, $subscribed_ids );
     930                                $retval = bbp_object_has_user_term( $topic_id, $user_id, '_bbp_subscription' );
    833931                        }
    834932                }
    835933        }
     
    838936}
    839937
    840938/**
    841  * Add a topic to user's subscriptions
     939 * Add a user subscription
    842940 *
    843941 * @since 2.5.0 bbPress (r5156)
    844942 *
    845943 * @param int $user_id Optional. User id
    846944 * @param int $object_id Optional. Topic id
    847945 * @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
     946 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
    854947 * @return bool Always true
    855948 */
    856949function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     
    864957                return false;
    865958        }
    866959
    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;
     960        if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {
     961                bbp_add_object_user_term( $object_id, $user_id, '_bbp_subscription' );
    879962        }
    880963
    881964        do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
     
    890973 *
    891974 * @param int $user_id Optional. User id
    892975 * @param int $forum_id Optional. forum id
    893  * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
    894976 * @uses bbp_get_forum() To get the forum
    895  * @uses update_user_option() To update the user's subscriptions
     977 * @uses bbp_add_user_subscription() To add the user subscription
    896978 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
    897979 * @return bool Always true
    898980 */
     
    906988                return false;
    907989        }
    908990
    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 );
     991        bbp_add_user_subscription( $user_id, $forum_id );
    914992
    915                 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
    916         }
    917 
    918993        do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
    919994
    920995        return true;
     
    9271002 *
    9281003 * @param int $user_id Optional. User id
    9291004 * @param int $topic_id Optional. Topic id
    930  * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    9311005 * @uses bbp_get_topic() To get the topic
    932  * @uses update_user_option() To update the user's subscriptions
     1006 * @uses bbp_add_user_subscription() To add the subscription
    9331007 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    9341008 * @return bool Always true
    9351009 */
     
    9431017                return false;
    9441018        }
    9451019
    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 );
     1020        bbp_add_user_subscription( $user_id, $topic_id );
    9511021
    952                 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    953         }
    954 
    9551022        do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    9561023
    9571024        return true;
    9581025}
    9591026
    9601027/**
    961  * Remove a topic from user's subscriptions
     1028 * Remove a user subscription
    9621029 *
    9631030 * @since 2.0.0 bbPress (r2668)
    9641031 *
    9651032 * @param int $user_id Optional. User id
    9661033 * @param int $object_id Optional. Topic id
    9671034 * @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
     1035 * @uses bbp_is_user_subscribed() To check if the user is already subscribed
    9721036 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    9731037 *                    topic id
    9741038 * @return bool True if the topic was removed from user's subscriptions,
     
    9841048                return false;
    9851049        }
    9861050
    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;
     1051        if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {
     1052                bbp_remove_object_user_term( $object_id, $user_id, '_bbp_subscription' );
    9991053        }
    10001054
    10011055        do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
     
    10101064 *
    10111065 * @param int $user_id Optional. User id
    10121066 * @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
     1067 * @uses bbp_remove_user_subscription() To remove the subscription
    10161068 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    10171069 *                    forum id
    10181070 * @return bool True if the forum was removed from user's subscriptions,
     
    10231075                return false;
    10241076        }
    10251077
    1026         $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
    1027         if ( empty( $subscriptions ) ) {
    1028                 return false;
    1029         }
     1078        bbp_remove_user_subscription( $user_id, $forum_id );
    10301079
    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' );
    1047 
    10481080        do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
    10491081
    10501082        return true;
     
    10571089 *
    10581090 * @param int $user_id Optional. User id
    10591091 * @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
     1092 * @uses bbp_remove_user_subscription() To remove the subscription
    10631093 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    10641094 *                    topic id
    10651095 * @return bool True if the topic was removed from user's subscriptions,
     
    10701100                return false;
    10711101        }
    10721102
    1073         $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
    1074         if ( empty( $subscriptions ) ) {
    1075                 return false;
    1076         }
     1103        bbp_remove_user_subscription( $user_id, $topic_id );
    10771104
    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' );
    1094 
    10951105        do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    10961106
    10971107        return true;
     
    11541164
    11551165        // Check current user's ability to edit the user
    11561166        } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1157                 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1167                bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit subscriptions of that user!', 'bbpress' ) );
    11581168        }
    11591169
    11601170        // Bail if we have errors
     
    12591269
    12601270        // Check current user's ability to edit the user
    12611271        } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1262                 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1272                bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit subscriptions of that user!', 'bbpress' ) );
    12631273        }
    12641274
    12651275        // Bail if we have errors
  • tests/phpunit/testcases/users/functions/favorites.php

     
    119119                $t = $this->factory->topic->create_many( 3 );
    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.
    125125                bbp_add_user_favorite( $u, $t[1] );
     
    144144                $t = $this->factory->topic->create_many( 3 );
    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.
    150152                bbp_remove_user_favorite( $u, $t[2] );
  • tests/phpunit/testcases/users/functions/functions.php

     
    8686        }
    8787
    8888        /**
     89         * @covers ::bbp_object_has_user_term
     90         */
     91        public function test_bbp_object_has_user_term() {
     92                $u = $this->factory->user->create();
     93                $t = $this->factory->topic->create();
     94
     95                $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy );
     96
     97                $this->assertFalse( $r );
     98
     99                // Add user term.
     100                add_post_meta( $t, $this->user_taxonomy, $u, false );
     101
     102                $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy );
     103
     104                $this->assertTrue( $r );
     105        }
     106
     107        /**
     108         * @covers ::bbp_add_object_user_term
     109         */
     110        public function test_bbp_add_object_user_term() {
     111                $u = $this->factory->user->create_many( 3 );
     112                $t = $this->factory->topic->create();
     113
     114                // Add object terms.
     115                foreach ( $u as $k => $v ) {
     116                        bbp_add_object_user_term( $t, $v, $this->user_taxonomy );
     117                }
     118
     119                $r = get_post_meta( $t, $this->user_taxonomy, false );
     120
     121                $this->assertCount( 3, $r );
     122        }
     123
     124        /**
     125         * @covers ::bbp_remove_object_user_term
     126         */
     127        public function test_bbp_remove_object_user_term() {
     128                $u = $this->factory->user->create();
     129                $t = $this->factory->topic->create();
     130
     131                // Add object terms.
     132                add_post_meta( $t, $this->user_taxonomy, $u, false );
     133
     134                $r = get_post_meta( $t, $this->user_taxonomy, false );
     135
     136                $this->assertCount( 1, $r );
     137
     138                $r = bbp_remove_object_user_term( $t, $u, $this->user_taxonomy );
     139
     140                $this->assertTrue( $r );
     141
     142                $r = get_post_meta( $t, $this->user_taxonomy, false );
     143
     144                $this->assertCount( 0, $r );
     145        }
     146
     147        /**
     148         * @covers ::bbp_get_objects_by_user_term
     149         */
     150        public function test_bbp_get_objects_by_user_term() {
     151                $u = $this->factory->user->create();
     152                $t = $this->factory->topic->create_many( 3 );
     153
     154                // Add object terms.
     155                foreach ( $t as $k => $v ) {
     156                        add_post_meta( $v, $this->user_taxonomy, $u, false );
     157                }
     158
     159                $r = bbp_get_objects_by_user_term( $u, $this->user_taxonomy, 'topic' );
     160
     161                $this->assertCount( 3, $r );
     162        }
     163
     164        /**
     165         * @covers ::bbp_get_user_terms_by_object
     166         */
     167        public function test_bbp_get_user_terms_by_object() {
     168                $u = $this->factory->user->create_many( 3 );
     169                $t = $this->factory->topic->create();
     170
     171                // Add object terms.
     172                foreach ( $u as $k => $v ) {
     173                        add_post_meta( $t, $this->user_taxonomy, $u, false );
     174                }
     175
     176                $r = bbp_get_user_terms_by_object( $t, $this->user_taxonomy );
     177
     178                $this->assertCount( 3, $r );
     179        }
     180
     181        /**
    89182         * @covers ::bbp_edit_user_handler
    90183         * @todo   Implement test_bbp_edit_user_handler().
    91184         */
     
    172265                        'This test has not been implemented yet.'
    173266                );
    174267        }
     268
     269        public function setUp() {
     270                parent::setUp();
     271
     272                $this->user_taxonomy = '_bbp_test_user_taxonomy';
     273        }
    175274}