add-post-to-activity-feed.php
1 month ago
add-post-to-group.php
1 month ago
add-post-to-user-activity-feed.php
1 month ago
add-user-to-group.php
11 months ago
change-user-member-type.php
11 months ago
create-group.php
11 months ago
end-friendship-with-user.php
11 months ago
follow-user.php
11 months ago
get-forum-subscribers.php
11 months ago
post-reply-topic-forum.php
11 months ago
post-topic-in-forum.php
11 months ago
remove-user-from-group.php
11 months ago
send-friend-request-user.php
11 months ago
send-message-to-group-members.php
11 months ago
send-message-to-user.php
11 months ago
send-notification-to-all-members-of-group.php
11 months ago
set-user-extended-profile.php
11 months ago
set-user-status.php
11 months ago
stop-following-user.php
11 months ago
subscribe-user-to-forum.php
11 months ago
unsubscribe-user-from-email-notifications.php
2 months ago
send-message-to-group-members.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SendMessageToGroupMembers. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SendMessageToGroupMembers |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\BuddyBoss\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Integrations\WordPress\WordPress; |
| 19 | use SureTriggers\Traits\SingletonLoader; |
| 20 | |
| 21 | /** |
| 22 | * SendMessageToGroupMembers |
| 23 | * |
| 24 | * @category SendMessageToGroupMembers |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class SendMessageToGroupMembers extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'BuddyBoss'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'bb_send_message_to_group_members'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function register( $actions ) { |
| 57 | $actions[ $this->integration ][ $this->action ] = [ |
| 58 | 'label' => __( 'Send Message to Group Members', 'suretriggers' ), |
| 59 | 'action' => $this->action, |
| 60 | 'function' => [ $this, '_action_listener' ], |
| 61 | ]; |
| 62 | |
| 63 | return $actions; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Action listener. |
| 68 | * |
| 69 | * @param int $user_id user_id. |
| 70 | * @param int $automation_id automation_id. |
| 71 | * @param array $fields fields. |
| 72 | * @param array $selected_options selectedOptions. |
| 73 | * @return array|void|string |
| 74 | * @throws Exception Exception. |
| 75 | */ |
| 76 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 77 | $sender_user_email = $selected_options['sender_user_email']; |
| 78 | |
| 79 | $group_id = $selected_options['bb_group']['value']; |
| 80 | $subject = $selected_options['subject']; |
| 81 | $message_content = $selected_options['message_content']; |
| 82 | |
| 83 | if ( ! function_exists( 'groups_get_group' ) ) { |
| 84 | return []; |
| 85 | } |
| 86 | |
| 87 | if ( function_exists( 'groups_get_group_members' ) ) { |
| 88 | $members = groups_get_group_members( |
| 89 | [ |
| 90 | 'group_id' => $group_id, |
| 91 | 'per_page' => -1, |
| 92 | 'type' => 'last_joined', |
| 93 | 'exclude_banned' => true, |
| 94 | ] |
| 95 | ); |
| 96 | if ( ! empty( $members['members'] ) ) { |
| 97 | $members_ids = []; |
| 98 | foreach ( $members['members'] as $member ) { |
| 99 | array_push( $members_ids, $member->ID ); |
| 100 | } |
| 101 | if ( is_email( $sender_user_email ) ) { |
| 102 | $user = get_user_by( 'email', $sender_user_email ); |
| 103 | if ( $user ) { |
| 104 | $sender_id = $user->ID; |
| 105 | } else { |
| 106 | return [ |
| 107 | 'status' => 'error', |
| 108 | 'message' => ' Sender user not found ', |
| 109 | ]; |
| 110 | } |
| 111 | } else { |
| 112 | return [ |
| 113 | 'status' => 'error', |
| 114 | 'message' => ' Please provide valid email exists. ', |
| 115 | ]; |
| 116 | } |
| 117 | if ( $members_ids || $sender_id ) { |
| 118 | // Attempt to send the message. |
| 119 | $msg = [ |
| 120 | 'sender_id' => $sender_id, |
| 121 | 'recipients' => $members_ids, |
| 122 | 'subject' => $subject, |
| 123 | 'content' => $message_content, |
| 124 | 'error_type' => 'wp_error', |
| 125 | ]; |
| 126 | |
| 127 | if ( function_exists( 'messages_new_message' ) ) { |
| 128 | $send = messages_new_message( $msg ); |
| 129 | if ( is_wp_error( $send ) ) { |
| 130 | $messages = $send->get_error_messages(); |
| 131 | return $messages; |
| 132 | } else { |
| 133 | $group = groups_get_group( $group_id ); |
| 134 | $context = [ |
| 135 | 'sender' => WordPress::get_user_context( $sender_id ), |
| 136 | 'subject' => $subject, |
| 137 | 'content' => $message_content, |
| 138 | 'group' => $group, |
| 139 | ]; |
| 140 | foreach ( $members_ids as $key => $member ) { |
| 141 | $context['recipients'][ $key ] = WordPress::get_user_context( $member ); |
| 142 | } |
| 143 | return $context; |
| 144 | } |
| 145 | } else { |
| 146 | return [ |
| 147 | 'status' => 'error', |
| 148 | 'message' => 'BuddyBoss Private Messaging module is not active.', |
| 149 | ]; |
| 150 | } |
| 151 | } else { |
| 152 | return [ |
| 153 | 'status' => 'error', |
| 154 | 'message' => 'Group members not found.', |
| 155 | ]; |
| 156 | } |
| 157 | } else { |
| 158 | return [ |
| 159 | 'status' => 'error', |
| 160 | 'message' => 'Group members not found.', |
| 161 | ]; |
| 162 | } |
| 163 | } else { |
| 164 | return [ |
| 165 | 'status' => 'error', |
| 166 | 'message' => 'BuddyBoss message module is not active.', |
| 167 | ]; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | SendMessageToGroupMembers::get_instance(); |
| 173 |