Skip to:
Content

bbPress.org

Ticket #3068: bbpress-engagements.patch

File bbpress-engagements.patch, 11.3 KB (added by johnjamesjacoby, 8 years ago)
  • src/includes/admin/tools/repair.php

     
    232232        $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' );
    233233        $result    = __( 'Failed!', 'bbpress' );
    234234
    235         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count'";
     235        $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')";
    236236        if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    237237                return array( 1, sprintf( $statement, $result ) );
    238238        }
     
    243243        $pps = bbp_get_public_status_id();
    244244        $cps = bbp_get_closed_status_id();
    245245
    246         $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    247                         SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
    248                                 FROM `{$bbp_db->posts}` AS `posts`
    249                                 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
    250                                         ON `posts`.`ID` = `postmeta`.`post_id`
    251                                         AND `postmeta`.`meta_key` = '_bbp_topic_id'
    252                                 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
    253                                         AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
    254                                         AND `posts`.`post_author` != '0'
    255                                 GROUP BY `postmeta`.`meta_value`)";
     246        $voice_id_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     247                        SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author
     248                                FROM {$bbp_db->posts} AS posts
     249                                LEFT JOIN {$bbp_db->postmeta} AS postmeta
     250                                        ON posts.ID = postmeta.post_id
     251                                        AND postmeta.meta_key = '_bbp_topic_id'
     252                                WHERE posts.post_type IN (%s, %s)
     253                                        AND posts.post_status IN (%s, %s))", $tpt, $rpt, $pps, $cps );
    256254
    257         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     255        if ( is_wp_error( $bbp_db->query( $voice_id_sql ) ) ) {
    258256                return array( 2, sprintf( $statement, $result ) );
    259257        }
    260258
     259        $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     260                                SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value)
     261                                        FROM {$bbp_db->postmeta}
     262                                        WHERE meta_key = '_bbp_engagement'
     263                                        GROUP BY post_id ORDER BY post_id)";
     264
     265        if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) {
     266                return array( 3, sprintf( $statement, $result ) );
     267        }
     268
    261269        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    262270}
    263271
  • src/includes/core/update.php

     
    326326                                bbp_admin_upgrade_user_favorites();
    327327                                bbp_admin_upgrade_user_topic_subscriptions();
    328328                                bbp_admin_upgrade_user_forum_subscriptions();
     329                                bbp_admin_repair_topic_voice_count();
    329330                        }
    330331                }
    331332        }
  • src/includes/topics/functions.php

     
    910910                bbp_update_topic_last_active_time   ( $topic_id, $last_active );
    911911                bbp_update_topic_reply_count        ( $topic_id, 0            );
    912912                bbp_update_topic_reply_count_hidden ( $topic_id, 0            );
    913                 bbp_update_topic_voice_count        ( $topic_id               );
    914913
    915914                // Walk up ancestors and do the dirty work
    916915                bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
     
    29522951 * @uses update_post_meta() To update the topic voice count meta
    29532952 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
    29542953 *                        count and topic id
    2955  * @return int Voice count
     2954 * @return void|int Voice count.
    29562955 */
    29572956function bbp_update_topic_voice_count( $topic_id = 0 ) {
    29582957
     
    29662965        }
    29672966
    29682967        // Query the DB to get voices in this topic
    2969         $bbp_db = bbp_db();
    2970         $query  = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
    2971         $voices = (int) $bbp_db->get_var( $query );
     2968        $bbp_db   = bbp_db();
     2969        $query    = "SELECT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )";
     2970        $prepared = $bbp_db->prepare( $query, $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2971        $result   = $bbp_db->get_col( $prepared );
     2972        $user_ids = array_unique( array_map( 'intval', (array) $result ) );
     2973        $count    = count( $user_ids );
    29722974
    2973         // Update the voice count for this topic id
    2974         update_post_meta( $topic_id, '_bbp_voice_count', $voices );
     2975        // Remove all voice id meta for the topic.
     2976        delete_post_meta( $topic_id, '_bbp_voice_id' );
    29752977
    2976         return (int) apply_filters( 'bbp_update_topic_voice_count', $voices, $topic_id );
     2978        // Update the voice ids and count for this topic id.
     2979        foreach ( $user_ids as $user_id ) {
     2980                bbp_add_user_engagement( $user_id, $topic_id );
     2981        }
     2982
     2983        // Update the voice count meta.
     2984        update_post_meta( $topic_id, '_bbp_voice_count', $count );
     2985
     2986        $count = apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
     2987
     2988        return bbp_number_not_negative( $count );
    29772989}
    29782990
    29792991/**
  • src/includes/users/functions.php

     
    261261        return (bool) apply_filters( 'bbp_is_object_of_user', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
    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 false
     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
    266460/**
     
    400594                return false;
    401595        }
    402596
    403         // Bail if to topic
     597        // Bail if no topic
    404598        $topic = bbp_get_topic( $topic_id );
    405599        if ( empty( $topic ) ) {
    406600                return false;