Skip to:
Content

BuddyPress.org

Ticket #3794: 3794.04.patch

File 3794.04.patch, 30.0 KB (added by imath, 9 years ago)
  • src/bp-activity/bp-activity-functions.php

    diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php
    index 2ca8d9f..fb3cbaf 100644
    function bp_activity_get_user_favorites( $user_id = 0 ) { 
    668668                $user_id = bp_displayed_user_id();
    669669        }
    670670
    671         // Get favorites for user.
    672         $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
     671        // Check cache
     672        $favs = wp_cache_get( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     673
     674        if ( empty( $favs ) ) {
     675                // Get favorites for user.
     676                $favs = bp_relationships_get_object_ids( 'activity_favorites', $user_id );
     677
     678                // Set the cache
     679                wp_cache_set( 'bp_activity_user_' . $user_id . '_favorites', $favs, 'bp' );
     680        }
    673681
    674682        /**
    675683         * Filters the favorited activity items for a specified user.
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    710718                $user_id = bp_loggedin_user_id();
    711719        }
    712720
    713         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    714         if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {
     721        $my_favs = bp_activity_get_user_favorites( $user_id );
     722        if ( ! is_array( $my_favs ) ) {
    715723                $my_favs = array();
    716724        }
    717725
    function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
    720728                return false;
    721729        }
    722730
    723         // Add to user's favorites.
    724         $my_favs[] = $activity_id;
     731        if ( ! bp_relationships_add( 'activity_favorites', $user_id, $activity_id ) ) {
     732                return false;
     733        }
     734
     735        // Reset the cache
     736        wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     737
     738        // Update the total number of activities the user favorites
     739        $my_favs_count = (int) bp_get_user_meta( $user_id, 'bp_activity_favorites_count', true );
     740        bp_update_user_meta( $user_id, 'bp_activity_favorites_count', $my_favs_count + 1 );
    725741
    726742        // Update the total number of users who have favorited this activity.
    727743        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    728744        $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
    729745
    730         // Update user meta.
    731         bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
    732 
    733746        // Update activity meta counts.
    734747        if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
    735748
    function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 
    791804                $user_id = bp_loggedin_user_id();
    792805        }
    793806
    794         $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    795         $my_favs = array_flip( (array) $my_favs );
    796 
    797         // Bail if the user has not previously favorited the item.
    798         if ( ! isset( $my_favs[ $activity_id ] ) ) {
     807        // Bail if favorites could not be removed for the user.
     808        if ( ! bp_relationships_remove_by_object_ids( 'activity_favorites', $activity_id, $user_id ) ) {
    799809                return false;
    800810        }
    801811
    802         // Remove the fav from the user's favs.
    803         unset( $my_favs[$activity_id] );
    804         $my_favs = array_unique( array_flip( $my_favs ) );
     812        // Reset the cache
     813        wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     814
     815        // Update the total number of activities the user favorites
     816        $my_favs_count = (int) bp_get_user_meta( $user_id, 'bp_activity_favorites_count', true );
     817
     818        if ( ! empty( $my_favs_count ) ) {
     819                bp_update_user_meta( $user_id, 'bp_activity_favorites_count', $my_favs_count - 1 );
     820        }
    805821
    806822        // Update the total number of users who have favorited this activity.
    807823        $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
     824
    808825        if ( ! empty( $fav_count ) ) {
    809826
    810827                // Deduct from total favorites.
    811828                if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {
    812829
    813                         // Update users favorites.
    814                         if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
    815 
    816                                 /**
    817                                  * Fires if bp_update_user_meta() is successful and before returning a true value for success.
    818                                  *
    819                                  * @since 1.2.1
    820                                  *
    821                                  * @param int $activity_id ID of the activity item being unfavorited.
    822                                  * @param int $user_id     ID of the user doing the unfavoriting.
    823                                  */
    824                                 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
    825 
    826                                 // Success.
    827                                 return true;
    828 
    829                         // Error updating.
    830                         } else {
    831                                 return false;
    832                         }
     830                        /**
     831                         * Fires if bp_update_user_meta() is successful and before returning a true value for success.
     832                         *
     833                         * @since 1.2.1
     834                         *
     835                         * @param int $activity_id ID of the activity item being unfavorited.
     836                         * @param int $user_id     ID of the user doing the unfavoriting.
     837                         */
     838                        do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     839
     840                        // Success.
     841                        return true;
    833842
    834843                // Error updating favorite count.
    835844                } else {
    function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 
    843852}
    844853
    845854/**
     855 * Remove favorites and update Users favorites count
     856 *
     857 * @since 2.5.0
     858 *
     859 * @param array|int $deleted List of deleted activities or the deleted activity ID
     860 */
     861function bp_activity_remove_user_favorites( $deleted ) {
     862        // Get the users to update favorites count for
     863        $user_ids = bp_relationships_get_item_ids( 'activity_favorites', $deleted );
     864
     865        // No users favorited one of the deleted activities
     866        if ( empty( $user_ids ) ) {
     867                return;
     868        }
     869
     870        $user_ids = array_unique( $user_ids );
     871
     872        // Remove favorites
     873        bp_relationships_remove_by_object_ids( 'activity_favorites', $deleted );
     874
     875        // Update Users favorites count
     876        foreach ( $user_ids as $user_id ) {
     877                // Reset the cache for the user
     878                wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' );
     879
     880                // Get the favorites for the user
     881                $favs = bp_activity_get_user_favorites( $user_id );
     882
     883                // Update the user's count
     884                bp_update_user_meta( $user_id, 'bp_activity_favorites_count', count( $favs ) );
     885        }
     886}
     887
     888/**
    846889 * Check whether an activity item exists with a given content string.
    847890 *
    848891 * @since 1.1.0
    function bp_activity_remove_all_user_data( $user_id = 0 ) { 
    10551098        // Clear the user's activity from the sitewide stream and clear their activity tables.
    10561099        bp_activity_delete( array( 'user_id' => $user_id ) );
    10571100
     1101        // Remove Favorites
     1102        bp_relationships_remove_by_item_ids( 'activity_favorites', $user_id );
     1103
    10581104        // Remove any usermeta.
    1059         bp_delete_user_meta( $user_id, 'bp_latest_update'       );
    1060         bp_delete_user_meta( $user_id, 'bp_favorite_activities' );
     1105        bp_delete_user_meta( $user_id, 'bp_latest_update'            );
     1106        bp_delete_user_meta( $user_id, 'bp_activity_favorites_count' );
    10611107
    10621108        // Execute additional code
    10631109        do_action( 'bp_activity_remove_data', $user_id ); // Deprecated! Do not use!
    function bp_activity_delete( $args = '' ) { 
    22792325                return false;
    22802326        }
    22812327
     2328        // Remove favorites and Update User count
     2329        bp_activity_remove_user_favorites( $activity_ids_deleted );
     2330
    22822331        // Check if the user's latest update has been deleted.
    22832332        $user_id = empty( $args['user_id'] )
    22842333                ? bp_loggedin_user_id()
  • src/bp-activity/bp-activity-loader.php

    diff --git src/bp-activity/bp-activity-loader.php src/bp-activity/bp-activity-loader.php
    index 3dfd0c1..646951a 100644
    class BP_Activity_Component extends BP_Component { 
    117117                        'search_string'         => __( 'Search Activity...', 'buddypress' ),
    118118                        'global_tables'         => $global_tables,
    119119                        'meta_tables'           => $meta_tables,
     120                        'relationship_types'    => array( 'activity_favorites' ),
    120121                );
    121122
    122123                parent::setup_globals( $args );
  • src/bp-activity/bp-activity-template.php

    diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php
    index 89abc6a..21e2969 100644
    class BP_Activity_Template { 
    290290                $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' );
    291291
    292292                // Get an array of the logged in user's favorite activities.
    293                 $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) );
     293                $this->my_favs = bp_activity_get_user_favorites( bp_loggedin_user_id() );
    294294
    295295                // Fetch specific activity items based on ID's.
    296296                if ( !empty( $include ) ) {
  • src/bp-activity/classes/class-bp-activity-activity.php

    diff --git src/bp-activity/classes/class-bp-activity-activity.php src/bp-activity/classes/class-bp-activity-activity.php
    index 629c439..8b280b0 100644
    class BP_Activity_Activity { 
    17011701         */
    17021702        public static function total_favorite_count( $user_id ) {
    17031703
    1704                 // Get activities from user meta.
    1705                 $favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    1706                 if ( ! empty( $favorite_activity_entries ) ) {
    1707                         return count( maybe_unserialize( $favorite_activity_entries ) );
    1708                 }
    1709 
    1710                 // No favorites.
    1711                 return 0;
     1704                // Get favorites count from user meta.
     1705                return (int) bp_get_user_meta( $user_id, 'bp_activity_favorites_count', true );
    17121706        }
    17131707
    17141708        /**
  • src/bp-core/admin/bp-core-admin-schema.php

    diff --git src/bp-core/admin/bp-core-admin-schema.php src/bp-core/admin/bp-core-admin-schema.php
    index bc86ddd..0832814 100644
    function bp_core_install( $active_components = false ) { 
    5454        // Install the signups table.
    5555        bp_core_maybe_install_signups();
    5656
     57        // Install Object Relationships
     58        bp_core_install_object_relationships();
     59
    5760        // Notifications.
    5861        if ( !empty( $active_components['notifications'] ) ) {
    5962                bp_core_install_notifications();
    function bp_core_upgrade_signups() { 
    526529        $wpdb->query( "ALTER TABLE {$wpdb->signups} ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
    527530        $wpdb->query( "ALTER TABLE {$wpdb->signups} DROP INDEX domain" );
    528531}
     532
     533/**
     534 * Install the object relationships table.
     535 *
     536 * @since 2.5.0
     537 */
     538function bp_core_install_object_relationships() {
     539        $sql             = array();
     540        $charset_collate = bp_core_set_charset();
     541        $bp_prefix       = bp_core_get_table_prefix();
     542
     543        $sql = array( "CREATE TABLE {$bp_prefix}bp_object_relationships (
     544                                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     545                                type varchar(75) NOT NULL,
     546                                item_id bigint(20) NOT NULL,
     547                                object_id bigint(20) NOT NULL,
     548                                KEY type (type),
     549                                KEY item_id (item_id),
     550                                KEY object_id (object_id)
     551                        ) {$charset_collate};" );
     552
     553        dbDelta( $sql );
     554}
  • src/bp-core/bp-core-classes.php

    diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php
    index d2801ea..90656d3 100644
    require dirname( __FILE__ ) . '/classes/class-bp-media-extractor.php'; 
    2525require dirname( __FILE__ ) . '/classes/class-bp-attachment.php';
    2626require dirname( __FILE__ ) . '/classes/class-bp-attachment-avatar.php';
    2727require dirname( __FILE__ ) . '/classes/class-bp-attachment-cover-image.php';
     28require dirname( __FILE__ ) . '/classes/class-bp-object-relationships.php';
  • src/bp-core/bp-core-component.php

    diff --git src/bp-core/bp-core-component.php src/bp-core/bp-core-component.php
    index 86fe8ae..a434bd9 100644
    class BP_Component { 
    301301                        $this->register_meta_tables( $r['meta_tables'] );
    302302                }
    303303
     304                // Set the specific relationship types for the component
     305                if ( ! empty( $r['relationship_types'] ) ) {
     306                        bp_relationships_register_types( $this->id, $r['relationship_types'] );
     307                }
     308
    304309                /** BuddyPress *******************************************************
    305310                 */
    306311
  • src/bp-core/bp-core-functions.php

    diff --git src/bp-core/bp-core-functions.php src/bp-core/bp-core-functions.php
    index 69a914e..0a60c2b 100644
    function bp_upload_dir() { 
    25912591
    25922592        return $bp->upload_dir;
    25932593}
     2594
     2595/** Object Relationships ******************************************************/
     2596
     2597/**
     2598 * Register the relationship types for the component.
     2599 *
     2600 * @since  2.5.0
     2601 *
     2602 * @param  string $component The component ID (eg: activity).
     2603 * @param  array  $types     The list of relationships type to add.
     2604 */
     2605function bp_relationships_register_types( $component, $types = array() ) {
     2606        return buddypress()->object_relationships->register_types( $component, $types );
     2607}
     2608
     2609/**
     2610 * Make sure a relationship is valid and active.
     2611 *
     2612 * @since  2.5.0
     2613 *
     2614 * @param  string $type The type to check.
     2615 * @return bool         True if valid, false otherwise.
     2616 */
     2617function bp_relationships_is_active_type( $type = '' ) {
     2618        $relationship_types = buddypress()->object_relationships->types;
     2619
     2620        return ! empty( buddypress()->object_relationships->types[ $type ] );
     2621}
     2622
     2623/**
     2624 * Add a new relationship between 2 objects
     2625 *
     2626 * @since  2.5.0
     2627 *
     2628 * @param  string  $type      The name of the relationship (eg: activity_favorites)
     2629 * @param  int     $item_id   The ID of the first object
     2630 * @param  int     $object_id The ID of the second object
     2631 * @return bool               True if the relationships was successfuly created. False otherwise.
     2632 */
     2633function bp_relationships_add( $type = '', $item_id = 0, $object_id = 0 ) {
     2634        return buddypress()->object_relationships->add( array(
     2635                'type'      => sanitize_key( $type ),
     2636                'item_id'   => (int) $item_id,
     2637                'object_id' => (int) $object_id,
     2638        ) );
     2639}
     2640
     2641/**
     2642 * Get The item ids for the requested object_ids
     2643 *
     2644 * @since  2.5.0
     2645 *
     2646 * @param  string           $type       The name of the relationship (eg: activity_favorites).
     2647 * @param  int|array|string $object_ids An object ID or a list of object IDs.
     2648 * @return array            the list of item IDs.
     2649 */
     2650function bp_relationships_get_item_ids( $type = '', $object_ids ) {
     2651        return buddypress()->object_relationships->get( array(
     2652                'type'  => sanitize_key( $type ),
     2653                'in'    => $object_ids,
     2654        ) );
     2655}
     2656
     2657/**
     2658 * Get The object ids for the requested item_ids
     2659 *
     2660 * @since  2.5.0
     2661 *
     2662 * @param  string           $type       The name of the relationship (eg: activity_favorites).
     2663 * @param  int|array|string $item_ids   An item ID or a list of item IDs.
     2664 * @return array            the list of object IDs.
     2665 */
     2666function bp_relationships_get_object_ids( $type = '', $item_ids ) {
     2667        return buddypress()->object_relationships->get( array(
     2668                'type'  => sanitize_key( $type ),
     2669                'field' => 'object_id',
     2670                'in'    => $item_ids,
     2671        ) );
     2672}
     2673
     2674/**
     2675 * Remove relationships using item ids for a given object ID (or all)
     2676 *
     2677 * @since  2.5.0
     2678 *
     2679 * @param  string            $type      The name of the relationship (eg: activity_favorites).
     2680 * @param  int|array|string  $item_ids  An item ID or a list of item IDs.
     2681 * @param  int               $object_id 0 to remove for all, a specific value to restrict.
     2682 * @return bool              True if the relationships was successfuly deleted. False otherwise.
     2683 */
     2684function bp_relationships_remove_by_item_ids( $type = '', $item_ids, $object_id = 0 ) {
     2685        return buddypress()->object_relationships->remove( array(
     2686                'type'  => sanitize_key( $type ),
     2687                'field' => 'object_id',
     2688                'in'    => $item_ids,
     2689                'for'   => (int) $object_id,
     2690        ) );
     2691}
     2692
     2693/**
     2694 * Remove relationships using object ids for a given item ID (or all)
     2695 *
     2696 * @since  2.5.0
     2697 *
     2698 * @param  string            $type        The name of the relationship (eg: activity_favorites).
     2699 * @param  int|array|string  $object_ids  An object ID or a list of object IDs.
     2700 * @param  int               $item_id     0 to remove for all, a specific value to restrict.
     2701 * @return bool              True if the relationships was successfuly deleted. False otherwise.
     2702 */
     2703function bp_relationships_remove_by_object_ids( $type = '', $object_ids, $item_id = 0 ) {
     2704        return buddypress()->object_relationships->remove( array(
     2705                'type' => sanitize_key( $type ),
     2706                'in'   => $object_ids,
     2707                'for'  => (int) $item_id,
     2708        ) );
     2709}
  • src/bp-core/bp-core-loader.php

    diff --git src/bp-core/bp-core-loader.php src/bp-core/bp-core-loader.php
    index 80cd4f6..5e925be 100644
    class BP_Core extends BP_Component { 
    250250                // bp-notifications instead.
    251251                $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications';
    252252
     253                // Init the object relationships
     254                $bp->object_relationships = new BP_Object_Relationships;
     255
    253256                /**
    254257                 * Used to determine if user has admin rights on current content. If the
    255258                 * logged in user is viewing their own profile and wants to delete
  • src/bp-core/bp-core-update.php

    diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php
    index a85c41e..0f9c2cf 100644
    function bp_version_updater() { 
    260260                if ( $raw_db_version < 9615 ) {
    261261                        bp_update_to_2_3();
    262262                }
     263
     264                // 2.5.0
     265                if ( $raw_db_version < 10449 ) {
     266                        bp_update_to_2_5();
     267                }
    263268        }
    264269
    265270        /** All done! *************************************************************/
    function bp_update_to_2_3() { 
    484489}
    485490
    486491/**
     492 * 2.5.0 update routine.
     493 *
     494 * - Add relationships table.
     495 *
     496 * @since 2.5.0
     497 */
     498function bp_update_to_2_5() {
     499        // Install Object Relationships
     500        bp_core_install_object_relationships();
     501
     502        // Migrate Member Favorites
     503        bp_migrate_activity_favorites();
     504}
     505
     506/**
    487507 * Updates the component field for new_members type.
    488508 *
    489509 * @since 2.2.0
    function bp_cleanup_friendship_activities() { 
    535555}
    536556
    537557/**
     558 * Migrate the Members favorites
     559 *
     560 * @since  2.5.0
     561 */
     562function bp_migrate_activity_favorites() {
     563        global $wpdb;
     564
     565        $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" );
     566
     567        foreach( $user_favorites as $user_favorite ) {
     568                $activity_ids = maybe_unserialize( $user_favorite->meta_value );
     569
     570                if ( empty( $activity_ids ) ) {
     571                        continue;
     572                }
     573
     574                // Validate Favorites
     575                $favorites = bp_activity_get_specific( array( 'activity_ids' => $activity_ids, 'show_hidden' => true ) );
     576
     577                if ( empty( $favorites['activities'] ) ) {
     578                        continue;
     579                }
     580
     581                // Update Count
     582                bp_update_user_meta( $user_favorite->user_id, 'bp_activity_favorites_count', count( $favorites['activities'] ) );
     583
     584                // Remove no more used meta
     585                bp_delete_user_meta( $user_favorite->user_id, 'bp_favorite_activities' );
     586
     587                foreach ( $favorites['activities'] as $activity ) {
     588                        bp_relationships_add( 'activity_favorites', $user_favorite->user_id, $activity->id );
     589                }
     590
     591        }
     592}
     593
     594/**
    538595 * Redirect user to BP's What's New page on first page load after activation.
    539596 *
    540597 * @since 1.7.0
  • src/bp-core/classes/class-bp-object-relationships.php

    diff --git src/bp-core/classes/class-bp-object-relationships.php src/bp-core/classes/class-bp-object-relationships.php
    index e69de29..4026a48 100644
     
     1<?php
     2/**
     3 * Core component classes.
     4 *
     5 * @package BuddyPress
     6 * @subpackage Core
     7 * @since 2.5.0
     8 */
     9
     10// Exit if accessed directly.
     11defined( 'ABSPATH' ) || exit;
     12
     13/**
     14 * BuddyPress objects relationship class.
     15 *
     16 * @since 2.5.0
     17 */
     18final class BP_Object_Relationships {
     19        /**
     20         * Name of the relationships table.
     21         *
     22         * @var string
     23         */
     24        public $table_name = '';
     25
     26        /**
     27         * Available relationship types.
     28         *
     29         * @var array
     30         */
     31        public $types = array();
     32
     33        /**
     34         * Constructor
     35         *
     36         * @since 2.5.0
     37         */
     38        public function __construct() {
     39                $this->table_name = bp_core_get_table_prefix() . 'bp_object_relationships';
     40        }
     41
     42        /**
     43         * Register relationship types, making sure they are unique
     44         *
     45         * @since 2.5.0
     46         *
     47         * @param string $component The component ID
     48         * @param array  $types     The list of relationships type to add
     49         */
     50        public function register_types( $component, $types = array() ) {
     51                foreach ( (array) $types as $type ) {
     52                        $type = sanitize_key( $type );
     53
     54                        if ( ! isset( $this->types[ $type ] ) ) {
     55                                $this->types[ $type ] = bp_is_active( $component );
     56                        }
     57                }
     58        }
     59
     60        /**
     61         * Add a relationship.
     62         *
     63         * @since 2.5.0
     64         *
     65         * @param array $args.
     66         * @return bool True on success, false otherwise
     67         */
     68        public function add( $args = array() ) {
     69                global $wpdb;
     70
     71                $r = array_intersect_key( $args, array(
     72                        'type'      => '',
     73                        'item_id'   => 0,
     74                        'object_id' => 0,
     75                ) );
     76
     77                if ( ! bp_relationships_is_active_type( $r['type'] ) || empty( $r['item_id'] ) || empty( $r['object_id'] ) ) {
     78                        return false;
     79                }
     80
     81                $added = $wpdb->insert(
     82                        $this->table_name,
     83                        $r,
     84                        array( '%s', '%d', '%d' )
     85                );
     86
     87                return (bool) $added;
     88        }
     89
     90        /**
     91         * Validate the query arguments
     92         *
     93         * @since 2.5.0
     94         *
     95         * @param  array      $args the query arguments
     96         * @return bool|array Validated query arguments or false.
     97         */
     98        public function validate_query_args( $args = array() ) {
     99                $r = wp_parse_args( $args, array(
     100                        'type'    => '',
     101                        'field'   => 'item_id',
     102                        'in'      => array(),
     103                ) );
     104
     105                if ( ( 'item_id' !== $r['field'] && 'object_id' !== $r['field'] ) || ! bp_relationships_is_active_type( $r['type'] ) || empty( $r['in'] ) ) {
     106                        return false;
     107                }
     108
     109                $r['in_field'] = 'object_id';
     110
     111                if ( $r['field'] !== 'item_id' ) {
     112                        $r['in_field'] = 'item_id';
     113                }
     114
     115                return $r;
     116        }
     117
     118        /**
     119         * Set the IN clause for a query
     120         *
     121         * @since 2.5.0
     122         *
     123         * @param  string $in_field Name of the field
     124         * @param  array  $in List of object/item IDs
     125         * @return string The where clause.
     126         */
     127        public function set_in( $in_field = '', $in = array() ) {
     128                global $wpdb;
     129
     130                $in = wp_parse_id_list( $in );
     131
     132                if ( 1 === count( $in ) ) {
     133                        return $wpdb->prepare( "{$in_field} = %d", reset( $in ) );
     134                } else {
     135                        $in = join( ',', $in );
     136                        return "{$in_field} IN ({$in})";
     137                }
     138        }
     139
     140        /**
     141         * Get item ids for a given object id or vice versa
     142         *
     143         * @since 2.5.0
     144         *
     145         * @param  array $args.
     146         * @return array List of item ids or object ids
     147         */
     148        public function get( $args = array() ) {
     149                global $wpdb;
     150
     151                $r = $this->validate_query_args( $args );
     152
     153                if ( ! is_array( $r ) ) {
     154                        return false;
     155                }
     156
     157                $sql = array(
     158                        'select' => sprintf( 'SELECT %s FROM %s', $r['field'], $this->table_name ),
     159                        'where'  => array(
     160                                'type' => $wpdb->prepare( 'type = %s', $r['type'] ),
     161                                'in'   => $this->set_in( $r['in_field'], $r['in'] ),
     162                        ),
     163                );
     164
     165                $sql['where'] = 'WHERE ' . join( ' AND ', $sql['where'] );
     166
     167                return $wpdb->get_col( join( ' ', $sql ) );
     168        }
     169
     170        /**
     171         * Remove item ids for a given object id or vice versa
     172         *
     173         * @since 2.5.0
     174         *
     175         * @param  array $args.
     176         * @return array List of item ids or object ids
     177         */
     178        public function remove( $args = array() ) {
     179                global $wpdb;
     180
     181                $r = $this->validate_query_args( $args );
     182
     183                if ( ! is_array( $r ) ) {
     184                        return false;
     185                }
     186
     187                $sql = array(
     188                        'select' => sprintf( 'DELETE FROM %s', $this->table_name ),
     189                        'where'  => array(
     190                                'type' => $wpdb->prepare( 'type = %s', $r['type'] ),
     191                                'in'   => $this->set_in( $r['in_field'], $r['in'] ),
     192                        ),
     193                );
     194
     195                if ( ! empty( $r['for'] ) ) {
     196                        $sql['where']['for'] = $wpdb->prepare( "{$r['field']} = %d", $r['for'] );
     197                }
     198
     199                $sql['where'] = 'WHERE ' . join( ' AND ', $sql['where'] );
     200
     201                return (bool) $wpdb->query( join( ' ', $sql ) );
     202        }
     203}
  • src/bp-loader.php

    diff --git src/bp-loader.php src/bp-loader.php
    index 77d5408..d74f986 100644
    class BuddyPress { 
    328328                /** Versions **********************************************************/
    329329
    330330                $this->version    = '2.5.0-alpha';
    331                 $this->db_version = 10071;
     331                $this->db_version = 10449;
    332332
    333333                /** Loading ***********************************************************/
    334334
  • tests/phpunit/testcases/activity/functions.php

    diff --git tests/phpunit/testcases/activity/functions.php tests/phpunit/testcases/activity/functions.php
    index 8a3304f..0144a2f 100644
    Bar!'; 
    13161316                $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
    13171317
    13181318                $this->assertFalse( bp_activity_add_user_favorite( $a, $u ) );
    1319                 $this->assertSame( array( $a ), bp_activity_get_user_favorites( $u ) );
     1319                $this->assertEquals( array( $a ), bp_activity_get_user_favorites( $u ) );
     1320                $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     1321
     1322                $this->set_current_user( $current_user );
     1323        }
     1324
     1325        /**
     1326         * @group favorites
     1327         * @group bp_activity_remove_user_favorite
     1328         */
     1329        public function test_remove_user_favorite_bad_activity_id() {
     1330                $u1 = $this->factory->user->create();
     1331                $u2 = $this->factory->user->create();
     1332                $a = $this->factory->activity->create();
     1333
     1334                // bp_activity_add_user_favorite() requires a logged-in user.
     1335                $current_user = bp_loggedin_user_id();
     1336                $this->set_current_user( $u1 );
     1337
     1338                // Only favorite for user 1
     1339                bp_activity_add_user_favorite( $a, $u1 );
     1340
     1341                $this->set_current_user( $u2 );
     1342
     1343                // Removing for user 2 should fail
     1344                $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
    13201345                $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
    13211346
    13221347                $this->set_current_user( $current_user );
    Bar!'; 
    13261351         * @group favorites
    13271352         * @group bp_activity_add_user_favorite
    13281353         */
    1329         public function test_add_user_favorite_not_yet_favorited() {
     1354        public function test_add_user_favorite_empty_activity_id() {
    13301355                $u = $this->factory->user->create();
    1331                 $a = $this->factory->activity->create();
    13321356
    13331357                // bp_activity_add_user_favorite() requires a logged-in user.
    13341358                $current_user = bp_loggedin_user_id();
    13351359                $this->set_current_user( $u );
    1336                 $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
     1360
     1361                // an empty activity id should not be favorited
     1362                $this->assertFalse( bp_activity_add_user_favorite( 0, $u ) );
    13371363
    13381364                $this->set_current_user( $current_user );
    13391365        }
    Bar!'; 
    13421368         * @group favorites
    13431369         * @group bp_activity_remove_user_favorite
    13441370         */
    1345         public function test_remove_user_favorite_bad_activity_id() {
     1371        public function test_remove_user_favorite_good_activity_id() {
     1372                $u = $this->factory->user->create();
     1373                $a = $this->factory->activity->create();
     1374
     1375                // bp_activity_add_user_favorite() requires a logged-in user.
     1376                $current_user = bp_loggedin_user_id();
     1377                $this->set_current_user( $u );
     1378
     1379                bp_activity_add_user_favorite( $a, $u );
     1380
     1381                // Removing the favorite
     1382                $this->assertTrue( bp_activity_remove_user_favorite( $a, $u ) );
     1383                $this->assertEquals( 0, bp_activity_get_meta( $a, 'favorite_count' ) );
     1384
     1385                $this->set_current_user( $current_user );
     1386        }
     1387
     1388        /**
     1389         * @group favorites
     1390         * @group bp_activity_get_user_favorites()
     1391         */
     1392        public function test_bp_activity_get_user_favorites() {
     1393                $u = $this->factory->user->create();
     1394
     1395                $activities = array(
     1396                        $this->factory->activity->create(),
     1397                        $this->factory->activity->create(),
     1398                        $this->factory->activity->create()
     1399                );
     1400
     1401                // bp_activity_add_user_favorite() requires a logged-in user.
     1402                $current_user = bp_loggedin_user_id();
     1403                $this->set_current_user( $u );
     1404
     1405                // Only favorite last 2 elements of the array
     1406                bp_activity_add_user_favorite( $activities[1], $u );
     1407                bp_activity_add_user_favorite( $activities[2], $u );
     1408
     1409                array_shift( $activities );
     1410
     1411                $this->assertEquals( $activities, bp_activity_get_user_favorites( $u ) );
     1412
     1413                $this->set_current_user( $current_user );
     1414        }
     1415
     1416        /**
     1417         * @group favorites
     1418         * @group bp_activity_total_favorites_for_user
     1419         */
     1420        public function test_bp_activity_total_favorites_for_user() {
    13461421                $u1 = $this->factory->user->create();
    13471422                $u2 = $this->factory->user->create();
    1348                 $a = $this->factory->activity->create();
     1423
     1424                $activities = array(
     1425                        $this->factory->activity->create(),
     1426                        $this->factory->activity->create()
     1427                );
    13491428
    13501429                // bp_activity_add_user_favorite() requires a logged-in user.
    13511430                $current_user = bp_loggedin_user_id();
    13521431                $this->set_current_user( $u1 );
    13531432
    1354                 // Only favorite for user 1
    1355                 bp_activity_add_user_favorite( $a, $u1 );
     1433                // Only add favorites to user 1
     1434                bp_activity_add_user_favorite( $activities[0], $u1 );
     1435                bp_activity_add_user_favorite( $activities[1], $u1 );
    13561436
    1357                 // Removing for user 2 should fail
    1358                 $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
    1359                 $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     1437                $this->assertEquals( 2, bp_activity_total_favorites_for_user( $u1 ) );
     1438
     1439                $this->set_current_user( $u2 );
     1440
     1441                $this->assertEquals( 0, bp_activity_total_favorites_for_user( $u2 ) );
    13601442
    13611443                $this->set_current_user( $current_user );
    13621444        }
    13631445
    13641446        /**
     1447         * @group favorites
     1448         * @group bp_activity_total_favorites_for_user
     1449         * @group bp_activity_remove_user_favorites
     1450         */
     1451        public function test_count_user_favorite_after_activity_removed() {
     1452                $u1 = $this->factory->user->create();
     1453                $u2 = $this->factory->user->create();
     1454                $a1 = $this->factory->activity->create();
     1455                $a2 = $this->factory->activity->create();
     1456
     1457                // bp_activity_add_user_favorite() requires a logged-in user.
     1458                $current_user = bp_loggedin_user_id();
     1459                $this->set_current_user( $u1 );
     1460
     1461                // favorite activity
     1462                bp_activity_add_user_favorite( $a1, $u1 );
     1463
     1464                $this->set_current_user( $u2 );
     1465
     1466                // favorite activities
     1467                bp_activity_add_user_favorite( $a1, $u2 );
     1468                bp_activity_add_user_favorite( $a2, $u2 );
     1469
     1470                // Delete the activity
     1471                bp_activity_delete_by_activity_id( $a1 );
     1472
     1473                $this->assertEquals( 0, bp_activity_total_favorites_for_user( $u1 ) );
     1474                $this->assertEquals( 1, bp_activity_total_favorites_for_user( $u2 ) );
     1475
     1476                $this->set_current_user( $current_user );
     1477        }
     1478
     1479        /**
     1480         * @group favorites
     1481         * @group bp_activity_favorites_migrate
     1482         */
     1483        public function test_bp_activity_favorites_migrate() {
     1484                $u1 = $this->factory->user->create();
     1485                $u2 = $this->factory->user->create();
     1486                $activities = $this->factory->activity->create_many( 10 );
     1487
     1488                $user_meta_favorites = array( $u1 => array(), $u2 => array() );
     1489
     1490                foreach ( $activities as $activity_id ) {
     1491                        $modulo = $activity_id % 2;
     1492
     1493                        if ( empty( $modulo ) ) {
     1494                                $user_meta_favorites[ $u2 ][] = $activity_id;
     1495                        } else {
     1496                                $user_meta_favorites[ $u1 ][] = $activity_id;
     1497                        }
     1498                }
     1499
     1500                bp_update_user_meta( $u1, 'bp_favorite_activities', $user_meta_favorites[ $u1 ] );
     1501                bp_update_user_meta( $u2, 'bp_favorite_activities', $user_meta_favorites[ $u2 ] );
     1502
     1503                // migrate
     1504                bp_migrate_activity_favorites();
     1505
     1506                $migrated = array(
     1507                        $u1 => bp_activity_get_user_favorites( $u1 ),
     1508                        $u2 => bp_activity_get_user_favorites( $u2 )
     1509                );
     1510
     1511                $this->assertEquals( sort( $user_meta_favorites[ $u1 ] ), sort( $migrated[ $u1 ] ) );
     1512                $this->assertEquals( sort( $user_meta_favorites[ $u2 ] ), sort( $migrated[ $u2 ] ) );
     1513        }
     1514
     1515        /**
    13651516         * @group bp_activity_post_update
    13661517         */
    13671518        public function test_bp_activity_post_update_empty_content() {