-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuddypress-extended-push-notifications.php
235 lines (166 loc) · 7.52 KB
/
buddypress-extended-push-notifications.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
// BuddyPress Extended Push Notifications:
// Friendship request/accepted, group membership, tagging a person in comments.
//Remove this if you have already added it through WC Push Notifications script
add_action( 'wp_enqueue_scripts', 'appp_custom_scripts' );
function appp_custom_scripts() {
wp_enqueue_script( 'appp-custom', get_stylesheet_directory_uri( 'js/apppresser-custom.js' , __FILE__ ), array('jquery', 'cordova-core'), 1.0 );
}
add_action('friends_friendship_requested', 'bp_send_friend_request', 10, 3);
function bp_send_friend_request( $friendship_id, $friendship_initiator_id, $friendship_friend_id ) {
$device_ids = [];
//$fp = fopen("testpush.txt", "w");
//fwrite($fp, "\n New friendship requested...");
//fwrite($fp, "\n friendship_id : ".$friendship_id);
//fwrite($fp, "\n friendship_initiator_id : ".$friendship_initiator_id);
//fwrite($fp, "\n friendship_friend_id : ".$friendship_friend_id);
$user_info = get_userdata($friendship_initiator_id);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
//fwrite($fp, "\n fname : ".$first_name);
$recipients = array( $friendship_friend_id );
$message = 'You have a friendship request from '.$first_name.' '.$last_name;
//fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
//fclose($fp);
}
add_action('friends_friendship_accepted', 'bp_accept_friend_request', 10, 3);
function bp_accept_friend_request( $friendship_id, $friendship_initiator_id, $friendship_friend_id ) {
$device_ids = [];
//$fp = fopen("testpush.txt", "w");
//fwrite($fp, "\n friendship accepted...");
//fwrite($fp, "\n friendship_id : ".$friendship_id);
//fwrite($fp, "\n friendship_initiator_id : ".$friendship_initiator_id);
//fwrite($fp, "\n friendship_friend_id : ".$friendship_friend_id);
$user_info = get_userdata($friendship_friend_id);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
//fwrite($fp, "\n fname : ".$first_name);
$recipients = array( $friendship_initiator_id );
$message = $first_name.' '.$last_name.' accepted your friend request';
//fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
//fclose($fp);
}
add_action('groups_membership_requested', 'bp_send_group_request', 10, 3);
function bp_send_group_request($requesting_user_id, $admins, $group_id) {
$device_ids = [];
//$fp = fopen("testpush.txt", "w");
//fwrite($fp, "\n Group join request...");
//fwrite($fp, "\n requesting_user_id : ".$requesting_user_id);
//fwrite($fp, "\n admins : ".$admins);
//fwrite($fp, "\n group_id : ".$group_id);
for($i = 0; $i < count($admins); $i++){
//fwrite($fp, "\n i : ".$i);
$adminid = $admins[0]->user_id;
//fwrite($fp, "\n admin : ".$adminid);
$user_info = get_userdata($requesting_user_id);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
//fwrite($fp, "\n fname : ".$first_name);
$recipients = array( $adminid );
$message = $first_name.' '.$last_name.' requested membership to group';
//fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
}
//fclose($fp);
}
add_action( 'groups_accept_invite', 'action_groups_accept_invite', 5, 3 );
function action_groups_accept_invite( $user_id, $group_id, $inviter_id ) {
//$fp = fopen("testpushnew.txt", "w");
//fwrite($fp, "\n Group invite accepted...");
//fwrite($fp, "\n user_id : ".$user_id);
//fwrite($fp, "\n group_id : ".$group_id);
//fwrite($fp, "\n inviter_id : ".$inviter_id);
$userid = $args['user_id'];
// fwrite($fp, "\n useridss : ".$userid);
$group = groups_get_group( array( 'group_id' => $group_id) );
// fwrite($fp, "\n group : ".$group->name);
$user_info = get_userdata($userid);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
// fwrite($fp, "\n fname : ".$first_name);
$recipients = array( $inviter_id );
$message = $first_name.' '.$last_name.' accepted your invitation to join group '.$group->name;
// fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
//fclose($fp);
};
add_action( 'groups_invite_user', 'action_groups_invite_user', 10, 1 );
function action_groups_invite_user($args) {
//$fp = fopen("testpush.txt", "w");
//fwrite($fp, "\n Group join invite...");
//fwrite($fp, "\n args : ".$args);
$userid = $args['user_id'];
//fwrite($fp, "\n useridss : ".$userid);
$group = groups_get_group( array( 'group_id' => $args['group_id']) );
//fwrite($fp, "\n group : ".$group->name);
$user_info = get_userdata($args['inviter_id']);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
//fwrite($fp, "\n fname : ".$first_name);
$recipients = array( $userid );
$message = $first_name.' '.$last_name.' invited you to join group '.$group->name;
//fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
//fclose($fp);
}
add_action( 'comment_post', 'show_message_function', 10, 2 );
function show_message_function( $comment_ID, $comment_approved ) {
//$fp = fopen("testpush.txt", "w");
//fwrite($fp, "\n new comment posted...");
//fwrite($fp, "\n comment_ID : ".$comment_ID);
//fwrite($fp, "\n comment_approved : ".$comment_approved);
$comment = get_comment( $comment_ID);
$content = $comment->comment_content;
$auther = $comment->comment_author;
$postid = $comment->comment_post_ID;
$post = get_post($postid);
$postname = $post->post_title;
//fwrite($fp, "\n content : ".$content);
//fwrite($fp, "\n content : ".$auther);
$matches = array();
preg_match_all("/\@[a-z0-9_]+/i", $content, $matches, PREG_SET_ORDER);
print_r($matches);
// exit(0);
for($i = 0; $i < count($matches); $i++){
$firstuser = $matches[$i][0];
//fwrite($fp, "\n firstuser : ".$firstuser);
if($firstuser){
$firstuser = str_replace("@","",$firstuser);
}
//fwrite($fp, "\n firstuser after : ".$firstuser);
$user = get_user_by( 'login', $firstuser);
//fwrite($fp, "\n userid : ".$user->ID);
$userid = $user->ID;
$recipients = array( $userid );
$message = $auther.' tagged you in the post '.$postname;
//fwrite($fp, "\n message : ".$message);
$push = new AppPresser_Notifications_Update;
$devices = $push->get_devices_by_user_id( $recipients );
if( $devices ) {
$push->notification_send( 'now', $message, 1, $devices );
}
}
//fclose($fp);
}