Skip to:
Content

BuddyPress.org

Ticket #3794: 3794.02.patch

File 3794.02.patch, 18.1 KB (added by imath, 10 years ago)
  • src/bp-activity/bp-activity-admin.php

    diff --git src/bp-activity/bp-activity-admin.php src/bp-activity/bp-activity-admin.php
    index 5547557..8fcae95 100644
    class BP_Activity_List_Table extends WP_List_Table { 
    15351535
    15361536                if ( $this->disable_blogforum_comments ) {
    15371537                        switch ( $item['type'] ) {
    1538                                 case 'new_blog_post' :
    1539                                 case 'new_blog_comment' :
    1540                                 case 'new_forum_topic' :
    1541                                 case 'new_forum_post' :
     1538                                case 'new_blog_post'     :
     1539                                case 'new_blog_comment'  :
     1540                                case 'new_forum_topic'   :
     1541                                case 'new_forum_post'    :
     1542                                case 'activity_favorite' :
    15421543                                        $can_comment = false;
    15431544                                        break;
    15441545                        }
    class BP_Activity_List_Table extends WP_List_Table { 
    15731574                        }
    15741575                }
    15751576
    1576                 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment );
     1577                return apply_filters( 'bp_activity_list_table_can_comment', $can_comment, $item['type'] );
    15771578        }
    15781579
    15791580        /**
  • src/bp-activity/bp-activity-classes.php

    diff --git src/bp-activity/bp-activity-classes.php src/bp-activity/bp-activity-classes.php
    index ad6f9f0..51f0290 100644
    class BP_Activity_Activity { 
    403403                        $excluded_types[] = 'last_activity';
    404404                }
    405405
     406                if ( empty( $filter['action'] ) || 'activity_favorite' != $filter['action'] ) {
     407                        $excluded_types[] = 'activity_favorite';
     408                }
     409
    406410                // Exclude 'new_member' items if xprofile component is not active
    407411                if ( ! bp_is_active( 'xprofile' ) ) {
    408412                        $excluded_types[] = 'new_member';
    class BP_Activity_Activity { 
    890894                else
    891895                        return false;
    892896
    893                 // Fetch the activity IDs so we can delete any comments for this activity item
     897                // Fetch the activity IDs so we can delete any children for this activity item
    894898                $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    895899
    896900                if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
    897901                        return false;
    898902                }
    899903
    900                 // Handle accompanying activity comments and meta deletion
     904                // Handle accompanying activity children and meta deletion
    901905                if ( $activity_ids ) {
    902906                        $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
    903                         $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
     907                        $activity_children_where_sql = "WHERE type IN ( 'activity_comment', 'activity_favorite' ) AND item_id IN ({$activity_ids_comma})";
    904908
    905                         // Fetch the activity comment IDs for our deleted activity items
    906                         $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     909                        // Fetch the activity child IDs for our deleted activity items
     910                        $activity_children = $wpdb->get_results( "SELECT id, user_id, type FROM {$bp->activity->table_name} {$activity_children_where_sql}" );
    907911
    908                         // We have activity comments!
    909                         if ( ! empty( $activity_comment_ids ) ) {
    910                                 // Delete activity comments
    911                                 $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     912                        // We have activity children!
     913                        if ( ! empty( $activity_children ) ) {
     914                                $activity_child_ids = array();
     915
     916                                // Delete activity children
     917                                $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_children_where_sql}" );
     918
     919                                foreach ( $activity_children as $child ) {
     920                                        $activity_child_ids[] = $child->id;
     921
     922                                        if ( 'activity_favorite' == $child->type ) {
     923                                                wp_cache_delete( 'bp_activity_user_' . $child->user_id . '_favorites', 'bp' );
     924                                        }
     925                                }
    912926
    913                                 // Merge activity IDs with activity comment IDs
    914                                 $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
     927                                // Merge activity IDs with activity child IDs
     928                                $activity_ids = array_merge( $activity_ids, $activity_child_ids );
    915929                        }
    916930
    917931                        // Delete all activity meta entries for activity items and activity comments
    class BP_Activity_Activity { 
    13621376         * @return int A count of the user's favorites.
    13631377         */
    13641378        public static function total_favorite_count( $user_id ) {
    1365                 if ( !$favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ) )
     1379                $favorite_activity_entries = bp_activity_get_user_favorites( $user_id );
     1380
     1381                if ( empty( $favorite_activity_entries ) ) {
    13661382                        return 0;
     1383                }
    13671384
    1368                 return count( maybe_unserialize( $favorite_activity_entries ) );
     1385                return count( $favorite_activity_entries );
    13691386        }
    13701387
    13711388        /**
     1389         * Get user_ids who favorited activities.
     1390         *
     1391         * @since BuddyPress (2.2.0)
     1392         *
     1393         * @param int $user_id
     1394         * @param int $activity_id
     1395         * @return array $favorites The favorited activities.
     1396         */
     1397        public static function get_favorites_by_user_id( $user_id = 0, $activity_id = 0 ) {
     1398                global $wpdb;
     1399                $bp = buddypress();
     1400
     1401                $favorites = array();
     1402
     1403                if ( empty( $user_id ) ) {
     1404                        return $favorites;
     1405                }
     1406
     1407                $where = array(
     1408                        "type = 'activity_favorite'",
     1409                        $wpdb->prepare( "user_id = %d", $user_id )
     1410                );
     1411
     1412                if ( ! empty( $activity_id ) ) {
     1413                        $where[] = $wpdb->prepare( "item_id = %d", $activity_id );
     1414                }
     1415
     1416                $where = 'WHERE ' . implode( ' AND ', $where );
     1417                $favorites = $wpdb->get_col( "SELECT item_id FROM {$bp->activity->table_name} {$where}" );
     1418
     1419                return array_map( 'intval', $favorites );
     1420        }
     1421
     1422        /**
    13721423         * Check whether an activity item exists with a given string content.
    13731424         *
    13741425         * @param string $content The content to filter by.
  • src/bp-activity/bp-activity-functions.php

    diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php
    index 94b5de1..ab3fd4b 100644
    function bp_activity_get_types() { 
    385385                $action = array_values( (array) $action );
    386386
    387387                for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
     388                        // Remove favorite type
     389                        if ( 'activity_favorite' == $action[$i]['key'] ) {
     390                                continue;
     391                        }
     392
    388393                        $actions[ $action[$i]['key'] ] = $action[$i]['value'];
    389394                }
    390395        }
    function bp_activity_get_types() { 
    402407 *
    403408 * @since BuddyPress (1.2.0)
    404409 *
    405  * @uses bp_get_user_meta()
     410 * @uses BP_Activity_Activity::get_favorites_by_user_id() to get user's favorites
    406411 * @uses apply_filters() To call the 'bp_activity_get_user_favorites' hook.
    407412 *
    408413 * @param int $user_id ID of the user whose favorites are being queried.
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    416421        }
    417422
    418423        // Get favorites for user
    419         $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
     424        $favs = wp_cache_get( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     425
     426        if ( empty( $favs ) ) {
     427                $favs = BP_Activity_Activity::get_favorites_by_user_id( $user_id );
     428                wp_cache_set( 'bp_activity_user_' . $user_id . '_favorites', $favs, 'bp' );
     429        }
    420430
    421431        return apply_filters( 'bp_activity_get_user_favorites', $favs );
    422432}
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    427437 * @since BuddyPress (1.2.0)
    428438 *
    429439 * @uses is_user_logged_in()
    430  * @uses bp_get_user_meta()
     440 * @uses bp_loggedin_user_id() to default to current user logged in
     441 * @uses BP_Activity_Activity::get_favorites_by_user_id() to check if the activity to favorite is not already favorited
    431442 * @uses bp_activity_get_meta()
    432  * @uses bp_update_user_meta()
    433443 * @uses bp_activity_update_meta()
     444 * @uses bp_activity_add_favorite() to create the 'activity_favorite' activity
    434445 * @uses do_action() To call the 'bp_activity_add_user_favorite' hook.
    435446 * @uses do_action() To call the 'bp_activity_add_user_favorite_fail' hook.
    436447 *
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    450461                $user_id = bp_loggedin_user_id();
    451462        }
    452463
    453         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    454         if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {
    455                 $my_favs = array();
    456         }
     464        // Check if this activity item is already in user's favorites
     465        $is_fav = BP_Activity_Activity::get_favorites_by_user_id( $user_id, $activity_id );
    457466
    458467        // Bail if the user has already favorited this activity item
    459         if ( in_array( $activity_id, $my_favs ) ) {
     468        if ( ! empty( $is_fav ) ) {
    460469                return false;
    461470        }
    462471
    463         // Add to user's favorites
    464         $my_favs[] = $activity_id;
    465 
    466472        // Update the total number of users who have favorited this activity
    467473        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    468         $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
     474        $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1;
    469475
    470         // Update user meta
    471         bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
     476        // Update activity meta counts for backcompat
     477        bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count );
    472478
    473         // Update activity meta counts
    474         if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
     479        // Save the favorite
     480        if ( bp_activity_add_favorite( $activity_id, $user_id ) ) {
     481                wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
    475482
    476483                // Execute additional code
    477484                do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    494501 * @since BuddyPress (1.2.0)
    495502 *
    496503 * @uses is_user_logged_in()
    497  * @uses bp_get_user_meta()
     504 * @uses bp_loggedin_user_id() to default to current user logged in
     505 * @uses bp_activity_delete_by_item_id() to delete the favorite
    498506 * @uses bp_activity_get_meta()
    499507 * @uses bp_activity_update_meta()
    500  * @uses bp_update_user_meta()
    501508 * @uses do_action() To call the 'bp_activity_remove_user_favorite' hook.
    502509 *
    503510 * @param int $activity_id ID of the activity item being unfavorited.
    function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 
    516523                $user_id = bp_loggedin_user_id();
    517524        }
    518525
    519         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    520         $my_favs = array_flip( (array) $my_favs );
     526        // the favorite is an activity, so let's delete it
     527        $removed = bp_activity_delete_by_item_id( array(
     528                'item_id' => $activity_id,
     529                'type'    => 'activity_favorite',
     530                'user_id' => $user_id,
     531        ) );
    521532
    522533        // Bail if the user has not previously favorited the item
    523         if ( ! isset( $my_favs[ $activity_id ] ) ) {
     534        if ( empty( $removed ) ) {
    524535                return false;
    525536        }
    526537
    527         // Remove the fav from the user's favs
    528         unset( $my_favs[$activity_id] );
    529         $my_favs = array_unique( array_flip( $my_favs ) );
     538        // Clean the user's favs cache
     539        wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
    530540
    531         // Update the total number of users who have favorited this activity
    532541        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    533         if ( ! empty( $fav_count ) ) {
    534542
    535                 // Deduct from total favorites
    536                 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {
     543        // Error getting favorite count
     544        if ( empty( $fav_count ) ) {
     545                return false;
     546        }
    537547
    538                         // Update users favorites
    539                         if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
     548        // Deduct from total favorites
     549        $count_updated = bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 );
    540550
    541                                 // Execute additional code
    542                                 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     551        // Error updating favorite count
     552        if ( empty( $count_updated ) ) {
     553                return false;
     554        }
    543555
    544                                 // Success
    545                                 return true;
     556        // Execute additional code
     557        do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
    546558
    547                         // Error updating
    548                         } else {
    549                                 return false;
    550                         }
     559        return true;
     560}
    551561
    552                 // Error updating favorite count
    553                 } else {
    554                         return false;
     562/**
     563 * Add an activity typed 'activity_favorite' for the favorited activity
     564 *
     565 * @since BuddyPress (2.2.0)
     566 * @uses  BP_Activity_Activity
     567 * @uses  bp_activity_get_permalink()
     568 * @uses  bp_core_get_userlink()
     569 * @uses  bp_activity_add()
     570 * @return int|bool the activity id created or false if it fails
     571 */
     572function bp_activity_add_favorite( $activity_id = 0, $user_id = 0 ) {
     573        if ( empty( $activity_id ) || empty( $user_id ) ) {
     574                return false;
     575        }
     576
     577        $bp = buddypress();
     578
     579        // Parent activity exists ?
     580        $activity = new BP_Activity_Activity( $activity_id );
     581
     582        if ( empty( $activity->type ) || 'activity_favorite' == $activity->type ) {
     583                return false;
     584        }
     585
     586        $primary_link   = bp_activity_get_permalink( $activity_id, $activity );
     587        $action         = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $user_id ) );
     588
     589        $args = array(
     590                'user_id'           => $user_id,
     591                'component'         => $bp->activity->id,
     592                'type'              => 'activity_favorite',
     593                'action'            => $action,
     594                'content'           => '',
     595                'primary_link'      => $primary_link,
     596                'item_id'           => $activity_id,
     597                'secondary_item_id' => $activity->user_id,
     598        );
     599
     600        return bp_activity_add( $args );
     601}
     602
     603/**
     604 * Migrate user's favorite (previously in bp_favorite_activities user meta) to activity table
     605 *
     606 * @since BuddyPress (2.2.0)
     607 * @uses  bp_activity_add_favorite() to create the 'activity_favorite' activities
     608 */
     609function bp_activity_favorites_migrate() {
     610        global $wpdb;
     611
     612        $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" );
     613
     614        foreach( $user_favorites as $meta ) {
     615                $meta_value = maybe_unserialize( $meta->meta_value );
     616
     617                if ( empty( $meta_value ) ) {
     618                        continue;
    555619                }
    556620
    557         // Error getting favorite count
    558         } else {
    559                 return false;
     621                foreach( $meta_value as $favorite ) {
     622                        bp_activity_add_favorite( $favorite, $meta->user_id );
     623                }
    560624        }
    561625}
    562626
    function bp_activity_register_activity_actions() { 
    913977                __( 'Activity Comments', 'buddypress' )
    914978        );
    915979
     980        bp_activity_set_action(
     981                $bp->activity->id,
     982                'activity_favorite',
     983                __( 'Favorited an update', 'buddypress' ),
     984                'bp_activity_format_activity_action_activity_favorites'
     985        );
     986
    916987        do_action( 'bp_activity_register_activity_actions' );
    917988
    918989        // Backpat. Don't use this.
    function bp_activity_format_activity_action_activity_comment( $action, $activity 
    9791050        return apply_filters( 'bp_activity_comment_action', $action, $activity );
    9801051}
    9811052
     1053/**
     1054 * Format 'activity_favorite' activity actions.
     1055 *
     1056 * @since BuddyPress (2.2.0)
     1057 *
     1058 * @param string $action Static activity action.
     1059 * @param object $activity Activity data object.
     1060 * @return string
     1061 */
     1062function bp_activity_format_activity_action_activity_favorites( $action, $activity ) {
     1063        $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) );
     1064        return apply_filters( 'bp_activity_favorite_action', $action, $activity );
     1065}
     1066
    9821067/******************************************************************************
    9831068 * Business functions are where all the magic happens in BuddyPress. They will
    9841069 * handle the actual saving or manipulation of information. Usually they will
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    16611746                                bp_activity_delete_children( $activity_id, $child->id );
    16621747                        }
    16631748                }
    1664                
     1749
    16651750                // Delete the comment itself
    16661751                bp_activity_delete( array(
    16671752                        'secondary_item_id' => $comment_id,
    function bp_activity_get_permalink( $activity_id, $activity_obj = false ) { 
    16981783                $activity_obj = $activity_obj->current_comment;
    16991784        }
    17001785
    1701         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 ) {
     1786        if ( in_array( $activity_obj->type, array( 'new_blog_post', 'new_blog_comment', 'new_forum_topic', 'new_forum_post', 'activity_favorite' ) ) ) {
    17021787                $link = $activity_obj->primary_link;
    17031788        } else {
    17041789                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 d65eac3..7e40afc 100644
    class BP_Activity_Template { 
    195195                $this->disable_blogforum_replies = isset( $bp->site_options['bp-disable-blogforum-comments'] ) ? $bp->site_options['bp-disable-blogforum-comments'] : false;
    196196
    197197                // Get an array of the logged in user's favorite activities
    198                 $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) );
     198                $this->my_favs = bp_activity_get_user_favorites( bp_loggedin_user_id() );
    199199
    200200                // Fetch specific activity items based on ID's
    201201                if ( !empty( $include ) ) {
    function bp_activity_can_comment_reply( $comment = '' ) { 
    28652865 * @return bool True if comment can receive comments.
    28662866 */
    28672867function bp_activity_can_favorite() {
     2868        global $activities_template;
     2869
     2870        $can_favorite = true;
     2871
     2872        // Don't favorite a favorite
     2873        if ( ! empty( $activities_template->activity->type ) && 'activity_favorite' == $activities_template->activity->type ) {
     2874                $can_favorite = false;
     2875        }
     2876
    28682877        return apply_filters( 'bp_activity_can_favorite', true );
    28692878}
    28702879
  • src/bp-core/bp-core-update.php

    diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php
    index 6b27154..4641747 100644
    function bp_version_updater() { 
    241241                if ( $raw_db_version < 8311 ) {
    242242                        bp_update_to_2_0_1();
    243243                }
     244
     245                // 2.2
     246                if ( $raw_db_version < 8312 ) {
     247                        bp_update_to_2_2();
     248                }
    244249        }
    245250
    246251        /** All done! *************************************************************/
    function bp_update_to_2_0_1() { 
    390395}
    391396
    392397/**
     398 * 2.2 database upgrade routine
     399 *
     400 * @since BuddyPress (2.2.0)
     401 *
     402 * @return void
     403 */
     404function bp_update_to_2_2() {
     405
     406        /** Migrate favorites data ****************************************************/
     407
     408        if ( bp_is_active( 'activity' ) ) {
     409                bp_activity_favorites_migrate();
     410        }
     411}
     412
     413/**
    393414 * Redirect user to BP's What's New page on first page load after activation.
    394415 *
    395416 * @since BuddyPress (1.7.0)
  • src/bp-loader.php

    diff --git src/bp-loader.php src/bp-loader.php
    index 41c13e0..0838e94 100644
    class BuddyPress { 
    301301                /** Versions **********************************************************/
    302302
    303303                $this->version    = '2.2-alpha';
    304                 $this->db_version = 8311;
     304                $this->db_version = 8312;
    305305
    306306                /** Loading ***********************************************************/
    307307