[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
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()->members->table_name_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 Type of record this is: 'invite' or 'request'. 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( 85 $args, 86 array( 87 'user_id' => 0, 88 'invitee_email' => '', 89 'inviter_id' => 0, 90 'item_id' => 0, 91 'secondary_item_id' => 0, 92 'type' => 'invite', 93 'content' => '', 94 'date_modified' => bp_core_current_time(), 95 'send_invite' => 0, 96 'accepted' => 0, 97 ), 98 'add_invitation' 99 ); 100 101 // Invitations must have an invitee and inviter. 102 if ( ! ( ( $r['user_id'] || $r['invitee_email'] ) && $r['inviter_id'] ) ) { 103 return false; 104 } 105 106 // If an email address is specified, it must be a valid email address. 107 if ( $r['invitee_email'] && ! is_email( $r['invitee_email'] ) ) { 108 return false; 109 } 110 111 /** 112 * Is this user allowed to extend invitations in this situation? 113 * 114 * @since 5.0.0 115 * 116 * @param array $r Describes the invitation to be added. 117 */ 118 if ( ! $this->allow_invitation( $r ) ) { 119 return false; 120 } 121 122 // Avoid creating duplicate invitations. 123 $invite_id = $this->invitation_exists( array( 124 'user_id' => $r['user_id'], 125 'invitee_email' => $r['invitee_email'], 126 'inviter_id' => $r['inviter_id'], 127 'item_id' => $r['item_id'], 128 'secondary_item_id' => $r['secondary_item_id'], 129 ) ); 130 131 if ( ! $invite_id ) { 132 // Set up the new invitation as a draft. 133 $invitation = new BP_Invitation; 134 $invitation->user_id = $r['user_id']; 135 $invitation->inviter_id = $r['inviter_id']; 136 $invitation->invitee_email = $r['invitee_email']; 137 $invitation->class = $this->class_name; 138 $invitation->item_id = $r['item_id']; 139 $invitation->secondary_item_id = $r['secondary_item_id']; 140 $invitation->type = $r['type']; 141 $invitation->content = $r['content']; 142 $invitation->date_modified = $r['date_modified']; 143 $invitation->invite_sent = 0; 144 $invitation->accepted = 0; 145 146 $invite_id = $invitation->save(); 147 } 148 149 // "Send" the invite if necessary. 150 if ( $invite_id && $r['send_invite'] ) { 151 $sent = $this->send_invitation_by_id( $invite_id, $r ); 152 if ( ! $sent ) { 153 return false; 154 } 155 } 156 157 return $invite_id; 158 } 159 160 /** 161 * Send an invitation notification. 162 * 163 * @since 5.0.0 164 * @access public 165 * 166 * @param int $invitation_id ID of invitation to send. 167 * @param array $args See BP_Invitation::mark_sent(). 168 * 169 * @return bool The result of `run_send_action()`. 170 */ 171 public function send_invitation_by_id( $invitation_id = 0, $args = array() ) { 172 $updated = false; 173 174 $invitation = new BP_Invitation( $invitation_id ); 175 176 if ( ! $invitation->id ) { 177 return false; 178 } 179 180 /** 181 * Fires before an invitation is sent. 182 * 183 * @since 5.0.0 184 * 185 * @param BP_Invitation object $invitation Invitation about to be sent. 186 */ 187 do_action( 'bp_invitations_send_invitation_by_id_before_send', $invitation ); 188 189 /* 190 * Before sending an invitation, check for outstanding requests to the same item. 191 * A sent invitation + a request = acceptance. 192 */ 193 $request_args = array( 194 'user_id' => $invitation->user_id, 195 'invitee_email' => $invitation->invitee_email, 196 'item_id' => $invitation->item_id, 197 'secondary_item_id' => $invitation->secondary_item_id, 198 ); 199 $request = $this->request_exists( $request_args ); 200 201 if ( ! empty( $request ) ) { 202 // Accept the request. 203 return $this->accept_request( $request_args ); 204 } 205 206 // Perform the send action. 207 $success = $this->run_send_action( $invitation ); 208 209 if ( $success ) { 210 BP_Invitation::mark_sent( $invitation->id, $args ); 211 } 212 213 return $success; 214 } 215 216 /** 217 * Add a request to an item for a specific user, related to a 218 * specific class. 219 * 220 * @since 5.0.0 221 * 222 * @param array $args { 223 * Array of arguments describing the invitation. All are optional. 224 * @type int $user_id ID of the invited user. 225 * @type int $inviter_id ID of the user who created the invitation. 226 * @type string $class Name of the invitations class. 227 * @type int $item_id ID associated with the invitation and class. 228 * @type int $secondary_item_id secondary ID associated with the 229 * invitation and class. 230 * @type string $type Type of record this is: 'invite' or 'request'. 231 * @type string $content Extra information provided by the requester 232 * or inviter. 233 * @type string $date_modified Date the invitation was last modified. 234 * @type int $invite_sent Has the invitation been sent, or is it a 235 * draft invite? 236 * } 237 * @return int|bool ID of the newly created invitation on success, false 238 * on failure. 239 */ 240 public function add_request( $args = array() ) { 241 242 $r = bp_parse_args( 243 $args, 244 array( 245 'user_id' => 0, 246 'inviter_id' => 0, 247 'invitee_email' => '', 248 'item_id' => 0, 249 'secondary_item_id' => 0, 250 'type' => 'request', 251 'content' => '', 252 'date_modified' => bp_core_current_time(), 253 'invite_sent' => 0, 254 'accepted' => 0, 255 ), 256 'add_request' 257 ); 258 259 // If there is no invitee, bail. 260 if ( ! ( $r['user_id'] || $r['invitee_email'] ) ) { 261 return false; 262 } 263 264 /** 265 * Is this user allowed to make a request in this situation? 266 * 267 * @since 5.0.0 268 * 269 * @param array $r Describes the invitation to be added. 270 */ 271 if ( ! $this->allow_request( $r ) ) { 272 return false; 273 } 274 275 /* 276 * Avoid creating duplicate requests. 277 */ 278 $base_args = array( 279 'user_id' => $r['user_id'], 280 'invitee_email' => $r['invitee_email'], 281 'item_id' => $r['item_id'], 282 'secondary_item_id' => $r['secondary_item_id'], 283 ); 284 if ( $this->request_exists( $base_args ) ) { 285 return false; 286 } 287 288 /* 289 * Check for outstanding invitations to the same item. 290 * A request + a sent invite = acceptance. 291 */ 292 $invite_args = array_merge( $base_args, array( 'invite_sent' => 'sent' ) ); 293 $invite = $this->invitation_exists( $invite_args ); 294 295 if ( $invite ) { 296 // Accept the invite. 297 return $this->accept_invitation( $base_args ); 298 } else { 299 // Set up the new request. 300 $request = new BP_Invitation; 301 $request->user_id = $r['user_id']; 302 $request->inviter_id = $r['inviter_id']; 303 $request->invitee_email = $r['invitee_email']; 304 $request->class = $this->class_name; 305 $request->item_id = $r['item_id']; 306 $request->secondary_item_id = $r['secondary_item_id']; 307 $request->type = $r['type']; 308 $request->content = $r['content']; 309 $request->date_modified = $r['date_modified']; 310 $request->invite_sent = $r['invite_sent']; 311 $request->accepted = $r['accepted']; 312 313 // Save the new invitation. 314 return $request->save(); 315 } 316 } 317 318 /** 319 * Send a request notification. 320 * 321 * @since 5.0.0 322 * @access public 323 * 324 * @param int $request_id ID of request to send. 325 * @param array $args See BP_Invitation::mark_sent(). 326 * 327 * @return bool The result of `run_send_action()`. 328 */ 329 public function send_request_notification_by_id( $request_id = 0, $args = array() ) { 330 $updated = false; 331 332 $request = new BP_Invitation( $request_id ); 333 334 if ( ! $request->id ) { 335 return false; 336 } 337 338 // Different uses may need different actions on sending. Plugins can hook in here to perform their own tasks. 339 do_action( 'bp_invitations_send_request_notification_by_id_before_send', $request_id, $request ); 340 341 /* 342 * Before sending notifications, check for outstanding invitations to the same item. 343 * A sent invitation + a request = acceptance. 344 */ 345 $invite_args = array( 346 'user_id' => $request->user_id, 347 'invitee_email' => $request->invitee_email, 348 'item_id' => $request->item_id, 349 'secondary_item_id' => $request->secondary_item_id, 350 'invite_sent' => 'sent' 351 ); 352 $invites = $this->invitation_exists( $invite_args ); 353 354 if ( ! empty( $invites ) ) { 355 // Accept the request. 356 return $this->accept_invitation( $invite_args ); 357 } 358 359 // Perform the send action. 360 $success = $this->run_send_action( $request ); 361 362 if ( $success ) { 363 BP_Invitation::mark_sent( $request->id, $args ); 364 } 365 366 return $success; 367 } 368 369 /** Retrieve ******************************************************************/ 370 371 /** 372 * Get a specific invitation by its ID. 373 * 374 * @since 5.0.0 375 * 376 * @param int $id ID of the invitation. 377 * @return BP_Invitation object 378 */ 379 public function get_by_id( $id = 0 ) { 380 return new BP_Invitation( $id ); 381 } 382 383 /** 384 * Get invitations, based on provided filter parameters. 385 * 386 * @since 5.0.0 387 * 388 * @see BP_Invitation::get() for a description of accepted parameters. 389 * 390 * @return array Located invitations. 391 */ 392 public function get_invitations( $args = array() ) { 393 // Default to returning invitations, not requests. 394 if ( empty( $args['type'] ) ) { 395 $args['type'] = 'invite'; 396 } 397 // Use the class_name property value. 398 $args['class'] = $this->class_name; 399 400 return BP_Invitation::get( $args ); 401 } 402 403 /** 404 * Get a count of the number of invitations that match provided filter parameters. 405 * 406 * @since 8.0.0 407 * 408 * @see BP_Invitation::get_total_count() for a description of accepted parameters. 409 * 410 * @return int Total number of invitations. 411 */ 412 public function get_invitations_total_count( $args = array() ) { 413 // Default to returning invitations, not requests. 414 if ( empty( $args['type'] ) ) { 415 $args['type'] = 'invite'; 416 } 417 // Use the class_name property value. 418 $args['class'] = $this->class_name; 419 420 return BP_Invitation::get_total_count( $args ); 421 } 422 423 /** 424 * Get requests, based on provided filter parameters. 425 * 426 * @since 5.0.0 427 * 428 * @see BP_Invitation::get() for a description of accepted parameters. 429 * 430 * @return array Located invitations. 431 */ 432 public function get_requests( $args = array() ) { 433 // Set request-specific parameters. 434 $args['type'] = 'request'; 435 $args['inviter_id'] = false; 436 $args['invite_sent'] = 'all'; 437 438 // Use the class_name property value. 439 $args['class'] = $this->class_name; 440 441 return BP_Invitation::get( $args ); 442 } 443 444 /** 445 * Check whether an invitation exists matching the passed arguments. 446 * 447 * @since 5.0.0 448 * 449 * @see BP_Invitation::get() for a description of accepted parameters. 450 * 451 * @return int|bool ID of first found invitation or false if none found. 452 */ 453 public function invitation_exists( $args = array() ) { 454 $is_invited = false; 455 456 $args['fields'] = 'ids'; 457 $invites = $this->get_invitations( $args ); 458 if ( $invites ) { 459 $is_invited = current( $invites ); 460 } 461 return $is_invited; 462 } 463 464 /** 465 * Check whether a request exists matching the passed arguments. 466 * 467 * @since 5.0.0 468 * 469 * @see BP_Invitation::get() for a description of accepted parameters. 470 * 471 * @return int|bool ID of existing request or false if none found. 472 */ 473 public function request_exists( $args = array() ) { 474 $has_request = false; 475 476 $args['fields'] = 'ids'; 477 $requests = $this->get_requests( $args ); 478 if ( $requests ) { 479 $has_request = current( $requests ); 480 } 481 return $has_request; 482 } 483 484 /** Update ********************************************************************/ 485 486 /** 487 * Accept invitation, based on provided filter parameters. 488 * 489 * @since 5.0.0 490 * 491 * @see BP_Invitation::get() for a description of 492 * accepted update/where arguments. 493 * 494 * @param array $args { 495 * Invitation characteristics. Some basic info is required to accept an invitation, 496 * because we'll need to accept all similar invitations and requests. 497 * 498 * @type int $user_id User ID of the invitee. 499 * Either 'user_id' or 'invitee_email' is required. 500 * @type string $invitee_email Email address of the invitee. 501 * Either 'user_id' or 'invitee_email' is required. 502 * @type int $item_id Item ID of the invitation to accept. 503 * @type int $secondary_item_id Optional. Secondary item ID if needed. 504 * @type string $invite_sent Optional. Defaults to only allowing the 505 * acceptance of sent invitations. 506 * @type string $date_modified Modified time in 'Y-m-d h:i:s' format, GMT. 507 * Defaults to current time if not specified. 508 * } 509 * 510 * @return int|bool Number of rows updated on success, false on failure. 511 */ 512 public function accept_invitation( $args = array() ) { 513 514 $r = bp_parse_args( 515 $args, 516 array( 517 'id' => false, 518 'user_id' => 0, 519 'invitee_email' => '', 520 'item_id' => null, 521 'secondary_item_id' => null, 522 'invite_sent' => 'sent', 523 'date_modified' => bp_core_current_time(), 524 ), 525 'accept_invitation' 526 ); 527 528 $r['class'] = $this->class_name; 529 530 if ( ! $r['id'] && ! ( ( $r['user_id'] || $r['invitee_email'] ) && $r['class'] && $r['item_id'] ) ) { 531 return false; 532 } 533 534 if ( ! $this->invitation_exists( $r ) ) { 535 return false; 536 } 537 538 $success = $this->run_acceptance_action( 'invite', $r ); 539 if ( $success ) { 540 // Mark invitations & requests to this item for this user. 541 $this->mark_accepted( $r ); 542 543 // Allow plugins an opportunity to act on the change. 544 do_action( 'bp_invitations_accepted_invite', $r ); 545 } 546 return $success; 547 } 548 549 /** 550 * Accept invitation, based on provided filter parameters. 551 * 552 * @since 5.0.0 553 * 554 * @see BP_Invitation::get() for a description of 555 * accepted update/where arguments. 556 * 557 * @param array $args { 558 * Invitation characteristics. Some basic info is required to accept an invitation, 559 * because we'll need to accept all similar invitations and requests. 560 * 561 * @type int $user_id User ID of the invitee. 562 * @type int $item_id Item ID of the invitation to accept. 563 * @type int $secondary_item_id Optional. Secondary item ID if needed. 564 * @type string $date_modified Modified time in 'Y-m-d h:i:s' format, GMT. 565 * Defaults to current time if not specified. 566 * } 567 * 568 * @return bool Number of rows updated on success, false on failure. 569 */ 570 public function accept_request( $args = array() ) { 571 572 $r = bp_parse_args( 573 $args, 574 array( 575 'user_id' => 0, 576 'item_id' => null, 577 'secondary_item_id' => null, 578 'date_modified' => bp_core_current_time(), 579 ), 580 'accept_request' 581 ); 582 583 $r['class'] = $this->class_name; 584 585 if ( ! ( $r['user_id'] && $r['class'] && $r['item_id'] ) ) { 586 return false; 587 } 588 589 if ( ! $this->request_exists( $r ) ) { 590 return false; 591 } 592 593 $success = $this->run_acceptance_action( 'request', $r ); 594 if ( $success ) { 595 // Update/Delete all related invitations & requests to this item for this user. 596 $this->mark_accepted( $r ); 597 598 // Allow plugins an opportunity to act on the change. 599 do_action( 'bp_invitations_accepted_request', $r ); 600 } 601 return $success; 602 } 603 604 /** 605 * Update invitation, based on provided filter parameters. 606 * 607 * @since 5.0.0 608 * 609 * @see BP_Invitation::get() for a description of 610 * accepted update/where arguments. 611 * 612 * @param array $update_args Associative array of fields to update, 613 * and the values to update them to. Of the format 614 * array( 'user_id' => 4 ) 615 * @param array $where_args Associative array of columns/values, to 616 * determine which invitations should be updated. Formatted as 617 * array( 'item_id' => 7 ) 618 * @return int|bool Number of rows updated on success, false on failure. 619 */ 620 public function update_invitation( $update_args = array(), $where_args = array() ) { 621 $update_args['class'] = $this->class_name; 622 return BP_Invitation::update( $update_args, $where_args ); 623 } 624 625 /** 626 * This is where custom actions are added (in child classes) 627 * to run when an invitation or request needs to be "sent." 628 * 629 * @since 5.0.0 630 * 631 * @param BP_Invitation $invitation The invitation to send. 632 * @return bool True on success, false on failure. 633 */ 634 abstract public function run_send_action( BP_Invitation $invitation ); 635 636 /** 637 * Mark invitations as sent that are found by user_id, inviter_id, 638 * invitee_email, class name, optional item id, 639 * optional secondary item id. 640 * 641 * @since 5.0.0 642 * 643 * @param array $args { 644 * Associative array of arguments. All arguments but $page and 645 * $per_page can be treated as filter values for get_where_sql() 646 * and get_query_clauses(). All items are optional. 647 * @type int|array $user_id ID of user being queried. Can be an 648 * array of user IDs. 649 * @type int|array $inviter_id ID of user who created the 650 * invitation. Can be an array of user IDs. 651 * Special cases 652 * @type string|array $invitee_email Email address of invited users 653 * being queried. Can be an array of addresses. 654 * @type string|array $class Name of the class to 655 * filter by. Can be an array of class names. 656 * @type int|array $item_id ID of associated item. Can be an array 657 * of multiple item IDs. 658 * @type int|array $secondary_item_id ID of secondary associated 659 * item. Can be an array of multiple IDs. 660 * } 661 */ 662 public function mark_sent( $args ) { 663 $args['class'] = $this->class_name; 664 return BP_Invitation::mark_sent_by_data( $args ); 665 } 666 667 /** 668 * This is where custom actions are added (in child classes) 669 * to run when an invitation or request is accepted. 670 * 671 * @since 5.0.0 672 * 673 * @param int $id The ID of the invitation to mark as sent. 674 * @return bool True on success, false on failure. 675 */ 676 abstract public function run_acceptance_action( $type, $r ); 677 678 /** 679 * Mark invitation as accepted by invitation ID. 680 * 681 * @since 5.0.0 682 * 683 * @see BP_Invitation::mark_accepted() 684 * for a description of arguments. 685 * @return bool True on success, false on failure. 686 */ 687 public function mark_accepted_by_id( $id, $args ) { 688 return BP_Invitation::mark_accepted( $id, $args ); 689 } 690 691 /** 692 * Mark invitations as sent that are found by user_id, inviter_id, 693 * invitee_email, class name, item id, and 694 * optional secondary item id. 695 * 696 * @since 5.0.0 697 * 698 * @see BP_Invitation::mark_accepted_by_data() 699 * for a description of arguments. 700 */ 701 public function mark_accepted( $args ) { 702 $args['class'] = $this->class_name; 703 return BP_Invitation::mark_accepted_by_data( $args ); 704 } 705 706 /** Delete ********************************************************************/ 707 708 /** 709 * Delete an invitation or invitations by query data. 710 * 711 * @since 5.0.0 712 * 713 * @see BP_Invitation::delete for a description of arguments. 714 * @return int|bool Number of rows deleted on success, false on failure. 715 */ 716 public function delete( $args ) { 717 if ( empty( $args['type'] ) ) { 718 $args['type'] = 'invite'; 719 } 720 $args['class'] = $this->class_name; 721 return BP_Invitation::delete( $args ); 722 } 723 724 /** 725 * Delete a request or requests by query data. 726 * 727 * @since 5.0.0 728 * 729 * @see BP_Invitation::delete for a description of arguments. 730 * 731 * @return int|bool Number of rows deleted on success, false on failure. 732 */ 733 public function delete_requests( $args ) { 734 $args['type'] = 'request'; 735 return $this->delete( $args ); 736 } 737 738 /** 739 * Delete all invitations by class. 740 * 741 * Used when clearing out invitations for an entire class. Possibly used 742 * when deactivating a component related to a class that created invitations. 743 * 744 * @since 5.0.0 745 * 746 * @return int|bool Number of rows deleted on success, false on failure. 747 */ 748 public function delete_all() { 749 return BP_Invitation::delete( array( 750 'class' => $this->class_name, 751 ) ); 752 } 753 754 /** 755 * Delete an invitation by id. 756 * 757 * @since 8.0.0 758 * 759 * @param int $id ID of the invitation to delete. 760 * @return int|bool Number of rows deleted on success, false on failure. 761 */ 762 public function delete_by_id( $id ) { 763 // Ensure that the invitation exists and was created by this class. 764 $invite = new BP_Invitation( $id ); 765 if ( ! $invite->id || sanitize_key( $this->class_name ) !== $invite->class ) { 766 return false; 767 } 768 769 return BP_Invitation::delete_by_id( $id ); 770 } 771 772 /** 773 * This is where custom actions are added (in child classes) 774 * to determine whether an invitation should be allowed. 775 * 776 * @since 5.0.0 777 * 778 * @param array $args The parameters describing the invitation. 779 * @return bool True if allowed, false to end process. 780 */ 781 public function allow_invitation( $args ) { 782 return true; 783 } 784 785 /** 786 * This is where custom actions are added (in child classes) 787 * to determine whether a request should be allowed. 788 * 789 * @since 5.0.0 790 * 791 * @param array $args The parameters describing the request. 792 * @return bool True if allowed, false to end process. 793 */ 794 public function allow_request( $args ) { 795 return true; 796 } 797 798 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |