Skip to:
Content

BuddyPress.org

Ticket #3794: 3794.03.patch

File 3794.03.patch, 18.3 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..741225d 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;
     472        // Save the favorite
     473        if ( bp_activity_add_favorite( $activity_id, $user_id ) ) {
    465474
    466         // Update the total number of users who have favorited this activity
    467         $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    468         $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
     475                // Update the total number of users who have favorited this activity
     476                $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
     477                $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1;
    469478
    470         // Update user meta
    471         bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
     479                // Update activity meta counts for backcompat
     480                bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count );
    472481
    473         // Update activity meta counts
    474         if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
     482                wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
    475483
    476484                // Execute additional code
    477485                do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    494502 * @since BuddyPress (1.2.0)
    495503 *
    496504 * @uses is_user_logged_in()
    497  * @uses bp_get_user_meta()
     505 * @uses bp_loggedin_user_id() to default to current user logged in
     506 * @uses bp_activity_delete_by_item_id() to delete the favorite
    498507 * @uses bp_activity_get_meta()
    499508 * @uses bp_activity_update_meta()
    500  * @uses bp_update_user_meta()
    501509 * @uses do_action() To call the 'bp_activity_remove_user_favorite' hook.
    502510 *
    503511 * @param int $activity_id ID of the activity item being unfavorited.
    function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 
    516524                $user_id = bp_loggedin_user_id();
    517525        }
    518526
    519         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    520         $my_favs = array_flip( (array) $my_favs );
     527        // the favorite is an activity, so let's delete it
     528        $removed = bp_activity_delete_by_item_id( array(
     529                'item_id' => $activity_id,
     530                'type'    => 'activity_favorite',
     531                'user_id' => $user_id,
     532        ) );
    521533
    522534        // Bail if the user has not previously favorited the item
    523         if ( ! isset( $my_favs[ $activity_id ] ) ) {
     535        if ( empty( $removed ) ) {
    524536                return false;
    525537        }
    526538
    527         // Remove the fav from the user's favs
    528         unset( $my_favs[$activity_id] );
    529         $my_favs = array_unique( array_flip( $my_favs ) );
     539        // Clean the user's favs cache
     540        wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
    530541
    531         // Update the total number of users who have favorited this activity
    532542        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    533         if ( ! empty( $fav_count ) ) {
    534543
    535                 // Deduct from total favorites
    536                 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {
     544        // Error getting favorite count
     545        if ( empty( $fav_count ) ) {
     546                return false;
     547        }
     548
     549        // Deduct from total favorites
     550        $count_updated = bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 );
    537551
    538                         // Update users favorites
    539                         if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
     552        // Error updating favorite count
     553        if ( empty( $count_updated ) ) {
     554                return false;
     555        }
    540556
    541                                 // Execute additional code
    542                                 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     557        // Execute additional code
     558        do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
    543559
    544                                 // Success
    545                                 return true;
     560        return true;
     561}
    546562
    547                         // Error updating
    548                         } else {
    549                                 return false;
    550                         }
     563/**
     564 * Add an activity typed 'activity_favorite' for the favorited activity
     565 *
     566 * @since BuddyPress (2.2.0)
     567 * @uses  BP_Activity_Activity
     568 * @uses  bp_activity_get_permalink()
     569 * @uses  bp_core_get_userlink()
     570 * @uses  bp_activity_add()
     571 * @return int|bool the activity id created or false if it fails
     572 */
     573function bp_activity_add_favorite( $activity_id = 0, $user_id = 0 ) {
     574        if ( empty( $activity_id ) || empty( $user_id ) ) {
     575                return false;
     576        }
    551577
    552                 // Error updating favorite count
    553                 } else {
    554                         return false;
     578        $bp = buddypress();
     579
     580        // Parent activity exists ?
     581        $activity = new BP_Activity_Activity( $activity_id );
     582
     583        if ( empty( $activity->type ) || 'activity_favorite' == $activity->type ) {
     584                return false;
     585        }
     586
     587        $primary_link   = bp_activity_get_permalink( $activity_id, $activity );
     588        $action         = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $user_id ) );
     589
     590        $args = array(
     591                'user_id'           => $user_id,
     592                'component'         => $bp->activity->id,
     593                'type'              => 'activity_favorite',
     594                'action'            => $action,
     595                'content'           => '',
     596                'primary_link'      => $primary_link,
     597                'item_id'           => $activity_id,
     598                'secondary_item_id' => $activity->user_id,
     599                'hide_sitewide'     => $activity->hide_sitewide,
     600        );
     601
     602        return bp_activity_add( $args );
     603}
     604
     605/**
     606 * Migrate user's favorite (previously in bp_favorite_activities user meta) to activity table
     607 *
     608 * @since BuddyPress (2.2.0)
     609 * @uses  bp_activity_add_favorite() to create the 'activity_favorite' activities
     610 */
     611function bp_activity_favorites_migrate() {
     612        global $wpdb;
     613
     614        $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" );
     615
     616        foreach( $user_favorites as $meta ) {
     617                $meta_value = maybe_unserialize( $meta->meta_value );
     618
     619                if ( empty( $meta_value ) ) {
     620                        continue;
    555621                }
    556622
    557         // Error getting favorite count
    558         } else {
    559                 return false;
     623                foreach( $meta_value as $favorite ) {
     624                        bp_activity_add_favorite( $favorite, $meta->user_id );
     625                }
    560626        }
    561627}
    562628
    function bp_activity_register_activity_actions() { 
    913979                __( 'Activity Comments', 'buddypress' )
    914980        );
    915981
     982        bp_activity_set_action(
     983                $bp->activity->id,
     984                'activity_favorite',
     985                __( 'Favorited an update', 'buddypress' ),
     986                'bp_activity_format_activity_action_activity_favorites'
     987        );
     988
    916989        do_action( 'bp_activity_register_activity_actions' );
    917990
    918991        // Backpat. Don't use this.
    function bp_activity_format_activity_action_activity_comment( $action, $activity 
    9791052        return apply_filters( 'bp_activity_comment_action', $action, $activity );
    9801053}
    9811054
     1055/**
     1056 * Format 'activity_favorite' activity actions.
     1057 *
     1058 * @since BuddyPress (2.2.0)
     1059 *
     1060 * @param string $action Static activity action.
     1061 * @param object $activity Activity data object.
     1062 * @return string
     1063 */
     1064function bp_activity_format_activity_action_activity_favorites( $action, $activity ) {
     1065        $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) );
     1066        return apply_filters( 'bp_activity_favorite_action', $action, $activity );
     1067}
     1068
    9821069/******************************************************************************
    9831070 * Business functions are where all the magic happens in BuddyPress. They will
    9841071 * handle the actual saving or manipulation of information. Usually they will
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    16611748                                bp_activity_delete_children( $activity_id, $child->id );
    16621749                        }
    16631750                }
    1664                
     1751
    16651752                // Delete the comment itself
    16661753                bp_activity_delete( array(
    16671754                        'secondary_item_id' => $comment_id,
    function bp_activity_get_permalink( $activity_id, $activity_obj = false ) { 
    16981785                $activity_obj = $activity_obj->current_comment;
    16991786        }
    17001787
    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 ) {
     1788        if ( in_array( $activity_obj->type, array( 'new_blog_post', 'new_blog_comment', 'new_forum_topic', 'new_forum_post', 'activity_favorite' ) ) ) {
    17021789                $link = $activity_obj->primary_link;
    17031790        } else {
    17041791                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