Skip to:
Content

BuddyPress.org

Ticket #6025: 6025.02.patch

File 6025.02.patch, 19.3 KB (added by dcavins, 12 years ago)

Add groups_send_invite_by_invitee

  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index c83e743..de8da3d 100644
    function groups_delete_invite( $user_id, $group_id ) {  
    10181018/**
    10191019 * Send all pending invites by a single user to a specific group.
    10201020 *
    1021  * @param int $user_id ID of the inviting user.
     1021 * @param int $inviter_id ID of the inviting user.
    10221022 * @param int $group_id ID of the group.
     1023 * @return bool False if there's actually a problem, true if the invitations that need to be sent are sent successfully (or if there's nothing to do).
    10231024 */
    1024 function groups_send_invites( $user_id, $group_id ) {
     1025function groups_send_invites( $inviter_id, $group_id ) {
    10251026
    1026         if ( empty( $user_id ) )
    1027                 $user_id = bp_loggedin_user_id();
     1027        if ( empty( $inviter_id ) ) {
     1028                $inviter_id = bp_loggedin_user_id();
     1029        }
     1030        if ( empty( $group_id ) ) {
     1031                $group_id = bp_get_current_group_id();
     1032        }
    10281033
    1029         // Send friend invites.
    1030         $invited_users = groups_get_invites_for_group( $user_id, $group_id );
    1031         $group = groups_get_group( array( 'group_id' => $group_id ) );
     1034        // If the inviter can't extend invitations to this group, bail.
     1035        if ( ! bp_groups_user_can_send_invites( $group_id, $inviter_id ) ) {
     1036                return false;
     1037        }
    10321038
    1033         for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) {
    1034                 $member = new BP_Groups_Member( $invited_users[$i], $group_id );
     1039        // Get outstanding invites to the group from this user.
     1040        $invited_users = groups_get_invites_for_group( $inviter_id, $group_id );
     1041        $success = 0;
    10351042
    1036                 // Send the actual invite
    1037                 groups_notification_group_invites( $group, $member, $user_id );
     1043        foreach ( $invited_users as $invitee_id ) {
     1044                if ( groups_send_invite_by_invitee( $group_id, $invitee_id, $inviter_id, $resend = false ) ) {
     1045                        $success++;
     1046                }
     1047        }
    10381048
    1039                 $member->invite_sent = 1;
    1040                 $member->save();
     1049        // If all invitations were successfully sent, return success.
     1050        if ( count( $invited_users ) == $success ) {
     1051                return true;
     1052        } else {
     1053                return false;
    10411054        }
    10421055
    1043         do_action( 'groups_send_invites', $group_id, $invited_users );
     1056}
     1057
     1058/**
     1059 * Send specific pending group invitations keyed by group and invitee id
     1060 *
     1061 * @param int   $group_id       ID of the group.
     1062 * @param int   $invitee_id ID of the invited user.
     1063 * @param int   $inviter_id ID of the user who created the invitation.
     1064 * @param bool  $resend         Whether to resend notification email if already sent.
     1065 *
     1066 * @since BuddyPress (2.2.0)
     1067 */
     1068function groups_send_invite_by_invitee( $group_id, $invitee_id, $inviter_id, $resend = false ) {
     1069
     1070        if ( empty( $group_id ) || empty( $invitee_id ) || empty( $inviter_id ) ) {
     1071                return false;
     1072        }
     1073
     1074        // Check that an invitation exists for this user/group combination.
     1075        // $type = 'all' checks for sent or unsent invitations.
     1076        // @TODO: If #5911 is adopted, add $inviter_id argument.
     1077        if ( ! groups_check_user_has_invite( $invitee_id, $group_id, $type = 'all' ) ) {
     1078                return false;
     1079        }
     1080
     1081        $group = groups_get_group( array( 'group_id' => $group_id ) );
     1082        $member = new BP_Groups_Member( $invitee_id, $group_id );
     1083
     1084        // Send the invitation email.
     1085        $sent = groups_notification_group_invites( $group, $member, $inviter_id, $resend );
     1086        // Save the invite_sent property if applicable.
     1087        $saved = false;
     1088
     1089        if ( $sent ) {
     1090                if ( ! empty( $member->invite_sent ) ) {
     1091                        // Nothing to update
     1092                        $saved = true;
     1093                } else {
     1094                        $member->invite_sent = 1;
     1095                        $saved = $member->save();
     1096                }
     1097        }
     1098
     1099        do_action( 'groups_send_invites', $group_id, array( $invitee_id ) );
     1100
     1101        $success = ( $sent && $saved ) ? true : false;
     1102        return $success;
    10441103}
    10451104
    10461105function groups_get_invites_for_group( $user_id, $group_id ) {
  • src/bp-groups/bp-groups-notifications.php

    diff --git src/bp-groups/bp-groups-notifications.php src/bp-groups/bp-groups-notifications.php
    index c4b1a48..465c72b 100644
    add_action( 'groups_promoted_member', 'groups_notification_promoted_member', 10,  
    296296 *
    297297 * @since BuddyPress (1.0.0)
    298298 *
    299  * @param BP_Groups_Group $group Group object.
    300  * @param BP_Groups_Member $member Member object.
    301  * @param int $inviter_user_id ID of the user who sent the invite.
    302  * @return bool|null False on failure.
     299 * @param       BP_Groups_Group  $group  Group object.
     300 * @param       BP_Groups_Member  $member  Member object.
     301 * @param       int  $inviter_user_id  ID of the user who sent the invite.
     302 * @param       bool  $resend  Whether to resend notification email if already sent.
     303 * @return      bool|null       False on failure.
    303304 */
    304 function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) {
     305function groups_notification_group_invites( &$group, &$member, $inviter_user_id, $resend = false ) {
    305306
    306         // Bail if member has already been invited
    307         if ( ! empty( $member->invite_sent ) ) {
    308                 return;
     307        // Bail if the group and member objects are not populated.
     308        if ( empty( $group->id ) || empty( $member->id ) ) {
     309                return false;
     310        }
     311
     312        // Bail if the passed $inviter_user_id isn't the inviter as recorded in the BP_Groups_Member object.
     313        // @TODO: This will be resolved by #5911's change to groups_check_user_has_invite()
     314        if ( $inviter_user_id != $member->inviter_id ) {
     315                return false;
     316        }
     317
     318        // Bail if member has already been invited and $resend isn't specified.
     319        if ( ! $resend && ! empty( $member->invite_sent ) ) {
     320                return true;
    309321        }
    310322
    311323        // @todo $inviter_ud may be used for caching, test without it
    … … To view %5$s\'s profile visit: %6$s  
    363375        $subject = apply_filters_ref_array( 'groups_notification_group_invites_subject', array( $subject, &$group ) );
    364376        $message = apply_filters_ref_array( 'groups_notification_group_invites_message', array( $message, &$group, $inviter_name, $inviter_link, $invites_link, $group_link, $settings_link ) );
    365377
    366         wp_mail( $to, $subject, $message );
     378        $success = wp_mail( $to, $subject, $message );
    367379
    368380        do_action( 'bp_groups_sent_invited_email', $invited_user_id, $subject, $message, $group );
     381
     382        return $success;
    369383}
    370384
    371385/** Notifications *************************************************************/
  • src/bp-groups/bp-groups-template.php

    diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php
    index d487aeb..64021a9 100644
    function bp_group_get_invite_status( $group_id = false ) {  
    18771877}
    18781878
    18791879/**
    1880  * Can the logged-in user send invitations in the specified group?
     1880 * Can the logged-in or specified user send invitations in the specified group?
    18811881 *
    18821882 * @since BuddyPress (1.5.0)
    18831883 *
    1884  * @param int $group_id Optional. The ID of the group whose status you want to
    1885  *        check. Default: the ID of the current group.
     1884 * @param int $group_id (optional) The ID of the group whose status you want to
     1885 *        check.
     1886 * @param int $user_id (optional) The ID of the user to check. Added BuddyPress (2.2.0)
    18861887 * @return bool $can_send_invites
    18871888 */
    1888 function bp_groups_user_can_send_invites( $group_id = false ) {
    1889         global $bp;
    1890 
     1889function bp_groups_user_can_send_invites( $group_id = false, $user_id = false ) {
    18911890        $can_send_invites = false;
    18921891        $invite_status    = false;
    18931892
    1894         if ( is_user_logged_in() ) {
    1895                 if ( bp_current_user_can( 'bp_moderate' ) ) {
     1893        // If $user_id isn't specified, we check against the logged-in user.
     1894        if ( ! $user_id ) {
     1895                $user_id = bp_loggedin_user_id();
     1896        }
     1897
     1898        if ( $user_id ) {
     1899                if ( user_can( $user_id, 'bp_moderate' ) ) {
    18961900                        // Super admins can always send invitations
    18971901                        $can_send_invites = true;
    1898 
    18991902                } else {
    1900                         // If no $group_id is provided, default to the current group id
    1901                         if ( !$group_id )
    1902                                 $group_id = isset( $bp->groups->current_group->id ) ? $bp->groups->current_group->id : 0;
    1903 
    1904                         // If no group has been found, bail
    1905                         if ( !$group_id )
    1906                                 return false;
    1907 
    1908                         $invite_status = bp_group_get_invite_status( $group_id );
    1909                         if ( !$invite_status )
    1910                                 return false;
    1911 
    1912                         switch ( $invite_status ) {
    1913                                 case 'admins' :
    1914                                         if ( groups_is_user_admin( bp_loggedin_user_id(), $group_id ) )
    1915                                                 $can_send_invites = true;
    1916                                         break;
    1917 
    1918                                 case 'mods' :
    1919                                         if ( groups_is_user_mod( bp_loggedin_user_id(), $group_id ) || groups_is_user_admin( bp_loggedin_user_id(), $group_id ) )
    1920                                                 $can_send_invites = true;
    1921                                         break;
    1922 
    1923                                 case 'members' :
    1924                                         if ( groups_is_user_member( bp_loggedin_user_id(), $group_id ) )
    1925                                                 $can_send_invites = true;
    1926                                         break;
     1903                        if ( ! $group_id ) {
     1904                                $group_id = bp_get_current_group_id();
     1905                        }
     1906                        if ( $group_id ) {
     1907                                $invite_status = bp_group_get_invite_status( $group_id );
     1908                                if ( $invite_status ) {
     1909                                        switch ( $invite_status ) {
     1910                                                case 'admins' :
     1911                                                        if ( groups_is_user_admin( $user_id, $group_id ) )
     1912                                                                $can_send_invites = true;
     1913                                                        break;
     1914
     1915                                                case 'mods' :
     1916                                                        if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) )
     1917                                                                $can_send_invites = true;
     1918                                                        break;
     1919
     1920                                                case 'members' :
     1921                                                        if ( groups_is_user_member( $user_id, $group_id ) )
     1922                                                                $can_send_invites = true;
     1923                                                        break;
     1924                                        }
     1925                                }
    19271926                        }
    19281927                }
    19291928        }
    19301929
    1931         return apply_filters( 'bp_groups_user_can_send_invites', $can_send_invites, $group_id, $invite_status );
     1930        return apply_filters( 'bp_groups_user_can_send_invites', $can_send_invites, $group_id, $invite_status, $user_id );
    19321931}
    19331932
    19341933/**
  • tests/phpunit/testcases/groups/class-bp-groups-member.php

    diff --git tests/phpunit/testcases/groups/class-bp-groups-member.php tests/phpunit/testcases/groups/class-bp-groups-member.php
    index 7d1aad3..4426240 100644
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {  
    1212                parent::tearDown();
    1313        }
    1414
    15         public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {
     15        public static function invite_user_to_group( $user_id, $group_id, $inviter_id, $draft = false ) {
    1616                $invite                = new BP_Groups_Member;
    1717                $invite->group_id      = $group_id;
    1818                $invite->user_id       = $user_id;
    1919                $invite->date_modified = bp_core_current_time();
    2020                $invite->inviter_id    = $inviter_id;
    2121                $invite->is_confirmed  = 0;
    22                 $invite->invite_sent   = 1;
     22                $invite->invite_sent   = $draft ? 0 : 1;
    2323
    2424                $invite->save();
    2525                return $invite->id;
    2626        }
    2727
     28        public static function create_group_membership_request( $user_id, $group_id ) {
     29                // Pending membership requests have inviter_id = 0, is_confirmed = 0
     30                $request                = new BP_Groups_Member;
     31                $request->group_id      = $group_id;
     32                $request->user_id       = $user_id;
     33                $request->date_modified = bp_core_current_time();
     34                $request->inviter_id    = 0;
     35                $request->is_confirmed  = 0;
     36                $request->is_admin  = 0;
     37
     38                $request->save();
     39                return $request->id;
     40        }
     41
    2842        public function test_get_recently_joined_with_filter() {
    2943                $g1 = $this->factory->group->create( array(
    3044                        'name' => 'Tab',
    … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {  
    127141                $ids = wp_list_pluck( $mm, 'user_id' );
    128142                $this->assertEquals( array( $u2 ), $ids );
    129143        }
     144
     145        public function test_groups_send_invite_by_invitee() {
     146                $g1 = $this->factory->group->create();
     147
     148                $u1 = $this->factory->user->create();
     149                $u2 = $this->factory->user->create();
     150                $u3 = $this->factory->user->create();
     151                $u4 = $this->factory->user->create();
     152                self::add_user_to_group( $u1, $g1 );
     153
     154                // Test expected functionality
     155                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     156                $this->assertTrue( is_null( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     157
     158                $success = groups_send_invite_by_invitee( $g1, $u2, $u1 );
     159
     160                $this->assertTrue( $success );
     161                $this->assertTrue( is_numeric( groups_check_user_has_invite( $u2, $g1, $type = 'sent' ) ) );
     162                unset( $success );
     163
     164                // Test when user doesn't have invitation.
     165                $this->assertTrue( is_null( groups_check_user_has_invite( $u3, $g1, $type = 'sent' ) ) );
     166
     167                $success = groups_send_invite_by_invitee( $g1, $u3, $u1 );
     168
     169                $this->assertFalse( $success );
     170                unset( $success );
     171
     172                // Test when user has submitted a request for membership, but has not been invited
     173                self::create_group_membership_request( $u3, $g1 );
     174                $this->assertTrue( is_numeric( groups_check_for_membership_request( $u3, $g1 ) ) );
     175                $this->assertTrue( is_null( groups_check_user_has_invite( $u3, $g1, $type = 'all' ) ) );
     176
     177                $success = groups_send_invite_by_invitee( $g1, $u3, $u1 );
     178
     179                $this->assertFalse( $success );
     180                unset( $success );
     181
     182                // Test when user already belongs to the group
     183                self::add_user_to_group( $u4, $g1 );
     184
     185                $success = groups_send_invite_by_invitee( $g1, $u4, $u1 );
     186
     187                $this->assertFalse( $success );
     188                unset( $success );
     189
     190                // Test when user/group isn't specified
     191                $success = groups_send_invite_by_invitee( null, $u2, $u1 );
     192
     193                $this->assertFalse( $success );
     194                unset( $success );
     195
     196                $success = groups_send_invite_by_invitee( $g1, null, $u1 );
     197
     198                $this->assertFalse( $success );
     199                unset( $success );
     200
     201                // Test when user/group doesn't exist
     202                // Bad group ID
     203                $success = groups_send_invite_by_invitee( 457819810, $u2, $u1 );
     204
     205                $this->assertFalse( $success );
     206                unset( $success );
     207
     208                // Bad invitee ID
     209                $success = groups_send_invite_by_invitee( $g1, 47161302, $u1 );
     210
     211                $this->assertFalse( $success );
     212                unset( $success );
     213
     214                // Bad inviter ID
     215                $success = groups_send_invite_by_invitee( $g1, $u2, 472816989 );
     216
     217                $this->assertFalse( $success );
     218                unset( $success );
     219        }
     220
     221        public function test_groups_send_invites() {
     222                $g1 = $this->factory->group->create();
     223                $g1_obj = groups_get_group( array( 'group_id' => $g1 ) );
     224
     225                $u1 = $this->factory->user->create();
     226                $u2 = $this->factory->user->create();
     227                $u3 = $this->factory->user->create();
     228                $u4 = $this->factory->user->create();
     229                $u5 = $this->factory->user->create();
     230
     231                $old_current_user = get_current_user_id();
     232
     233                self::add_user_to_group( $u1, $g1 );
     234
     235                // Test expected functionality
     236                // Create some draft invitations that we can send
     237                self::invite_user_to_group( $u2, $g1, $u1, $draft = true );
     238                self::invite_user_to_group( $u3, $g1, $u1, $draft = true );
     239
     240                $success = groups_send_invites( $u1, $g1 );
     241
     242                $this->assertTrue( $success );
     243                unset( $success );
     244
     245                // User has no outstanding invites to send.
     246                self::add_user_to_group( $u4, $g1 );
     247                $success = groups_send_invites( $u4, $g1 );
     248               
     249                $this->assertTrue( $success );
     250                unset( $success );
     251
     252                // User doesn't belong to group.
     253                $success = groups_send_invites( $u5, $g1 );
     254               
     255                $this->assertFalse( $success );
     256                unset( $success );
     257
     258                // Test when user/group isn't specified
     259                // User isn't specified, but current user does have outstanding invites.
     260                $this->set_current_user( $u1 );
     261               
     262                $success = groups_send_invites( null, $g1 );
     263
     264                $this->assertTrue( $success );
     265                unset( $success );
     266                $this->set_current_user( $old_current_user );
     267
     268                // Group isn't specified, we're not in a group context
     269                $success = groups_send_invites( $u1, null );
     270
     271                $this->assertFalse( $success );
     272                unset( $success );
     273
     274                // Group isn't specified, we are in a group context
     275                $this->go_to( bp_get_group_permalink( $g1_obj ) );
     276
     277                $success = groups_send_invites( $u1, null );
     278
     279                $this->assertTrue( $success );
     280                unset( $success );
     281
     282                // Test when user/group doesn't exist
     283                // Bad user ID
     284                $success = groups_send_invites( 569845135, $g1 );
     285
     286                $this->assertFalse( $success );
     287                unset( $success );
     288
     289                // Bad group ID
     290                $success = groups_send_invites( $u1, 586234125 );
     291
     292                $this->assertFalse( $success );
     293                unset( $success );
     294        }
     295
     296        public function test_bp_groups_user_can_send_invites() {
     297                $u_members = $this->factory->user->create();
     298                $u_mods = $this->factory->user->create();
     299                $u_admins = $this->factory->user->create();
     300                $u_siteadmin = $this->factory->user->create();
     301                $user_siteadmin = new WP_User( $u_siteadmin );
     302                $user_siteadmin->add_role( 'administrator' );
     303
     304                $g = $this->factory->group->create();
     305
     306                $now = time();
     307                $old_current_user = get_current_user_id();
     308
     309                // Create member-level user
     310                $this->add_user_to_group( $u_members, $g, array(
     311                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     312                ) );
     313                // Create mod-level user
     314                $this->add_user_to_group( $u_mods, $g, array(
     315                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     316                ) );
     317                $m_mod = new BP_Groups_Member( $u_mods, $g );
     318                $m_mod->promote( 'mod' );
     319                // Create admin-level user
     320                $this->add_user_to_group( $u_admins, $g, array(
     321                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     322                ) );
     323                $m_admin = new BP_Groups_Member( $u_admins, $g );
     324                $m_admin->promote( 'admin' );
     325
     326                // Test with no status
     327                // In bp_group_get_invite_status(), no status falls back to "members"
     328                $this->assertTrue( '' == groups_get_groupmeta( $g, 'invite_status' ) );
     329                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_members ) );
     330                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) );
     331                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) );
     332                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) );
     333
     334                // Test with members status
     335                groups_update_groupmeta( $g, 'invite_status', 'members' );
     336                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_members ) );
     337                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) );
     338                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) );
     339                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) );
     340                // Falling back to current user
     341                $this->set_current_user( $u_members );
     342                $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) );
     343
     344                // Test with mod status
     345                groups_update_groupmeta( $g, 'invite_status', 'mods' );
     346                $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_members ) );
     347                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) );
     348                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) );
     349                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) );
     350                // Falling back to current user
     351                $this->set_current_user( $u_members );
     352                $this->assertFalse( bp_groups_user_can_send_invites( $g, null ) );
     353                $this->set_current_user( $u_mods );
     354                $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) );               
     355
     356                // Test with admin status
     357                groups_update_groupmeta( $g, 'invite_status', 'admins' );
     358                $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_members ) );
     359                $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_mods ) );
     360                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) );
     361                $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) );
     362                // Falling back to current user
     363                $this->set_current_user( $u_mods );
     364                $this->assertFalse( bp_groups_user_can_send_invites( $g, null ) );
     365                $this->set_current_user( $u_admins );
     366                $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) );
     367
     368                // Bad or null parameters
     369                $this->assertFalse( bp_groups_user_can_send_invites( 59876454257, $u_members ) );
     370                $this->assertFalse( bp_groups_user_can_send_invites( $g, 958647515 ) );
     371                // Not in group context
     372                $this->assertFalse( bp_groups_user_can_send_invites( null, $u_members ) );             
     373                // In group context
     374                $g_obj = groups_get_group( array( 'group_id' => $g ) );
     375                $this->go_to( bp_get_group_permalink( $g_obj ) );
     376                groups_update_groupmeta( $g, 'invite_status', 'members' );
     377
     378                $this->assertTrue( bp_groups_user_can_send_invites( null, $u_members ) );
     379
     380                $this->set_current_user( $old_current_user );
     381
     382        }
    130383}
    131384