Skip to:
Content

bbPress.org

Changeset 6311


Ignore:
Timestamp:
02/26/2017 05:35:31 AM (7 years ago)
Author:
johnjamesjacoby
Message:

Engagements: First pass at engagement user functions.

Also update favorite/subscription documentation where inaccurate.

See #3068.

File:
1 edited

Legend:

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

    r6305 r6311  
    262262}
    263263
     264/** Engagements ***************************************************************/
     265
     266/**
     267 * Get the users who have engaged in a topic
     268 *
     269 * @since 2.6.0 bbPress (r6310)
     270 *
     271 * @param int $topic_id Optional. Topic id
     272 * @uses bbp_get_users_for_object() To get user ids who engaged
     273 * @uses apply_filters() Calls 'bbp_get_topic_engagements' with the users and
     274 *                        topic id
     275 * @return array|bool Results if the topic has any engagements, otherwise false
     276 */
     277function bbp_get_topic_engagements( $topic_id = 0 ) {
     278    $topic_id = bbp_get_topic_id( $topic_id );
     279    $users    = bbp_get_users_for_object( $topic_id, '_bbp_engagement' );
     280
     281    return (array) apply_filters( 'bbp_get_topic_engagements', $users, $topic_id );
     282}
     283
     284/**
     285 * Get a user's topic engagements
     286 *
     287 * @since 2.6.0 bbPress (r6310)
     288 *
     289 * @param int $user_id Optional. User id
     290 * @uses bbp_has_topics() To get the topics
     291 * @uses apply_filters() Calls 'bbp_get_user_engagements' with the topic query and
     292 *                        user id
     293 * @return array|bool Results if user has engaged, otherwise false
     294 */
     295function bbp_get_user_engagements( $user_id = 0 ) {
     296    $user_id     = bbp_get_user_id( $user_id );
     297    $engagements = bbp_has_topics( array(
     298        'meta_query' => array(
     299            array(
     300                'key'     => '_bbp_engagement',
     301                'value'   => $user_id,
     302                'compare' => 'NUMERIC'
     303            )
     304        )
     305    ) );
     306
     307    return apply_filters( 'bbp_get_user_engagements', $engagements, $user_id );
     308}
     309
     310/**
     311 * Get a user's engaged topic ids
     312 *
     313 * @since 2.6.0 bbPress (r6310)
     314 *
     315 * @param int $user_id Optional. User id
     316 * @uses bbp_get_user_id() To get the user id
     317 * @uses bbp_get_topic_post_type() To get the topic post type
     318 * @uses apply_filters() Calls 'bbp_get_user_engaged_topic_ids' with
     319 *                        the engaged topics and user id
     320 * @return array|bool Results if user has engaged, otherwise null
     321 */
     322function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
     323    $user_id     = bbp_get_user_id( $user_id );
     324    $engagements = new WP_Query( array(
     325        'fields'        => 'ids',
     326        'post_type'     => bbp_get_topic_post_type(),
     327        'nopaging'      => true,
     328        'no_found_rows' => true,
     329        'meta_query'    => array( array(
     330            'key'     => '_bbp_engagement',
     331            'value'   => $user_id,
     332            'compare' => 'NUMERIC'
     333        ) )
     334    ) );
     335
     336    return (array) apply_filters( 'bbp_get_user_engaged_topic_ids', $engagements->posts, $user_id );
     337}
     338
     339/**
     340 * Check if a user is engaged in a topic or not
     341 *
     342 * @since 2.6.0 bbPress (r6310)
     343 *
     344 * @param int $user_id Optional. User id
     345 * @param int $topic_id Optional. Topic id
     346 * @uses bbp_get_user_id() To get the user id
     347 * @uses bbp_get_user_engaged_topic_ids() To get the user engaged topics
     348 * @uses bbp_get_topic() To get the topic
     349 * @uses bbp_get_topic_id() To get the topic id
     350 * @uses bbp_is_object_of_user() To check if the user has engaged
     351 * @uses apply_filters() Calls 'bbp_is_user_engaged' with the bool, user id,
     352 *                        topic id and engagements
     353 * @return bool True if the topic is in user's engagements, otherwise false
     354 */
     355function bbp_is_user_engaged( $user_id = 0, $topic_id = 0 ) {
     356    $retval      = false;
     357    $user_id     = bbp_get_user_id( $user_id, true, true );
     358    $engagements = bbp_get_user_engaged_topic_ids( $user_id );
     359
     360    if ( ! empty( $engagements ) ) {
     361
     362        // Checking a specific topic id
     363        if ( ! empty( $topic_id ) ) {
     364            $topic    = bbp_get_topic( $topic_id );
     365            $topic_id = ! empty( $topic ) ? $topic->ID : 0;
     366
     367        // Using the global topic id
     368        } elseif ( bbp_get_topic_id() ) {
     369            $topic_id = bbp_get_topic_id();
     370
     371        // Use the current post id
     372        } elseif ( ! bbp_get_topic_id() ) {
     373            $topic_id = get_the_ID();
     374        }
     375
     376        // Is topic_id in the user's engagements
     377        if ( ! empty( $topic_id ) ) {
     378            $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_engagement' );
     379        }
     380    }
     381
     382    return (bool) apply_filters( 'bbp_is_user_engaged', (bool) $retval, $user_id, $topic_id, $engagements );
     383}
     384
     385/**
     386 * Add a topic to user's engagements
     387 *
     388 * @since 2.6.0 bbPress (r6310)
     389 *
     390 * @param int $user_id Optional. User id
     391 * @param int $topic_id Optional. Topic id
     392 * @uses bbp_is_user_engaged() To check if the user is engaged in a topic
     393 * @uses do_action() Calls 'bbp_add_user_engagement' with the user id and topic id
     394 * @return bool Always true
     395 */
     396function bbp_add_user_engagement( $user_id = 0, $topic_id = 0 ) {
     397
     398    // Bail if not enough info
     399    if ( empty( $user_id ) || empty( $topic_id ) ) {
     400        return false;
     401    }
     402
     403    // Bail if no topic
     404    $topic = bbp_get_topic( $topic_id );
     405    if ( empty( $topic ) ) {
     406        return false;
     407    }
     408
     409    // Bail if already a engaged
     410    if ( bbp_is_user_engaged( $user_id, $topic_id ) ) {
     411        return false;
     412    }
     413
     414    // Bail if add fails
     415    if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
     416        return false;
     417    }
     418
     419    do_action( 'bbp_add_user_engagement', $user_id, $topic_id );
     420
     421    return true;
     422}
     423
     424/**
     425 * Remove a topic from user's engagements
     426 *
     427 * @since 2.6.0 bbPress (r6310)
     428 *
     429 * @param int $user_id Optional. User id
     430 * @param int $topic_id Optional. Topic id
     431 * @uses bbp_is_user_engaged() To check if the user is engaged in a topic
     432 * @uses do_action() Calls 'bbp_remove_user_engagement' with the user & topic id
     433 * @return bool True if the topic was removed from user's engagements, otherwise
     434 *               false
     435 */
     436function bbp_remove_user_engagement( $user_id, $topic_id ) {
     437
     438    // Bail if not enough info
     439    if ( empty( $user_id ) || empty( $topic_id ) ) {
     440        return false;
     441    }
     442
     443    // Bail if not already engaged
     444    if ( ! bbp_is_user_engaged( $user_id, $topic_id ) ) {
     445        return false;
     446    }
     447
     448    // Bail if remove fails
     449    if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
     450        return false;
     451    }
     452
     453    do_action( 'bbp_remove_user_engagement', $user_id, $topic_id );
     454
     455    return true;
     456}
     457
    264458/** Favorites *****************************************************************/
    265459
     
    318512 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    319513 *                        the favorites and user id
    320  * @return array|bool Results if user has favorites, otherwise false
     514 * @return array|bool Results if user has favorites, otherwise null
    321515 */
    322516function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
     
    401595    }
    402596
    403     // Bail if to topic
     597    // Bail if no topic
    404598    $topic = bbp_get_topic( $topic_id );
    405599    if ( empty( $topic ) ) {
     
    675869 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
    676870 *                        the subscriptions and user id
    677  * @return array|bool Results if user has subscriptions, otherwise false
     871 * @return array|bool Results if user has subscriptions, otherwise null
    678872 */
    679873function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
     
    704898 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    705899 *                        the subscriptions and user id
    706  * @return array|bool Results if user has subscriptions, otherwise false
     900 * @return array|bool Results if user has subscriptions, otherwise null
    707901 */
    708902function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
     
    789983 * @uses bbp_get_forum_id() To get the forum id
    790984 * @uses bbp_is_object_of_user() To check if the user has a subscription
    791  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     985 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool, user id,
    792986 *                        forum id and subsriptions
    793987 * @return bool True if the forum is in user's subscriptions, otherwise false
     
    8461040 * @uses bbp_get_topic_id() To get the topic id
    8471041 * @uses bbp_is_object_of_user() To check if the user is subscribed
    848  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     1042 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool, user id,
    8491043 *                        topic id and subsriptions
    8501044 * @return bool True if the topic is in user's subscriptions, otherwise false
Note: See TracChangeset for help on using the changeset viewer.