Skip to:
Content

BuddyPress.org

Ticket #3794: 3794.patch

File 3794.patch, 17.5 KB (added by imath, 5 years ago)
  • src/bp-activity/bp-activity-classes.php

    diff --git src/bp-activity/bp-activity-classes.php src/bp-activity/bp-activity-classes.php
    index a67e088..9f604c3 100644
    class BP_Activity_Activity { 
    386386                // Exclude 'last_activity' items unless the 'action' filter has
    387387                // been explicitly set
    388388                if ( empty( $filter['object'] ) ) {
    389                         $where_conditions[] = "a.type != 'last_activity'";
     389                        $where_conditions[] = "a.type NOT IN ('last_activity', 'activity_favorite' )";
    390390                }
    391391
    392392                // Filter the where conditions
    class BP_Activity_Activity { 
    839839                else
    840840                        return false;
    841841
    842                 // Fetch the activity IDs so we can delete any comments for this activity item
     842                // Fetch the activity IDs so we can delete any children for this activity item
    843843                $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    844844
    845845                if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
    846846                        return false;
    847847                }
    848848
    849                 // Handle accompanying activity comments and meta deletion
     849                // Handle accompanying activity children and meta deletion
    850850                if ( $activity_ids ) {
    851851                        $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
    852                         $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
     852                        $activity_children_where_sql = "WHERE type IN ( 'activity_comment', 'activity_favorite' ) AND item_id IN ({$activity_ids_comma})";
    853853
    854                         // Fetch the activity comment IDs for our deleted activity items
    855                         $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     854                        // Fetch the activity child IDs for our deleted activity items
     855                        $activity_children = $wpdb->get_results( "SELECT id, user_id, type FROM {$bp->activity->table_name} {$activity_children_where_sql}" );
    856856
    857857                        // We have activity comments!
    858                         if ( ! empty( $activity_comment_ids ) ) {
    859                                 // Delete activity comments
    860                                 $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     858                        if ( ! empty( $activity_children ) ) {
     859                                $activity_child_ids = array();
     860
     861                                // Delete activity children
     862                                $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_children_where_sql}" );
     863
     864                                foreach ( $activity_children as $child ) {
     865                                        $activity_child_ids[] = $child->id;
     866
     867                                        if ( 'activity_favorite' == $child->type ) {
     868                                                wp_cache_delete( 'bp_activity_user_' . $child->user_id . '_favorites', 'bp' );
     869                                        }
     870                                }
    861871
    862                                 // Merge activity IDs with activity comment IDs
    863                                 $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
     872                                // Merge activity IDs with activity child IDs
     873                                $activity_ids = array_merge( $activity_ids, $activity_child_ids );
    864874                        }
    865875
    866876                        // Delete all activity meta entries for activity items and activity comments
    class BP_Activity_Activity { 
    13111321         * @return int A count of the user's favorites.
    13121322         */
    13131323        public static function total_favorite_count( $user_id ) {
    1314                 if ( !$favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ) )
     1324                $favorite_activity_entries = bp_activity_get_user_favorites( $user_id );
     1325
     1326                if ( empty( $favorite_activity_entries ) )
    13151327                        return 0;
    13161328
    1317                 return count( maybe_unserialize( $favorite_activity_entries ) );
     1329                return count( $favorite_activity_entries );
     1330        }
     1331
     1332        /**
     1333         * Get user_ids who favorited activities.
     1334         *
     1335         * @since BuddyPress (2.1.0)
     1336         *
     1337         * @param int $user_id
     1338         * @param int $activity_id
     1339         * @return array $favorites The favorited activities.
     1340         */
     1341        public static function get_favorites_by_user_id( $user_id = 0, $activity_id = 0 ) {
     1342                global $wpdb;
     1343                $bp = buddypress();
     1344
     1345                $favorites = array();
     1346
     1347                if ( empty( $user_id ) )
     1348                        return $favorites;
     1349
     1350                $where = array(
     1351                        "type = 'activity_favorite'",
     1352                        $wpdb->prepare( "user_id = %d", $user_id )
     1353                );
     1354
     1355                if ( ! empty( $activity_id ) ) {
     1356                        $where[] = $wpdb->prepare( "item_id = %d", $activity_id );
     1357                }
     1358
     1359                $where = 'WHERE ' . implode( ' AND ', $where );
     1360
     1361                $favorites = $wpdb->get_col( "SELECT item_id FROM {$bp->activity->table_name} {$where}" );
     1362                return $favorites;
    13181363        }
    13191364
    13201365        /**
  • src/bp-activity/bp-activity-functions.php

    diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php
    index 3454cf0..64973bc 100644
    function bp_activity_get_types() { 
    350350        foreach ( buddypress()->activity->actions as $action ) {
    351351                $action = array_values( (array) $action );
    352352
    353                 for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ )
     353                for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
     354                        // Remove favorite type
     355                        if ( 'activity_favorite' == $action[$i]['key'] ) {
     356                                continue;
     357                        }
     358
    354359                        $actions[ $action[$i]['key'] ] = $action[$i]['value'];
     360                }
    355361        }
    356362
    357363        // This was a mis-named activity type from before BP 1.6
    function bp_activity_get_types() { 
    367373 *
    368374 * @since BuddyPress (1.2)
    369375 *
    370  * @uses bp_get_user_meta()
     376 * @uses BP_Activity_Activity::get_favorites_by_user_id() to get user's favorites
    371377 * @uses apply_filters() To call the 'bp_activity_get_user_favorites' hook.
    372378 *
    373379 * @param int $user_id ID of the user whose favorites are being queried.
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    380386                $user_id = bp_displayed_user_id();
    381387
    382388        // Get favorites for user
    383         $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
     389        $favs = wp_cache_get( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     390
     391        if ( empty( $favs ) ) {
     392                $favs = BP_Activity_Activity::get_favorites_by_user_id( $user_id );
     393                wp_cache_set( 'bp_activity_user_' . $user_id . '_favorites', $favs, 'bp' );
     394        }
     395       
    384396
    385397        return apply_filters( 'bp_activity_get_user_favorites', $favs );
    386398}
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    391403 * @since BuddyPress (1.2)
    392404 *
    393405 * @uses is_user_logged_in()
    394  * @uses bp_get_user_meta()
     406 * @uses bp_loggedin_user_id() to default to current user logged in
     407 * @uses BP_Activity_Activity::get_favorites_by_user_id() to check if the activity to favorite is not already favorited
    395408 * @uses bp_activity_get_meta()
    396  * @uses bp_update_user_meta()
    397409 * @uses bp_activity_update_meta()
     410 * @uses bp_activity_add_favorite() to create the 'activity_favorite' activity
    398411 * @uses do_action() To call the 'bp_activity_add_user_favorite' hook.
    399412 * @uses do_action() To call the 'bp_activity_add_user_favorite_fail' hook.
    400413 *
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    405418function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {
    406419
    407420        // Favorite activity stream items are for logged in users only
    408         if ( !is_user_logged_in() )
     421        if ( ! is_user_logged_in() )
    409422                return false;
    410423
    411424        // Fallback to logged in user if no user_id is passed
    412425        if ( empty( $user_id ) )
    413426                $user_id = bp_loggedin_user_id();
    414427
    415         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    416         if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {
    417                 $my_favs = array();
    418         }
     428        $is_fav = BP_Activity_Activity::get_favorites_by_user_id( $user_id, $activity_id );
    419429
    420         // Bail if the user has already favorited this activity item
    421         if ( in_array( $activity_id, $my_favs ) ) {
     430        // Bail if Already favorited
     431        if ( ! empty( $is_fav ) ) {
    422432                return false;
    423433        }
    424434
    425         // Add to user's favorites
    426         $my_favs[] = $activity_id;
    427 
    428435        // Update the total number of users who have favorited this activity
    429436        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    430         $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
     437        $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1;
    431438
    432         // Update user meta
    433         bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
     439        // Update activity meta counts for backcompat
     440        bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count );
    434441
    435         // Update activity meta counts
    436         if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
     442        // Save the favorite
     443        if ( bp_activity_add_favorite( $activity_id, $user_id ) ) {
     444                wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
    437445
    438446                // Execute additional code
    439447                do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    456464 * @since BuddyPress (1.2)
    457465 *
    458466 * @uses is_user_logged_in()
    459  * @uses bp_get_user_meta()
     467 * @uses bp_loggedin_user_id() to default to current user logged in
     468 * @uses bp_activity_delete_by_item_id() to delete the favorite
    460469 * @uses bp_activity_get_meta()
    461470 * @uses bp_activity_update_meta()
    462  * @uses bp_update_user_meta()
    463471 * @uses do_action() To call the 'bp_activity_remove_user_favorite' hook.
    464472 *
    465473 * @param int $activity_id ID of the activity item being unfavorited.
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    469477function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {
    470478
    471479        // Favorite activity stream items are for logged in users only
    472         if ( !is_user_logged_in() )
     480        if ( ! is_user_logged_in() )
    473481                return false;
    474482
    475483        // Fallback to logged in user if no user_id is passed
    476484        if ( empty( $user_id ) )
    477485                $user_id = bp_loggedin_user_id();
    478486
    479         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    480         $my_favs = array_flip( (array) $my_favs );
     487        // the favorite is an activity, so let's delete it
     488        $removed = bp_activity_delete_by_item_id(  array(
     489                'item_id' => $activity_id,
     490                'type'    => 'activity_favorite',
     491                'user_id' => $user_id,
     492        ) );
    481493
    482494        // Bail if the user has not previously favorited the item
    483         if ( ! isset( $my_favs[ $activity_id ] ) ) {
     495        if ( empty( $removed ) ) {
    484496                return false;
    485497        }
    486498
    487         // Remove the fav from the user's favs
    488         unset( $my_favs[$activity_id] );
    489         $my_favs = array_unique( array_flip( $my_favs ) );
    490 
    491         // Update the total number of users who have favorited this activity
    492         if ( $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ) ) {
     499        wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     500       
     501        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
     502 
     503        // Error getting favorite count
     504        if ( empty( $fav_count ) ) {
     505                return false;
     506        }
     507       
     508        // Deduct from total favorites
     509        $count_updated = bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 );
    493510
    494                 // Deduct from total favorites
    495                 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {
     511        // Error updating favorite count
     512        if ( empty( $count_updated ) ) {
     513                return false;
     514        }
    496515
    497                         // Update users favorites
    498                         if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
     516        // Execute additional code
     517        do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
    499518
    500                                 // Execute additional code
    501                                 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     519        return true;
     520}
    502521
    503                                 // Success
    504                                 return true;
     522/**
     523 * Add an activity typed 'activity_favorite' for the favorited activity
     524 *
     525 * @since BuddyPress (2.1.0)
     526 * @uses  BP_Activity_Activity
     527 * @uses  bp_activity_get_permalink()
     528 * @uses  bp_core_get_userlink()
     529 * @uses  bp_activity_add()
     530 * @return int|bool the activity id created or false if it fails
     531 */
     532function bp_activity_add_favorite( $activity_id = 0, $user_id = 0 ) {
     533        if ( empty( $activity_id ) || empty( $user_id ) )
     534                return false;
    505535
    506                         // Error updating
    507                         } else {
    508                                 return false;
    509                         }
     536        $bp = buddypress();
    510537
    511                 // Error updating favorite count
    512                 } else {
    513                         return false;
    514                 }
     538        // Parent activity exists ?
     539        $activity = new BP_Activity_Activity( $activity_id );
    515540
    516         // Error getting favorite count
    517         } else {
     541        if ( empty( $activity->type ) || 'activity_favorite' == $activity->type ) {
    518542                return false;
    519543        }
     544
     545        $primary_link   = bp_activity_get_permalink( $activity_id, $activity );
     546        $action         = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $user_id ) );
     547
     548        $args = array(
     549                'user_id'           => $user_id,
     550                'component'         => $bp->activity->id,
     551                'type'              => 'activity_favorite',
     552                'action'            => $action,
     553                'content'           => '',
     554                'primary_link'      => $primary_link,
     555                'item_id'           => $activity_id,
     556                'secondary_item_id' => $activity->user_id,
     557        );
     558
     559        return bp_activity_add( $args );
     560}
     561
     562/**
     563 * Migrate user's favorite (previously in bp_favorite_activities user meta) to activity table
     564 *
     565 * @since BuddyPress (2.1.0)
     566 * @uses  bp_activity_add_favorite() to create the 'activity_favorite' activities
     567 */
     568function bp_activity_favorites_migrate() {
     569        global $wpdb;
     570
     571        $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" );
     572       
     573        foreach( $user_favorites as $meta ) {
     574                $meta_value = maybe_unserialize( $meta->meta_value );
     575
     576                if ( empty( $meta_value ) )
     577                        continue;
     578
     579                foreach( $meta_value as $favorite ) {
     580                        bp_activity_add_favorite( $favorite, $meta->user_id );
     581                }
     582        }
    520583}
    521584
    522585/**
    function bp_activity_register_activity_actions() { 
    854917                __( 'Activity Comments', 'buddypress' )
    855918        );
    856919
     920        bp_activity_set_action(
     921                $bp->activity->id,
     922                'activity_favorite',
     923                __( 'Favorited an update', 'buddypress' ),
     924                'bp_activity_format_activity_action_activity_favorites'
     925        );
     926
    857927        do_action( 'bp_activity_register_activity_actions' );
    858928
    859929        // Backpat. Don't use this.
    function bp_activity_format_activity_action_activity_comment( $action, $activity 
    919989        return apply_filters( 'bp_activity_comment_action', $action, $activity );
    920990}
    921991
     992/**
     993 * Format 'activity_favorite' activity actions.
     994 *
     995 * @since BuddyPress (2.1.0)
     996 *
     997 * @param string $action Static activity action.
     998 * @param object $activity Activity data object.
     999 * @return string
     1000 */
     1001function bp_activity_format_activity_action_activity_favorites( $action, $activity ) {
     1002        $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) );
     1003        return apply_filters( 'bp_activity_favorite_action', $action, $activity );
     1004}
     1005
    9221006/******************************************************************************
    9231007 * Business functions are where all the magic happens in BuddyPress. They will
    9241008 * handle the actual saving or manipulation of information. Usually they will
    function bp_activity_get_permalink( $activity_id, $activity_obj = false ) { 
    16021686                $activity_obj = $activity_obj->current_comment;
    16031687        }
    16041688
    1605         if ( 'new_blog_post' == $activity_obj->type || 'new_blog_comment' == $activity_obj->type || 'new_forum_topic' == $activity_obj->type || 'new_forum_post' == $activity_obj->type ) {
     1689        if ( 'new_blog_post' == $activity_obj->type || 'new_blog_comment' == $activity_obj->type || 'new_forum_topic' == $activity_obj->type || 'new_forum_post' == $activity_obj->type || 'activity_favorite' == $activity_obj->type ) {
    16061690                $link = $activity_obj->primary_link;
    16071691        } else {
    16081692                if ( 'activity_comment' == $activity_obj->type ) {
  • src/bp-activity/bp-activity-template.php

    diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php
    index cd0c279..bce88de 100644
    class BP_Activity_Template { 
    194194                $this->disable_blogforum_replies = isset( $bp->site_options['bp-disable-blogforum-comments'] ) ? $bp->site_options['bp-disable-blogforum-comments'] : false;
    195195
    196196                // Get an array of the logged in user's favorite activities
    197                 $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) );
     197                $this->my_favs = bp_activity_get_user_favorites( bp_loggedin_user_id() );
    198198
    199199                // Fetch specific activity items based on ID's
    200200                if ( !empty( $include ) ) {
    function bp_activity_can_comment_reply( $comment ) { 
    27362736 * @return bool $can_favorite True if comment can receive comments.
    27372737 */
    27382738function bp_activity_can_favorite() {
    2739         $can_favorite = true;
     2739        global $activities_template;
     2740
     2741        $can_favorite = true;
     2742       
     2743        // Just in case, let's avoid potential troubles.
     2744        if ( ! empty( $activities_template->activity->type ) && 'activity_favorite' == $activities_template->activity->type ) {
     2745                $can_favorite = false;
     2746        }
    27402747
    27412748        return apply_filters( 'bp_activity_can_favorite', $can_favorite );
    27422749}
  • src/bp-core/bp-core-update.php

    diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php
    index 20bdb6c..0ff73de 100644
    function bp_version_updater() { 
    240240                if ( $raw_db_version < 8311 ) {
    241241                        bp_update_to_2_0_1();
    242242                }
     243
     244                // 2.1
     245                if ( $raw_db_version < 8312 ) {
     246                        bp_update_to_2_1();
     247                }
    243248        }
    244249
    245250        /** All done! *************************************************************/
    function bp_update_to_2_0_1() { 
    389394}
    390395
    391396/**
     397 * 2.1 database upgrade routine
     398 *
     399 * @since BuddyPress (2.1.0)
     400 *
     401 * @return void
     402 */
     403function bp_update_to_2_1() {
     404        /** Migrate favorites data ******************************************/
     405        if ( bp_is_active( 'activity' ) ) {
     406                bp_activity_favorites_migrate();
     407        }
     408}
     409
     410/**
    392411 * Redirect user to BP's What's New page on first page load after activation.
    393412 *
    394413 * @since BuddyPress (1.7.0)
  • src/bp-loader.php

    diff --git src/bp-loader.php src/bp-loader.php
    index bdd3012..ca5aea5 100644
    class BuddyPress { 
    302302                /** Versions **********************************************************/
    303303
    304304                $this->version    = '2.1-alpha';
    305                 $this->db_version = 8311;
     305                $this->db_version = 8312;
    306306
    307307                /** Loading ***********************************************************/
    308308