Changeset 13104 for trunk/src/bp-groups/bp-groups-functions.php
- Timestamp:
- 09/09/2021 02:12:32 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/bp-groups-functions.php
r13103 r13104 538 538 539 539 /** 540 * Get a group slug by its ID. 541 * 542 * @since 1.0.0 543 * 544 * @param int $group_id The numeric ID of the group. 545 * @return string The group's slug. 546 */ 547 function groups_get_slug( $group_id ) { 548 $group = groups_get_group( $group_id ); 549 return !empty( $group->slug ) ? $group->slug : ''; 540 * Get slug from a group. 541 * 542 * @since 1.0.0 543 * @since 10.0.0 Updated to use `bp_get_group`. 544 * 545 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. 546 * @return bool|string The group's slug. False if group doesn't exist. 547 */ 548 function groups_get_slug( $group ) { 549 550 $group = bp_get_group( $group ); 551 552 if ( empty( $group->id ) ) { 553 return false; 554 } 555 556 return ! empty( $group->slug ) ? $group->slug : ''; 550 557 } 551 558 … … 580 587 * 581 588 * @since 1.0.0 582 * 583 * @param int $group_id ID of the group. 584 * @param int $user_id Optional. ID of the user. Defaults to the currently 585 * logged-in user. 589 * @since 10.0.0 Updated to use `bp_get_group`. 590 * 591 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. 592 * @param int $user_id Optional. ID of the user. Defaults to the currently 593 * logged-in user. 586 594 * @return bool True on success, false on failure. 587 595 */ 588 function groups_leave_group( $group_id, $user_id = 0 ) { 589 590 if ( empty( $user_id ) ) 596 function groups_leave_group( $group, $user_id = 0 ) { 597 598 $group = bp_get_group( $group ); 599 600 if ( empty( $group->id ) ) { 601 return false; 602 } 603 604 if ( empty( $user_id ) ) { 591 605 $user_id = bp_loggedin_user_id(); 606 } 592 607 593 608 // Don't let single admins leave the group. 594 if ( count( groups_get_group_admins( $group _id ) ) < 2 ) {595 if ( groups_is_user_admin( $user_id, $group _id ) ) {609 if ( count( groups_get_group_admins( $group->id ) ) < 2 ) { 610 if ( groups_is_user_admin( $user_id, $group->id ) ) { 596 611 bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' ); 597 612 return false; … … 599 614 } 600 615 601 if ( ! BP_Groups_Member::delete( $user_id, $group _id ) ) {616 if ( ! BP_Groups_Member::delete( $user_id, $group->id ) ) { 602 617 return false; 603 618 } … … 609 624 * 610 625 * @since 1.0.0 611 * 612 * @param int $group_id ID of the group. 613 * @param int $user_id ID of the user leaving the group. 626 * @since 10.0.0 Updated to add the `$group` parameter. 627 * 628 * @param int $group_id ID of the group. 629 * @param int $user_id ID of the user leaving the group. 630 * @param BP_Groups_Group $group The group object. 614 631 */ 615 do_action( 'groups_leave_group', $group _id, $user_id);632 do_action( 'groups_leave_group', $group->id, $user_id, $group ); 616 633 617 634 return true; … … 622 639 * 623 640 * @since 1.0.0 624 * 625 * @param int $group_id ID of the group. 626 * @param int $user_id Optional. ID of the user. Defaults to the currently 627 * logged-in user. 641 * @since 10.0.0 Updated to use `bp_get_group`. 642 * 643 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. 644 * @param int $user_id Optional. ID of the user. Defaults to the currently 645 * logged-in user. 628 646 * @return bool True on success, false on failure. 629 647 */ 630 function groups_join_group( $group_id, $user_id = 0 ) { 631 632 if ( empty( $user_id ) ) 648 function groups_join_group( $group, $user_id = 0 ) { 649 650 $group = bp_get_group( $group ); 651 652 if ( empty( $group->id ) ) { 653 return false; 654 } 655 656 $group_id = $group->id; 657 658 if ( empty( $user_id ) ) { 633 659 $user_id = bp_loggedin_user_id(); 660 } 634 661 635 662 // Check if the user has an outstanding invite. If so, delete it. 636 if ( groups_check_user_has_invite( $user_id, $group_id ) ) 663 if ( groups_check_user_has_invite( $user_id, $group_id ) ) { 637 664 groups_delete_invite( $user_id, $group_id ); 665 } 638 666 639 667 // Check if the user has an outstanding request. If so, delete it. 640 if ( groups_check_for_membership_request( $user_id, $group_id ) ) 668 if ( groups_check_for_membership_request( $user_id, $group_id ) ) { 641 669 groups_delete_membership_request( null, $user_id, $group_id ); 670 } 642 671 643 672 // User is already a member, just return true. 644 if ( groups_is_user_member( $user_id, $group_id ) ) 673 if ( groups_is_user_member( $user_id, $group_id ) ) { 645 674 return true; 646 647 $new_member = new BP_Groups_Member; 675 } 676 677 $new_member = new BP_Groups_Member(); 648 678 $new_member->group_id = $group_id; 649 679 $new_member->user_id = $user_id; … … 654 684 $new_member->is_confirmed = 1; 655 685 656 if ( ! $new_member->save() )686 if ( ! $new_member->save() ) { 657 687 return false; 658 659 $bp = buddypress(); 660 661 if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id ) 662 $group = groups_get_group( $group_id ); 663 else 664 $group = $bp->groups->current_group; 688 } 665 689 666 690 // Record this in activity streams. 667 691 if ( bp_is_active( 'activity' ) ) { 668 groups_record_activity( array( 669 'type' => 'joined_group', 670 'item_id' => $group_id, 671 'user_id' => $user_id, 672 ) ); 692 groups_record_activity( 693 array( 694 'type' => 'joined_group', 695 'item_id' => $group_id, 696 'user_id' => $user_id, 697 ) 698 ); 673 699 } 674 700 … … 677 703 * 678 704 * @since 1.0.0 679 * 680 * @param int $group_id ID of the group. 681 * @param int $user_id ID of the user joining the group. 705 * @since 10.0.0 Added the `$group` parameter. 706 * 707 * @param int $group_id ID of the group. 708 * @param int $user_id ID of the user joining the group. 709 * @param BP_Groups_Group $group The group object. 682 710 */ 683 do_action( 'groups_join_group', $group_id, $user_id );711 do_action( 'groups_join_group', $group_id, $user_id, $group ); 684 712 685 713 return true; … … 690 718 * 691 719 * @since 1.0.0 692 * 693 * @param int $group_id Optional. The ID of the group whose last_activity is 694 * being updated. Default: the current group's ID. 695 * @return false|null False on failure. 696 */ 697 function groups_update_last_activity( $group_id = 0 ) { 698 699 if ( empty( $group_id ) ) { 700 $group_id = buddypress()->groups->current_group->id; 701 } 702 703 if ( empty( $group_id ) ) { 720 * @since 10.0.0 Updated to use `bp_get_group`. 721 * 722 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. 723 * Default: the current group's ID. 724 * @return bool False on failure. 725 */ 726 function groups_update_last_activity( $group = 0 ) { 727 728 $group = bp_get_group( $group ); 729 730 if ( empty( $group->id ) ) { 704 731 return false; 705 732 } 706 733 707 groups_update_groupmeta( $group _id, 'last_activity', bp_core_current_time() );734 groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() ); 708 735 } 709 736 add_action( 'groups_join_group', 'groups_update_last_activity' );
Note: See TracChangeset
for help on using the changeset viewer.