Ticket #6210: 6210-06182019.diff
File 6210-06182019.diff, 181.7 KB (added by , 6 years ago) |
---|
-
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 244d1a1f9..928948797 100644
function bp_core_install_emails() { 540 540 */ 541 541 do_action( 'bp_core_install_emails' ); 542 542 } 543 544 /** 545 * Install database tables for the Invitations API 546 * 547 * @since 5.0.0 548 * 549 * @uses bp_core_set_charset() 550 * @uses bp_core_get_table_prefix() 551 * @uses dbDelta() 552 */ 553 function bp_core_install_invitations() { 554 $sql = array(); 555 $charset_collate = $GLOBALS['wpdb']->get_charset_collate(); 556 $bp_prefix = bp_core_get_table_prefix(); 557 $sql[] = "CREATE TABLE {$bp_prefix}bp_invitations ( 558 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, 559 user_id bigint(20) NOT NULL, 560 inviter_id bigint(20) NOT NULL, 561 invitee_email varchar(100) DEFAULT NULL, 562 class varchar(120) NOT NULL, 563 item_id bigint(20) NOT NULL, 564 secondary_item_id bigint(20) DEFAULT NULL, 565 type varchar(12) NOT NULL DEFAULT 'invite', 566 content longtext DEFAULT '', 567 date_modified datetime NOT NULL, 568 invite_sent tinyint(1) NOT NULL DEFAULT '0', 569 accepted tinyint(1) NOT NULL DEFAULT '0', 570 KEY user_id (user_id), 571 KEY inviter_id (inviter_id), 572 KEY invitee_email (invitee_email), 573 KEY class (class), 574 KEY item_id (item_id), 575 KEY secondary_item_id (secondary_item_id), 576 KEY type (type), 577 KEY invite_sent (invite_sent), 578 KEY accepted (accepted) 579 ) {$charset_collate};"; 580 dbDelta( $sql ); 581 582 // @TODO: Migrate invitations here 583 584 /** 585 * Fires after BuddyPress adds the invitations table. 586 * 587 * @since 5.0.0 588 */ 589 do_action( 'bp_core_install_invitations' ); 590 } -
src/bp-core/bp-core-functions.php
diff --git src/bp-core/bp-core-functions.php src/bp-core/bp-core-functions.php index f3c8be614..dfeb65368 100644
function bp_email_get_schema() { 3479 3479 /* translators: do not remove {} brackets or translate its contents. */ 3480 3480 'post_title' => __( '[{{{site.name}}}] You have an invitation to the group: "{{group.name}}"', 'buddypress' ), 3481 3481 /* translators: do not remove {} brackets or translate its contents. */ 3482 'post_content' => __( "<a href=\"{{{inviter.url}}}\">{{inviter.name}}</a> has invited you to join the group: "{{group.name}}".\n <a href=\"{{{invites.url}}}\">Go here to accept your invitation</a> or <a href=\"{{{group.url}}}\">visit the group</a> to learn more.", 'buddypress' ),3482 'post_content' => __( "<a href=\"{{{inviter.url}}}\">{{inviter.name}}</a> has invited you to join the group: "{{group.name}}".\n{{invite.message}}\n<a href=\"{{{invites.url}}}\">Go here to accept your invitation</a> or <a href=\"{{{group.url}}}\">visit the group</a> to learn more.", 'buddypress' ), 3483 3483 /* translators: do not remove {} brackets or translate its contents. */ 3484 3484 'post_excerpt' => __( "{{inviter.name}} has invited you to join the group: \"{{group.name}}\".\n\nTo accept your invitation, visit: {{{invites.url}}}\n\nTo learn more about the group, visit: {{{group.url}}}.\nTo view {{inviter.name}}'s profile, visit: {{{inviter.url}}}", 'buddypress' ), 3485 3485 ), … … function bp_email_get_schema() { 3495 3495 /* translators: do not remove {} brackets or translate its contents. */ 3496 3496 'post_title' => __( '[{{{site.name}}}] Membership request for group: {{group.name}}', 'buddypress' ), 3497 3497 /* translators: do not remove {} brackets or translate its contents. */ 3498 'post_content' => __( "<a href=\"{{{profile.url}}}\">{{requesting-user.name}}</a> wants to join the group "{{group.name}}". As you are an administrator of this group, you must either accept or reject the membership request.\n\n<a href=\"{{{group-requests.url}}}\">Go here to manage this</a> and all other pending requests.", 'buddypress' ),3498 'post_content' => __( "<a href=\"{{{profile.url}}}\">{{requesting-user.name}}</a> wants to join the group "{{group.name}}".\n {{request.message}}\n As you are an administrator of this group, you must either accept or reject the membership request.\n\n<a href=\"{{{group-requests.url}}}\">Go here to manage this</a> and all other pending requests.", 'buddypress' ), 3499 3499 /* translators: do not remove {} brackets or translate its contents. */ 3500 3500 'post_excerpt' => __( "{{requesting-user.name}} wants to join the group \"{{group.name}}\". As you are the administrator of this group, you must either accept or reject the membership request.\n\nTo manage this and all other pending requests, visit: {{{group-requests.url}}}\n\nTo view {{requesting-user.name}}'s profile, visit: {{{profile.url}}}", 'buddypress' ), 3501 3501 ), -
new file src/bp-core/bp-core-invitations-cache.php
diff --git src/bp-core/bp-core-invitations-cache.php src/bp-core/bp-core-invitations-cache.php new file mode 100644 index 000000000..7cf867398
- + 1 <?php 2 /** 3 * BuddyPress Invitation Caching Functions. 4 * 5 * Caching functions handle the clearing of cached objects and pages on specific 6 * actions throughout BuddyPress. 7 * 8 * @package BuddyPress 9 * @subpackage InvitationsCache 10 * @since 5.0.0 11 */ 12 13 /** 14 * Resets all incremented bp_invitations caches. 15 * 16 * @since 5.0.0 17 */ 18 function bp_invitations_reset_cache_incrementor() { 19 bp_core_reset_incrementor( 'bp_invitations' ); 20 } 21 add_action( 'bp_invitation_after_save', 'bp_invitations_reset_cache_incrementor' ); 22 add_action( 'bp_invitation_after_delete', 'bp_invitations_reset_cache_incrementor' ); -
src/bp-core/bp-core-update.php
diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php index b2cc9761a..27e3819bc 100644
function bp_version_updater() { 211 211 bp_update_option( 'bp-active-components', $default_components ); 212 212 bp_core_add_page_mappings( $default_components, 'delete' ); 213 213 bp_core_install_emails(); 214 bp_core_install_invitations(); 214 215 215 216 // Upgrades. 216 217 } else { … … function bp_update_to_2_7() { 551 552 * 5.0.0 update routine. 552 553 * 553 554 * - Make sure the custom visibility is disabled for the default profile field. 555 * - Create the invitations table. 556 * - Migrate requests and invitations to the new table. 554 557 * 555 558 * @since 5.0.0 556 559 */ … … function bp_update_to_5_0() { 579 582 '%s' 580 583 ) 581 584 ); 585 586 bp_core_install_invitations(); 587 588 bp_groups_migrate_invitations(); 582 589 } 583 590 584 591 /** -
new file src/bp-core/classes/class-bp-invitation-manager.php
diff --git src/bp-core/classes/class-bp-invitation-manager.php src/bp-core/classes/class-bp-invitation-manager.php new file mode 100644 index 000000000..397258511
- + 1 <?php 2 /** 3 * Core invitations class. 4 * 5 * @package BuddyPress 6 * @subpackage Core 7 * @since 5.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * BP Invitations class. 15 * 16 * Extend it to manage your class's invitations. 17 * Your extension class, must, at a minimum, provide the 18 * run_send_action() and run_acceptance_action() methods. 19 * 20 * @since 5.0.0 21 */ 22 abstract class BP_Invitation_Manager { 23 24 /** 25 * The name of the related class. 26 * 27 * @since 5.0.0 28 * @access public 29 * @var string 30 */ 31 protected $class_name; 32 33 /** 34 * Construct parameters. 35 * 36 * @since 5.0.0 37 * 38 * @param array|string $args { 39 * } 40 */ 41 public function __construct( $args = array() ) { 42 $this->class_name = get_class( $this ); 43 } 44 45 /** 46 * Get the invitations table name. 47 * 48 * @since 5.0.0 49 * @access public 50 * @return string 51 */ 52 public static function get_table_name() { 53 return buddypress()->table_prefix . 'bp_invitations'; 54 } 55 56 /** Create ********************************************************************/ 57 58 /** 59 * Add an invitation to a specific user, from a specific user, related to a 60 * specific class. 61 * 62 * @since 5.0.0 63 * 64 * @param array $args { 65 * Array of arguments describing the invitation. All are optional. 66 * @type int $user_id ID of the invited user. 67 * @type int $inviter_id ID of the user who created the invitation. 68 * @type string $invitee_email Email address of the invited user. 69 * @type int $item_id ID associated with the invitation and class. 70 * @type int $secondary_item_id secondary ID associated with the 71 * invitation and class. 72 * @type string $type @TODO. 73 * @type string $content Extra information provided by the requester 74 * or inviter. 75 * @type string $date_modified Date the invitation was last modified. 76 * @type int $send_invite Should the invitation also be sent, or is it a 77 * draft invite? 78 * } 79 * @return int|bool ID of the newly created invitation on success, false 80 * on failure. 81 */ 82 public function add_invitation( $args = array() ) { 83 84 $r = bp_parse_args( $args, array( 85 'user_id' => 0, 86 'invitee_email' => '', 87 'inviter_id' => 0, 88 'item_id' => 0, 89 'secondary_item_id' => 0, 90 'type' => 'invite', 91 'content' => '', 92 'date_modified' => bp_core_current_time(), 93 'send_invite' => 0, 94 'accepted' => 0 95 ), 'add_invitation' ); 96 97 // Invitations must have an invitee and inviter. 98 if ( ! ( ( $r['user_id'] || $r['invitee_email'] ) && $r['inviter_id'] ) ) { 99 return false; 100 } 101 102 /** 103 * Is this user allowed to extend invitations in this situation? 104 * 105 * @since 5.0.0 106 * 107 * @param array $r Describes the invitation to be added. 108 */ 109 if ( ! $this->allow_invitation( $r ) ) { 110 return false; 111 } 112 113 // Avoid creating duplicate invitations. 114 $invite_id = $this->invitation_exists( array( 115 'user_id' => $r['user_id'], 116 'invitee_email' => $r['invitee_email'], 117 'inviter_id' => $r['inviter_id'], 118 'item_id' => $r['item_id'], 119 'secondary_item_id' => $r['secondary_item_id'], 120 ) ); 121 122 if ( ! $invite_id ) { 123 // Set up the new invitation as a draft. 124 $invitation = new BP_Invitation; 125 $invitation->user_id = $r['user_id']; 126 $invitation->inviter_id = $r['inviter_id']; 127 $invitation->invitee_email = $r['invitee_email']; 128 $invitation->class = $this->class_name; 129 $invitation->item_id = $r['item_id']; 130 $invitation->secondary_item_id = $r['secondary_item_id']; 131 $invitation->type = $r['type']; 132 $invitation->content = $r['content']; 133 $invitation->date_modified = $r['date_modified']; 134 $invitation->invite_sent = 0; 135 $invitation->accepted = 0; 136 137 $invite_id = $invitation->save(); 138 } 139 140 // "Send" the invite if necessary. 141 if ( $invite_id && $r['send_invite'] ) { 142 $sent = $this->send_invitation_by_id( $invite_id ); 143 if ( ! $sent ) { 144 return false; 145 } 146 } 147 148 return $invite_id; 149 } 150 151 /** 152 * Send an invitation notification. 153 * 154 * @since 5.0.0 155 * @access public 156 * 157 * @param int $invitation_id ID of invitation to send. 158 * 159 * @return int|bool The number of rows updated, or false on error. 160 */ 161 public function send_invitation_by_id( $invitation_id = 0 ) { 162 $updated = false; 163 164 $invitation = new BP_Invitation( $invitation_id ); 165 166 if ( ! $invitation->id ) { 167 return false; 168 } 169 170 /** 171 * Fires before an invitation is sent. 172 * 173 * @since 5.0.0 174 * 175 * @param BP_Invitation object $invitation Invitation about to be sent. 176 */ 177 do_action( 'bp_invitations_send_invitation_by_id_before_send', $invitation ); 178 179 /* 180 * Before sending an invitation, check for outstanding requests to the same item. 181 * A sent invitation + a request = acceptance. 182 */ 183 $request_args = array( 184 'user_id' => $invitation->user_id, 185 'invitee_email' => $invitation->invitee_email, 186 'item_id' => $invitation->item_id, 187 'secondary_item_id' => $invitation->secondary_item_id, 188 ); 189 $request = $this->request_exists( $request_args ); 190 191 if ( ! empty( $request ) ) { 192 // Accept the request. 193 return $this->accept_request( $request_args ); 194 } 195 196 // Perform the send action. 197 $this->run_send_action( $invitation ); 198 199 $updated = BP_Invitation::mark_sent( $invitation->id ); 200 201 return $updated; 202 } 203 204 /** 205 * Add a request to an item for a specific user, related to a 206 * specific class. 207 * 208 * @since 5.0.0 209 * 210 * @param array $args { 211 * Array of arguments describing the invitation. All are optional. 212 * @type int $user_id ID of the invited user. 213 * @type int $inviter_id ID of the user who created the invitation. 214 * @type string $class Name of the invitations class. 215 * @type int $item_id ID associated with the invitation and class. 216 * @type int $secondary_item_id secondary ID associated with the 217 * invitation and class. 218 * @type string $type @TODO. 219 * @type string $content Extra information provided by the requester 220 * or inviter. 221 * @type string $date_modified Date the invitation was last modified. 222 * @type int $invite_sent Has the invitation been sent, or is it a 223 * draft invite? 224 * } 225 * @return int|bool ID of the newly created invitation on success, false 226 * on failure. 227 */ 228 public function add_request( $args = array() ) { 229 230 $r = bp_parse_args( $args, array( 231 'user_id' => 0, 232 'inviter_id' => 0, 233 'invitee_email' => '', 234 'item_id' => 0, 235 'secondary_item_id' => 0, 236 'type' => 'request', 237 'content' => '', 238 'date_modified' => bp_core_current_time(), 239 'invite_sent' => 0, 240 'accepted' => 0 241 ), 'add_request' ); 242 243 // If there is no invitee, bail. 244 if ( ! ( $r['user_id'] || $r['invitee_email'] ) ) { 245 return false; 246 } 247 248 /** 249 * Is this user allowed to make a request in this situation? 250 * 251 * @since 5.0.0 252 * 253 * @param array $r Describes the invitation to be added. 254 */ 255 if ( ! $this->allow_request( $r ) ) { 256 return false; 257 } 258 259 /* 260 * Avoid creating duplicate requests. 261 */ 262 $base_args = array( 263 'user_id' => $r['user_id'], 264 'invitee_email' => $r['invitee_email'], 265 'item_id' => $r['item_id'], 266 'secondary_item_id' => $r['secondary_item_id'], 267 ); 268 if ( $this->request_exists( $base_args ) ) { 269 return false; 270 } 271 272 /* 273 * Check for outstanding invitations to the same item. 274 * A request + a sent invite = acceptance. 275 */ 276 $invite_args = array_merge( $base_args, array( 'invite_sent' => 'sent' ) ); 277 $invite = $this->invitation_exists( $invite_args ); 278 279 if ( $invite ) { 280 // Accept the invite. 281 return $this->accept_invitation( $base_args ); 282 } else { 283 // Set up the new request. 284 $request = new BP_Invitation; 285 $request->user_id = $r['user_id']; 286 $request->inviter_id = $r['inviter_id']; 287 $request->invitee_email = $r['invitee_email']; 288 $request->class = $this->class_name; 289 $request->item_id = $r['item_id']; 290 $request->secondary_item_id = $r['secondary_item_id']; 291 $request->type = $r['type']; 292 $request->content = $r['content']; 293 $request->date_modified = $r['date_modified']; 294 $request->invite_sent = $r['invite_sent']; 295 $request->accepted = $r['accepted']; 296 297 // Save the new invitation. 298 return $request->save(); 299 } 300 } 301 302 /** 303 * Send a request notification. 304 * 305 * @since 5.0.0 306 * @access public 307 * 308 * @param int $request_id ID of request to send. 309 * 310 * @return int|bool The number of rows updated, or false on error. 311 */ 312 public function send_request_notification_by_id( $request_id = 0 ) { 313 $updated = false; 314 315 $request = new BP_Invitation( $request_id ); 316 317 if ( ! $request->id ) { 318 return false; 319 } 320 321 // Different uses may need different actions on sending. Plugins can hook in here to perform their own tasks. 322 do_action( 'bp_invitations_send_request_notification_by_id_before_send', $request_id, $request ); 323 324 /* 325 * Before sending notifications, check for outstanding invitations to the same item. 326 * A sent invitation + a request = acceptance. 327 */ 328 $args = array( 329 'user_id' => $request->user_id, 330 'invitee_email' => $request->invitee_email, 331 'item_id' => $request->item_id, 332 'secondary_item_id' => $request->secondary_item_id, 333 'invite_sent' => 'sent' 334 ); 335 $invites = $this->invitation_exists( $args ); 336 337 if ( ! empty( $invites ) ) { 338 // Accept the request. 339 return $this->accept_invitation( $args ); 340 } 341 342 // Perform the send action. 343 $this->run_send_action( $request ); 344 345 $updated = BP_Invitation::mark_sent( $request->id ); 346 347 return $updated; 348 } 349 350 /** Retrieve ******************************************************************/ 351 352 /** 353 * Get a specific invitation by its ID. 354 * 355 * @since 5.0.0 356 * 357 * @param int $id ID of the invitation. 358 * @return BP_Invitation object 359 */ 360 public function get_by_id( $id = 0 ) { 361 return new BP_Invitation( $id ); 362 } 363 364 /** 365 * Get invitations, based on provided filter parameters. 366 * 367 * @since 5.0.0 368 * 369 * @see BP_Invitation::get() for a description of accepted parameters. 370 * 371 * @return array Located invitations. 372 */ 373 public function get_invitations( $args = array() ) { 374 // Default to returning invitations, not requests. 375 if ( empty( $args['type'] ) ) { 376 $args['type'] = 'invite'; 377 } 378 // Use the class_name property value. 379 $args['class'] = $this->class_name; 380 381 return BP_Invitation::get( $args ); 382 } 383 384 /** 385 * Get requests, based on provided filter parameters. 386 * 387 * @since 5.0.0 388 * 389 * @see BP_Invitation::get() for a description of accepted parameters. 390 * 391 * @return array Located invitations. 392 */ 393 public function get_requests( $args = array() ) { 394 // Set request-specific parameters. 395 $args['type'] = 'request'; 396 $args['inviter_id'] = false; 397 $args['invite_sent'] = 'all'; 398 399 // Use the class_name property value. 400 $args['class'] = $this->class_name; 401 402 return BP_Invitation::get( $args ); 403 } 404 405 /** 406 * Check whether an invitation exists matching the passed arguments. 407 * 408 * @since 5.0.0 409 * 410 * @see BP_Invitation::get() for a description of accepted parameters. 411 * 412 * @return bool|int ID of first found invitation or false if none found. 413 */ 414 public function invitation_exists( $args = array() ) { 415 $is_invited = false; 416 417 $args['fields'] = 'ids'; 418 $invites = $this->get_invitations( $args ); 419 if ( $invites ) { 420 $is_invited = current( $invites ); 421 } 422 return $is_invited; 423 } 424 425 /** 426 * Check whether a request exists matching the passed arguments. 427 * 428 * @since 5.0.0 429 * 430 * @see BP_Invitation::get() for a description of accepted parameters. 431 * 432 * @return bool|int ID of existing request or false if none found. 433 */ 434 public function request_exists( $args = array() ) { 435 $has_request = false; 436 437 $args['fields'] = 'ids'; 438 $requests = $this->get_requests( $args ); 439 if ( $requests ) { 440 $has_request = current( $requests ); 441 } 442 return $has_request; 443 } 444 445 /** Update ********************************************************************/ 446 447 /** 448 * Accept invitation, based on provided filter parameters. 449 * 450 * @since 5.0.0 451 * 452 * @see BP_Invitation::get() for a description of 453 * accepted update/where arguments. 454 * 455 * @param array $update_args Associative array of fields to update, 456 * and the values to update them to. Of the format 457 * array( 'user_id' => 4 ) 458 * 459 * @return int|bool Number of rows updated on success, false on failure. 460 */ 461 public function accept_invitation( $args = array() ) { 462 463 /* 464 * Some basic info is required to accept an invitation, 465 * because we'll need to mark all similar invitations and requests. 466 * The following, except the optional 'secondary_item_id', are required. 467 */ 468 $r = bp_parse_args( $args, array( 469 'user_id' => 0, 470 'invitee_email' => '', 471 'item_id' => null, 472 'secondary_item_id' => null, 473 'invite_sent' => 'sent', 474 ), 'accept_invitation' ); 475 $r['class'] = $this->class_name; 476 477 if ( ! ( ( $r['user_id'] || $r['invitee_email'] ) && $r['class'] && $r['item_id'] ) ) { 478 return false; 479 } 480 481 $success = $this->run_acceptance_action( 'invite', $r ); 482 if ( $success ) { 483 // Mark invitations & requests to this item for this user. 484 $this->mark_accepted( $r ); 485 486 // Allow plugins an opportunity to act on the change. 487 do_action( 'bp_invitations_accepted_invite', $r ); 488 } 489 return $success; 490 } 491 492 /** 493 * Accept invitation, based on provided filter parameters. 494 * 495 * @since 5.0.0 496 * 497 * @see BP_Invitation::get() for a description of 498 * accepted update/where arguments. 499 * 500 * @param array $update_args Associative array of fields to update, 501 * and the values to update them to. Of the format 502 * array( 'user_id' => 4 ) 503 * 504 * @return bool Number of rows updated on success, false on failure. 505 */ 506 public function accept_request( $args = array() ) { 507 /* 508 * Some basic info is required to accept an invitation, 509 * because we'll need to accept all similar invitations and requests. 510 * The following, except the optional 'secondary_item_id', are required. 511 */ 512 $r = bp_parse_args( $args, array( 513 'user_id' => 0, 514 'item_id' => null, 515 'secondary_item_id' => null, 516 ), 'accept_request' ); 517 $r['class'] = $this->class_name; 518 519 if ( ! ( $r['user_id'] && $r['class'] && $r['item_id'] ) ) { 520 return false; 521 } 522 523 $success = $this->run_acceptance_action( 'request', $r ); 524 if ( $success ) { 525 // Update/Delete all related invitations & requests to this item for this user. 526 $this->mark_accepted( $r ); 527 528 // Allow plugins an opportunity to act on the change. 529 do_action( 'bp_invitations_accepted_request', $r ); 530 } 531 return $success; 532 } 533 534 /** 535 * Update invitation, based on provided filter parameters. 536 * 537 * @since 5.0.0 538 * 539 * @see BP_Invitation::get() for a description of 540 * accepted update/where arguments. 541 * 542 * @param array $update_args Associative array of fields to update, 543 * and the values to update them to. Of the format 544 * array( 'user_id' => 4 ) 545 * @param array $where_args Associative array of columns/values, to 546 * determine which invitations should be updated. Formatted as 547 * array( 'item_id' => 7 ) 548 * @return int|bool Number of rows updated on success, false on failure. 549 */ 550 public function update_invitation( $update_args = array(), $where_args = array() ) { 551 $update_args['class'] = $this->class_name; 552 return BP_Invitation::update( $update_args, $where_args ); 553 } 554 555 556 /** 557 * This is where custom actions are added (in child classes) 558 * to run when an invitation or request needs to be "sent." 559 * 560 * @since 5.0.0 561 * 562 * @param BP_Invitation $invitation The invitation to send. 563 * @return bool True on success, false on failure. 564 */ 565 abstract public function run_send_action( BP_Invitation $invitation ); 566 567 /** 568 * Mark invitations as sent that are found by user_id, inviter_id, 569 * invitee_email, class name, optional item id, 570 * optional secondary item id. 571 * 572 * @since 5.0.0 573 * 574 * @param array $args { 575 * Associative array of arguments. All arguments but $page and 576 * $per_page can be treated as filter values for get_where_sql() 577 * and get_query_clauses(). All items are optional. 578 * @type int|array $user_id ID of user being queried. Can be an 579 * array of user IDs. 580 * @type int|array $inviter_id ID of user who created the 581 * invitation. Can be an array of user IDs. 582 * Special cases 583 * @type string|array $invitee_email Email address of invited users 584 * being queried. Can be an array of addresses. 585 * @type string|array $class Name of the class to 586 * filter by. Can be an array of class names. 587 * @type int|array $item_id ID of associated item. Can be an array 588 * of multiple item IDs. 589 * @type int|array $secondary_item_id ID of secondary associated 590 * item. Can be an array of multiple IDs. 591 * } 592 */ 593 public function mark_sent( $args ) { 594 $args['class'] = $this->class_name; 595 return BP_Invitation::mark_sent_by_data( $args ); 596 } 597 598 /** 599 * This is where custom actions are added (in child classes) 600 * to run when an invitation or request is accepted. 601 * 602 * @since 5.0.0 603 * 604 * @param int $id The ID of the invitation to mark as sent. 605 * @return bool True on success, false on failure. 606 */ 607 abstract public function run_acceptance_action( $type = 'invite', $r ); 608 609 /** 610 * Mark invitation as accepted by invitation ID. 611 * 612 * @since 5.0.0 613 * 614 * @param int $id The ID of the invitation to mark as sent. 615 * @return bool True on success, false on failure. 616 */ 617 public function mark_accepted_by_id( $id ) { 618 return BP_Invitation::mark_accepted( $id ); 619 } 620 621 /** 622 * Mark invitations as sent that are found by user_id, inviter_id, 623 * invitee_email, class name, optional item id, 624 * optional secondary item id. 625 * 626 * @since 5.0.0 627 * 628 * @see BP_Invitation::mark_accepted_by_data() 629 * for a description of arguments. 630 */ 631 public function mark_accepted( $args ) { 632 $args['class'] = $this->class_name; 633 return BP_Invitation::mark_accepted_by_data( $args ); 634 } 635 636 /** Delete ********************************************************************/ 637 638 /** 639 * Delete an invitation or invitations by query data. 640 * 641 * @since 5.0.0 642 * 643 * @see BP_Invitation::delete for a description of arguments. 644 * @return int|false Number of rows deleted on success, false on failure. 645 */ 646 public function delete( $args ) { 647 if ( empty( $args['type'] ) ) { 648 $args['type'] = 'invite'; 649 } 650 $args['class'] = $this->class_name; 651 return BP_Invitation::delete( $args ); 652 } 653 654 /** 655 * Delete a request or requests by query data. 656 * 657 * @since 5.0.0 658 * 659 * @see BP_Invitation::delete for a description of arguments. 660 * 661 * @return int|false Number of rows deleted on success, false on failure. 662 */ 663 public function delete_requests( $args ) { 664 $args['type'] = 'request'; 665 return $this->delete( $args ); 666 } 667 668 /** 669 * Delete all invitations by class. 670 * 671 * Used when clearing out invitations for an entire class. Possibly used 672 * when deactivating a component related to a class that created invitations. 673 * 674 * @since 5.0.0 675 * 676 * @return int|false Number of rows deleted on success, false on failure. 677 */ 678 public function delete_all() { 679 return BP_Invitation::delete( array( 680 'class' => $this->class_name, 681 ) ); 682 } 683 684 /** 685 * This is where custom actions are added (in child classes) 686 * to determine whether an invitation should be allowed. 687 * 688 * @since 5.0.0 689 * 690 * @param array $args The parameters describing the invitation. 691 * @return bool True if allowed, false to end process. 692 */ 693 public function allow_invitation( $args ) { 694 return true; 695 } 696 697 /** 698 * This is where custom actions are added (in child classes) 699 * to determine whether a request should be allowed. 700 * 701 * @since 5.0.0 702 * 703 * @param array $args The parameters describing the request. 704 * @return bool True if allowed, false to end process. 705 */ 706 public function allow_request( $args ) { 707 return true; 708 } 709 710 } -
new file src/bp-core/classes/class-bp-invitation.php
diff --git src/bp-core/classes/class-bp-invitation.php src/bp-core/classes/class-bp-invitation.php new file mode 100644 index 000000000..18ec7b27c
- + 1 <?php 2 3 /** 4 * BuddyPress Invitation Class 5 * 6 * @package BuddyPress 7 * @subpackage Invitations 8 * 9 * @since 5.0.0 10 */ 11 12 // Exit if accessed directly. 13 defined( 'ABSPATH' ) || exit; 14 15 /** 16 * BuddyPress Invitations. 17 * 18 * Use this class to create, access, edit, or delete BuddyPress Invitations. 19 * 20 * @since 5.0.0 21 */ 22 class BP_Invitation { 23 24 /** 25 * The invitation ID. 26 * 27 * @since 5.0.0 28 * @access public 29 * @var int 30 */ 31 public $id; 32 33 /** 34 * The ID of the invited user. 35 * 36 * @since 5.0.0 37 * @access public 38 * @var int 39 */ 40 public $user_id; 41 42 /** 43 * The ID of the user who created the invitation. 44 * 45 * @since 5.0.0 46 * @access public 47 * @var int 48 */ 49 public $inviter_id; 50 51 /** 52 * The email address of the invited user. 53 * Used when extending an invitation to someone who does not belong to the site. 54 * 55 * @since 5.0.0 56 * @access public 57 * @var string 58 */ 59 public $invitee_email; 60 61 /** 62 * The name of the related class. 63 * 64 * @since 5.0.0 65 * @access public 66 * @var string 67 */ 68 public $class; 69 70 /** 71 * The ID associated with the invitation and component. 72 * Example: the group ID if a group invitation 73 * 74 * @since 5.0.0 75 * @access public 76 * @var int 77 */ 78 public $item_id; 79 80 /** 81 * The secondary ID associated with the invitation and component. 82 * Example: a taxonomy term ID if invited to a site's category-specific RSS feed 83 * 84 * @since 5.0.0 85 * @access public 86 * @var int 87 */ 88 public $secondary_item_id = null; 89 90 91 /** 92 * Invite or request. 93 * 94 * @since 5.0.0 95 * @access public 96 * @var string 97 */ 98 public $type; 99 100 /** 101 * Extra information provided by the requester or inviter. 102 * 103 * @since 5.0.0 104 * @access public 105 * @var string 106 */ 107 public $content; 108 109 /** 110 * The date the invitation was last modified. 111 * 112 * @since 5.0.0 113 * @access public 114 * @var string 115 */ 116 public $date_modified; 117 118 /** 119 * Has the invitation been sent, or is it a draft invite? 120 * 121 * @since 5.0.0 122 * @access public 123 * @var bool 124 */ 125 public $invite_sent; 126 127 /** 128 * Has the invitation been accepted by the invitee? 129 * 130 * @since 5.0.0 131 * @access public 132 * @var bool 133 */ 134 public $accepted; 135 136 137 /** Public Methods ****************************************************/ 138 139 /** 140 * Constructor method. 141 * 142 * @since 5.0.0 143 * 144 * @param int $id Optional. Provide an ID to access an existing 145 * invitation item. 146 */ 147 public function __construct( $id = 0 ) { 148 if ( ! empty( $id ) ) { 149 $this->id = (int) $id; 150 $this->populate(); 151 } 152 } 153 154 /** 155 * Update or insert invitation details into the database. 156 * 157 * @since 5.0.0 158 * 159 * @global wpdb $wpdb WordPress database object. 160 * 161 * @return bool True on success, false on failure. 162 */ 163 public function save() { 164 165 // Return value 166 $retval = false; 167 168 // Default data and format 169 $data = array( 170 'user_id' => $this->user_id, 171 'inviter_id' => $this->inviter_id, 172 'invitee_email' => $this->invitee_email, 173 'class' => sanitize_key( $this->class ), 174 'item_id' => $this->item_id, 175 'secondary_item_id' => $this->secondary_item_id, 176 'type' => $this->type, 177 'content' => $this->content, 178 'date_modified' => $this->date_modified, 179 'invite_sent' => $this->invite_sent, 180 'accepted' => $this->accepted, 181 ); 182 $data_format = array( '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d' ); 183 184 /** 185 * Fires before an invitation is saved. 186 * 187 * @since 5.0.0 188 * 189 * @param BP_Invitation object $this Characteristics of the invitation to be saved. 190 */ 191 do_action_ref_array( 'bp_invitation_before_save', array( &$this ) ); 192 193 // Update 194 if ( ! empty( $this->id ) ) { 195 $result = self::_update( $data, array( 'ID' => $this->id ), $data_format, array( '%d' ) ); 196 // Insert 197 } else { 198 $result = self::_insert( $data, $data_format ); 199 } 200 201 // Set the invitation ID if successful 202 if ( ! empty( $result ) && ! is_wp_error( $result ) ) { 203 global $wpdb; 204 205 $this->id = $wpdb->insert_id; 206 $retval = $wpdb->insert_id; 207 } 208 209 /** 210 * Fires after an invitation is saved. 211 * 212 * @since 5.0.0 213 * 214 * @param BP_Invitation object $this Characteristics of the invitation just saved. 215 */ 216 do_action_ref_array( 'bp_invitation_after_save', array( &$this ) ); 217 218 // Return the result 219 return $retval; 220 } 221 222 /** 223 * Fetch data for an existing invitation from the database. 224 * 225 * @since 5.0.0 226 * 227 * @global BuddyPress $bp The one true BuddyPress instance. 228 * @global wpdb $wpdb WordPress database object. 229 */ 230 public function populate() { 231 global $wpdb; 232 $invites_table_name = BP_Invitation_Manager::get_table_name(); 233 234 // Check cache for invitation data. 235 $invitation = wp_cache_get( $this->id, 'bp_invitations' ); 236 237 // Cache missed, so query the DB. 238 if ( false === $invitation ) { 239 $invitation = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$invites_table_name} WHERE id = %d", $this->id ) ); 240 wp_cache_set( $this->id, $invitation,'bp_invitations' ); 241 } 242 243 // No invitation found so set the ID and bail. 244 if ( empty( $invitation ) || is_wp_error( $invitation ) ) { 245 $this->id = 0; 246 return; 247 } 248 249 $this->user_id = (int) $invitation->user_id; 250 $this->inviter_id = (int) $invitation->inviter_id; 251 $this->invitee_email = $invitation->invitee_email; 252 $this->class = sanitize_key( $invitation->class ); 253 $this->item_id = (int) $invitation->item_id; 254 $this->secondary_item_id = (int) $invitation->secondary_item_id; 255 $this->type = $invitation->type; 256 $this->content = $invitation->content; 257 $this->date_modified = $invitation->date_modified; 258 $this->invite_sent = (int) $invitation->invite_sent; 259 $this->accepted = (int) $invitation->accepted; 260 261 } 262 263 /** Protected Static Methods ******************************************/ 264 265 /** 266 * Create an invitation entry. 267 * 268 * @since 5.0.0 269 * 270 * @param array $data { 271 * Array of invitation data, passed to {@link wpdb::insert()}. 272 * @type int $user_id ID of the invited user. 273 * @type int $inviter_id ID of the user who created the invitation. 274 * @type string $invitee_email Email address of the invited user. 275 * @type string $class Name of the related class. 276 * @type int item_id ID associated with the invitation and component. 277 * @type int secondary_item_id secondary ID associated with the 278 * invitation and component. 279 * @type string content Extra information provided by the requester 280 * or inviter. 281 * @type string date_modified Date the invitation was last modified. 282 * @type int invite_sent Has the invitation been sent, or is it a 283 * draft invite? 284 * } 285 * @param array $data_format See {@link wpdb::insert()}. 286 * @return int|false The number of rows inserted, or false on error. 287 */ 288 protected static function _insert( $data = array(), $data_format = array() ) { 289 global $wpdb; 290 return $wpdb->insert( BP_Invitation_Manager::get_table_name(), $data, $data_format ); 291 } 292 293 /** 294 * Update invitations. 295 * 296 * @since 5.0.0 297 * 298 * @see wpdb::update() for further description of paramater formats. 299 * 300 * @param array $data Array of invitation data to update, passed to 301 * {@link wpdb::update()}. Accepts any property of a 302 * BP_Invitation object. 303 * @param array $where The WHERE params as passed to wpdb::update(). 304 * Typically consists of array( 'ID' => $id ) to specify the ID 305 * of the item being updated. See {@link wpdb::update()}. 306 * @param array $data_format See {@link wpdb::insert()}. 307 * @param array $where_format See {@link wpdb::insert()}. 308 * @return int|false The number of rows updated, or false on error. 309 */ 310 protected static function _update( $data = array(), $where = array(), $data_format = array(), $where_format = array() ) { 311 global $wpdb; 312 return $wpdb->update( BP_Invitation_Manager::get_table_name(), $data, $where, $data_format, $where_format ); 313 } 314 315 /** 316 * Delete invitations. 317 * 318 * @since 5.0.0 319 * 320 * @see wpdb::update() for further description of paramater formats. 321 * 322 * @param array $where Array of WHERE clauses to filter by, passed to 323 * {@link wpdb::delete()}. Accepts any property of a 324 * BP_Invitation object. 325 * @param array $where_format See {@link wpdb::insert()}. 326 * @return int|false The number of rows updated, or false on error. 327 */ 328 protected static function _delete( $where = array(), $where_format = array() ) { 329 global $wpdb; 330 return $wpdb->delete( BP_Invitation_Manager::get_table_name(), $where, $where_format ); 331 } 332 333 /** 334 * Assemble the WHERE clause of a get() SQL statement. 335 * 336 * Used by BP_Invitation::get() to create its WHERE 337 * clause. 338 * 339 * @since 5.0.0 340 * 341 * @param array $args See {@link BP_Invitation::get()} 342 * for more details. 343 * @return string WHERE clause. 344 */ 345 protected static function get_where_sql( $args = array() ) { 346 global $wpdb; 347 348 $where_conditions = array(); 349 $where = ''; 350 351 // id 352 if ( ! empty( $args['id'] ) ) { 353 $id_in = implode( ',', wp_parse_id_list( $args['id'] ) ); 354 $where_conditions['id'] = "id IN ({$id_in})"; 355 } 356 357 // user_id 358 if ( ! empty( $args['user_id'] ) ) { 359 $user_id_in = implode( ',', wp_parse_id_list( $args['user_id'] ) ); 360 $where_conditions['user_id'] = "user_id IN ({$user_id_in})"; 361 } 362 363 // inviter_id. 0 can be meaningful, in the case of requests. 364 if ( ! empty( $args['inviter_id'] ) || 0 === $args['inviter_id'] ) { 365 $inviter_id_in = implode( ',', wp_parse_id_list( $args['inviter_id'] ) ); 366 $where_conditions['inviter_id'] = "inviter_id IN ({$inviter_id_in})"; 367 } 368 369 // invitee_email 370 if ( ! empty( $args['invitee_email'] ) ) { 371 if ( ! is_array( $args['invitee_email'] ) ) { 372 $invitee_emails = explode( ',', $args['invitee_email'] ); 373 } else { 374 $invitee_emails = $args['invitee_email']; 375 } 376 377 $email_clean = array(); 378 foreach ( $invitee_emails as $email ) { 379 $email_clean[] = $wpdb->prepare( '%s', $email ); 380 } 381 382 $invitee_email_in = implode( ',', $email_clean ); 383 $where_conditions['invitee_email'] = "invitee_email IN ({$invitee_email_in})"; 384 } 385 386 // class 387 if ( ! empty( $args['class'] ) ) { 388 if ( ! is_array( $args['class'] ) ) { 389 $class_names = explode( ',', $args['class'] ); 390 } else { 391 $class_names = $args['class']; 392 } 393 394 $cn_clean = array(); 395 foreach ( $class_names as $cn ) { 396 $cn_clean[] = $wpdb->prepare( '%s', sanitize_key( $cn ) ); 397 } 398 399 $cn_in = implode( ',', $cn_clean ); 400 $where_conditions['class'] = "class IN ({$cn_in})"; 401 } 402 403 // item_id 404 if ( ! empty( $args['item_id'] ) ) { 405 $item_id_in = implode( ',', wp_parse_id_list( $args['item_id'] ) ); 406 $where_conditions['item_id'] = "item_id IN ({$item_id_in})"; 407 } 408 409 // secondary_item_id 410 if ( ! empty( $args['secondary_item_id'] ) ) { 411 $secondary_item_id_in = implode( ',', wp_parse_id_list( $args['secondary_item_id'] ) ); 412 $where_conditions['secondary_item_id'] = "secondary_item_id IN ({$secondary_item_id_in})"; 413 } 414 415 // type 416 if ( ! empty( $args['type'] ) && 'all' !== $args['type'] ) { 417 if ( 'invite' == $args['type'] || 'request' == $args['type'] ) { 418 $type_clean = $wpdb->prepare( '%s', $args['type'] ); 419 $where_conditions['type'] = "type = {$type_clean}"; 420 } 421 } 422 423 // invite_sent 424 // Only create a where statement if something less than "all" has been 425 // specifically requested. 426 if ( ! empty( $args['invite_sent'] ) && 'all' !== $args['invite_sent'] ) { 427 if ( $args['invite_sent'] == 'draft' ) { 428 $where_conditions['invite_sent'] = "invite_sent = 0"; 429 } else if ( $args['invite_sent'] == 'sent' ) { 430 $where_conditions['invite_sent'] = "invite_sent = 1"; 431 } 432 } 433 434 // accepted 435 if ( ! empty( $args['accepted'] ) && 'all' !== $args['accepted'] ) { 436 if ( $args['accepted'] == 'pending' ) { 437 $where_conditions['accepted'] = "accepted = 0"; 438 } else if ( $args['accepted'] == 'accepted' ) { 439 $where_conditions['accepted'] = "accepted = 1"; 440 } 441 } 442 443 // search_terms 444 if ( ! empty( $args['search_terms'] ) ) { 445 $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%'; 446 $where_conditions['search_terms'] = $wpdb->prepare( "( class LIKE %s )", $search_terms_like, $search_terms_like ); 447 } 448 449 // Custom WHERE 450 if ( ! empty( $where_conditions ) ) { 451 $where = 'WHERE ' . implode( ' AND ', $where_conditions ); 452 } 453 454 return $where; 455 } 456 457 /** 458 * Assemble the ORDER BY clause of a get() SQL statement. 459 * 460 * Used by BP_Invitation::get() to create its ORDER BY 461 * clause. 462 * 463 * @since 5.0.0 464 * 465 * @param array $args See {@link BP_Invitation::get()} 466 * for more details. 467 * @return string ORDER BY clause. 468 */ 469 protected static function get_order_by_sql( $args = array() ) { 470 471 // Setup local variable 472 $conditions = array(); 473 $retval = ''; 474 475 // Order by 476 if ( ! empty( $args['order_by'] ) ) { 477 $order_by = implode( ', ', (array) $args['order_by'] ); 478 $conditions['order_by'] = "{$order_by}"; 479 } 480 481 // Sort order direction 482 if ( ! empty( $args['sort_order'] ) ) { 483 $sort_order = bp_esc_sql_order( $args['sort_order'] ); 484 $conditions['sort_order'] = "{$sort_order}"; 485 } 486 487 // Custom ORDER BY 488 if ( ! empty( $conditions ) ) { 489 $retval = 'ORDER BY ' . implode( ' ', $conditions ); 490 } 491 492 return $retval; 493 } 494 495 /** 496 * Assemble the LIMIT clause of a get() SQL statement. 497 * 498 * Used by BP_Invitation::get() to create its LIMIT clause. 499 * 500 * @since 5.0.0 501 * 502 * @param array $args See {@link BP_Invitation::get()} 503 * for more details. 504 * @return string LIMIT clause. 505 */ 506 protected static function get_paged_sql( $args = array() ) { 507 global $wpdb; 508 509 // Setup local variable 510 $retval = ''; 511 512 // Custom LIMIT 513 if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) { 514 $page = absint( $args['page'] ); 515 $per_page = absint( $args['per_page'] ); 516 $offset = $per_page * ( $page - 1 ); 517 $retval = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page ); 518 } 519 520 return $retval; 521 } 522 523 /** 524 * Assemble query clauses, based on arguments, to pass to $wpdb methods. 525 * 526 * The insert(), update(), and delete() methods of {@link wpdb} expect 527 * arguments of the following forms: 528 * 529 * - associative arrays whose key/value pairs are column => value, to 530 * be used in WHERE, SET, or VALUES clauses 531 * - arrays of "formats", which tell $wpdb->prepare() which type of 532 * value to expect when sanitizing (eg, array( '%s', '%d' )) 533 * 534 * This utility method can be used to assemble both kinds of params, 535 * out of a single set of associative array arguments, such as: 536 * 537 * $args = array( 538 * 'user_id' => 4, 539 * 'class' => 'BP_Groups_Invitation_Manager', 540 * ); 541 * 542 * This will be converted to: 543 * 544 * array( 545 * 'data' => array( 546 * 'user_id' => 4, 547 * 'class' => 'BP_Groups_Invitation_Manager', 548 * ), 549 * 'format' => array( 550 * '%d', 551 * '%s', 552 * ), 553 * ) 554 * 555 * which can easily be passed as arguments to the $wpdb methods. 556 * 557 * @since 5.0.0 558 * 559 * @param $args Associative array of filter arguments. 560 * See {@BP_Invitation::get()} for a breakdown. 561 * @return array Associative array of 'data' and 'format' args. 562 */ 563 protected static function get_query_clauses( $args = array() ) { 564 $where_clauses = array( 565 'data' => array(), 566 'format' => array(), 567 ); 568 569 // id 570 if ( ! empty( $args['id'] ) ) { 571 $where_clauses['data']['id'] = absint( $args['id'] ); 572 $where_clauses['format'][] = '%d'; 573 } 574 575 // user_id 576 if ( ! empty( $args['user_id'] ) ) { 577 $where_clauses['data']['user_id'] = absint( $args['user_id'] ); 578 $where_clauses['format'][] = '%d'; 579 } 580 581 // inviter_id 582 if ( ! empty( $args['inviter_id'] ) ) { 583 $where_clauses['data']['inviter_id'] = absint( $args['inviter_id'] ); 584 $where_clauses['format'][] = '%d'; 585 } 586 587 // invitee_email 588 if ( ! empty( $args['invitee_email'] ) ) { 589 $where_clauses['data']['invitee_email'] = $args['invitee_email']; 590 $where_clauses['format'][] = '%s'; 591 } 592 593 // class 594 if ( ! empty( $args['class'] ) ) { 595 $where_clauses['data']['class'] = $args['class']; 596 $where_clauses['format'][] = '%s'; 597 } 598 599 // item_id 600 if ( ! empty( $args['item_id'] ) ) { 601 $where_clauses['data']['item_id'] = absint( $args['item_id'] ); 602 $where_clauses['format'][] = '%d'; 603 } 604 605 // secondary_item_id 606 if ( ! empty( $args['secondary_item_id'] ) ) { 607 $where_clauses['data']['secondary_item_id'] = absint( $args['secondary_item_id'] ); 608 $where_clauses['format'][] = '%d'; 609 } 610 611 // type 612 if ( ! empty( $args['type'] ) && 'all' !== $args['type'] ) { 613 if ( 'invite' == $args['type'] || 'request' == $args['type'] ) { 614 $where_clauses['data']['type'] = $args['type']; 615 $where_clauses['format'][] = '%s'; 616 } 617 } 618 619 // invite_sent 620 // Only create a where statement if something less than "all" has been 621 // specifically requested. 622 if ( isset( $args['invite_sent'] ) && 'all' !== $args['invite_sent'] ) { 623 if ( $args['invite_sent'] == 'draft' ) { 624 $where_clauses['data']['invite_sent'] = 0; 625 $where_clauses['format'][] = '%d'; 626 } else if ( $args['invite_sent'] == 'sent' ) { 627 $where_clauses['data']['invite_sent'] = 1; 628 $where_clauses['format'][] = '%d'; 629 } 630 } 631 632 // accepted 633 if ( ! empty( $args['accepted'] ) && 'all' !== $args['accepted'] ) { 634 if ( $args['accepted'] == 'pending' ) { 635 $where_clauses['data']['accepted'] = 0; 636 $where_clauses['format'][] = '%d'; 637 } else if ( $args['accepted'] == 'accepted' ) { 638 $where_clauses['data']['accepted'] = 1; 639 $where_clauses['format'][] = '%d'; 640 } 641 } 642 643 return $where_clauses; 644 } 645 646 /** Public Static Methods *********************************************/ 647 648 /** 649 * Get invitations, based on provided filter parameters. 650 * 651 * @since 5.0.0 652 * 653 * @param array $args { 654 * Associative array of arguments. All arguments but $page and 655 * $per_page can be treated as filter values for get_where_sql() 656 * and get_query_clauses(). All items are optional. 657 * @type int|array $id ID of invitation being updated. 658 * Can be an array of IDs. 659 * @type int|array $user_id ID of user being queried. Can be an 660 * Can be an array of IDs. 661 * @type int|array $inviter_id ID of user who created the 662 * invitation. Can be an array of IDs. 663 * @type string|array $invitee_email Email address of invited users 664 * being queried. Can be an array of 665 * addresses. 666 * @type string|array $class Name of the class to filter by. 667 * Can be an array of class names. 668 * @type int|array $item_id ID of associated item. 669 * Can be an array of multiple item IDs. 670 * @type int|array $secondary_item_id ID of secondary associated item. 671 * Can be an array of multiple IDs. 672 * @type string|array $type Type of item. An "invite" is sent 673 * from one user to another. 674 * A "request" is submitted by a 675 * user and no inviter is required. 676 * 'all' returns all. Default: 'all'. 677 * @type string $invite_sent Limit to draft, sent or all 678 * 'draft' limits to unsent invites, 679 * 'sent' returns only sent invites, 680 * 'all' returns all. Default: 'all'. 681 * @type bool $accepted Limit to accepted or 682 * not-yet-accepted invitations. 683 * 'accepted' returns accepted invites, 684 * 'pending' returns pending invites, 685 * 'all' returns all. Default: 'pending' 686 * @type string $search_terms Term to match against class field. 687 * @type string $order_by Database column to order by. 688 * @type string $sort_order Either 'ASC' or 'DESC'. 689 * @type string $order_by Field to order results by. 690 * @type string $sort_order ASC or DESC. 691 * @type int $page Number of the current page of results. 692 * Default: false (no pagination, 693 * all items). 694 * @type int $per_page Number of items to show per page. 695 * Default: false (no pagination, 696 * all items). 697 * @type string $fields Which fields to return. Specify 'item_ids' to fetch a list of Item_IDs. 698 * Specify 'ids' to fetch a list of Invitation IDs. 699 * Default: 'all' (return BP_Invitation objects). 700 * } 701 * 702 * @return array BP_Invitation objects | IDs of found invit. 703 */ 704 public static function get( $args = array() ) { 705 global $wpdb; 706 $invites_table_name = BP_Invitation_Manager::get_table_name(); 707 708 // Parse the arguments 709 $r = bp_parse_args( $args, array( 710 'id' => false, 711 'user_id' => false, 712 'inviter_id' => false, 713 'invitee_email' => false, 714 'class' => false, 715 'item_id' => false, 716 'secondary_item_id' => false, 717 'type' => 'all', 718 'invite_sent' => 'all', 719 'accepted' => 'pending', 720 'search_terms' => '', 721 'order_by' => false, 722 'sort_order' => false, 723 'page' => false, 724 'per_page' => false, 725 'fields' => 'all', 726 ), 'bp_invitations_invitation_get' ); 727 728 $sql = array( 729 'select' => "SELECT", 730 'fields' => '', 731 'from' => "FROM {$invites_table_name} i", 732 'where' => '', 733 'orderby' => '', 734 'pagination' => '', 735 ); 736 737 if ( 'item_ids' === $r['fields'] ) { 738 $sql['fields'] = "DISTINCT i.item_id"; 739 } else if ( 'user_ids' === $r['fields'] ) { 740 $sql['fields'] = "DISTINCT i.user_id"; 741 } else { 742 $sql['fields'] = 'DISTINCT i.id'; 743 } 744 745 // WHERE 746 $sql['where'] = self::get_where_sql( array( 747 'id' => $r['id'], 748 'user_id' => $r['user_id'], 749 'inviter_id' => $r['inviter_id'], 750 'invitee_email' => $r['invitee_email'], 751 'class' => $r['class'], 752 'item_id' => $r['item_id'], 753 'secondary_item_id' => $r['secondary_item_id'], 754 'type' => $r['type'], 755 'invite_sent' => $r['invite_sent'], 756 'accepted' => $r['accepted'], 757 'search_terms' => $r['search_terms'], 758 ) ); 759 760 // ORDER BY 761 $sql['orderby'] = self::get_order_by_sql( array( 762 'order_by' => $r['order_by'], 763 'sort_order' => $r['sort_order'] 764 ) ); 765 766 // LIMIT %d, %d 767 $sql['pagination'] = self::get_paged_sql( array( 768 'page' => $r['page'], 769 'per_page' => $r['per_page'], 770 ) ); 771 772 $paged_invites_sql = "{$sql['select']} {$sql['fields']} {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['pagination']}"; 773 774 /** 775 * Filters the pagination SQL statement. 776 * 777 * @since 5.0.0 778 * 779 * @param string $value Concatenated SQL statement. 780 * @param array $sql Array of SQL parts before concatenation. 781 * @param array $r Array of parsed arguments for the get method. 782 */ 783 $paged_invites_sql = apply_filters( 'bp_invitations_get_paged_invitations_sql', $paged_invites_sql, $sql, $r ); 784 785 $cached = bp_core_get_incremented_cache( $paged_invites_sql, 'bp_invitations' ); 786 if ( false === $cached ) { 787 $paged_invite_ids = $wpdb->get_col( $paged_invites_sql ); 788 bp_core_set_incremented_cache( $paged_invites_sql, 'bp_invitations', $paged_invite_ids ); 789 } else { 790 $paged_invite_ids = $cached; 791 } 792 793 // Special return format cases. 794 if ( 'ids' === $r['fields'] || 'item_ids' === $r['fields'] || 'user_ids' === $r['fields'] ) { 795 // We only want the field that was found. 796 return array_map( 'intval', $paged_invite_ids ); 797 } 798 799 $uncached_ids = bp_get_non_cached_ids( $paged_invite_ids, 'bp_invitations' ); 800 if ( $uncached_ids ) { 801 $ids_sql = implode( ',', array_map( 'intval', $uncached_ids ) ); 802 $data_objects = $wpdb->get_results( "SELECT i.* FROM {$invites_table_name} i WHERE i.id IN ({$ids_sql})" ); 803 foreach ( $data_objects as $data_object ) { 804 wp_cache_set( $data_object->id, $data_object, 'bp_invitations' ); 805 } 806 } 807 808 $paged_invites = array(); 809 foreach ( $paged_invite_ids as $paged_invite_id ) { 810 $paged_invites[] = new BP_Invitation( $paged_invite_id ); 811 } 812 813 return $paged_invites; 814 } 815 816 /** 817 * Get a count of total invitations matching a set of arguments. 818 * 819 * @since 5.0.0 820 * 821 * @see BP_Invitation::get() for a description of 822 * arguments. 823 * 824 * @param array $args See {@link BP_Invitation::get()}. 825 * @return int Count of located items. 826 */ 827 public static function get_total_count( $args ) { 828 global $wpdb; 829 $invites_table_name = BP_Invitation_Manager::get_table_name(); 830 831 // Build the query 832 $select_sql = "SELECT COUNT(*)"; 833 $from_sql = "FROM {$invites_table_name}"; 834 $where_sql = self::get_where_sql( $args ); 835 $sql = "{$select_sql} {$from_sql} {$where_sql}"; 836 837 // Return the queried results 838 return $wpdb->get_var( $sql ); 839 } 840 841 /** 842 * Update invitations. 843 * 844 * @since 5.0.0 845 * 846 * @see BP_Invitation::get() for a description of 847 * accepted update/where arguments. 848 * 849 * @param array $update_args Associative array of fields to update, 850 * and the values to update them to. Of the format 851 * array( 'user_id' => 4, 'class' => 'BP_Groups_Invitation_Manager', ) 852 * @param array $where_args Associative array of columns/values, to 853 * determine which rows should be updated. Of the format 854 * array( 'item_id' => 7, 'class' => 'BP_Groups_Invitation_Manager', ) 855 * @return int|bool Number of rows updated on success, false on failure. 856 */ 857 public static function update( $update_args = array(), $where_args = array() ) { 858 $update = self::get_query_clauses( $update_args ); 859 $where = self::get_query_clauses( $where_args ); 860 861 /** 862 * Fires before an invitation is updated. 863 * 864 * @since 5.0.0 865 * 866 * @param array $where_args Associative array of columns/values describing 867 * invitations about to be deleted. 868 * @param array $update_args Array of new values. 869 */ 870 do_action( 'bp_invitation_before_update', $where_args, $update_args ); 871 872 $retval = self::_update( $update['data'], $where['data'], $update['format'], $where['format'] ); 873 874 return $retval; 875 } 876 877 /** 878 * Delete invitations. 879 * 880 * @since 5.0.0 881 * 882 * @see BP_Invitation::get() for a description of 883 * accepted where arguments. 884 * 885 * @param array $args Associative array of columns/values, to determine 886 * which rows should be deleted. Of the format 887 * array( 'item_id' => 7, 'class' => 'BP_Groups_Invitation_Manager', ) 888 * @return int|bool Number of rows deleted on success, false on failure. 889 */ 890 public static function delete( $args = array() ) { 891 $where = self::get_query_clauses( $args ); 892 893 /** 894 * Fires before an invitation is deleted. 895 * 896 * @since 5.0.0 897 * 898 * @param array $args Characteristics of the invitations to be deleted. 899 */ 900 do_action( 'bp_invitation_before_delete', $args ); 901 902 $retval = self::_delete( $where['data'], $where['format'] ); 903 904 /** 905 * Fires after an invitation is deleted. 906 * 907 * @since 5.0.0 908 * 909 * @param array $args Characteristics of the invitations just deleted. 910 */ 911 do_action( 'bp_invitation_after_delete', $args ); 912 913 return $retval; 914 } 915 916 /** Convenience methods ***********************************************/ 917 918 /** 919 * Delete a single invitation by ID. 920 * 921 * @since 5.0.0 922 * 923 * @see BP_Invitation::delete() for explanation of 924 * return value. 925 * 926 * @param int $id ID of the invitation item to be deleted. 927 * @return bool True on success, false on failure. 928 */ 929 public static function delete_by_id( $id ) { 930 return self::delete( array( 931 'id' => $id, 932 ) ); 933 } 934 935 /** Sent status ***********************************************************/ 936 937 /** 938 * Mark specific invitations as sent by invitation ID. 939 * 940 * @since 5.0.0 941 * 942 * @param int $id The ID of the invitation to mark as sent. 943 */ 944 public static function mark_sent( $id = 0 ) { 945 946 if ( ! $id ) { 947 return false; 948 } 949 950 // Values to be updated 951 $update_args = array( 952 'invite_sent' => 'sent', 953 ); 954 955 // WHERE clauses 956 $where_args = array( 957 'id' => $id, 958 ); 959 960 return self::update( $update_args, $where_args ); 961 } 962 963 /** 964 * Mark invitations as sent that are found by user_id, inviter_id, item id, and optional 965 * secondary item id, and class name. 966 * 967 * @since 5.0.0 968 * 969 * @param array $args See BP_Invitation::update(). 970 */ 971 public static function mark_sent_by_data( $args ) { 972 973 // Values to be updated 974 $update_args = array( 975 'invite_sent' => 'sent', 976 ); 977 978 return self::update( $update_args, $args ); 979 } 980 981 /** Accepted status ***********************************************************/ 982 983 /** 984 * Mark specific invitations as accepted by invitation ID. 985 * 986 * @since 5.0.0 987 * 988 * @param int $id The ID of the invitation to mark as sent. 989 */ 990 public static function mark_accepted( $id = 0 ) { 991 992 if ( ! $id ) { 993 return false; 994 } 995 996 // Values to be updated 997 $update_args = array( 998 'accepted' => 'accepted', 999 ); 1000 1001 // WHERE clauses 1002 $where_args = array( 1003 'id' => $id, 1004 ); 1005 1006 return self::update( $update_args, $where_args ); 1007 } 1008 1009 /** 1010 * Mark invitations as accepted that are found by user_id, inviter_id, 1011 * item id, and optional secondary item id, and class name. 1012 * 1013 * @since 5.0.0 1014 * 1015 * @param array $args See BP_Invitation::update(). 1016 */ 1017 public static function mark_accepted_by_data( $args ) { 1018 1019 // Values to be updated 1020 $update_args = array( 1021 'accepted' => 'accepted', 1022 ); 1023 1024 return self::update( $update_args, $args ); 1025 } 1026 1027 } -
src/bp-groups/actions/leave-group.php
diff --git src/bp-groups/actions/leave-group.php src/bp-groups/actions/leave-group.php index 860e9fc1d..1a3913a59 100644
function groups_action_leave_group() { 58 58 /** This filter is documented in bp-groups/bp-groups-actions.php */ 59 59 bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) ); 60 60 } 61 add_action( 'bp_actions', 'groups_action_leave_group' ); 62 No newline at end of file 61 add_action( 'bp_actions', 'groups_action_leave_group' ); 62 63 /** 64 * Clean up requests/invites when a member leaves a group. 65 * 66 * @since 5.0.0 67 */ 68 function groups_action_clean_up_invites_requests( $user_id, $group_id ) { 69 $invites_class = new BP_Groups_Invitation_Manager(); 70 // Remove invitations/requests where the deleted user is the receiver. 71 $invites_class->delete( array( 72 'user_id' => $user_id, 73 'item_id' => $group_id, 74 'type' => 'all' 75 ) ); 76 // Remove invitations where the deleted user is the sender. 77 $invites_class->delete( array( 78 'inviter_id' => $user_id, 79 'item_id' => $group_id, 80 ) ); 81 } 82 add_action( 'bp_groups_member_after_delete', 'groups_action_clean_up_invites_requests', 10, 2 ); -
src/bp-groups/bp-groups-cache.php
diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php index b44ea09f6..87f57e121 100644
function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $ 254 254 add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' ); 255 255 add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' ); 256 256 257 /** 258 * Clear caches on saving a group invitation or request. 259 * The save action is called when inserting a new record or using the save() method 260 * to update an existing record. 261 * 262 * @since 5.0.0 263 * 264 * @param BP_Invitation object $invitation Characteristics of the invitation just saved. 265 */ 266 function bp_groups_clear_user_group_cache_on_invitation_save( BP_Invitation $invitation ) { 267 if ( sanitize_key( 'BP_Groups_Invitation_Manager' ) !== $invitation->class ) { 268 return; 269 } 270 271 wp_cache_delete( $invitation->id, 'bp_groups_invitations_as_memberships' ); 272 } 273 add_action( 'bp_invitation_after_save', 'bp_groups_clear_user_group_cache_on_invitation_save', 10, 2 ); 274 275 /** 276 * Clear caches on invitation deletion or update. 277 * This also catches changes like sending an invite or marking one as accepted. 278 * 279 * @since 5.0.0 280 * 281 * @param array $args Associative array of columns/values describing invitations about to be deleted. 282 */ 283 function bp_groups_clear_user_group_cache_on_invitation_change( $args ) { 284 // Will the delete request act on invitations? 285 if ( empty( $args['class'] ) || 'BP_Groups_Invitation_Manager' !== $args['class'] ) { 286 return; 287 } 288 289 $args['fields' ] = 'ids'; 290 $affected_invitation_ids = groups_get_invites( $args ); 291 foreach ( $affected_invitation_ids as $invitation_id ) { 292 wp_cache_delete( $invitation_id, 'bp_groups_invitations_as_memberships' ); 293 } 294 } 295 add_action( 'bp_invitation_before_delete', 'bp_groups_clear_user_group_cache_on_invitation_change' ); 296 add_action( 'bp_invitation_before_update', 'bp_groups_clear_user_group_cache_on_invitation_change' ); 297 257 298 /** 258 299 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook. 259 300 * … … function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) 269 310 wp_cache_delete( $membership->id, 'bp_groups_memberships' ); 270 311 } 271 312 add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 ); 272 add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );273 add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );274 313 275 314 /** 276 315 * Reset cache incrementor for the Groups component. -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index b391afd76..b1865ddb6 100644
function bp_get_user_groups( $user_id, $args = array() ) { 949 949 950 950 $user_id = intval( $user_id ); 951 951 952 // Standard memberships 952 953 $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 953 954 if ( false === $membership_ids ) { 954 955 $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id ); … … function bp_get_user_groups( $user_id, $args = array() ) { 965 966 } 966 967 } 967 968 969 // Prime the invitations- and requests-as-memberships cache 970 $invitation_ids = array(); 971 if ( true !== $r['is_confirmed'] || false !== $r['invite_sent'] ) { 972 $invitation_ids = groups_get_invites( array( 973 'user_id' => $user_id, 974 'invite_sent' => 'all', 975 'type' => 'all', 976 'fields' => 'ids' 977 ) ); 978 979 // Prime the invitations cache. 980 $uncached_invitation_ids = bp_get_non_cached_ids( $invitation_ids, 'bp_groups_invitations_as_memberships' ); 981 982 $uncached_invitations = groups_get_invites( array( 983 'ids' => $uncached_invitation_ids, 984 'invite_sent' => 'all', 985 'type' => 'all' 986 ) ); 987 foreach ( $uncached_invitations as $uncached_invitation ) { 988 // Reshape the result as a membership db entry. 989 $invitation = new StdClass; 990 $invitation->id = $uncached_invitation->id; 991 $invitation->group_id = $uncached_invitation->item_id; 992 $invitation->user_id = $uncached_invitation->user_id; 993 $invitation->inviter_id = $uncached_invitation->inviter_id; 994 $invitation->is_admin = false; 995 $invitation->is_mod = false; 996 $invitation->user_title = ''; 997 $invitation->date_modified = $uncached_invitation->date_modified; 998 $invitation->comments = $uncached_invitation->content; 999 $invitation->is_confirmed = false; 1000 $invitation->is_banned = false; 1001 $invitation->invite_sent = $uncached_invitation->invite_sent; 1002 wp_cache_set( $uncached_invitation->id, $invitation, 'bp_groups_invitations_as_memberships' ); 1003 } 1004 1005 } 1006 1007 968 1008 // Assemble filter array for use in `wp_list_filter()`. 969 1009 $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) ); 970 1010 foreach ( $filters as $filter_name => $filter_value ) { … … function bp_get_user_groups( $user_id, $args = array() ) { 1006 1046 $groups[ $group_id ] = $membership; 1007 1047 } 1008 1048 1049 // Populate group invitations array from cache, and normalize. 1050 foreach ( $invitation_ids as $invitation_id ) { 1051 $invitation = wp_cache_get( $invitation_id, 'bp_groups_invitations_as_memberships' ); 1052 1053 // Sanity check. 1054 if ( ! isset( $invitation->group_id ) ) { 1055 continue; 1056 } 1057 1058 // Integer values. 1059 foreach ( $int_keys as $index ) { 1060 $invitation->{$index} = intval( $invitation->{$index} ); 1061 } 1062 1063 // Boolean values. 1064 foreach ( $bool_keys as $index ) { 1065 $invitation->{$index} = (bool) $invitation->{$index}; 1066 } 1067 1068 foreach ( $filters as $filter_name => $filter_value ) { 1069 if ( ! isset( $invitation->{$filter_name} ) || $filter_value != $invitation->{$filter_name} ) { 1070 continue 2; 1071 } 1072 } 1073 1074 $group_id = (int) $invitation->group_id; 1075 1076 $groups[ $group_id ] = $invitation; 1077 } 1078 1009 1079 // By default, results are ordered by membership id. 1010 1080 if ( 'group_id' === $r['orderby'] ) { 1011 1081 ksort( $groups ); … … function groups_is_user_banned( $user_id, $group_id ) { 1209 1279 * Check whether a user has an outstanding invitation to a group. 1210 1280 * 1211 1281 * @since 2.6.0 1282 * @since 5.0.0 Added $type parameter. 1212 1283 * 1213 * @param int $user_id ID of the user. 1214 * @param int $group_id ID of the group. 1284 * @param int $user_id ID of the user. 1285 * @param int $group_id ID of the group. 1286 * @param string $type If 'sent', results are limited to those invitations 1287 * that have actually been sent (non-draft). 1288 * Possible values: 'sent', 'draft', or 'all' Default: 'sent'. 1215 1289 * @return int|bool ID of the membership if the user is invited, otherwise false. 1216 1290 */ 1217 function groups_is_user_invited( $user_id, $group_id ) { 1218 $is_invited = false; 1219 1220 $user_groups = bp_get_user_groups( $user_id, array( 1221 'invite_sent' => true, 1222 'is_confirmed' => false, 1223 ) ); 1224 1225 if ( isset( $user_groups[ $group_id ] ) ) { 1226 $is_invited = $user_groups[ $group_id ]->id; 1227 } 1228 1229 return $is_invited; 1291 function groups_is_user_invited( $user_id, $group_id, $type = 'sent' ) { 1292 return groups_check_has_invite_from_user( $user_id, $group_id, false, $type ); 1230 1293 } 1231 1294 1232 1295 /** … … function groups_is_user_invited( $user_id, $group_id ) { 1239 1302 * @return int|bool ID of the membership if the user is pending, otherwise false. 1240 1303 */ 1241 1304 function groups_is_user_pending( $user_id, $group_id ) { 1242 $is_pending = false; 1243 1244 $user_groups = bp_get_user_groups( $user_id, array( 1245 'invite_sent' => false, 1246 'is_confirmed' => false, 1247 ) ); 1248 1249 if ( isset( $user_groups[ $group_id ] ) ) { 1250 $is_pending = $user_groups[ $group_id ]->id; 1305 if ( empty( $user_id ) || empty( $group_id ) ) { 1306 return false; 1251 1307 } 1252 1308 1253 return $is_pending; 1309 $args = array( 1310 'user_id' => $user_id, 1311 'item_id' => $group_id, 1312 ); 1313 $invites_class = new BP_Groups_Invitation_Manager(); 1314 1315 return $invites_class->request_exists( $args ); 1254 1316 } 1255 1317 1256 1318 /** … … function groups_is_user_creator( $user_id, $group_id ) { 1269 1331 /** Group Invitations *********************************************************/ 1270 1332 1271 1333 /** 1272 * Get IDs of users with outstanding invites to a given group from a specified user.1334 * Get group objects for groups that a user is currently invited to. 1273 1335 * 1274 1336 * @since 1.0.0 1275 1337 * 1276 * @param int $user_id ID of the invit inguser.1338 * @param int $user_id ID of the invited user. 1277 1339 * @param int|bool $limit Limit to restrict to. 1278 1340 * @param int|bool $page Optional. Page offset of results to return. 1279 1341 * @param string|array|bool $exclude Array of comma-separated list of group IDs 1280 1342 * to exclude from results. 1281 * @return array $value IDs of users who have been invited to the group by the 1282 * user but have not yet accepted. 1343 * @return array { 1344 * @type array $groups Array of groups returned by paginated query. 1345 * @type int $total Count of groups matching query. 1346 * } 1283 1347 */ 1284 1348 function groups_get_invites_for_user( $user_id = 0, $limit = false, $page = false, $exclude = false ) { 1285 1286 if ( empty( $user_id ) ) 1349 if ( empty( $user_id ) ) { 1287 1350 $user_id = bp_loggedin_user_id(); 1351 } 1352 1353 $group_ids = groups_get_invited_to_group_ids( $user_id ); 1354 1355 // Remove excluded groups. 1356 if ( $exclude ) { 1357 $group_ids = array_diff( $group_ids, wp_parse_id_list( $exclude ) ); 1358 } 1359 1360 // Avoid passing an empty array. 1361 if ( ! $group_ids ) { 1362 $group_ids = array( 0 ); 1363 } 1364 1365 // Get a filtered list of groups. 1366 $args = array( 1367 'include' => $group_ids, 1368 'show_hidden' => true, 1369 'per_page' => $limit, 1370 'page' => $page, 1371 ); 1372 $groups = groups_get_groups( $args ); 1288 1373 1289 return BP_Groups_Member::get_invites( $user_id, $limit, $page, $exclude);1374 return array( 'groups' => $groups['groups'], 'total' => groups_get_invite_count_for_user( $user_id ) ); 1290 1375 } 1291 1376 1292 1377 /** … … function groups_get_invite_count_for_user( $user_id = 0 ) { 1302 1387 $user_id = bp_loggedin_user_id(); 1303 1388 } 1304 1389 1305 return BP_Groups_Member::get_invite_count_for_user( $user_id ); 1390 return count( groups_get_invited_to_group_ids( $user_id ) ); 1391 } 1392 1393 /** 1394 * Get an array of group IDs to which a user is invited. 1395 * 1396 * @since 5.0.0 1397 * 1398 * @param int $user_id The user ID. 1399 * 1400 * @return array Array of group IDs. 1401 */ 1402 function groups_get_invited_to_group_ids( $user_id = 0 ) { 1403 if ( empty( $user_id ) ) { 1404 $user_id = bp_loggedin_user_id(); 1405 } 1406 1407 $group_ids = groups_get_invites( array( 1408 'user_id' => $user_id, 1409 'invite_sent' => 'sent', 1410 'fields' => 'item_ids' 1411 ) ); 1412 1413 return array_unique( $group_ids ); 1306 1414 } 1307 1415 1308 1416 /** … … function groups_get_invite_count_for_user( $user_id = 0 ) { 1318 1426 * ID of the logged-in user. 1319 1427 * @type string $date_modified Optional. Modified date for the invitation. 1320 1428 * Default: current date/time. 1321 * @type bool $is_confirmed Optional. Whether the invitation should be 1322 * marked confirmed. Default: false. 1429 * @type string $content Optional. Message to invitee. 1430 * @type bool $send_invite Optional. Whether the invitation should be 1431 * sent now. Default: false. 1323 1432 * } 1324 1433 * @return bool True on success, false on failure. 1325 1434 */ 1326 1435 function groups_invite_user( $args = '' ) { 1327 1436 1328 $ args= bp_parse_args( $args, array(1437 $r = bp_parse_args( $args, array( 1329 1438 'user_id' => false, 1330 1439 'group_id' => false, 1331 1440 'inviter_id' => bp_loggedin_user_id(), 1332 1441 'date_modified' => bp_core_current_time(), 1333 'is_confirmed' => 0 1442 'content' => '', 1443 'send_invite' => 0 1334 1444 ), 'groups_invite_user' ); 1335 extract( $args, EXTR_SKIP );1336 1337 if ( ! $user_id || ! $group_id || ! $inviter_id ) {1338 return false;1339 }1340 1341 // If the user has already requested membership, accept the request.1342 if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id ) ) {1343 groups_accept_membership_request( $membership_id, $user_id, $group_id );1344 1445 1345 // Otherwise, create a new invitation.1346 } elseif ( ! groups_is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) {1347 $invite = new BP_Groups_Member;1348 $invite->group_id = $group_id;1349 $invite->user_id = $user_id;1350 $invite->date_modified = $date_modified;1351 $invite->inviter_id = $inviter_id;1352 $invite->is_confirmed = $is_confirmed;1446 $inv_args = array( 1447 'user_id' => $r['user_id'], 1448 'item_id' => $r['group_id'], 1449 'inviter_id' => $r['inviter_id'], 1450 'date_modified' => $r['date_modified'], 1451 'content' => $r['content'], 1452 'send_invite' => $r['send_invite'] 1453 ); 1353 1454 1354 if ( !$invite->save() ) 1355 return false; 1455 // Create the unsent invitataion. 1456 $invites_class = new BP_Groups_Invitation_Manager(); 1457 $created = $invites_class->add_invitation( $inv_args ); 1356 1458 1357 1358 1359 1360 1361 1362 * @param array $argsArray of parsed arguments for the group invite.1363 */1364 do_action( 'groups_invite_user', $args );1365 }1459 /** 1460 * Fires after the creation of a new group invite. 1461 * 1462 * @since 1.0.0 1463 * 1464 * @param array $r Array of parsed arguments for the group invite. 1465 * @param int|bool $created The ID of the invitation or false if it couldn't be created. 1466 */ 1467 do_action( 'groups_invite_user', $r, $created ); 1366 1468 1367 return true;1469 return $created; 1368 1470 } 1369 1471 1370 1472 /** 1371 1473 * Uninvite a user from a group. 1372 1474 * 1373 * Functionally, this is equivalent to removing a user from a group.1374 *1375 1475 * @since 1.0.0 1376 1476 * 1377 1477 * @param int $user_id ID of the user. 1378 1478 * @param int $group_id ID of the group. 1479 * @param int $inviter_id ID of the inviter. 1379 1480 * @return bool True on success, false on failure. 1380 1481 */ 1381 function groups_uninvite_user( $user_id, $group_id ) { 1382 1383 if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) ) 1482 function groups_uninvite_user( $user_id, $group_id, $inviter_id = false ) { 1483 if ( empty( $user_id ) || empty( $group_id ) ) { 1384 1484 return false; 1485 } 1385 1486 1386 /** 1387 * Fires after uninviting a user from a group. 1388 * 1389 * @since 1.0.0 1390 * 1391 * @param int $group_id ID of the group being uninvited from. 1392 * @param int $user_id ID of the user being uninvited. 1393 */ 1394 do_action( 'groups_uninvite_user', $group_id, $user_id ); 1487 $invites_class = new BP_Groups_Invitation_Manager(); 1488 $success = $invites_class->delete( array( 1489 'user_id' => $user_id, 1490 'item_id' => $group_id, 1491 'inviter_id' => $inviter_id, 1492 ) ); 1395 1493 1396 return true; 1494 if ( $success ) { 1495 /** 1496 * Fires after uninviting a user from a group. 1497 * 1498 * @since 1.0.0 1499 * @since 2.7.0 Added $inviter_id parameter 1500 * 1501 * @param int $group_id ID of the group being uninvited from. 1502 * @param int $user_id ID of the user being uninvited. 1503 * @param int $inviter_id ID of the inviter. 1504 */ 1505 do_action( 'groups_uninvite_user', $group_id, $user_id, $inviter_id ); 1506 } 1507 1508 return $success; 1397 1509 } 1398 1510 1399 1511 /** … … function groups_uninvite_user( $user_id, $group_id ) { 1408 1520 * @return bool True when the user is a member of the group, otherwise false. 1409 1521 */ 1410 1522 function groups_accept_invite( $user_id, $group_id ) { 1523 $invites_class = new BP_Groups_Invitation_Manager(); 1524 $args = array( 1525 'user_id' => $user_id, 1526 'item_id' => $group_id, 1527 'invite_sent' => 'sent', 1528 ); 1411 1529 1412 // If the user is already a member (because BP at one point allowed two invitations to 1413 // slip through), delete all existing invitations/requests and return true. 1414 if ( groups_is_user_member( $user_id, $group_id ) ) { 1415 if ( groups_check_user_has_invite( $user_id, $group_id ) ) { 1416 groups_delete_invite( $user_id, $group_id ); 1417 } 1418 1419 if ( groups_check_for_membership_request( $user_id, $group_id ) ) { 1420 groups_delete_membership_request( null, $user_id, $group_id ); 1421 } 1422 1423 return true; 1424 } 1425 1426 $member = new BP_Groups_Member( $user_id, $group_id ); 1427 1428 // Save the inviter ID so that we can pass it to the action below. 1429 $inviter_id = $member->inviter_id; 1430 1431 $member->accept_invite(); 1432 1433 if ( !$member->save() ) { 1434 return false; 1435 } 1436 1437 // Remove request to join. 1438 if ( $member->check_for_membership_request( $user_id, $group_id ) ) { 1439 $member->delete_request( $user_id, $group_id ); 1440 } 1441 1442 // Modify group meta. 1443 groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); 1444 1445 /** 1446 * Fires after a user has accepted a group invite. 1447 * 1448 * @since 1.0.0 1449 * @since 2.8.0 The $inviter_id arg was added. 1450 * 1451 * @param int $user_id ID of the user who accepted the group invite. 1452 * @param int $group_id ID of the group being accepted to. 1453 * @param int $inviter_id ID of the user who invited this user to the group. 1454 */ 1455 do_action( 'groups_accept_invite', $user_id, $group_id, $inviter_id ); 1456 1457 return true; 1530 return $invites_class->accept_invitation( $args ); 1458 1531 } 1459 1532 1460 1533 /** 1461 1534 * Reject a group invitation. 1462 1535 * 1463 1536 * @since 1.0.0 1537 * @since 5.0.0 The $inviter_id arg was added. 1538 * 1539 * @param int $user_id ID of the user. 1540 * @param int $group_id ID of the group. 1541 * @param int $inviter_id ID of the inviter. 1464 1542 * 1465 * @param int $user_id ID of the user.1466 * @param int $group_id ID of the group.1467 1543 * @return bool True on success, false on failure. 1468 1544 */ 1469 function groups_reject_invite( $user_id, $group_id ) {1470 if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )1545 function groups_reject_invite( $user_id, $group_id, $inviter_id = false ) { 1546 if ( empty( $user_id ) || empty( $group_id ) ) { 1471 1547 return false; 1548 } 1549 1550 $invites_class = new BP_Groups_Invitation_Manager(); 1551 $success = $invites_class->delete( array( 1552 'user_id' => $user_id, 1553 'item_id' => $group_id, 1554 'inviter_id' => $inviter_id, 1555 ) ); 1472 1556 1473 1557 /** 1474 1558 * Fires after a user rejects a group invitation. 1475 1559 * 1476 1560 * @since 1.0.0 1561 * @since 5.0.0 The $inviter_id arg was added. 1477 1562 * 1478 1563 * @param int $user_id ID of the user rejecting the invite. 1479 1564 * @param int $group_id ID of the group being rejected. 1565 * @param int $inviter_id ID of the inviter. 1480 1566 */ 1481 do_action( 'groups_reject_invite', $user_id, $group_id );1567 do_action( 'groups_reject_invite', $user_id, $group_id, $inviter_id ); 1482 1568 1483 return true;1569 return $success; 1484 1570 } 1485 1571 1486 1572 /** 1487 1573 * Delete a group invitation. 1488 1574 * 1489 1575 * @since 1.0.0 1576 * @since 5.0.0 The $inviter_id arg was added. 1490 1577 * 1491 1578 * @param int $user_id ID of the invited user. 1492 1579 * @param int $group_id ID of the group. 1580 * @param int $inviter_id ID of the inviter. 1581 * 1493 1582 * @return bool True on success, false on failure. 1494 1583 */ 1495 function groups_delete_invite( $user_id, $group_id ) {1496 if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )1584 function groups_delete_invite( $user_id, $group_id, $inviter_id = false ) { 1585 if ( empty( $user_id ) || empty( $group_id ) ) { 1497 1586 return false; 1587 } 1588 1589 $invites_class = new BP_Groups_Invitation_Manager(); 1590 $success = $invites_class->delete( array( 1591 'user_id' => $user_id, 1592 'item_id' => $group_id, 1593 'inviter_id' => $inviter_id, 1594 ) ); 1498 1595 1499 1596 /** 1500 1597 * Fires after the deletion of a group invitation. 1501 1598 * 1502 1599 * @since 1.9.0 1600 * @since 5.0.0 The $inviter_id arg was added. 1503 1601 * 1504 1602 * @param int $user_id ID of the user whose invitation is being deleted. 1505 1603 * @param int $group_id ID of the group whose invitation is being deleted. 1604 * @param int $inviter_id ID of the inviter. 1506 1605 */ 1507 do_action( 'groups_delete_invite', $user_id, $group_id );1606 do_action( 'groups_delete_invite', $user_id, $group_id, $inviter_id ); 1508 1607 1509 1608 return true; 1510 1609 } 1511 1610 1512 1611 /** 1513 * Send all pending invites by a single user to a specific group.1612 * Send some or all pending invites by a single user to a specific group. 1514 1613 * 1515 1614 * @since 1.0.0 1615 * @since 5.0.0 Parameters changed to associative array. 1516 1616 * 1517 * @param int $user_id ID of the inviting user. 1518 * @param int $group_id ID of the group. 1617 * @param array $args { 1618 * An array of optional arguments. 1619 * @type int $user_id ID of the invited user. 1620 * @type string $invitee_email Email address of the invited user, if not a member of the site. 1621 * @type string $group_id ID of the group or an array of group IDs. 1622 * @type string $inviter_id ID of the user extending the invitation. 1623 * @type bool $force_resend Whether to resend the email & notification if one has already been sent. 1624 * } 1519 1625 */ 1520 function groups_send_invites( $user_id, $group_id ) { 1626 function groups_send_invites( $args = array() ) { 1627 // Backward compatibility with old method of passing arguments. 1628 if ( ! is_array( $args ) || func_num_args() > 1 ) { 1629 _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 1521 1630 1522 if ( empty( $user_id ) ) 1523 $user_id = bp_loggedin_user_id(); 1631 $old_args_keys = array( 1632 0 => 'inviter_id', 1633 1 => 'group_id', 1634 ); 1524 1635 1525 // Send friend invites. 1526 $invited_users = groups_get_invites_for_group( $user_id, $group_id ); 1527 $group = groups_get_group( $group_id ); 1636 $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 1637 } 1528 1638 1529 for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) { 1530 $member = new BP_Groups_Member( $invited_users[$i], $group_id ); 1639 $r = bp_parse_args( $args, array( 1640 'user_id' => false, 1641 'invitee_email' => '', 1642 'group_id' => 0, 1643 'inviter_id' => bp_loggedin_user_id(), 1644 'force_resend' => false, 1645 ), 'groups_send_invitation' ); 1531 1646 1532 // Skip if we've already sent an invite to this user. 1533 if ( $member->invite_sent ) { 1534 continue; 1535 } 1647 /* 1648 * We will generally only want to fetch unsent invitations. 1649 * If force_resend is true, then we need to fetch both sent and draft invites. 1650 */ 1651 if ( $r['force_resend'] ) { 1652 $args['invite_sent'] = 'all'; 1653 } else { 1654 $args['invite_sent'] = 'draft'; 1655 } 1536 1656 1537 // Send the actual invite. 1538 groups_notification_group_invites( $group, $member, $user_id ); 1657 $args = array( 1658 'user_id' => $r['user_id'], 1659 'invitee_email' => $r['invitee_email'], 1660 'item_id' => $r['group_id'], 1661 'inviter_id' => $r['inviter_id'], 1662 ); 1663 $invites = groups_get_invites( $args ); 1539 1664 1540 $member->invite_sent = 1; 1541 $member->save(); 1665 $invited_users = array(); 1666 1667 $invites_class = new BP_Groups_Invitation_Manager(); 1668 foreach ( $invites as $invite ) { 1669 $invited_users[] = $invite->user_id; 1670 $invites_class->send_invitation_by_id( $invite->id ); 1542 1671 } 1543 1672 1544 1673 /** … … function groups_send_invites( $user_id, $group_id ) { 1551 1680 * @param array $invited_users Array of users being invited to the group. 1552 1681 * @param int $user_id ID of the inviting user. 1553 1682 */ 1554 do_action( 'groups_send_invites', $ group_id, $invited_users, $user_id);1683 do_action( 'groups_send_invites', $r['group_id'], $invited_users, $r['inviter_id'] ); 1555 1684 } 1556 1685 1557 1686 /** 1558 * Get IDs of users with outstanding invites to a given group from a specified user.1687 * Get IDs of users with outstanding invites to a given group. 1559 1688 * 1560 1689 * @since 1.0.0 1561 1690 * @since 2.9.0 Added $sent as a parameter. … … function groups_get_invites_for_group( $user_id, $group_id, $sent = null ) { 1573 1702 return BP_Groups_Group::get_invites( $user_id, $group_id, $sent ); 1574 1703 } 1575 1704 1705 /** 1706 * Get invitations to a given group filtered by arguments. 1707 * 1708 * @since 5.0.0 1709 * 1710 * @param int $group_id ID of the group. 1711 * @param array $args Invitation arguments. 1712 * See BP_Invitation::get() for list. 1713 * 1714 * @return array $invites Matching BP_Invitation objects. 1715 */ 1716 function groups_get_invites( $args = array() ) { 1717 $invites_class = new BP_Groups_Invitation_Manager(); 1718 return $invites_class->get_invitations( $args ); 1719 } 1720 1576 1721 /** 1577 1722 * Check to see whether a user has already been invited to a group. 1578 1723 * … … function groups_get_invites_for_group( $user_id, $group_id, $sent = null ) { 1586 1731 * @param int $group_id ID of potential group. 1587 1732 * @param string $type Optional. Use 'sent' to check for sent invites, 1588 1733 * 'all' to check for all. Default: 'sent'. 1589 * @return int|bool ID of the membership if found, otherwise false.1734 * @return int|bool ID of the first found membership if found, otherwise false. 1590 1735 */ 1591 1736 function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent' ) { 1592 $invite = false; 1737 return groups_check_has_invite_from_user( $user_id, $group_id, false, $type ); 1738 } 1739 1740 /** 1741 * Check to see whether a user has already been invited to a group by a particular user. 1742 * 1743 * By default, the function checks for invitations that have been sent. 1744 * Entering 'all' as the $type parameter will return unsent invitations as 1745 * well (useful to make sure AJAX requests are not duplicated). 1746 * 1747 * @since 5.0.0 1748 * 1749 * @param int $user_id ID of potential group member. 1750 * @param int $group_id ID of potential group. 1751 * @param string $inviter_id Optional. Use 'sent' to check for sent invites, 1752 * 'all' to check for all. Default: 'sent'. 1753 * @param string $type Optional. Specify a user ID to limit to only invited from that user. 1754 * Default: 'false'. 1755 * @return int|bool ID of the first found membership if found, otherwise false. 1756 */ 1757 function groups_check_has_invite_from_user( $user_id, $group_id, $inviter_id = false, $type = 'sent' ) { 1758 if ( empty( $user_id ) || empty( $group_id ) ) { 1759 return false; 1760 } 1593 1761 1594 1762 $args = array( 1595 'is_confirmed' => false, 1596 'is_banned' => null, 1597 'is_admin' => null, 1598 'is_mod' => null, 1763 'user_id' => $user_id, 1764 'item_id' => $group_id, 1765 'invite_sent' => 'sent', 1599 1766 ); 1600 1601 if ( 'sent' === $type ) { 1602 $args['invite_sent'] = true; 1767 if ( $inviter_id ) { 1768 $args['inviter_id'] = $inviter_id; 1603 1769 } 1604 1605 $user_groups = bp_get_user_groups( $user_id, $args ); 1606 1607 if ( isset( $user_groups[ $group_id ] ) && 0 !== $user_groups[ $group_id ]->inviter_id ) { 1608 $invite = $user_groups[ $group_id ]->id; 1770 if ( $type === 'draft' || $type === 'all' ) { 1771 $args['invite_sent'] = $type; 1609 1772 } 1610 1773 1611 return $invite; 1774 $invites_class = new BP_Groups_Invitation_Manager(); 1775 1776 return $invites_class->invitation_exists( $args ); 1612 1777 } 1613 1778 1614 1779 /** … … function groups_remove_member( $user_id, $group_id ) { 1785 1950 * 1786 1951 * @since 1.0.0 1787 1952 * 1788 * @param int $requesting_user_id ID of the user requesting membership. 1789 * @param int $group_id ID of the group. 1953 * @param array|string $args { 1954 * Array of arguments. 1955 * @type int $user_id ID of the user being invited. 1956 * @type int $group_id ID of the group to which the user is being invited. 1957 * @type string $content Optional. Message to invitee. 1958 * @type string $date_modified Optional. Modified date for the invitation. 1959 * Default: current date/time. 1960 * } 1790 1961 * @return bool True on success, false on failure. 1791 1962 */ 1792 function groups_send_membership_request( $requesting_user_id, $group_id ) { 1793 1794 // Prevent duplicate requests. 1795 if ( groups_check_for_membership_request( $requesting_user_id, $group_id ) ) 1796 return false; 1963 function groups_send_membership_request( $args = array() ) { 1964 // Backward compatibility with old method of passing arguments. 1965 if ( ! is_array( $args ) || func_num_args() > 1 ) { 1966 _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 1797 1967 1798 // Check if the user is already a member or is banned. 1799 if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) ) 1800 return false; 1968 $old_args_keys = array( 1969 0 => 'user_id', 1970 1 => 'group_id', 1971 ); 1801 1972 1802 // Check if the user is already invited - if so, simply accept invite. 1803 if ( groups_check_user_has_invite( $requesting_user_id, $group_id ) ) { 1804 groups_accept_invite( $requesting_user_id, $group_id ); 1805 return true; 1973 $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 1806 1974 } 1807 1975 1808 $requesting_user = new BP_Groups_Member; 1809 $requesting_user->group_id = $group_id; 1810 $requesting_user->user_id = $requesting_user_id; 1811 $requesting_user->inviter_id = 0; 1812 $requesting_user->is_admin = 0; 1813 $requesting_user->user_title = ''; 1814 $requesting_user->date_modified = bp_core_current_time(); 1815 $requesting_user->is_confirmed = 0; 1816 $requesting_user->comments = isset( $_POST['group-request-membership-comments'] ) ? $_POST['group-request-membership-comments'] : ''; 1976 $r = bp_parse_args( $args, array( 1977 'user_id' => false, 1978 'group_id' => false, 1979 'content' => '', 1980 'date_modified' => bp_core_current_time(), 1981 ), 'groups_send_membership_request' ); 1982 1983 $inv_args = array( 1984 'user_id' => $r['user_id'], 1985 'item_id' => $r['group_id'], 1986 'content' => $r['content'], 1987 'date_modified' => $r['date_modified'], 1988 ); 1817 1989 1818 if ( $requesting_user->save() ) {1819 $admins = groups_get_group_admins( $group_id);1990 $invites_class = new BP_Groups_Invitation_Manager(); 1991 $request_id = $invites_class->add_request( $inv_args ); 1820 1992 1821 // Saved okay, now send the email notification. 1822 for ( $i = 0, $count = count( $admins ); $i < $count; ++$i ) 1823 groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id ); 1993 // If a new request was created, send the emails. 1994 if ( $request_id && is_int( $request_id ) ) { 1995 $invites_class->send_request_notification_by_id( $request_id ); 1996 $admins = groups_get_group_admins( $r['group_id'] ); 1824 1997 1825 1998 /** 1826 1999 * Fires after the creation of a new membership request. … … function groups_send_membership_request( $requesting_user_id, $group_id ) { 1830 2003 * @param int $requesting_user_id ID of the user requesting membership. 1831 2004 * @param array $admins Array of group admins. 1832 2005 * @param int $group_id ID of the group being requested to. 1833 * @param int $request ing_user->id ID of the membership.2006 * @param int $request_id ID of the request. 1834 2007 */ 1835 do_action( 'groups_membership_requested', $r equesting_user_id, $admins, $group_id, $requesting_user->id );2008 do_action( 'groups_membership_requested', $r['user_id'], $admins, $r['group_id'], $request_id ); 1836 2009 1837 return true;2010 return $request_id; 1838 2011 } 1839 2012 1840 2013 return false; … … function groups_send_membership_request( $requesting_user_id, $group_id ) { 1844 2017 * Accept a pending group membership request. 1845 2018 * 1846 2019 * @since 1.0.0 2020 * @since 5.0.0 Deprecated $membership_id argument. 1847 2021 * 1848 * @param int $membership_id ID of the membership object.1849 * @param int $user_id Optional. ID of the user who requested membership.2022 * @param int $membership_id Deprecated 5.0.0. 2023 * @param int $user_id Required. ID of the user who requested membership. 1850 2024 * Provide this value along with $group_id to override 1851 2025 * $membership_id. 1852 * @param int $group_id Optional. ID of the group to which membership is being2026 * @param int $group_id Required. ID of the group to which membership is being 1853 2027 * requested. Provide this value along with $user_id to 1854 2028 * override $membership_id. 1855 2029 * @return bool True on success, false on failure. 1856 2030 */ 1857 2031 function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 1858 2032 1859 if ( !empty( $user_id ) && !empty( $group_id ) ) { 1860 $membership = new BP_Groups_Member( $user_id, $group_id ); 1861 } else { 1862 $membership = new BP_Groups_Member( false, false, $membership_id ); 2033 if ( ! empty( $membership_id ) ) { 2034 _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 1863 2035 } 1864 2036 1865 $membership->accept_request(); 1866 1867 if ( !$membership->save() ) { 2037 if ( ! $user_id || ! $group_id ) { 1868 2038 return false; 1869 2039 } 1870 2040 1871 // Check if the user has an outstanding invite, if so delete it. 1872 if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) ) { 1873 groups_delete_invite( $membership->user_id, $membership->group_id ); 1874 } 1875 1876 /** 1877 * Fires after a group membership request has been accepted. 1878 * 1879 * @since 1.0.0 1880 * 1881 * @param int $user_id ID of the user who accepted membership. 1882 * @param int $group_id ID of the group that was accepted membership to. 1883 * @param bool $value If membership was accepted. 1884 */ 1885 do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id, true ); 2041 $invites_class = new BP_Groups_Invitation_Manager(); 2042 $args = array( 2043 'user_id' => $user_id, 2044 'item_id' => $group_id, 2045 ); 1886 2046 1887 return true;2047 return $invites_class->accept_request( $args ); 1888 2048 } 1889 2049 1890 2050 /** … … function groups_accept_membership_request( $membership_id, $user_id = 0, $group_ 1892 2052 * 1893 2053 * @since 1.0.0 1894 2054 * 1895 * @param int $membership_id ID of the membership object.2055 * @param int $membership_id Deprecated 5.0.0. 1896 2056 * @param int $user_id Optional. ID of the user who requested membership. 1897 2057 * Provide this value along with $group_id to override 1898 2058 * $membership_id. … … function groups_accept_membership_request( $membership_id, $user_id = 0, $group_ 1902 2062 * @return bool True on success, false on failure. 1903 2063 */ 1904 2064 function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 1905 if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) ) { 2065 2066 if ( ! empty( $membership_id ) ){ 2067 _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 2068 } 2069 2070 if ( ! groups_delete_membership_request( false, $user_id, $group_id ) ) { 1906 2071 return false; 1907 2072 } 1908 2073 … … function groups_reject_membership_request( $membership_id, $user_id = 0, $group_ 1915 2080 * @param int $group_id ID of the group that was rejected membership to. 1916 2081 * @param bool $value If membership was accepted. 1917 2082 */ 1918 do_action( 'groups_membership_rejected', $ membership->user_id, $membership->group_id, false );2083 do_action( 'groups_membership_rejected', $user_id, $group_id, false ); 1919 2084 1920 2085 return true; 1921 2086 } … … function groups_reject_membership_request( $membership_id, $user_id = 0, $group_ 1925 2090 * 1926 2091 * @since 1.2.0 1927 2092 * 1928 * @param int $membership_id ID of the membership object.2093 * @param int $membership_id Deprecated 5.0.0. 1929 2094 * @param int $user_id Optional. ID of the user who requested membership. 1930 2095 * Provide this value along with $group_id to override 1931 2096 * $membership_id. … … function groups_reject_membership_request( $membership_id, $user_id = 0, $group_ 1935 2100 * @return false|BP_Groups_Member True on success, false on failure. 1936 2101 */ 1937 2102 function groups_delete_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 1938 if ( !empty( $user_id ) && !empty( $group_id ) ) 1939 $membership = new BP_Groups_Member( $user_id, $group_id ); 1940 else 1941 $membership = new BP_Groups_Member( false, false, $membership_id ); 2103 if ( ! empty( $membership_id ) ){ 2104 _deprecated_argument( __METHOD__, '5.0.0', sprintf( __( 'Argument `membership_id` passed to %1$s is deprecated. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 2105 } 1942 2106 1943 if ( ! BP_Groups_Member::delete_request( $membership->user_id, $membership->group_id ) )2107 if ( empty( $user_id ) || empty( $group_id ) ) { 1944 2108 return false; 2109 } 2110 2111 $invites_class = new BP_Groups_Invitation_Manager(); 2112 $success = $invites_class->delete_requests( array( 2113 'user_id' => $user_id, 2114 'item_id' => $group_id 2115 ) ); 2116 2117 return $success; 2118 } 1945 2119 1946 return $membership; 2120 /** 2121 * Get group membership requests filtered by arguments. 2122 * 2123 * @since 5.0.0 2124 * 2125 * @param int $group_id ID of the group. 2126 * @param array $args Invitation arguments. 2127 * See BP_Invitation::get() for list. 2128 * 2129 * @return array $requests Matching BP_Invitation objects. 2130 */ 2131 function groups_get_requests( $args = array() ) { 2132 $invites_class = new BP_Groups_Invitation_Manager(); 2133 return $invites_class->get_requests( $args ); 1947 2134 } 1948 2135 1949 2136 /** … … function groups_delete_membership_request( $membership_id, $user_id = 0, $group_ 1953 2140 * 1954 2141 * @param int $user_id ID of the user. 1955 2142 * @param int $group_id ID of the group. 1956 * @return int|bool ID of the membershipif found, otherwise false.2143 * @return int|bool ID of the request if found, otherwise false. 1957 2144 */ 1958 2145 function groups_check_for_membership_request( $user_id, $group_id ) { 1959 $request = false; 2146 if ( empty( $user_id ) || empty( $group_id ) ) { 2147 return false; 2148 } 1960 2149 1961 $user_groups = bp_get_user_groups( $user_id, array( 1962 'is_confirmed' => false, 1963 'is_banned' => false, 1964 'is_admin' => null, 1965 'is_mod' => null 2150 $args = array( 2151 'user_id' => $user_id, 2152 'item_id' => $group_id, 2153 ); 2154 $invites_class = new BP_Groups_Invitation_Manager(); 2155 2156 return $invites_class->request_exists( $args ); 2157 } 2158 2159 /** 2160 * Get an array of group IDs to which a user has requested membership. 2161 * 2162 * @since 5.0.0 2163 * 2164 * @param int $user_id The user ID. 2165 * 2166 * @return array Array of group IDs. 2167 */ 2168 function groups_get_membership_requested_group_ids( $user_id = 0 ) { 2169 if ( ! $user_id ) { 2170 $user_id = bp_loggedin_user_id(); 2171 } 2172 2173 $group_ids = groups_get_requests( array( 2174 'user_id' => $user_id, 2175 'fields' => 'item_ids' 1966 2176 ) ); 1967 2177 1968 if ( isset( $user_groups[ $group_id ] ) && 0 === $user_groups[ $group_id ]->inviter_id ) { 1969 $request = $user_groups[ $group_id ]->id; 1970 } 2178 return $group_ids; 2179 } 2180 2181 /** 2182 * Get an array of group IDs to which a user has requested membership. 2183 * 2184 * @since 5.0.0 2185 * 2186 * @param int $user_id The user ID. 2187 * 2188 * @return array Array of group IDs. 2189 */ 2190 function groups_get_membership_requested_user_ids( $group_id = 0 ) { 2191 if ( ! $group_id ) { 2192 $group_id = bp_get_current_group_id(); 2193 } 2194 2195 $requests = groups_get_requests( array( 2196 'item_id' => $group_id, 2197 'fields' => 'user_ids' 2198 ) ); 1971 2199 1972 return $request; 2200 //@TODO-6210: What about those who make a request by email only (not yet site members)? 2201 return $requests; 1973 2202 } 1974 2203 1975 2204 /** … … function groups_check_for_membership_request( $user_id, $group_id ) { 1980 2209 * @param int $group_id ID of the group. 1981 2210 * @return bool True on success, false on failure. 1982 2211 */ 1983 function groups_accept_all_pending_membership_requests( $group_id ) { 1984 $user_ids = BP_Groups_Member::get_all_membership_request_user_ids( $group_id ); 2212 function groups_accept_all_pending_membership_requests( $group_id = 0 ) { 2213 if ( ! $group_id ) { 2214 $group_id = bp_get_current_group_id(); 2215 } 1985 2216 1986 if ( !$user_ids ) 2217 $user_ids = groups_get_membership_requested_user_ids( $group_id ); 2218 2219 if ( ! $user_ids ) { 1987 2220 return false; 2221 } 1988 2222 1989 foreach ( (array) $user_ids as $user_id ) 2223 foreach ( (array) $user_ids as $user_id ) { 1990 2224 groups_accept_membership_request( false, $user_id, $group_id ); 2225 } 1991 2226 1992 2227 /** 1993 2228 * Fires after the acceptance of all pending membership requests to a group. … … function bp_groups_pending_requests_personal_data_exporter( $email_address, $pag 2663 2898 ); 2664 2899 } 2665 2900 2666 $requests = BP_Groups_Member::get_user_memberships( $user->ID,array(2667 ' type' => 'pending_request',2901 $requests = groups_get_requests( array( 2902 'user_id' => $user->ID, 2668 2903 'page' => $page, 2669 2904 'per_page' => $number, 2670 2905 ) ); 2671 2906 2672 2907 foreach ( $requests as $request ) { 2673 $group = groups_get_group( $request-> group_id );2908 $group = groups_get_group( $request->item_id ); 2674 2909 2675 2910 $item_data = array( 2676 2911 array( … … function bp_groups_pending_sent_invitations_personal_data_exporter( $email_addre 2729 2964 ); 2730 2965 } 2731 2966 2732 $invitations = BP_Groups_Member::get_user_memberships( $user->ID,array(2733 ' type' => 'pending_sent_invitation',2734 'page' => $page,2735 'per_page' => $number,2967 $invitations = groups_get_invites( array( 2968 'inviter_id' => $user->ID, 2969 'page' => $page, 2970 'per_page' => $number, 2736 2971 ) ); 2737 2972 2738 2973 foreach ( $invitations as $invitation ) { 2739 $group = groups_get_group( $invitation-> group_id );2974 $group = groups_get_group( $invitation->item_id ); 2740 2975 2741 2976 $item_data = array( 2742 2977 array( … … function bp_groups_pending_received_invitations_personal_data_exporter( $email_a 2799 3034 ); 2800 3035 } 2801 3036 2802 $invitations = BP_Groups_Member::get_user_memberships( $user->ID,array(2803 ' type' => 'pending_received_invitation',3037 $invitations = groups_get_invites( array( 3038 'user_id' => $user->ID, 2804 3039 'page' => $page, 2805 3040 'per_page' => $number, 2806 3041 ) ); 2807 3042 2808 3043 foreach ( $invitations as $invitation ) { 2809 $group = groups_get_group( $invitation-> group_id );3044 $group = groups_get_group( $invitation->item_id ); 2810 3045 2811 3046 $item_data = array( 2812 3047 array( … … function bp_groups_pending_received_invitations_personal_data_exporter( $email_a 2843 3078 'done' => $done, 2844 3079 ); 2845 3080 } 3081 3082 /** 3083 * Migrate invitations and requests from pre-5.0 group_members table to invitations table. 3084 * 3085 * @since 5.0.0 3086 */ 3087 function bp_groups_migrate_invitations() { 3088 global $wpdb; 3089 $bp = buddypress(); 3090 3091 $records = $wpdb->get_results( "SELECT id, group_id, user_id, inviter_id, date_modified, comments, invite_sent FROM {$bp->groups->table_name_members} WHERE is_confirmed = 0 AND is_banned = 0" ); 3092 3093 $processed = array(); 3094 foreach ( $records as $record ) { 3095 $invitation = new BP_Invitation; 3096 $invitation->user_id = $record->user_id; 3097 $invitation->inviter_id = $record->inviter_id; 3098 $invitation->invitee_email = ''; 3099 $invitation->class = 'BP_Groups_Invitation_Manager'; 3100 $invitation->item_id = $record->group_id; 3101 $invitation->secondary_item_id = 0; 3102 $invitation->type = ( 0 === (int) $record->inviter_id ) ? 'request' : 'invite'; 3103 $invitation->content = $record->comments; 3104 $invitation->date_modified = $record->date_modified; 3105 $invitation->invite_sent = $record->invite_sent; 3106 $invitation->accepted = 0; 3107 3108 $invite_id = $invitation->save(); 3109 $processed[] = (int) $record->id; 3110 } 3111 3112 $ids_to_delete = implode( ',', $processed ); 3113 if ( $ids_to_delete ) { 3114 $wpdb->query( "DELETE FROM {$bp->groups->table_name_members} WHERE ID IN ($ids_to_delete)" ); 3115 } 3116 } -
src/bp-groups/bp-groups-notifications.php
diff --git src/bp-groups/bp-groups-notifications.php src/bp-groups/bp-groups-notifications.php index b94850fb5..481c70770 100644
function groups_notification_new_membership_request( $requesting_user_id = 0, $a 143 143 'notification_type' => 'groups-membership-request', 144 144 ); 145 145 146 $request_message = ''; 147 $requests = groups_get_requests( $args = array( 148 'user_id' => $requesting_user_id, 149 'item_id' => $group_id, 150 ) ); 151 if ( $requests ) { 152 $request_message = current( $requests )->content; 153 } 154 146 155 $group = groups_get_group( $group_id ); 147 156 $args = array( 148 157 'tokens' => array( … … function groups_notification_new_membership_request( $requesting_user_id = 0, $a 151 160 'group.name' => $group->name, 152 161 'group.id' => $group_id, 153 162 'group-requests.url' => esc_url( bp_get_group_permalink( $group ) . 'admin/membership-requests' ), 154 'membership.id' => $membership_id,155 163 'profile.url' => esc_url( bp_core_get_user_domain( $requesting_user_id ) ), 156 164 'requesting-user.id' => $requesting_user_id, 157 165 'requesting-user.name' => bp_core_get_user_displayname( $requesting_user_id ), 166 'request.message' => $request_message, 158 167 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ), 159 168 ), 160 169 ); … … add_action( 'groups_promoted_member', 'groups_notification_promoted_member', 10, 289 298 * 290 299 * @since 1.0.0 291 300 * 292 * @param BP_Groups_Group $group Group object.293 * @param BP_Groups_Member $member Member object.294 * @param int $inviter_user_id ID of the user who sent the invite.301 * @param BP_Groups_Group $group Group object. 302 * @param BP_Groups_Member|int $member Member object or invited_user_id. 303 * @param int $inviter_user_id ID of the user who sent the invite. 295 304 */ 296 305 function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) { 297 306 298 // Bail if member has already been invited.299 if ( ! empty( $member->invite_sent ) ) {300 return;301 }302 303 307 // @todo $inviter_ud may be used for caching, test without it 304 308 $inviter_ud = bp_core_get_core_userdata( $inviter_user_id ); 305 $invited_user_id = $member->user_id; 309 310 if ( $member instanceof BP_Groups_Member ) { 311 $invited_user_id = $member->user_id; 312 } else if ( is_int( $member ) ) { 313 $invited_user_id = $member; 314 } 306 315 307 316 // Trigger a BuddyPress Notification. 308 317 if ( bp_is_active( 'notifications' ) ) { … … function groups_notification_group_invites( &$group, &$member, $inviter_user_id 326 335 'notification_type' => 'groups-invitation', 327 336 ); 328 337 338 $invite_message = ''; 339 $invitations = groups_get_invites( $args = array( 340 'user_id' => $invited_user_id, 341 'item_id' => $group->id, 342 'inviter_id' => $inviter_user_id, 343 ) ); 344 if ( $invitations ) { 345 $invite_message = current( $invitations )->content; 346 } 347 329 348 $args = array( 330 349 'tokens' => array( 331 'group' => $group, 332 'group.url' => bp_get_group_permalink( $group ), 333 'group.name' => $group->name, 334 'inviter.name' => bp_core_get_userlink( $inviter_user_id, true, false, true ), 335 'inviter.url' => bp_core_get_user_domain( $inviter_user_id ), 336 'inviter.id' => $inviter_user_id, 337 'invites.url' => esc_url( $invited_link . '/invites/' ), 338 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ), 350 'group' => $group, 351 'group.url' => bp_get_group_permalink( $group ), 352 'group.name' => $group->name, 353 'inviter.name' => bp_core_get_userlink( $inviter_user_id, true, false, true ), 354 'inviter.url' => bp_core_get_user_domain( $inviter_user_id ), 355 'inviter.id' => $inviter_user_id, 356 'invites.url' => esc_url( $invited_link . '/invites/' ), 357 'invite.message' => $invite_message, 358 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ), 339 359 ), 340 360 ); 361 341 362 bp_send_email( 'groups-invitation', (int) $invited_user_id, $args ); 342 363 } 343 364 -
src/bp-groups/bp-groups-template.php
diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php index 575eeaceb..b3880a6bf 100644
function bp_group_request_reject_link() { 5486 5486 function bp_get_group_request_reject_link() { 5487 5487 global $requests_template; 5488 5488 5489 $link = add_query_arg( array( 5490 '_wpnonce' => wp_create_nonce( 'groups_reject_membership_request' ), 5491 'user_id' => $requests_template->request->user_id, 5492 'action' => 'reject' 5493 ), trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . 'admin/membership-requests/' ); 5494 5489 5495 /** 5490 5496 * Filters the URL to use to reject a membership request. 5491 5497 * … … function bp_group_request_reject_link() { 5493 5499 * 5494 5500 * @param string $value URL to use to reject a membership request. 5495 5501 */ 5496 return apply_filters( 'bp_get_group_request_reject_link', wp_nonce_url( trailingslashit( bp_get_group_permalink( groups_get_current_group() ) . 'admin/membership-requests/reject/' . $requests_template->request->membership_id ), 'groups_reject_membership_request' ));5502 return apply_filters( 'bp_get_group_request_reject_link', $link ); 5497 5503 } 5498 5504 5499 5505 /** … … function bp_group_request_accept_link() { 5510 5516 function bp_get_group_request_accept_link() { 5511 5517 global $requests_template; 5512 5518 5519 $link = add_query_arg( array( 5520 '_wpnonce' => wp_create_nonce( 'groups_accept_membership_request' ), 5521 'user_id' => $requests_template->request->user_id, 5522 'action' => 'accept' 5523 ), trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . 'admin/membership-requests/' ); 5524 5513 5525 /** 5514 5526 * Filters the URL to use to accept a membership request. 5515 5527 * … … function bp_group_request_accept_link() { 5517 5529 * 5518 5530 * @param string $value URL to use to accept a membership request. 5519 5531 */ 5520 return apply_filters( 'bp_get_group_request_accept_link', wp_nonce_url( trailingslashit( bp_get_group_permalink( groups_get_current_group() ) . 'admin/membership-requests/accept/' . $requests_template->request->membership_id ), 'groups_accept_membership_request' ));5532 return apply_filters( 'bp_get_group_request_accept_link', $link ); 5521 5533 } 5522 5534 5523 5535 /** -
src/bp-groups/classes/class-bp-group-member-query.php
diff --git src/bp-groups/classes/class-bp-group-member-query.php src/bp-groups/classes/class-bp-group-member-query.php index d872810e3..859075921 100644
class BP_Group_Member_Query extends BP_User_Query { 231 231 $sql['orderby'] = "ORDER BY date_modified"; 232 232 $sql['order'] = 'first_joined' === $this->query_vars['type'] ? 'ASC' : 'DESC'; 233 233 234 $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" ); 234 $group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" ); 235 236 $invited_member_ids = array(); 237 238 // If appropriate, fetch invitations and add them to the results. 239 if ( ! $is_confirmed || ! is_null( $this->query_vars['invite_sent'] ) || ! is_null( $this->query_vars['inviter_id'] ) ) { 240 $invite_args = array( 241 'item_id' => $this->query_vars['group_id'], 242 'fields' => 'user_ids', 243 'type' => 'all', 244 ); 245 246 if ( ! is_null( $this->query_vars['invite_sent'] ) ) { 247 $invite_args['invite_sent'] = ! empty( $this->query_vars['invite_sent'] ) ? 'sent' : 'draft'; 248 } 249 250 // If inviter_id. 251 if ( ! is_null( $this->query_vars['inviter_id'] ) ) { 252 $inviter_id = $this->query_vars['inviter_id']; 253 254 // Empty: inviter_id = 0. (pass false, 0, or empty array). 255 if ( empty( $inviter_id ) ) { 256 $invite_args['type'] = 'request'; 257 258 /* 259 * The string 'any' matches any non-zero value (inviter_id != 0). 260 * These are invitations, not requests. 261 */ 262 } elseif ( 'any' === $inviter_id ) { 263 $invite_args['type'] = 'invite'; 264 265 // Assume that a list of inviter IDs has been passed. 266 } else { 267 $invite_args['type'] = 'invite'; 268 // Parse and sanitize. 269 $inviter_ids = wp_parse_id_list( $inviter_id ); 270 if ( ! empty( $inviter_ids ) ) { 271 $invite_args['inviter_id'] = $inviter_ids; 272 } 273 } 274 } 275 276 /* 277 * If first_joined is the "type" of query, sort the oldest 278 * requests and invitations to the top. 279 */ 280 if ( 'first_joined' === $this->query_vars['type'] ) { 281 $invite_args['order_by'] = 'date_modified'; 282 $invite_args['sort_order'] = 'ASC'; 283 } 284 285 $invited_member_ids = groups_get_invites( $invite_args ); 286 } 287 288 $this->group_member_ids = array_merge( $group_member_ids, $invited_member_ids ); 235 289 236 290 /** 237 291 * Filters the member IDs for the current group member query. … … class BP_Group_Member_Query extends BP_User_Query { 330 384 } 331 385 } 332 386 387 // Add accurate invitation info from the invitations table. 388 $invites = groups_get_invites( array( 389 'user_id' => $user_ids_sql, 390 'item_id' => $this->query_vars['group_id'], 391 'type' => 'all', 392 ) ); 393 foreach ( $invites as $invite ) { 394 if ( isset( $this->results[ $invite->user_id ] ) ) { 395 $this->results[ $invite->user_id ]->comments = $invite->content; 396 $this->results[ $invite->user_id ]->is_confirmed = 0; 397 $this->results[ $invite->user_id ]->invitation_id = $invite->id; 398 $this->results[ $invite->user_id ]->invite_sent = (int) $invite->invite_sent; 399 $this->results[ $invite->user_id ]->inviter_id = $invite->inviter_id; 400 401 // Backfill properties that are not being set above. 402 if ( ! isset( $this->results[ $invite->user_id ]->user_id ) ) { 403 $this->results[ $invite->user_id ]->user_id = $invite->user_id; 404 } 405 if ( ! isset( $this->results[ $invite->user_id ]->is_admin ) ) { 406 $this->results[ $invite->user_id ]->is_admin = 0; 407 } 408 if ( ! isset( $this->results[ $invite->user_id ]->is_mod ) ) { 409 $this->results[ $invite->user_id ]->is_mod = 0; 410 } 411 if ( ! isset( $this->results[ $invite->user_id ]->is_banned ) ) { 412 $this->results[ $invite->user_id ]->is_banned = 0; 413 } 414 if ( ! isset( $this->results[ $invite->user_id ]->date_modified ) ) { 415 $this->results[ $invite->user_id ]->date_modified = $invite->date_modified; 416 } 417 if ( ! isset( $this->results[ $invite->user_id ]->user_title ) ) { 418 $this->results[ $invite->user_id ]->user_title = ''; 419 } 420 if ( ! isset( $this->results[ $invite->user_id ]->membership_id ) ) { 421 $this->results[ $invite->user_id ]->membership_id = 0; 422 } 423 } 424 } 425 333 426 // Don't filter other BP_User_Query objects on the same page. 334 427 remove_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10 ); 335 428 } -
src/bp-groups/classes/class-bp-groups-group.php
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php index 5c40c011b..8469129a5 100644
class BP_Groups_Group { 771 771 * yet accepted. 772 772 */ 773 773 public static function get_invites( $user_id, $group_id, $sent = null ) { 774 global $wpdb; 775 776 $bp = buddypress(); 777 $sql = $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d and is_confirmed = 0 AND inviter_id = %d", $group_id, $user_id ); 778 779 // Query for a specific invite sent status. 780 if ( ! is_null( $sent ) ) { 781 $sql .= $wpdb->prepare( ' AND invite_sent = %d', $sent ); 774 if ( 0 === $sent ) { 775 $sent_arg = 'draft'; 776 } else if ( 1 === $sent ) { 777 $sent_arg = 'sent'; 778 } else { 779 $sent_arg = 'all'; 782 780 } 783 781 784 return $wpdb->get_col( $sql ); 782 return groups_get_invites( array( 783 'item_id' => $group_id, 784 'inviter_id' => $user_id, 785 'invite_sent' => $sent_arg, 786 'fields' => 'user_ids', 787 ) ); 785 788 } 786 789 787 790 /** … … class BP_Groups_Group { 959 962 * } 960 963 */ 961 964 public static function get_membership_requests( $group_id, $limit = null, $page = null ) { 962 global $wpdb; 963 964 if ( !empty( $limit ) && !empty( $page ) ) { 965 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 965 $args = array( 966 'item_id' => $group_id 967 ); 968 if ( $limit ) { 969 $args['per_page'] = $limit; 966 970 } 971 if ( $page ) { 972 $args['page'] = $page; 973 } 967 974 968 $bp = buddypress(); 969 970 $paged_requests = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0{$pag_sql}", $group_id ) ); 971 $total_requests = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ); 975 $requests = groups_get_requests( $args ); 976 $total = count( groups_get_membership_requested_user_ids( $group_id ) ); 972 977 973 return array( 'requests' => $ paged_requests, 'total' => $total_requests);978 return array( 'requests' => $requests, 'total' => $total ); 974 979 } 975 980 976 981 /** … … class BP_Groups_Group { 1635 1640 * failure. 1636 1641 */ 1637 1642 public static function delete_all_invites( $group_id ) { 1638 global $wpdb; 1643 if ( empty( $group_id ) ) { 1644 return false; 1645 } 1639 1646 1640 $ bp = buddypress();1647 $invites_class = new BP_Groups_Invitation_Manager(); 1641 1648 1642 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d AND invite_sent = 1", $group_id ) ); 1649 return $invites_class->delete( array( 1650 'item_id' => $group_id, 1651 ) ); 1643 1652 } 1644 1653 1645 1654 /** -
new file src/bp-groups/classes/class-bp-groups-invitation-manager.php
diff --git src/bp-groups/classes/class-bp-groups-invitation-manager.php src/bp-groups/classes/class-bp-groups-invitation-manager.php new file mode 100644 index 000000000..9abf2b854
- + 1 <?php 2 /** 3 * Group invitations class. 4 * 5 * @package BuddyPress 6 * @subpackage Core 7 * @since 5.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Group invitations class. 15 * 16 * An extension of the core Invitations class that adapts the 17 * core logic to accommodate group invitation behavior. 18 * 19 * @since 5.0.0 20 */ 21 class BP_Groups_Invitation_Manager extends BP_Invitation_Manager { 22 /** 23 * Construct parameters. 24 * 25 * @since 3.1.0 26 * 27 * @param array|string $args { 28 29 * } 30 */ 31 public function __construct( $args = '' ) { 32 parent::__construct(); 33 } 34 35 /** 36 * This is where custom actions are added to run when notifications of an 37 * invitation or request need to be generated & sent. 38 * 39 * @since 2.7.0 40 * 41 * @param int $id The ID of the invitation to mark as sent. 42 * @return bool True on success, false on failure. 43 */ 44 public function run_send_action( BP_Invitation $invitation ) { 45 // Notify group admins of the pending request 46 if ( 'request' === $invitation->type ) { 47 $admins = groups_get_group_admins( $invitation->item_id ); 48 49 foreach ( $admins as $admin ) { 50 groups_notification_new_membership_request( $invitation->user_id, $admin->user_id, $invitation->item_id, $invitation->id ); 51 } 52 return true; 53 54 // Notify the invitee of the invitation. 55 } else { 56 $group = groups_get_group( $invitation->item_id ); 57 groups_notification_group_invites( $group, $invitation->user_id, $invitation->inviter_id ); 58 return true; 59 } 60 } 61 62 /** 63 * This is where custom actions are added to run when an invitation 64 * or request is accepted. 65 * 66 * @since 2.7.0 67 * 68 * @param string $type Are we accepting an invitation or request? 69 * @param array $r Parameters that describe the invitation being accepted. 70 * @return bool True on success, false on failure. 71 */ 72 public function run_acceptance_action( $type = 'invite', $r ) { 73 // If the user is already a member (because BP at one point allowed two invitations to 74 // slip through), return early. 75 if ( groups_is_user_member( $r['user_id'], $r['item_id'] ) ) { 76 return true; 77 } 78 79 // Create the new membership 80 $member = new BP_Groups_Member( $r['user_id'], $r['item_id'] ); 81 82 if ( 'request' === $type ) { 83 $member->accept_request(); 84 } else { 85 $member->accept_invite(); 86 } 87 88 if ( ! $member->save() ) { 89 return false; 90 } 91 92 if ( 'request' === $type ) { 93 /** 94 * Fires after a group membership request has been accepted. 95 * 96 * @since 1.0.0 97 * 98 * @param int $user_id ID of the user who accepted membership. 99 * @param int $group_id ID of the group that was accepted membership to. 100 * @param bool $value If membership was accepted. 101 */ 102 do_action( 'groups_membership_accepted', $r['user_id'], $r['item_id'], true ); 103 } else { 104 // Get an inviter_id from the invitation. 105 $invites = groups_get_invites( $r ); 106 $inviter_id = 0; 107 if ( $invites ) { 108 $inviter_id = current( $invites )->inviter_id; 109 } 110 111 /** 112 * Fires after a user has accepted a group invite. 113 * 114 * @since 1.0.0 115 * @since 2.8.0 The $inviter_id arg was added. 116 * 117 * @param int $user_id ID of the user who accepted the group invite. 118 * @param int $group_id ID of the group being accepted to. 119 * @param int $inviter_id ID of the user who invited this user to the group. 120 */ 121 do_action( 'groups_accept_invite', $r['user_id'], $r['item_id'], $inviter_id ); 122 } 123 124 // Modify group meta. 125 groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() ); 126 127 return true; 128 } 129 130 /** 131 * With group invitations, we don't need to keep the old record, so we delete rather than 132 * mark invitations as "accepted." 133 * 134 * @since 2.7.0 135 * 136 * @see BP_Invitation::mark_accepted_by_data() 137 * for a description of arguments. 138 */ 139 public function mark_accepted( $args ) { 140 // Delete all existing invitations/requests to this group for this user. 141 $this->delete( array( 142 'user_id' => $args['user_id'], 143 'item_id' => $args['item_id'], 144 'type' => 'all' 145 ) ); 146 } 147 148 /** 149 * Should this invitation be created? 150 * 151 * @since 5.0.0 152 */ 153 public function allow_invitation( $args ) { 154 // Does the inviter have this capability? 155 if ( ! bp_user_can( $args['inviter_id'], 'groups_send_invitation', array( 'group_id' => $args['item_id'] ) ) ) { 156 return false; 157 } 158 159 // Is the invited user eligible to receive an invitation? 160 if ( ! bp_user_can( $args['user_id'], 'groups_receive_invitation', array( 'group_id' => $args['item_id'] ) ) ) { 161 return false; 162 } 163 164 // Prevent duplicated invitations. 165 if ( groups_check_has_invite_from_user( $args['user_id'], $args['item_id'], $args['inviter_id'], 'all' ) ) { 166 return false; 167 } 168 169 return true; 170 } 171 172 /** 173 * Should this request be created? 174 * 175 * @since 5.0.0 176 */ 177 public function allow_request( $args ) { 178 // Does the requester have this capability? (Also checks for duplicates.) 179 if ( ! bp_user_can( $args['user_id'], 'groups_request_membership', array( 'group_id' => $args['item_id'] ) ) ) { 180 return false; 181 } 182 183 return true; 184 } 185 } -
src/bp-groups/classes/class-bp-groups-member.php
diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php index 66f833022..dc54b9466 100644
class BP_Groups_Member { 767 767 } 768 768 769 769 /** 770 * Get a user's outstanding group invitations.770 * Get group objects for groups that a user is currently invited to. 771 771 * 772 772 * @since 1.6.0 773 773 * … … class BP_Groups_Member { 784 784 * } 785 785 */ 786 786 public static function get_invites( $user_id, $limit = false, $page = false, $exclude = false ) { 787 global $wpdb; 788 789 $pag_sql = ( !empty( $limit ) && !empty( $page ) ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 790 791 if ( !empty( $exclude ) ) { 792 $exclude = implode( ',', wp_parse_id_list( $exclude ) ); 793 $exclude_sql = " AND g.id NOT IN ({$exclude})"; 794 } else { 795 $exclude_sql = ''; 796 } 797 798 $bp = buddypress(); 799 800 $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d {$exclude_sql} ORDER BY m.date_modified ASC {$pag_sql}", $user_id ) ); 801 802 return array( 'groups' => $paged_groups, 'total' => self::get_invite_count_for_user( $user_id ) ); 787 return groups_get_invites_for_user( $user_id, $limit, $page, $exclude ); 803 788 } 804 789 805 790 /** … … class BP_Groups_Member { 811 796 * @return int 812 797 */ 813 798 public static function get_invite_count_for_user( $user_id = 0 ) { 814 global $wpdb; 815 816 $bp = buddypress(); 817 818 $count = wp_cache_get( $user_id, 'bp_group_invite_count' ); 819 820 if ( false === $count ) { 821 $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d", $user_id ) ); 822 wp_cache_set( $user_id, $count, 'bp_group_invite_count' ); 823 } 824 825 return $count; 799 return groups_get_invite_count_for_user( $user_id ); 826 800 } 827 801 828 802 /** … … class BP_Groups_Member { 859 833 'where' => '', 860 834 'limits' => '', 861 835 ); 862 836 //@TODO 6210. 863 837 switch ( $r['type'] ) { 864 case 'pending_request' :865 $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id = 0", $user_id );866 break;867 868 case 'pending_received_invitation' :869 $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id != 0", $user_id );870 break;871 872 case 'pending_sent_invitation' :873 $sql['where'] = $wpdb->prepare( "inviter_id = %d AND is_confirmed = 0", $user_id );874 break;875 876 838 case 'membership' : 877 839 default : 878 840 $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 1", $user_id ); … … class BP_Groups_Member { 912 874 * @return int|null The ID of the invitation if found; null if not found. 913 875 */ 914 876 public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) { 915 global $wpdb; 916 917 if ( empty( $user_id ) ) 918 return false; 919 920 $bp = buddypress(); 921 $sql = "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0"; 922 923 if ( 'sent' == $type ) 924 $sql .= " AND invite_sent = 1"; 925 926 $query = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) ); 927 928 return is_numeric( $query ) ? (int) $query : $query; 877 return groups_is_user_invited( $user_id, $group_id, $type ); 929 878 } 930 879 931 880 /** … … class BP_Groups_Member { 937 886 * 938 887 * @param int $user_id ID of the user. 939 888 * @param int $group_id ID of the group. 889 * @param int $inviter_id ID of the inviter. Specify if you want to delete 890 * a specific invite. Leave false if you want to 891 * delete all invites to this group. 940 892 * @return int Number of records deleted. 941 893 */ 942 public static function delete_invite( $user_id, $group_id ) { 943 global $wpdb; 944 945 if ( empty( $user_id ) ) { 946 return false; 947 } 948 894 public static function delete_invite( $user_id, $group_id, $inviter_id = false ) { 949 895 /** 950 896 * Fires before a group invitation is deleted. 951 897 * 952 898 * @since 2.6.0 899 * @since 5.0.0 Added $inviter_id 953 900 * 954 901 * @param int $user_id ID of the user. 955 902 * @param int $group_id ID of the group. 903 * @param int $inviter_id ID of the inviter. 956 904 */ 957 do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id ); 958 959 $table_name = buddypress()->groups->table_name_members; 960 961 $sql = "DELETE FROM {$table_name} 962 WHERE user_id = %d 963 AND group_id = %d 964 AND is_confirmed = 0 965 AND inviter_id != 0"; 966 967 $prepared = $wpdb->prepare( $sql, $user_id, $group_id ); 905 do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id, $inviter_id ); 968 906 969 return $wpdb->query( $prepared );907 return groups_delete_invite( $user_id, $group_id, $inviter_id ); 970 908 } 971 909 972 910 /** … … class BP_Groups_Member { 979 917 * @return int Number of records deleted. 980 918 */ 981 919 public static function delete_request( $user_id, $group_id ) { 982 global $wpdb; 983 984 if ( empty( $user_id ) ) 985 return false; 986 987 $bp = buddypress(); 988 989 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id = 0 AND invite_sent = 0", $user_id, $group_id ) ); 920 return groups_delete_membership_request( false, $user_id, $group_id ); 990 921 } 991 922 992 923 /** … … class BP_Groups_Member { 1104 1035 * @return int Database ID of the membership if found; int 0 on failure. 1105 1036 */ 1106 1037 public static function check_for_membership_request( $user_id, $group_id ) { 1107 global $wpdb; 1108 1109 if ( empty( $user_id ) ) 1110 return false; 1111 1112 $bp = buddypress(); 1113 1114 return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND is_banned = 0 AND inviter_id = 0", $user_id, $group_id ) ); 1038 return groups_is_user_pending( $user_id, $group_id ); 1115 1039 } 1116 1040 1117 1041 /** … … class BP_Groups_Member { 1292 1216 * @return array IDs of users with outstanding membership requests. 1293 1217 */ 1294 1218 public static function get_all_membership_request_user_ids( $group_id ) { 1295 global $wpdb; 1296 1297 $bp = buddypress(); 1298 1299 return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ) ); 1219 return groups_get_membership_requested_user_ids( $group_id ); 1300 1220 } 1301 1221 1302 1222 /** -
src/bp-groups/screens/single/admin/membership-requests.php
diff --git src/bp-groups/screens/single/admin/membership-requests.php src/bp-groups/screens/single/admin/membership-requests.php index ca12c2cc0..2b99f4f03 100644
function groups_screen_group_admin_requests() { 23 23 return false; 24 24 } 25 25 26 $request_action = (string) bp_action_variable( 1 ); 27 $membership_id = (int) bp_action_variable( 2 ); 26 $request_action = isset( $_GET['action'] ) ? $_GET['action'] : false; 27 $user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : false; 28 $group_id = bp_get_current_group_id(); 28 29 29 if ( !empty( $request_action ) && !empty( $membership_id )) {30 if ( 'accept' == $request_action && is_numeric( $membership_id )) {30 if ( $request_action && $user_id && $group_id ) { 31 if ( 'accept' === $request_action ) { 31 32 32 33 // Check the nonce first. 33 if ( ! check_admin_referer( 'groups_accept_membership_request' ) )34 if ( ! check_admin_referer( 'groups_accept_membership_request' ) ) { 34 35 return false; 36 } 35 37 36 38 // Accept the membership request. 37 if ( ! groups_accept_membership_request( $membership_id ) )39 if ( ! groups_accept_membership_request( false, $user_id, $group_id ) ) { 38 40 bp_core_add_message( __( 'There was an error accepting the membership request. Please try again.', 'buddypress' ), 'error' ); 39 else41 } else { 40 42 bp_core_add_message( __( 'Group membership request accepted', 'buddypress' ) ); 43 } 41 44 42 } elseif ( 'reject' == $request_action && is_numeric( $membership_id )) {45 } elseif ( 'reject' === $request_action ) { 43 46 /* Check the nonce first. */ 44 if ( ! check_admin_referer( 'groups_reject_membership_request' ) )47 if ( ! check_admin_referer( 'groups_reject_membership_request' ) ) { 45 48 return false; 49 } 46 50 47 51 // Reject the membership request. 48 if ( ! groups_reject_membership_request( $membership_id ) )52 if ( ! groups_reject_membership_request( false, $user_id, $group_id ) ) { 49 53 bp_core_add_message( __( 'There was an error rejecting the membership request. Please try again.', 'buddypress' ), 'error' ); 50 else54 } else { 51 55 bp_core_add_message( __( 'Group membership request rejected', 'buddypress' ) ); 56 } 52 57 } 53 58 59 // Was the member added to the group? 60 $membership_id = groups_is_user_member( $user_id, $group_id ); 61 54 62 /** 55 63 * Fires before the redirect if a group membership request has been handled. 56 64 * … … function groups_screen_group_admin_requests() { 58 66 * 59 67 * @param int $id ID of the group that was edited. 60 68 * @param string $request_action Membership request action being performed. 61 * @param int $membership_id The key of the action_variables array that you want. 69 * @param int $membership_id The membership ID of the new user; false if rejected. 70 * @param int $user_id The ID of the requesting user. 71 * @param int $group_id The ID of the requested group. 62 72 */ 63 do_action( 'groups_group_request_managed', $bp->groups->current_group->id, $request_action, $membership_id );73 do_action( 'groups_group_request_managed', $bp->groups->current_group->id, $request_action, $membership_id, $user_id, $group_id ); 64 74 bp_core_redirect( bp_get_group_permalink( groups_get_current_group() ) . 'admin/membership-requests/' ); 65 75 } 66 76 … … function groups_screen_group_admin_requests() { 82 92 */ 83 93 bp_core_load_template( apply_filters( 'groups_template_group_admin_requests', 'groups/single/home' ) ); 84 94 } 85 add_action( 'bp_screens', 'groups_screen_group_admin_requests' ); 86 No newline at end of file 95 add_action( 'bp_screens', 'groups_screen_group_admin_requests' ); -
src/class-buddypress.php
diff --git src/class-buddypress.php src/class-buddypress.php index e6d8d609b..ea807f44b 100644
class BuddyPress { 487 487 require( $this->plugin_dir . 'bp-core/bp-core-moderation.php' ); 488 488 require( $this->plugin_dir . 'bp-core/bp-core-loader.php' ); 489 489 require( $this->plugin_dir . 'bp-core/bp-core-customizer-email.php' ); 490 require( $this->plugin_dir . 'bp-core/bp-core-invitations-cache.php' ); 490 491 491 492 // Maybe load deprecated functionality (this double negative is proof positive!) 492 493 if ( ! bp_get_option( '_bp_ignore_deprecated_code', ! $this->load_deprecated ) ) { … … class BuddyPress { 571 572 'BP_Walker_Category_Checklist' => 'core', 572 573 'BP_Walker_Nav_Menu_Checklist' => 'core', 573 574 'BP_Walker_Nav_Menu' => 'core', 575 'BP_Invitation_Manager' => 'core', 576 'BP_Invitation' => 'core', 574 577 575 578 'BP_Core_Friends_Widget' => 'friends', 576 579 -
new file tests/phpunit/assets/invitations-extensions.php
diff --git tests/phpunit/assets/invitations-extensions.php tests/phpunit/assets/invitations-extensions.php new file mode 100644 index 000000000..4815cc7ce
- + 1 <?php 2 /** 3 * The following implementations of BP_Attachment act as dummy plugins 4 * for our unit tests 5 */ 6 class BPTest_Invitation_Manager_Extension extends BP_Invitation_Manager { 7 public function __construct( $args = array() ) { 8 parent::__construct( $args ); 9 } 10 11 public function run_send_action( BP_Invitation $invitation ) { 12 return true; 13 } 14 15 public function run_acceptance_action( $type = 'invite', $r ) { 16 return true; 17 } 18 } -
new file tests/phpunit/testcases/core/invitations.php
diff --git tests/phpunit/testcases/core/invitations.php tests/phpunit/testcases/core/invitations.php new file mode 100644 index 000000000..ba20d63c4
- + 1 <?php 2 3 include_once BP_TESTS_DIR . 'assets/invitations-extensions.php'; 4 5 /** 6 * @group core 7 * @group invitations 8 */ 9 class BP_Tests_Invitations extends BP_UnitTestCase { 10 public function test_bp_invitations_add_invitation_vanilla() { 11 $old_current_user = get_current_user_id(); 12 13 $u1 = $this->factory->user->create(); 14 $u2 = $this->factory->user->create(); 15 $u3 = $this->factory->user->create(); 16 $this->set_current_user( $u1 ); 17 18 $invites_class = new BPTest_Invitation_Manager_Extension(); 19 20 // Create a couple of invitations. 21 $invite_args = array( 22 'user_id' => $u3, 23 'inviter_id' => $u1, 24 'item_id' => 1, 25 'send_invite' => 'sent', 26 ); 27 $i1 = $invites_class->add_invitation( $invite_args ); 28 $invite_args['inviter_id'] = $u2; 29 $i2 = $invites_class->add_invitation( $invite_args ); 30 31 $get_invites = array( 32 'user_id' => $u3, 33 'fields' => 'ids', 34 ); 35 $invites = $invites_class->get_invitations( $get_invites ); 36 $this->assertEqualSets( array( $i1, $i2 ), $invites ); 37 38 $this->set_current_user( $old_current_user ); 39 } 40 41 public function test_bp_invitations_add_invitation_avoid_duplicates() { 42 $old_current_user = get_current_user_id(); 43 44 $u1 = $this->factory->user->create(); 45 $u2 = $this->factory->user->create(); 46 $this->set_current_user( $u1 ); 47 48 $invites_class = new BPTest_Invitation_Manager_Extension(); 49 50 // Create an invitation. 51 $invite_args = array( 52 'user_id' => $u2, 53 'inviter_id' => $u1, 54 'item_id' => 1, 55 'send_invite' => 'sent', 56 ); 57 $i1 = $invites_class->add_invitation( $invite_args ); 58 // Attempt to create a duplicate. 59 $this->assertFalse( $invites_class->add_invitation( $invite_args ) ); 60 61 $this->set_current_user( $old_current_user ); 62 } 63 64 public function test_bp_invitations_add_invitation_invite_plus_request_should_accept() { 65 $old_current_user = get_current_user_id(); 66 67 $u1 = $this->factory->user->create(); 68 $u2 = $this->factory->user->create(); 69 $u3 = $this->factory->user->create(); 70 $this->set_current_user( $u1 ); 71 72 $invites_class = new BPTest_Invitation_Manager_Extension(); 73 74 // Create an invitation. 75 $invite_args = array( 76 'user_id' => $u3, 77 'inviter_id' => $u1, 78 'item_id' => 1, 79 'send_invite' => 'sent', 80 ); 81 $i1 = $invites_class->add_invitation( $invite_args ); 82 83 // Create a request. 84 $request_args = array( 85 'user_id' => $u3, 86 'item_id' => 1, 87 ); 88 $r1 = $invites_class->add_request( $request_args ); 89 90 $get_invites = array( 91 'user_id' => $u3, 92 'accepted' => 'accepted' 93 ); 94 $invites = $invites_class->get_invitations( $get_invites ); 95 $this->assertEqualSets( array( $i1 ), wp_list_pluck( $invites, 'id' ) ); 96 97 $this->set_current_user( $old_current_user ); 98 } 99 100 public function test_bp_invitations_add_invitation_unsent_invite_plus_request_should_not_accept() { 101 $old_current_user = get_current_user_id(); 102 103 $u1 = $this->factory->user->create(); 104 $u2 = $this->factory->user->create(); 105 $u3 = $this->factory->user->create(); 106 $this->set_current_user( $u1 ); 107 108 $invites_class = new BPTest_Invitation_Manager_Extension(); 109 110 // Create an invitation. 111 $invite_args = array( 112 'user_id' => $u3, 113 'inviter_id' => $u1, 114 'item_id' => 1, 115 'send_invite' => 0, 116 ); 117 $i1 = $invites_class->add_invitation( $invite_args ); 118 119 // Create a request. 120 $request_args = array( 121 'user_id' => $u3, 122 'item_id' => 1, 123 ); 124 $r1 = $invites_class->add_request( $request_args ); 125 126 $get_invites = array( 127 'user_id' => $u3, 128 'accepted' => 'accepted' 129 ); 130 $invites = $invites_class->get_invitations( $get_invites ); 131 $this->assertEqualSets( array(), wp_list_pluck( $invites, 'id' ) ); 132 133 $this->set_current_user( $old_current_user ); 134 } 135 136 public function test_bp_invitations_add_invitation_unsent_invite_plus_request_then_send_invite_should_accept() { 137 $old_current_user = get_current_user_id(); 138 139 $u1 = $this->factory->user->create(); 140 $u2 = $this->factory->user->create(); 141 $u3 = $this->factory->user->create(); 142 $this->set_current_user( $u1 ); 143 144 $invites_class = new BPTest_Invitation_Manager_Extension(); 145 146 // Create an invitation. 147 $invite_args = array( 148 'user_id' => $u3, 149 'inviter_id' => $u1, 150 'item_id' => 1, 151 'send_invite' => 0, 152 ); 153 $i1 = $invites_class->add_invitation( $invite_args ); 154 155 // Create a request. 156 $request_args = array( 157 'user_id' => $u3, 158 'item_id' => 1, 159 ); 160 $r1 = $invites_class->add_request( $request_args ); 161 162 $invites_class->send_invitation_by_id( $i1 ); 163 164 // Check that both the request and invitation are marked 'accepted'. 165 $get_invites = array( 166 'user_id' => $u3, 167 'type' => 'all', 168 'accepted' => 'accepted', 169 'fields' => 'ids' 170 ); 171 $invites = $invites_class->get_invitations( $get_invites ); 172 $this->assertEqualSets( array( $i1, $r1 ), $invites ); 173 174 $this->set_current_user( $old_current_user ); 175 } 176 177 public function test_bp_invitations_add_request_vanilla() { 178 $old_current_user = get_current_user_id(); 179 180 $u1 = $this->factory->user->create(); 181 $this->set_current_user( $u1 ); 182 183 $invites_class = new BPTest_Invitation_Manager_Extension(); 184 185 // Create a couple of requests. 186 $request_args = array( 187 'user_id' => $u1, 188 'item_id' => 7, 189 ); 190 $r1 = $invites_class->add_request( $request_args ); 191 $request_args['item_id'] = 4; 192 $r2 = $invites_class->add_request( $request_args ); 193 194 $get_requests = array( 195 'user_id' => $u1, 196 'fields' => 'ids' 197 ); 198 $requests = $invites_class->get_requests( $get_requests ); 199 $this->assertEqualSets( array( $r1, $r2 ), $requests ); 200 201 $this->set_current_user( $old_current_user ); 202 } 203 204 public function test_bp_invitations_add_request_avoid_duplicates() { 205 $old_current_user = get_current_user_id(); 206 207 $invites_class = new BPTest_Invitation_Manager_Extension(); 208 209 $u1 = $this->factory->user->create(); 210 $this->set_current_user( $u1 ); 211 212 // Create a couple of requests. 213 $request_args = array( 214 'user_id' => $u1, 215 'item_id' => 7, 216 ); 217 $r1 = $invites_class->add_request( $request_args ); 218 // Attempt to create a duplicate. 219 $this->assertFalse( $invites_class->add_request( $request_args ) ); 220 221 $this->set_current_user( $old_current_user ); 222 } 223 224 public function test_bp_invitations_add_request_request_plus_sent_invite_should_accept() { 225 $old_current_user = get_current_user_id(); 226 227 $u1 = $this->factory->user->create(); 228 $u2 = $this->factory->user->create(); 229 $this->set_current_user( $u1 ); 230 231 $invites_class = new BPTest_Invitation_Manager_Extension(); 232 233 // Create a request. 234 $request_args = array( 235 'user_id' => $u2, 236 'item_id' => 1, 237 ); 238 $r1 = $invites_class->add_request( $request_args ); 239 240 // Create an invitation. 241 $invite_args = array( 242 'user_id' => $u2, 243 'inviter_id' => $u1, 244 'item_id' => 1, 245 'send_invite' => 1, 246 ); 247 $i1 = $invites_class->add_invitation( $invite_args ); 248 249 // Check that both the request and invitation are marked 'accepted'. 250 $get_invites = array( 251 'user_id' => $u2, 252 'type' => 'all', 253 'accepted' => 'accepted', 254 'fields' => 'ids' 255 ); 256 $invites = $invites_class->get_invitations( $get_invites ); 257 $this->assertEqualSets( array( $r1, $i1 ), $invites ); 258 259 $this->set_current_user( $old_current_user ); 260 } 261 262 } -
tests/phpunit/testcases/groups/class-bp-groups-group.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-group.php tests/phpunit/testcases/groups/class-bp-groups-group.php index a76d4e72c..f03a4c801 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 1372 1372 */ 1373 1373 public function test_get_group_extras_invited() { 1374 1374 $u = self::factory()->user->create(); 1375 $g = self::factory()->group->create(); 1375 $u2 = self::factory()->user->create(); 1376 $g = self::factory()->group->create( array( 'creator_id' => $u2, 'status' => 'private' ) ); 1376 1377 1377 $invite = new BP_Groups_Member;1378 $invite->group_id = $g;1379 $invite->user_id = $u;1380 $invite->date_modified = bp_core_current_time();1381 $invite->invite_sent = true;1382 $invite->is_confirmed = false;1383 $invite->save();1378 // Outstanding invitations should be left intact. 1379 groups_invite_user( array( 1380 'user_id' => $u, 1381 'group_id' => $g, 1382 'inviter_id' => $u2, 1383 'send_invite' => 1, 1384 ) ); 1384 1385 1385 1386 $paged_groups = array(); 1386 1387 $paged_groups[] = new stdClass; … … class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 1411 1412 */ 1412 1413 public function test_get_group_extras_pending() { 1413 1414 $u = self::factory()->user->create(); 1414 $g = self::factory()->group->create( );1415 $g = self::factory()->group->create( array( 'status' => 'private' ) ); 1415 1416 1416 $invite = new BP_Groups_Member; 1417 $invite->group_id = $g; 1418 $invite->user_id = $u; 1419 $invite->date_modified = bp_core_current_time(); 1420 $invite->invite_sent = false; 1421 $invite->is_confirmed = false; 1422 $invite->save(); 1417 // Create membership request 1418 groups_send_membership_request( array( 1419 'user_id' => $u, 1420 'group_id' => $g, 1421 ) ); 1423 1422 1424 1423 $paged_groups = array(); 1425 1424 $paged_groups[] = new stdClass; -
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 b052f81fe..480c2ae0f 100644
class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 20 20 array_map( 'groups_delete_group', self::$group_ids ); 21 21 } 22 22 23 public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {24 $invite = new BP_Groups_Member;25 $invite->group_id = $group_id;26 $invite->user_id = $user_id;27 $invite->date_modified = bp_core_current_time();28 $invite->inviter_id = $inviter_id;29 $invite->is_confirmed = 0;30 $invite->invite_sent = 1;31 32 $invite->save();33 return $invite->id;34 }35 36 public static function create_group_membership_request( $user_id, $group_id ) {37 $request = new BP_Groups_Member;38 $request->group_id = $group_id;39 $request->user_id = $user_id;40 $request->date_modified = bp_core_current_time();41 $request->inviter_id = 0;42 $request->is_confirmed = 0;43 44 $request->save();45 return $request->id;46 }47 48 23 public function test_get_recently_joined_with_filter() { 49 24 $g1 = self::factory()->group->create( array( 50 25 'name' => 'Tab', … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 133 108 } 134 109 135 110 public function test_get_invites_with_exclude() { 111 $u1 = self::factory()->user->create(); 112 $u2 = self::factory()->user->create(); 136 113 $g1 = self::factory()->group->create( array( 137 'name' => 'RC Cola', 114 'status' => 'private', 115 'creator_id' => $u1 138 116 ) ); 139 117 $g2 = self::factory()->group->create( array( 140 'name' => 'Pepsi', 118 'status' => 'private', 119 'creator_id' => $u1 141 120 ) ); 142 121 143 $u1 = self::factory()->user->create(); 144 $u2 = self::factory()->user->create(); 145 self::add_user_to_group( $u1, $g1 ); 146 self::add_user_to_group( $u1, $g2 ); 147 self::invite_user_to_group( $u2, $g1, $u1 ); 148 self::invite_user_to_group( $u2, $g2, $u1 ); 122 groups_invite_user( array( 123 'user_id' => $u2, 124 'group_id' => $g1, 125 'inviter_id' => $u1, 126 'send_invite' => 1, 127 ) ); 128 groups_invite_user( array( 129 'user_id' => $u2, 130 'group_id' => $g2, 131 'inviter_id' => $u1, 132 'send_invite' => 1, 133 ) ); 149 134 150 135 $groups = BP_Groups_Member::get_invites( $u2, false, false, array( 'awesome', $g1 ) ); 151 136 … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 282 267 ) ); 283 268 284 269 // Membership requests should be removed. 285 self::create_group_membership_request( $u1, $g ); 270 groups_send_membership_request( array( 271 'user_id' => $u1, 272 'group_id' => $g 273 ) ); 274 286 275 groups_reject_membership_request( null, $u1, $g ); 287 276 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 288 277 $this->assertEquals( 0, $u1_has_request ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 325 314 ) ); 326 315 327 316 // Outstanding invitations should be left intact. 328 self::invite_user_to_group( $u2, $g, $u1 ); 317 groups_invite_user( array( 318 'user_id' => $u2, 319 'group_id' => $g, 320 'inviter_id' => $u1, 321 'send_invite' => 1, 322 ) ); 329 323 groups_reject_membership_request( null, $u2, $g ); 330 324 $u2_has_invite = groups_check_user_has_invite( $u2, $g ); 331 325 $this->assertTrue( is_numeric( $u2_has_invite ) && $u2_has_invite > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 343 337 ) ); 344 338 345 339 // Membership requests should be removed. 346 self::create_group_membership_request( $u1, $g ); 340 groups_send_membership_request( array( 341 'user_id' => $u1, 342 'group_id' => $g 343 ) ); 347 344 groups_delete_membership_request( null, $u1, $g ); 348 345 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 349 346 $this->assertEquals( 0, $u1_has_request ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 386 383 ) ); 387 384 388 385 // Outstanding invitations should be left intact. 389 self::invite_user_to_group( $u2, $g, $u1 ); 386 groups_invite_user( array( 387 'user_id' => $u2, 388 'group_id' => $g, 389 'inviter_id' => $u1, 390 'send_invite' => 1, 391 ) ); 392 390 393 groups_delete_membership_request( null, $u2, $g ); 391 394 $u2_has_invite = groups_check_user_has_invite( $u2, $g ); 392 395 $this->assertTrue( is_numeric( $u2_has_invite ) && $u2_has_invite > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 410 413 ) ); 411 414 412 415 // The invitation should be removed. 413 self::invite_user_to_group( $u2, $g, $u1 ); 416 groups_invite_user( array( 417 'user_id' => $u2, 418 'group_id' => $g, 419 'inviter_id' => $u1, 420 'send_invite' => 1, 421 ) ); 422 414 423 groups_reject_invite( $u2, $g ); 415 424 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 416 425 $this->assertEquals( 0, $u2_has_invite ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 450 459 ) ); 451 460 452 461 // Membership requests should be left intact. 453 self::create_group_membership_request( $u1, $g ); 462 groups_send_membership_request( array( 463 'user_id' => $u1, 464 'group_id' => $g 465 ) ); 454 466 groups_reject_invite( $u1, $g ); 455 467 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 456 468 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 474 486 ) ); 475 487 476 488 // The invitation should be removed. 477 self::invite_user_to_group( $u2, $g, $u1 ); 489 groups_invite_user( array( 490 'user_id' => $u2, 491 'group_id' => $g, 492 'inviter_id' => $u1, 493 'send_invite' => 1, 494 ) ); 495 478 496 groups_delete_invite( $u2, $g ); 479 497 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 480 498 $this->assertEquals( 0, $u2_has_invite ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 547 565 ) ); 548 566 549 567 // Membership requests should be left intact. 550 self::create_group_membership_request( $u1, $g ); 568 groups_send_membership_request( array( 569 'user_id' => $u1, 570 'group_id' => $g 571 ) ); 551 572 groups_delete_invite( $u1, $g ); 552 573 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 553 574 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 571 592 ) ); 572 593 573 594 // The invitation should be removed. 574 self::invite_user_to_group( $u2, $g, $u1 ); 595 groups_invite_user( array( 596 'user_id' => $u2, 597 'group_id' => $g, 598 'inviter_id' => $u1, 599 'send_invite' => 1, 600 ) ); 575 601 groups_uninvite_user( $u2, $g ); 576 602 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 577 603 $this->assertEquals( 0, $u2_has_invite ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 612 638 ) ); 613 639 614 640 // Membership requests should be left intact. 615 self::create_group_membership_request( $u1, $g ); 641 groups_send_membership_request( array( 642 'user_id' => $u1, 643 'group_id' => $g 644 ) ); 616 645 groups_uninvite_user( $u1, $g ); 617 646 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 618 647 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 672 701 $m1 = new BP_Groups_Member( $u1, $g ); 673 702 $m1->promote( 'admin' ); 674 703 675 self::invite_user_to_group( $u2, $g, $u1 ); 676 704 groups_invite_user( array( 705 'user_id' => $u2, 706 'group_id' => $g, 707 'inviter_id' => $u1, 708 'send_invite' => 1, 709 ) ); 677 710 groups_join_group( $g, $u2 ); 678 711 // Upon joining the group, outstanding invitations should be cleaned up. 679 712 $this->assertEquals( null, groups_check_user_has_invite( $u2, $g, 'any' ) ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 686 719 public function test_groups_join_group_cleanup_requests() { 687 720 $u1 = self::factory()->user->create(); 688 721 $g = self::factory()->group->create(); 689 self::create_group_membership_request( $u1, $g ); 722 723 groups_send_membership_request( array( 724 'user_id' => $u1, 725 'group_id' => $g 726 ) ); 690 727 691 728 groups_join_group( $g, $u1 ); 692 729 // Upon joining the group, outstanding requests should be cleaned up. … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 825 862 public function test_groups_get_invites_for_user() { 826 863 $u1 = self::factory()->user->create(); 827 864 $u2 = self::factory()->user->create(); 828 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 829 $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 830 $g3 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 831 832 self::invite_user_to_group( $u2, $g1, $u1 ); 833 self::invite_user_to_group( $u2, $g2, $u1 ); 834 self::invite_user_to_group( $u2, $g3, $u1 ); 865 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 866 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 867 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 835 868 869 groups_invite_user( array( 870 'user_id' => $u2, 871 'group_id' => $g1, 872 'inviter_id' => $u1, 873 'send_invite' => 1, 874 ) ); 875 groups_invite_user( array( 876 'user_id' => $u2, 877 'group_id' => $g2, 878 'inviter_id' => $u1, 879 'send_invite' => 1, 880 ) ); 881 groups_invite_user( array( 882 'user_id' => $u2, 883 'group_id' => $g3, 884 'inviter_id' => $u1, 885 'send_invite' => 1, 886 ) ); 836 887 $groups = groups_get_invites_for_user( $u2 ); 837 888 838 889 $this->assertEqualSets( array( $g1, $g2, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 848 899 849 900 $u1 = self::factory()->user->create(); 850 901 $u2 = self::factory()->user->create(); 851 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );852 $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );853 $g3 = self::factory()->group->create( array( 'creator_id' => $u1 ) );902 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 903 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 904 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 854 905 855 self::invite_user_to_group( $u2, $g1, $u1 ); 856 self::invite_user_to_group( $u2, $g2, $u1 ); 857 self::invite_user_to_group( $u2, $g3, $u1 ); 906 groups_invite_user( array( 907 'user_id' => $u2, 908 'group_id' => $g1, 909 'inviter_id' => $u1, 910 'send_invite' => 1, 911 ) ); 912 groups_invite_user( array( 913 'user_id' => $u2, 914 'group_id' => $g2, 915 'inviter_id' => $u1, 916 'send_invite' => 1, 917 ) ); 918 groups_invite_user( array( 919 'user_id' => $u2, 920 'group_id' => $g3, 921 'inviter_id' => $u1, 922 'send_invite' => 1, 923 ) ); 858 924 859 925 $this->set_current_user( $u2 ); 860 926 $groups = groups_get_invites_for_user(); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 871 937 public function test_groups_get_invites_for_user_with_exclude() { 872 938 $u1 = self::factory()->user->create(); 873 939 $u2 = self::factory()->user->create(); 874 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );875 $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );876 $g3 = self::factory()->group->create( array( 'creator_id' => $u1 ) );940 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 941 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 942 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 877 943 878 self::invite_user_to_group( $u2, $g1, $u1 ); 879 self::invite_user_to_group( $u2, $g2, $u1 ); 880 self::invite_user_to_group( $u2, $g3, $u1 ); 944 groups_invite_user( array( 945 'user_id' => $u2, 946 'group_id' => $g1, 947 'inviter_id' => $u1, 948 'send_invite' => 1, 949 ) ); 950 groups_invite_user( array( 951 'user_id' => $u2, 952 'group_id' => $g2, 953 'inviter_id' => $u1, 954 'send_invite' => 1, 955 ) ); 956 groups_invite_user( array( 957 'user_id' => $u2, 958 'group_id' => $g3, 959 'inviter_id' => $u1, 960 'send_invite' => 1, 961 ) ); 881 962 882 963 $groups = groups_get_invites_for_user( $u2, false, false, array( $g2 ) ); 883 964 $this->assertEqualSets( array( $g1, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 891 972 public function test_groups_get_invite_count_for_user() { 892 973 $u1 = self::factory()->user->create(); 893 974 $u2 = self::factory()->user->create(); 894 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );895 $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );896 $g3 = self::factory()->group->create( array( 'creator_id' => $u1 ) );975 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 976 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 977 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 897 978 898 self::invite_user_to_group( $u2, $g1, $u1 ); 899 self::invite_user_to_group( $u2, $g2, $u1 ); 900 self::invite_user_to_group( $u2, $g3, $u1 ); 979 groups_invite_user( array( 980 'user_id' => $u2, 981 'group_id' => $g1, 982 'inviter_id' => $u1, 983 'send_invite' => 1, 984 ) ); 985 groups_invite_user( array( 986 'user_id' => $u2, 987 'group_id' => $g2, 988 'inviter_id' => $u1, 989 'send_invite' => 1, 990 ) ); 991 groups_invite_user( array( 992 'user_id' => $u2, 993 'group_id' => $g3, 994 'inviter_id' => $u1, 995 'send_invite' => 1, 996 ) ); 901 997 902 998 $this->assertEquals( 3, groups_get_invite_count_for_user( $u2 ) ); 903 999 } … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 910 1006 public function test_groups_get_invite_count_for_user_ignore_drafts() { 911 1007 $u1 = self::factory()->user->create(); 912 1008 $u2 = self::factory()->user->create(); 913 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1009 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 914 1010 915 1011 // Create draft invitation. 916 1012 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 933 1029 public function test_groups_invite_user() { 934 1030 $u1 = self::factory()->user->create(); 935 1031 $u2 = self::factory()->user->create(); 936 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1032 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 937 1033 938 1034 // Create draft invitation 939 1035 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 957 1053 public function test_groups_send_invites() { 958 1054 $u1 = self::factory()->user->create(); 959 1055 $u2 = self::factory()->user->create(); 960 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1056 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 961 1057 962 1058 // Create draft invitation 963 1059 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 969 1065 ) ); 970 1066 971 1067 // Send the invitation 972 groups_send_invites( $u1, $g1 ); 1068 groups_send_invites( array( 1069 'group_id' => $g1, 1070 'inviter_id' => $u1, 1071 ) ); 973 1072 974 1073 // Check that the invitation has been sent. 975 1074 $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 977 1076 } 978 1077 979 1078 /** 980 * @group groups_ accept_invite1079 * @group groups_send_invites 981 1080 * @group group_invitations 982 1081 * @group group_membership 1082 * @expectedDeprecated groups_send_invites 983 1083 */ 984 public function test_groups_ accept_invite() {1084 public function test_groups_send_invites_deprecated_args() { 985 1085 $u1 = self::factory()->user->create(); 986 1086 $u2 = self::factory()->user->create(); 987 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1087 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 988 1088 989 1089 // Create draft invitation 990 1090 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 998 1098 // Send the invitation 999 1099 groups_send_invites( $u1, $g1 ); 1000 1100 1101 // Check that the invitation has been sent. 1102 $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' ); 1103 $this->assertTrue( is_numeric( $sent ) && $sent > 0 ); 1104 } 1105 1106 /** 1107 * @group groups_accept_invite 1108 * @group group_invitations 1109 * @group group_membership 1110 */ 1111 public function test_groups_accept_invite() { 1112 $u1 = self::factory()->user->create(); 1113 $u2 = self::factory()->user->create(); 1114 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1115 1116 // Create draft invitation 1117 groups_invite_user( array( 1118 'user_id' => $u2, 1119 'group_id' => $g1, 1120 'inviter_id' => $u1, 1121 'date_modified' => bp_core_current_time(), 1122 'is_confirmed' => 0, 1123 'send_invite' => 1 1124 ) ); 1125 1001 1126 // Accept the invitation 1002 1127 groups_accept_invite( $u2, $g1 ); 1003 1128 … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1017 1142 public function test_groups_accept_invite_removes_membership_requests() { 1018 1143 $u1 = self::factory()->user->create(); 1019 1144 $u2 = self::factory()->user->create(); 1020 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1145 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1021 1146 1022 1147 // Create draft invitation 1023 1148 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1029 1154 ) ); 1030 1155 1031 1156 // Create membership request 1032 groups_send_membership_request( $u2, $g1 ); 1157 $request_id = groups_send_membership_request( array( 1158 'user_id' => $u2, 1159 'group_id' => $g1, 1160 ) ); 1161 1033 1162 $request = groups_check_for_membership_request( $u2, $g1 ); 1163 1034 1164 $this->assertTrue( is_numeric( $request ) && $request > 0 ); 1035 1165 1036 1166 // Send the invitation 1037 groups_send_invites( $u1, $g1 ); 1167 groups_send_invites( array( 1168 'group_id' => $g1, 1169 'inviter_id' => $u1, 1170 ) ); 1038 1171 1039 1172 // Accept the invitation 1040 1173 groups_accept_invite( $u2, $g1 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1052 1185 public function test_groups_sent_invite_plus_request_equals_member() { 1053 1186 $u1 = self::factory()->user->create(); 1054 1187 $u2 = self::factory()->user->create(); 1055 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1188 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1056 1189 1057 1190 // Create draft invitation 1058 1191 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1060 1193 'group_id' => $g1, 1061 1194 'inviter_id' => $u1, 1062 1195 'date_modified' => bp_core_current_time(), 1063 'is_confirmed' => 0 1196 'is_confirmed' => 0, 1197 'send_invite' => 1 1064 1198 ) ); 1065 1199 1066 // Send the invitation1067 groups_send_invites( $u1, $g1 );1068 1069 1200 // Create membership request 1070 groups_send_membership_request( $u2, $g1 ); 1201 groups_send_membership_request( array( 1202 'user_id' => $u2, 1203 'group_id' => $g1 1204 ) ); 1071 1205 1072 1206 // User should now be a group member 1073 1207 $member = groups_is_user_member( $u2, $g1 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1083 1217 $u1 = self::factory()->user->create(); 1084 1218 $u2 = self::factory()->user->create(); 1085 1219 $u3 = self::factory()->user->create(); 1086 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1220 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1087 1221 1088 self::invite_user_to_group( $u2, $g1, $u1 ); 1089 self::invite_user_to_group( $u3, $g1, $u1 ); 1222 groups_invite_user( array( 1223 'user_id' => $u2, 1224 'group_id' => $g1, 1225 'inviter_id' => $u1, 1226 'send_invite' => 1, 1227 ) ); 1228 groups_invite_user( array( 1229 'user_id' => $u3, 1230 'group_id' => $g1, 1231 'inviter_id' => $u1, 1232 'send_invite' => 1, 1233 ) ); 1090 1234 1091 1235 groups_delete_all_group_invites( $g1 ); 1092 1236 1093 1237 // Get group invitations of any type, from any user in the group. 1094 $invitees = new BP_Group_Member_Query( array( 1238 1239 $invitees = groups_get_invites( array( 1095 1240 'group_id' => $g1, 1096 'is_confirmed' => 0, 1097 'invite_sent' => null, 1098 'inviter_id' => 'any', 1241 'invite_sent' => 'all', 1099 1242 ) ); 1100 1243 1101 $this->assertTrue( empty( $invitees ->results) );1244 $this->assertTrue( empty( $invitees ) ); 1102 1245 } 1103 1246 1104 1247 /** … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1129 1272 */ 1130 1273 public function test_groups_send_invites_fail_on_empty_user_id() { 1131 1274 $u1 = self::factory()->user->create(); 1132 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1275 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1133 1276 1134 1277 // Create draft invitation with empty inviter_id 1135 1278 $invite_created = groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1151 1294 public function test_groups_send_invites_fail_on_empty_inviter_id() { 1152 1295 $u1 = self::factory()->user->create(); 1153 1296 $u2 = self::factory()->user->create(); 1154 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1297 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1155 1298 1156 1299 // Create draft invitation with empty inviter_id 1157 1300 $invite_created = groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1174 1317 public function test_groups_get_invites_for_group_with_sent_parameter() { 1175 1318 $u1 = self::factory()->user->create(); 1176 1319 $u2 = self::factory()->user->create(); 1177 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1320 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1178 1321 1179 1322 // Create draft invitation 1180 1323 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1182 1325 'group_id' => $g1, 1183 1326 'inviter_id' => $u1, 1184 1327 'date_modified' => bp_core_current_time(), 1185 'is_confirmed' => 0 1328 'is_confirmed' => 0, 1329 'send_invite' => 1 1186 1330 ) ); 1187 1331 1188 // Send the invitation; this will set the 'invite_sent' value to 1.1189 groups_send_invites( $u1, $g1 );1190 1191 1332 // Default groups_get_invites_for_group() call 1192 1333 $i = groups_get_invites_for_group( $u1, $g1 ); 1193 1334 $this->assertEqualSets( array( $u2 ), $i ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1208 1349 */ 1209 1350 public function test_groups_send_membership_request() { 1210 1351 $u1 = self::factory()->user->create(); 1211 $g1 = self::factory()->group->create(); 1352 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1353 1354 // Create membership request 1355 groups_send_membership_request( array( 1356 'user_id' => $u1, 1357 'group_id' => $g1 1358 ) ); 1359 1360 $request = groups_check_for_membership_request( $u1, $g1 ); 1361 $this->assertTrue( is_numeric( $request ) && $request > 0 ); 1362 } 1363 1364 /** 1365 * @group groups_send_membership_request 1366 * @group group_membership_requests 1367 * @group group_membership 1368 * @expectedDeprecated groups_send_membership_request 1369 */ 1370 public function test_groups_send_membership_request_deprecated_args() { 1371 $u1 = self::factory()->user->create(); 1372 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1212 1373 1213 1374 // Create membership request 1214 1375 groups_send_membership_request( $u1, $g1 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1224 1385 */ 1225 1386 public function test_groups_accept_membership_request_by_membership_id() { 1226 1387 $u1 = self::factory()->user->create(); 1227 $g1 = self::factory()->group->create( );1388 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1228 1389 1229 1390 // Create membership request 1230 groups_send_membership_request( $u1, $g1 ); 1391 groups_send_membership_request( array( 1392 'user_id' => $u1, 1393 'group_id' => $g1 1394 ) ); 1231 1395 1232 1396 // Get group invitations of any type, from any user in the group. 1233 1397 $member = new BP_Groups_Member( $u1, $g1 ); 1234 1398 1235 groups_accept_membership_request( $member->id);1399 groups_accept_membership_request( false, $u1, $g1 ); 1236 1400 1237 1401 // User should now be a group member. 1238 1402 $member = groups_is_user_member( $u1, $g1 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1247 1411 */ 1248 1412 public function test_groups_accept_membership_request_by_user_id_group_id() { 1249 1413 $u1 = self::factory()->user->create(); 1250 $g1 = self::factory()->group->create( );1414 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1251 1415 1252 1416 // Create membership request 1253 groups_send_membership_request( $u1, $g1 ); 1417 groups_send_membership_request( array( 1418 'user_id' => $u1, 1419 'group_id' => $g1 1420 ) ); 1254 1421 1255 1422 groups_accept_membership_request( null, $u1, $g1 ); 1256 1423 … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1268 1435 public function test_groups_membership_request_plus_invite_equals_member() { 1269 1436 $u1 = self::factory()->user->create(); 1270 1437 $u2 = self::factory()->user->create(); 1271 $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );1438 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1272 1439 1273 1440 // Create membership request 1274 groups_send_membership_request( $u2, $g1 ); 1441 groups_send_membership_request( array( 1442 'user_id' => $u2, 1443 'group_id' => $g1 1444 ) ); 1275 1445 1276 1446 // Create draft invitation 1277 1447 groups_invite_user( array( … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1279 1449 'group_id' => $g1, 1280 1450 'inviter_id' => $u1, 1281 1451 'date_modified' => bp_core_current_time(), 1282 'is_confirmed' => 0 1452 'is_confirmed' => 0, 1453 'send_invite' => 1 1283 1454 ) ); 1284 1455 1285 // Send the invitation1286 groups_send_invites( $u1, $g1 );1287 1288 1456 // User should now be a group member 1289 1457 $member = groups_is_user_member( $u2, $g1 ); 1290 1458 $this->assertTrue( is_numeric( $member ) && $member > 0 ); … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1299 1467 $u1 = self::factory()->user->create(); 1300 1468 $u2 = self::factory()->user->create(); 1301 1469 $u3 = self::factory()->user->create(); 1302 $g1 = self::factory()->group->create( );1470 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1303 1471 1304 1472 // Create membership request 1305 groups_send_membership_request( $u1, $g1 ); 1306 groups_send_membership_request( $u2, $g1 ); 1307 groups_send_membership_request( $u3, $g1 ); 1473 groups_send_membership_request( array( 1474 'user_id' => $u1, 1475 'group_id' => $g1 1476 ) ); 1477 groups_send_membership_request( array( 1478 'user_id' => $u2, 1479 'group_id' => $g1 1480 ) ); 1481 groups_send_membership_request( array( 1482 'user_id' => $u3, 1483 'group_id' => $g1 1484 ) ); 1308 1485 1309 1486 groups_accept_all_pending_membership_requests( $g1 ); 1310 1487 … … class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1402 1579 $this->assertSame( self::$group_ids[0], $memberships[0]->group_id ); 1403 1580 } 1404 1581 1405 /**1406 * @ticket BP78591407 */1408 public function test_get_user_memberships_type_pending_request() {1409 groups_join_group( self::$group_ids[0], self::$user_ids[0] );1410 groups_send_membership_request( self::$user_ids[0], self::$group_ids[1] );1411 1412 $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(1413 'type' => 'pending_request',1414 ) );1415 1416 $this->assertCount( 1, $memberships );1417 $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );1418 }1419 1420 /**1421 * @ticket BP78591422 */1423 public function test_get_user_memberships_type_pending_received_invitation() {1424 groups_join_group( self::$group_ids[0], self::$user_ids[0] );1425 groups_invite_user( array(1426 'user_id' => self::$user_ids[0],1427 'group_id' => self::$group_ids[1],1428 'inviter_id' => self::$user_ids[1],1429 ) );1430 1431 $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(1432 'type' => 'pending_received_invitation',1433 ) );1434 1435 $this->assertCount( 1, $memberships );1436 $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );1437 }1438 1439 /**1440 * @ticket BP78591441 */1442 public function test_get_user_memberships_type_pending_sent_invitation() {1443 groups_join_group( self::$group_ids[0], self::$user_ids[0] );1444 groups_invite_user( array(1445 'user_id' => self::$user_ids[1],1446 'group_id' => self::$group_ids[1],1447 'inviter_id' => self::$user_ids[0],1448 ) );1449 1450 $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(1451 'type' => 'pending_sent_invitation',1452 ) );1453 1454 $this->assertCount( 1, $memberships );1455 $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );1456 }1457 1458 1582 /** 1459 1583 * @ticket BP7476 1460 1584 */ -
tests/phpunit/testcases/groups/functions.php
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php index c4a24592a..ee04ef6a0 100644
class BP_Tests_Groups_Functions extends BP_UnitTestCase { 12 12 self::$user_ids = $factory->user->create_many( 3 ); 13 13 self::$group_ids = $factory->group->create_many( 2, array( 14 14 'creator_id' => self::$user_ids[2], 15 'status' => 'private' 15 16 ) ); 16 17 } 17 18 … … class BP_Tests_Groups_Functions extends BP_UnitTestCase { 138 139 $this->set_current_user( $u2 ); 139 140 140 141 $g = self::factory()->group->create(); 141 groups_send_membership_request( $u1, $g ); 142 groups_send_membership_request( array( 143 'user_id' => $u1, 144 'group_id' => $g, 145 ) ); 142 146 143 147 groups_accept_membership_request( 0, $u1, $g ); 144 148 … … class BP_Tests_Groups_Functions extends BP_UnitTestCase { 262 266 $u2 = self::factory()->user->create(); 263 267 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 264 268 265 groups_send_membership_request( $u2, $g ); 269 groups_send_membership_request( array( 270 'user_id' => $u2, 271 'group_id' => $g, 272 ) ); 266 273 groups_accept_membership_request( 0, $u2, $g ); 267 274 268 275 $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); … … Bar!'; 632 639 public function test_get_invite_count_for_user() { 633 640 $u1 = self::factory()->user->create(); 634 641 $u2 = self::factory()->user->create(); 635 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );642 $g = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 636 643 637 644 // create invitation 638 645 groups_invite_user( array( 639 646 'user_id' => $u2, 640 647 'group_id' => $g, 641 648 'inviter_id' => $u1, 649 'send_invite' => 1 642 650 ) ); 643 651 644 // send the invite645 // this function is imperative to set the 'invite_sent' flag in the DB646 // why is this separated from groups_invite_user()?647 // @see groups_screen_group_invite()648 groups_send_invites( $u1, $g );649 650 652 // assert invite count 651 653 $this->assertEquals( 1, groups_get_invite_count_for_user( $u2 ) ); 652 654 … … Bar!'; 890 892 * @ticket BP7698 891 893 */ 892 894 public function test_bp_groups_pending_requests_personal_data_exporter() { 893 groups_send_membership_request( self::$user_ids[0], self::$group_ids[0] ); 895 groups_send_membership_request( array( 896 'user_id' => self::$user_ids[0], 897 'group_id' => self::$group_ids[0], 898 ) ); 894 899 895 900 $test_user = new WP_User( self::$user_ids[0] ); 896 901 -
tests/phpunit/testcases/groups/functions/bpGetUserGroups.php
diff --git tests/phpunit/testcases/groups/functions/bpGetUserGroups.php tests/phpunit/testcases/groups/functions/bpGetUserGroups.php index acf28c6b1..8e4c79ae8 100644
class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 26 26 ) ); 27 27 self::$groups = $f->group->create_many( 4, array( 28 28 'creator_id' => self::$admin_user, 29 'status' => 'private' 29 30 ) ); 30 31 31 32 $now = time(); … … class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 516 517 $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null; 517 518 $_SERVER['SERVER_NAME'] = ''; 518 519 519 groups_send_membership_request( self::$user, self::$groups[2] ); 520 groups_send_membership_request( array( 521 'user_id' => self::$user, 522 'group_id' => self::$groups[2] 523 ) ); 520 524 521 525 // For `wp_mail()`. 522 526 if ( is_null( $server_name ) ) { … … class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 541 545 $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null; 542 546 $_SERVER['SERVER_NAME'] = ''; 543 547 544 groups_send_membership_request( self::$user, self::$groups[2] ); 548 groups_send_membership_request( array( 549 'user_id' => self::$user, 550 'group_id' => self::$groups[2], 551 ) ); 545 552 546 553 // Populate cache. 547 554 $g1 = bp_get_user_groups( self::$user ); 548 555 549 556 $m = new BP_Groups_Member( self::$user, self::$groups[2] ); 550 557 551 groups_accept_membership_request( $m->id, self::$user, self::$groups[2] );558 groups_accept_membership_request( false, self::$user, self::$groups[2] ); 552 559 553 560 // For `wp_mail()`. 554 561 if ( is_null( $server_name ) ) { -
tests/phpunit/testcases/groups/functions/groupsIsUser.php
diff --git tests/phpunit/testcases/groups/functions/groupsIsUser.php tests/phpunit/testcases/groups/functions/groupsIsUser.php index f7d6dd356..ef35382c9 100644
class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 21 21 ) ); 22 22 self::$groups = $f->group->create_many( 3, array( 23 23 'creator_id' => self::$admin_user, 24 'status' => 'private' 24 25 ) ); 25 26 26 27 $now = time(); … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 173 174 $i = groups_invite_user( array( 174 175 'user_id' => self::$user, 175 176 'group_id' => self::$groups[1], 176 'inviter_id' => 123, 177 'inviter_id' => self::$admin_user, 178 'send_invite' => 1, 177 179 ) ); 178 180 179 // Send invite.180 $m = new BP_Groups_Member( self::$user, self::$groups[1] );181 $m->invite_sent = 1;182 $m->save();183 184 181 $this->assertNotEmpty( groups_is_user_invited( self::$user, self::$groups[1] ) ); 185 182 } 186 183 187 public function test_groups_is_user_pending_should_return_false_for_ pending_member() {184 public function test_groups_is_user_pending_should_return_false_for_invited_member() { 188 185 groups_invite_user( array( 189 186 'user_id' => self::$user, 190 187 'group_id' => self::$groups[1], 191 ' inviter_id' => 123,188 'send_invite' => 1 192 189 ) ); 193 190 194 // Send invite.195 $m = new BP_Groups_Member( self::$user, self::$groups[1] );196 $m->invite_sent = 1;197 $m->save();198 199 191 $this->assertEquals( false, groups_is_user_pending( self::$user, self::$groups[1] ) ); 200 192 } 201 193 … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 204 196 } 205 197 206 198 public function test_groups_is_user_pending_should_return_true_for_pending_member() { 207 208 $m = new BP_Groups_Member; 209 $m->group_id = self::$groups[1]; 210 $m->user_id = self::$user; 211 $m->inviter_id = 0; 212 $m->is_admin = 0; 213 $m->user_title = ''; 214 $m->date_modified = bp_core_current_time(); 215 $m->is_confirmed = 0; 216 $m->comments = 'request'; 217 $m->save(); 199 groups_send_membership_request( array( 200 'user_id' => self::$user, 201 'group_id' => self::$groups[1], 202 ) ); 218 203 219 204 $this->assertNotEmpty( groups_is_user_pending( self::$user, self::$groups[1] ) ); 220 205 } -
tests/phpunit/testcases/groups/notifications.php
diff --git tests/phpunit/testcases/groups/notifications.php tests/phpunit/testcases/groups/notifications.php index c844c91ef..2aafb30e0 100644
class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 16 16 $this->set_current_user( self::factory()->user->create() ); 17 17 18 18 $this->requesting_user_id = self::factory()->user->create(); 19 $this->group = self::factory()->group->create( );19 $this->group = self::factory()->group->create( array( 'status' => 'private' ) ); 20 20 $this->filter_fired = ''; 21 21 } 22 22 … … class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 232 232 'is_admin' => 1, 233 233 ) ); 234 234 235 groups_send_membership_request( $users[2], $this->group ); 235 groups_send_membership_request( array( 236 'user_id' => $users[2], 237 'group_id' => $this->group 238 ) ); 236 239 237 240 // Both admins should get a notification. 238 241 $get_args = array( … … class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 247 250 $this->assertNotEmpty( $u0_notifications ); 248 251 $this->assertNotEmpty( $u1_notifications ); 249 252 250 $this->assertTrue( groups_invite_user( array( 253 groups_accept_membership_request( false, $users[2], $this->group ); 254 255 $u0_notifications = BP_Notifications_Notification::get( $get_args ); 256 $u1_notifications = BP_Notifications_Notification::get( $get_args ); 257 $this->assertEmpty( $u0_notifications ); 258 $this->assertEmpty( $u1_notifications ); 259 } 260 261 public function test_membership_request_notifications_should_be_cleared_when_request_is_accepted_via_invite() { 262 $users = self::factory()->user->create_many( 3 ); 263 264 $this->add_user_to_group( $users[0], $this->group, array( 265 'is_admin' => 1, 266 ) ); 267 $this->add_user_to_group( $users[1], $this->group, array( 268 'is_admin' => 1, 269 ) ); 270 271 groups_send_membership_request( array( 272 'user_id' => $users[2], 273 'group_id' => $this->group 274 ) ); 275 276 // Both admins should get a notification. 277 $get_args = array( 278 'user_id' => $users[0], 279 'item_id' => $this->group, 280 'secondary_item_id' => $users[2], 281 'component_action' => 'new_membership_request', 282 'is_new' => true, 283 ); 284 $u0_notifications = BP_Notifications_Notification::get( $get_args ); 285 $u1_notifications = BP_Notifications_Notification::get( $get_args ); 286 $this->assertNotEmpty( $u0_notifications ); 287 $this->assertNotEmpty( $u1_notifications ); 288 289 // 'Accept' the request by sending an invite. 290 groups_invite_user( array( 251 291 'user_id' => $users[2], 252 292 'group_id' => $this->group, 253 ) ) ); 293 'send_invite' => true 294 ) ); 254 295 255 296 $u0_notifications = BP_Notifications_Notification::get( $get_args ); 256 297 $u1_notifications = BP_Notifications_Notification::get( $get_args ); -
tests/phpunit/testcases/groups/template/bpGroupStatusMessage.php
diff --git tests/phpunit/testcases/groups/template/bpGroupStatusMessage.php tests/phpunit/testcases/groups/template/bpGroupStatusMessage.php index b6d5e1173..bcc1ff3eb 100644
class BP_Tests_Groups_Template_BpGroupStatusMessage extends BP_UnitTestCase { 98 98 99 99 $this->set_current_user( $u ); 100 100 101 groups_send_membership_request( $u, $g ); 101 groups_send_membership_request( array( 102 'user_id' => $u, 103 'group_id' => $g 104 ) ); 102 105 103 106 if ( bp_has_groups( array( 'include' => array( $g ) ) ) ) { 104 107 while ( bp_groups() ) { … … class BP_Tests_Groups_Template_BpGroupStatusMessage extends BP_UnitTestCase { 141 144 $GLOBALS['groups_template'] = new stdClass; 142 145 $GLOBALS['groups_template']->group = groups_get_group( $groups[0] ); 143 146 144 groups_send_membership_request( $u, $groups[1] ); 147 groups_send_membership_request( array( 148 'user_id' => $u, 149 'group_id' => $groups[1] 150 ) ); 145 151 146 152 $group1 = groups_get_group( array( 147 153 'group_id' => $groups[1], -
tests/phpunit/testcases/groups/user_can.php
diff --git tests/phpunit/testcases/groups/user_can.php tests/phpunit/testcases/groups/user_can.php index 01b4bd254..fee0706f4 100644
class BP_Tests_Groups_User_Can_Filter extends BP_UnitTestCase { 108 108 'status' => 'private' 109 109 ) ); 110 110 $u1 = $this->factory->user->create(); 111 groups_send_membership_request( $u1, $g1 ); 111 groups_send_membership_request( array( 112 'user_id' => $u1, 113 'group_id' => $g1 114 ) ); 112 115 113 116 $this->assertFalse( bp_user_can( $u1, 'groups_request_membership', array( 'group_id' => $g1 ) ) ); 114 117 }