PluginProbe
WPMobile.App / trunk
WPMobile.App vtrunk
wpappninja / inc / api / push.php

push.php in WPMobile.App trunk, at inc/api/push.php

1,142 lines 35.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 // FIREBASE FUNCTIONS
5 function wpmobile_base64UrlEncode($text)
6 {
7 return str_replace(
8 ['+', '/', '='],
9 ['-', '_', ''],
10 base64_encode($text)
11 );
12 }
13 function wpmobile_getAuthConfig() {
14
15 if (get_option('wpmobile_firebase_config', '') == '') {
16 return;
17 }
18
19 $authConfigString = @file_get_contents(get_option('wpmobile_firebase_config', ''));
20 $authConfig = json_decode($authConfigString);
21 return $authConfig->project_id;
22 }
23 function wpmobile_getOauthToken() {
24
25 if (get_wpappninja_option('debugpush', '0') === '1' && $_GET['pagename'] == 'wpappninja' && $_GET['type'] == 'googlebearer') {
26 echo 'OPTION: '. get_option('wpmobile_firebase_config', '');
27 echo "\r\n";
28 echo "\r\n";
29 }
30
31 if (get_option('wpmobile_firebase_config', '') == '') {
32 return;
33 }
34
35 $authConfigString = @file_get_contents(get_option('wpmobile_firebase_config', ''));
36
37 if (get_wpappninja_option('debugpush', '0') === '1' && $_GET['pagename'] == 'wpappninja' && $_GET['type'] == 'googlebearer') {
38 echo 'FILE: '. $authConfigString;
39 echo "\r\n";
40 echo "\r\n";
41 }
42
43 $authConfig = json_decode($authConfigString);
44 $secret = openssl_get_privatekey($authConfig->private_key);
45 $header = json_encode([
46 'typ' => 'JWT',
47 'alg' => 'RS256'
48 ]);
49
50 $time = time();
51 $payload = json_encode([
52 "iss" => $authConfig->client_email,
53 "scope" => "https://www.googleapis.com/auth/firebase.messaging",
54 "aud" => "https://oauth2.googleapis.com/token",
55 "exp" => $time + 180,
56 "iat" => $time
57 ]);
58
59 $base64UrlHeader = wpmobile_base64UrlEncode($header);
60 $base64UrlPayload = wpmobile_base64UrlEncode($payload);
61 $result = openssl_sign($base64UrlHeader . "." . $base64UrlPayload, $signature, $secret, OPENSSL_ALGO_SHA256);
62 $base64UrlSignature = wpmobile_base64UrlEncode($signature);
63 $jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature;
64
65 /*$options = array('http' => array(
66 'method' => 'POST',
67 'content' => 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion='.$jwt,
68 'header' =>
69 "Content-Type: application/x-www-form-urlencoded"
70 ));
71 $context = stream_context_create($options);
72 $responseText = file_get_contents("https://oauth2.googleapis.com/token", false, $context);*/
73
74 $payload = [
75 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
76 'assertion' => $jwt,
77 ];
78
79 $response = wp_remote_post("https://oauth2.googleapis.com/token", [
80 'body' => $payload,
81 'headers' => [
82 'Content-Type' => 'application/x-www-form-urlencoded',
83 ],
84 'timeout' => 20,
85 ]);
86
87 if (is_wp_error($response)) {
88 $responseText = false;
89 //error_log('Erreur WP_HTTP: ' . $response->get_error_message());
90 } else {
91 $responseText = wp_remote_retrieve_body($response);
92 }
93
94 if (get_wpappninja_option('debugpush', '0') === '1' && $_GET['pagename'] == 'wpappninja' && $_GET['type'] == 'googlebearer') {
95 echo 'RESPONSE: '. $responseText;
96 echo "\r\n";
97 echo "\r\n";
98 }
99
100 $responseOAuth = json_decode($responseText, true);
101 return $responseOAuth['access_token'];
102 }
103
104 // PROCESS PUSH
105 function wpappninja_send_push($ids, $title, $content, $image, $postID, $permalink, $custom_category, $pushID) {
106
107 if ($postID == "0") {
108 $postID = get_bloginfo('url') . "/?wpappninja_read_enhanced=-welcome";
109 }
110
111 if (get_wpappninja_option('speed') == '1') {
112 $postID = get_bloginfo('url') . "/wpmobileapp-shortcode/?wpappninja_read_push=" . $pushID;
113 if (get_wpappninja_option('redirection_type', '1') == '1' && strpos($permalink, "http") !== false) {
114 $postID = wpappninja_cache_friendly($permalink);
115 }
116 $permalink = $postID;
117 }
118
119 if ($postID == "-1") {
120 $postID = get_bloginfo('url') . "/?wpappninja_read_enhanced=-" . $pushID;
121 }
122 $query_postID = parse_url($postID, PHP_URL_QUERY);
123 if ($query_postID) {
124 $postID .= '&wpmobile_from_push=' . uniqid();
125 } else {
126 $postID .= '?wpmobile_from_push=' . uniqid();
127 }
128
129 global $wpdb;
130 $_wpappninja_ids = $ids;
131
132 $title = stripslashes($title);
133 $content = stripslashes($content);
134
135 $silent = array();
136 $categories = get_the_category($postID);
137 $catID = '';
138
139 $response = '';
140 if (get_wpappninja_option('debugpush', '0') === '1') {
141 $response .= "=== START DEBUG ===\n";
142 }
143
144 $certFile = get_option('wpappninja_pem_file', '');
145
146 $iostitle = wpappninja_nice_cut($title, 30);
147 $ioscontent = wpappninja_nice_cut($content, 128);
148
149 if (get_wpappninja_option('iosjusttitle', 'off') == 'on') {
150 $iostitle = wpappninja_nice_cut(wpappninja_get_appname(true), 30);
151 $ioscontent = wpappninja_nice_cut($title, 128);
152 }
153
154 // iOS notifications
155 if ($certFile != '') {
156
157 $idsIOS = [];
158
159 foreach ($ids as $key => $ios_id) {
160 if (substr($ios_id, 0, 5) == "_IOS_") { // ios style
161 unset($ids[$key]);
162 $idsIOS[] = str_replace('_IOS_', '', $ios_id);
163 }
164 }
165
166 if (get_wpappninja_option('debugpush', '0') === '1') {
167 $response .= "iOS: " . count($idsIOS) . "\n";
168 }
169
170 $mini_ios = array_chunk($idsIOS, 10000);
171 for($i=0;$i<count($mini_ios);$i++) {
172
173 $bypass = wp_remote_post( "https://push.wpmobile.app/", array(
174 'method' => 'POST',
175 'timeout' => 10,
176 'redirection' => 1,
177 'body' => array( 'url'=>get_bloginfo('url') . '/', 'postID'=> strval($postID), 'title'=>$iostitle, 'content'=>$ioscontent, 'certFile' => file_get_contents($certFile), 'ids' => json_encode($mini_ios[$i]) ),
178 )
179 );
180
181 if (get_wpappninja_option('debugpush', '0') === '1') {
182 if ( is_wp_error( $bypass ) ) {
183 $response .= "Erreur : " . $bypass->get_error_message();
184 } else {
185 $response .= "HTTP Code: " . wp_remote_retrieve_response_code( $bypass ) . "\n";
186 $response .= "HTTP Message: " . wp_remote_retrieve_response_message( $bypass ) . "\n\n";
187 $response .= wp_remote_retrieve_body( $bypass ) . "\n\n";
188 }
189 }
190
191 usleep(30000);
192 }
193 } else {
194 foreach ($ids as $key => $ios_id) {
195 if (substr($ios_id, 0, 5) == "_IOS_") {
196 unset($ids[$key]);
197 }
198 }
199 }
200
201 // Android notifications
202 if(count($ids) > 0 && get_option('wpmobile_firebase_config', '') != '' && get_wpappninja_option('sdk2019') == '1') {
203
204 $intro = wpappninja_nice_cut($content, 99);
205 $msg = wpappninja_nice_cut($content, 254);
206
207 $url = 'https://fcm.googleapis.com/v1/projects/'.wpmobile_getAuthConfig().'/messages:send';
208
209 if (get_wpappninja_option('debugpush', '0') === '1') {
210 $response .= "Android: " . count($ids) . "\n";
211 }
212
213 if (get_transient( "wpmobileAndroidTopic") == true) {
214
215 $message = array(
216 "title" => $title,
217 "body" => $msg,
218 );
219
220 $fields = array(
221 'message' => array(
222 'android' => array('priority'=>'high'),
223 'topic'=>'main',
224 'data'=>array(
225 "title" => $title,
226 "body" => $msg,
227 "link" => $postID,
228 )
229 ),
230 );
231
232 $headers = array('Authorization' => 'Bearer ' . wpmobile_getOauthToken(), 'Content-Type' => 'application/json');
233 $result = wp_remote_post( $url, array(
234 'method' => 'POST',
235 'timeout' => 10,
236 'redirection' => 1,
237 'headers' => $headers,
238 'body' => json_encode($fields)
239 ));
240
241 if (get_wpappninja_option('debugpush', '0') === '1') {
242 if ( is_wp_error( $result ) ) {
243 $response .= "Erreur : " . $result->get_error_message();
244 } else {
245 $response .= "HTTP Code: " . wp_remote_retrieve_response_code( $result ) . "\n";
246 $response .= "HTTP Message: " . wp_remote_retrieve_response_message( $result ) . "\n\n";
247 $response .= wp_remote_retrieve_body( $result ) . "\n\n";
248 }
249 }
250
251 } else {
252 $oAuthTokenPush = wpmobile_getOauthToken();
253 foreach($ids as $id) {
254
255 $message = array(
256 "title" => $title,
257 "body" => $msg,
258 );
259
260 $fields = array(
261 'message' => array(
262 'android' => array('priority'=>'high'),
263 'token' => $id,
264 'data'=>array(
265 "title" => $title,
266 "body" => $msg,
267 "link" => $postID,
268 )
269 ),
270 );
271
272 $headers = array('Authorization' => 'Bearer ' . $oAuthTokenPush, 'Content-Type' => 'application/json');
273 $result = wp_remote_post( $url, array(
274 'method' => 'POST',
275 'timeout' => 10,
276 'redirection' => 1,
277 'headers' => $headers,
278 'body' => json_encode($fields)
279 ));
280
281 if (get_wpappninja_option('debugpush', '0') === '1') {
282 if ( is_wp_error( $result ) ) {
283 $response .= "Erreur : " . $result->get_error_message();
284 } else {
285 $response .= "HTTP Code: " . wp_remote_retrieve_response_code( $result ) . "\n";
286 $response .= "HTTP Message: " . wp_remote_retrieve_response_message( $result ) . "\n\n";
287 $response .= wp_remote_retrieve_body( $result ) . "\n\n";
288 }
289 }
290
291 usleep(3000);
292 }
293 }
294 }
295
296 return $response;
297 }
298
299 // SUBSCRIBE TO PUSH
300 function wpappninja_push_register() {
301
302 global $wpdb;
303
304 if (isset($_GET['wpmobile_sdk2019_id']) && isset($_GET['wpmobile_sdk2019_token'])) {
305
306 $_POST['u'] = $_GET['wpmobile_sdk2019_id'];
307 $_POST['regId'] = $_GET['wpmobile_sdk2019_token'];
308
309 $user_id = $_POST['u'];
310 $user_bdd_id = $wpdb->get_row($wpdb->prepare("SELECT `id` FROM {$wpdb->prefix}wpappninja_push_perso WHERE `user_id` = %s", $user_id));
311
312 if (!isset($user_bdd_id->id)) {
313 $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}wpappninja_push_perso (`user_id`) VALUES (%s)", $user_id));
314 $lastid = $wpdb->insert_id;
315 } else {
316 $lastid = $user_bdd_id->id;
317 }
318
319 setcookie( "HTTP_X_WPAPPNINJA_ID", $lastid, time() + 8640000, COOKIEPATH, COOKIE_DOMAIN );
320 }
321
322 if (!isset($_POST['regId']) || !isset($_POST['u'])) {
323 return;
324 }
325
326 $lang = wpappninja_get_lang();
327
328 $id = sanitize_text_field($_POST['regId']);
329 $device = sanitize_text_field($_POST['u']);
330
331 if (substr($id, 0, 5) == "_IOS_" && isset($_SERVER['HTTP_X_WPAPPNINJA_ID'])) {
332 $device = $_SERVER['HTTP_X_WPAPPNINJA_ID'];
333 }
334
335 $device_sha = sha1($device);
336 $install = $wpdb->get_results($wpdb->prepare("SELECT `device_id` FROM {$wpdb->prefix}wpappninja_installs WHERE `device_id` = %s", $device_sha));
337
338 if (count($install) == 0) {
339 wpappninja_stats_log('install', 1);
340
341 $device_type = 0; // android
342 if (substr($id, 0, 5) == "_IOS_") {
343 $device_type = 1;
344 }
345
346 $wpdb->query($wpdb->prepare("INSERT IGNORE INTO {$wpdb->prefix}wpappninja_installs (`device_id`, `device_type`) VALUES (%s, %d)", $device_sha, $device_type));
347 }
348
349 if ($id != '' AND $device != '') {
350 $registered = $wpdb->get_results($wpdb->prepare("SELECT `device_id` FROM {$wpdb->prefix}wpappninja_ids WHERE `device_id` = %s", $device));
351
352 if (isset($registered[0]) && $registered[0]->device_id != "") {
353 $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}wpappninja_ids SET `registration_id` = %s, `lang` = %s WHERE `device_id` = %s", $id, $lang, $device));
354 } else {
355 $wpdb->query($wpdb->prepare("INSERT IGNORE INTO {$wpdb->prefix}wpappninja_ids (`registration_id`, `device_id`, `lang`) VALUES (%s, %s, %s)", $id, $device, $lang));
356 }
357 }
358
359 if (substr($id, 0, 5) != "_IOS_" && get_option('wpmobile_firebase_config', '') != '') {
360 $fields = array(
361 'to' => '/topics/main',
362 'registration_tokens' => array($id)
363 );
364 $headers = array('Authorization' => 'Bearer ' . wpmobile_getOauthToken(), 'access_token_auth' => 'true', 'Content-Type' => 'application/json');
365 $result = wp_remote_post( 'https://iid.googleapis.com/iid/v1:batchAdd', array(
366 'method' => 'POST',
367 'timeout' => 10,
368 'redirection' => 1,
369 'headers' => $headers,
370 'body' => json_encode($fields)
371 ));
372 }
373 }
374
375 // SEND PUSH
376 function wpmobileapp_push($title, $message, $image, $link, $lang_2letters = 'all', $send_timestamp = '', $user_email = '') {
377
378 global $wpdb;
379
380 $title = wp_strip_all_tags($title);
381 $title = strip_shortcodes($title);
382
383 $lang_array = wpappninja_available_lang();
384
385 $post_id = $link;
386 if (!preg_match('#^http#', $link)) {
387 $post_id = "0";
388 }
389
390 $content = wp_strip_all_tags($message);
391 $content = strip_shortcodes($content);
392 $content = preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $content));
393 $content = trim(preg_replace('/\s+/', ' ', $content));
394
395 if (!preg_match('#^http#', $image)) {
396 $image = ' ';
397 }
398
399 $timestamp = $send_timestamp;
400 if ($timestamp == "") {
401 $timestamp = current_time('timestamp');
402 }
403
404 $category = $user_email;
405 $lang = $lang_2letters;
406 $post_id = apply_filters('wpmobile_push_url', $post_id);
407 $title = apply_filters('wpmobile_push_title', $title);
408 $content = apply_filters('wpmobile_push_text', $content);
409 $image = apply_filters('wpmobile_push_image', $image);
410
411 $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}wpappninja_push (`id_post`, `titre`, `message`, `image`, `send_date`, `lang`, `category`) VALUES (%s, %s, %s, %s, %s, %s, %s)", $post_id, $title, $content, $image, $timestamp, $lang, $category));
412 }
413
414 // NEW MAIL
415 add_filter( 'wp_mail', 'wpmobileapp_send_push_mail', 1 );
416 function wpmobile_get_first_url_with_bloginfo_url($content) {
417 $bloginfo_url = get_bloginfo('url');
418 $pattern = '/(' . preg_quote($bloginfo_url, '/') . '[^\s"\']+)/i';
419 if (preg_match($pattern, $content, $matches)) {
420 return $matches[0];
421 }
422 return "";
423 }
424 function wpmobileapp_send_push_mail( $args ) {
425
426 if (get_wpappninja_option('wpmobile_auto_mail') == '1') {
427 $emailsto = $args['to'];
428
429 $all_recipients = array();
430
431 if (isset($args['headers']) && is_array($args['headers'])) {
432 foreach ($args['headers'] as $header) {
433 if (strpos($header, 'Bcc:') !== false) {
434 $bcc_emails = explode(',', str_replace('Bcc:', '', $header));
435 foreach ($bcc_emails as $bcc_email) {
436 $bcc_email = trim($bcc_email);
437 if (is_email($bcc_email)) {
438 $all_recipients[] = $bcc_email;
439 }
440 }
441 }
442 if (strpos($header, 'Cc:') !== false) {
443 $bcc_emails = explode(',', str_replace('Cc:', '', $header));
444 foreach ($bcc_emails as $bcc_email) {
445 $bcc_email = trim($bcc_email);
446 if (is_email($bcc_email)) {
447 $all_recipients[] = $bcc_email;
448 }
449 }
450 }
451 }
452 }
453
454 if (!is_array($emailsto)) {
455 $emailsto = explode(',', $emailsto);
456 }
457
458 foreach ($emailsto as $emailto) {
459 if (is_email($emailto)) {
460 $all_recipients[] = $emailto;
461 }
462 }
463
464 if ($args['subject'] != "" && $args['message'] != "") {
465 $permalink = wpmobile_get_first_url_with_bloginfo_url(wp_strip_all_tags($args['message']));
466 foreach ($all_recipients as $recipient) {
467 wpmobileapp_push($args['subject'], wp_strip_all_tags($args['message']), "", $permalink, 'all', '', $recipient);
468 }
469 }
470 }
471
472 return $args;
473 }
474
475 // NEW POST
476 add_action( 'new_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
477 add_action( 'draft_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
478 add_action( 'auto-draft_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
479 add_action( 'private_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
480 add_action( 'trash_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
481 add_action( 'pending_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
482 add_action( 'future_to_publish', 'wpmobileapp_send_push_post', 10, 1 );
483 function wpmobileapp_send_push_post($post) {
484
485 if (get_wpappninja_option('wpmobile_auto_post') == '1' && !get_transient("wpmobile_push_slow_down")) {
486
487 set_transient( 'wpmobile_push_slow_down', true, 30 );
488
489 $posttype = get_post_type($post);
490
491 if (in_array($posttype, array('post'))) {
492
493 $ID = $post->ID;
494 $title = $post->post_title;
495 $permalink = get_permalink( $ID );
496 $image = wpappninja_get_image($ID);
497 $content = get_the_excerpt($ID);
498
499 if ($title != "" && $permalink != "") {
500
501 $already_sent = get_option('wpmobile_auto_push_sent', array());
502
503 if (!in_array($ID, $already_sent)) {
504
505 $allowed_cats = get_wpappninja_option('wpmobile_auto_post_cats', array());
506
507 if (!empty($allowed_cats)) {
508 $post_cats = wp_get_post_categories($ID);
509
510 if (empty(array_intersect($allowed_cats, $post_cats))) {
511 return;
512 }
513 }
514
515 $already_sent[] = $ID;
516 update_option('wpmobile_auto_push_sent', $already_sent);
517 wpmobileapp_push($title, $content, $image, $permalink, 'all', '', '');
518 }
519 }
520 }
521 }
522 }
523
524 // NEW POST
525 add_action( 'publish_post', 'wpmobileapp_send_push_post_update', PHP_INT_MAX, 2 );
526 function wpmobileapp_send_push_post_update($ID, $post) {
527
528 if (get_wpappninja_option('wpmobile_auto_post_update') == '1' && !get_transient("wpmobile_push_slow_down")) {
529
530 set_transient( 'wpmobile_push_slow_down', true, 30 );
531
532 $title = $post->post_title;
533 $permalink = get_permalink( $ID );
534 $image = wpappninja_get_image($ID);
535 $content = get_the_excerpt($ID);
536
537 $posttype = get_post_type($post);
538
539 if (in_array($posttype, array('post'))) {
540
541 if ($title != "" && $permalink != "") {
542
543 $already_sent = get_option('wpmobile_auto_push_sent', array());
544 $already_sent[] = $ID;
545 update_option('wpmobile_auto_push_sent', $already_sent);
546
547 $allowed_cats = get_wpappninja_option('wpmobile_auto_post_cats', array());
548
549 if (!empty($allowed_cats)) {
550 $post_cats = wp_get_post_categories($ID);
551
552 if (empty(array_intersect($allowed_cats, $post_cats))) {
553 return;
554 }
555 }
556
557 wpmobileapp_push($title, $content, $image, $permalink, 'all', '', '');
558 }
559 }
560 }
561 }
562
563 // WOOCOMMERCE ORDER UPDATE
564 add_action('woocommerce_order_status_changed', 'wpmobileapp_send_push_wc', 10, 3);
565 function wpmobileapp_send_push_wc($order_id,$old_status,$new_status) {
566
567 $wc_translation = array(
568 'pending' => _x( 'Pending payment', 'Order status', 'wpappninja' ),
569 'processing' => _x( 'Processing', 'Order status', 'wpappninja' ),
570 'on-hold' => _x( 'On hold', 'Order status', 'wpappninja' ),
571 'completed' => _x( 'Completed', 'Order status', 'wpappninja' ),
572 'cancelled' => _x( 'Cancelled', 'Order status', 'wpappninja' ),
573 'refunded' => _x( 'Refunded', 'Order status', 'wpappninja' ),
574 'failed' => _x( 'Failed', 'Order status', 'wpappninja' ),
575 );
576
577 if ($wc_translation[$old_status] != "") {
578 $old_status = $wc_translation[$old_status];
579 }
580
581 if ($wc_translation[$new_status] != "") {
582 $new_status = $wc_translation[$new_status];
583 }
584
585 if (get_wpappninja_option('wpmobile_auto_wc') == '1') {
586
587 $title = sprintf( __( 'Order status changed from %s to %s', 'wpappninja' ), $old_status, $new_status );
588
589 $order = new WC_Order( $order_id );
590 $user_email = $order->get_billing_email();
591
592 if ($user_email != "" && $title != "") {
593 $message = '#' . $order_id . ' ' . __( 'Order updates', 'wpappninja' );
594 $image = ' ';
595 $link = $order->get_view_order_url();
596
597 wpmobileapp_push($title, $message, $image, $link, 'all', '', $user_email);
598 }
599 }
600 }
601
602 // WOOCOMMERCE NOTE
603 add_action('woocommerce_new_customer_note', 'wpmobileapp_woo_note', 10, 1 );
604 function wpmobileapp_woo_note($array) {
605
606 if (get_wpappninja_option('wpmobile_auto_wc') == '1') {
607
608 $order_id = $array['order_id'];
609 $title = $array['customer_note'];
610
611 $order = new WC_Order( $order_id );
612 $user_email = $order->get_billing_email();
613
614 if ($user_email != "" && $title != "") {
615 $message = '#' . $order_id . ' ' . __( 'Order updates', 'woocommerce' );
616 $image = ' ';
617 $link = $order->get_view_order_url();
618
619 wpmobileapp_push($title, $message, $image, $link, $lang_2letters = 'all', $send_timestamp = '', $user_email);
620 }
621 }
622 }
623
624 // BETTERMESSAGES
625 add_action('better_messages_message_sent', 'wpmobileapp_bettermessages', 10, 1 );
626 function wpmobileapp_bettermessages( $message ){
627
628 if (get_wpappninja_option('wpmobile_auto_bm') == '1') {
629 $thread_id = $message->thread_id;
630 $thread = Better_Messages()->functions->get_thread( $thread_id );
631
632 $send_push = isset( $message->send_push ) ? $message->send_push : false;
633
634 if ( ! $send_push ) {
635 return;
636 }
637
638 $recipients = array_keys( $message->recipients );
639 $pushsent = array();
640
641 $online_users = [];
642 if( Better_Messages()->websocket ){
643 $online_users = Better_Messages()->websocket->get_online_users();
644 }
645
646 foreach ( $recipients as $user_id ) {
647
648 if ( in_array( $user_id, $pushsent ) ) {
649 continue;
650 }
651
652 if( in_array( $user_id, $online_users ) ) {
653 continue;
654 }
655
656 $pushsent[] = $user_id;
657
658 $muted_threads = Better_Messages()->functions->get_user_muted_threads( $user_id );
659 if ( isset( $muted_threads[ $thread_id ] ) ) {
660 continue;
661 }
662
663 if ( $message->sender_id == $user_id ) {
664 continue;
665 }
666
667 $url = Better_Messages()->functions->get_user_thread_url( $thread_id, $user_id );
668 $subject = sprintf( __( 'New message from %s', 'bp-better-messages' ), Better_Messages()->functions->get_name( $message->sender_id ) );
669 $content = sprintf( __( 'You have new message from %s', 'bp-better-messages' ), Better_Messages()->functions->get_name( $message->sender_id ) );
670 $sender_avatar = htmlspecialchars_decode( Better_Messages_Functions()->get_avatar( $message->sender_id, 40, [ 'html' => false ] ) );
671 $user_info = get_userdata( $user_id );
672 $user_email = $user_info->user_email;
673
674 if ($subject != "" AND $content != "" AND $user_email != "") {
675 wpmobileapp_push( $subject, $content, $sender_avatar, $url, 'all', '', $user_email );
676 }
677 }
678 }
679 }
680
681 // BUDDYPRESS
682 add_action('bp_notification_after_save', 'wpmobileapp_send_push_bp');
683 function wpmobileapp_send_push_bp( BP_Notifications_Notification $n ) {
684
685 if (get_wpappninja_option('wpmobile_auto_bp') == '1') {
686
687 $user_id = $n->user_id;
688 $user_info = get_userdata($user_id);
689 $user_email = $user_info->user_email;
690
691 $bp = buddypress();
692 $notification = $n;
693
694 if ( isset( $bp->{ $notification->component_name }->notification_callback ) && is_callable( $bp->{ $notification->component_name }->notification_callback ) ) {
695
696 $content = call_user_func( $bp->{ $notification->component_name }->notification_callback, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1, 'array', $notification->id );
697 }
698
699 $title = $content['text'];
700
701 if ($user_email != "" && $title != "") {
702 $message = "";
703
704 if (function_exists("bb_notification_avatar_url")) {
705 $image = bb_notification_avatar_url( $notification );
706 } else {
707 $image = " ";
708 }
709
710 $link = $content['link'];
711
712 wpmobileapp_push($title, $message, $image, $link, 'all', '', $user_email);
713 }
714 }
715 }
716
717 // GFORM
718 add_action('gform_pre_send_email', 'wpmobileapp_send_push_gravity', 10, 4);
719 function wpmobileapp_send_push_gravity( $email, $message_format, $notification, $entry ) {
720
721 if (get_wpappninja_option('wpmobile_auto_gravity') == '1') {
722
723 $user_email = $email['to'];
724 $title = $email['subject'];
725
726 if ($user_email != "" && $title != "") {
727 $message = $email['message'];
728 $image = " ";
729 $link = "";
730
731 wpmobileapp_push($title, $message, $image, $link, 'all', '', $user_email);
732 }
733 }
734
735 return $email;
736 }
737
738 // PEEPSO
739 add_action('peepso_notifications_data_before_add', 'wpmobileapp_send_push_peepso');
740 function wpmobileapp_send_push_peepso( $array ) {
741
742 if (get_wpappninja_option('wpmobile_auto_peepso') == '1') {
743 $user_id = $array['not_user_id'];
744 $user_info = get_userdata($user_id);
745 $user_email = $user_info->user_email;
746
747 $from_id = $array['not_from_user_id'];
748
749 $PeepSoUser = PeepSoUser::get_instance($from_id);
750 $from_login = $PeepSoUser->get_fullname();
751
752 $title = $from_login . ' ' . $array['not_message'];
753
754 if(method_exists('PeepSoNotifications','parse')) {
755 $title = $from_login . ' ' . PeepSoNotifications::parse($array);
756 }
757
758 if ($user_email != "" && $title != "") {
759 $message = "";
760 $image = " ";
761 $link = PeepSo::get_page('notifications');
762
763 wpmobileapp_push($title, $message, $image, $link, 'all', '', $user_email);
764 }
765 }
766
767 return $array;
768 }
769 add_action('peepso_action_add_message_recipient_after', function($data) {
770
771 if (get_wpappninja_option('wpmobile_auto_peepso') == '1') {
772 $to_id = $data['mrec_user_id'];
773 $from_id = get_current_user_id();
774
775 if ( $to_id == $from_id ) {
776 return;
777 }
778
779 $from_user = PeepSoUser::get_instance( $from_id );
780
781 $to_user_info = get_userdata($to_id);
782 $to_user_email = $to_user_info->user_email;
783
784 $post = get_post( $data['mrec_msg_id'] );
785
786 $title = $from_user->get_firstname();
787
788 $message = $post->post_content;
789 $message = strip_tags( $message );
790 $message = apply_filters( 'peepso_remove_shortcodes', $message );
791
792 if ( $message == PeepSoMessagesPlugin::MESSAGE_INLINE_LEFT_CONVERSATION ) {
793 $message = __( 'Left the conversation', 'wpappninja' );
794 } elseif ( $message == PeepSoMessagesPlugin::MESSAGE_INLINE_NEW_GROUP ) {
795 $message = __( 'Created a new group conversation', 'wpappninja' );
796 }
797
798 if ( ! strlen( $message ) ) {
799 $message = __( 'New message. Click to view', 'wpappninja' );
800 }
801
802 $link = PeepSo::get_page( 'messages' );
803
804 $image = " ";
805
806 if (is_email($to_user_email) && strlen($title) > 2) {
807 wpmobileapp_push( $title, $message, $image, $link, 'all', '', $to_user_email );
808 }
809 }
810 });
811 add_action('peepso_friends_requests_after_add', function($from_id, $to_id) {
812 if (get_wpappninja_option('wpmobile_auto_peepso') == '1') {
813
814 $to_user = PeepSoUser::get_instance( $to_id );
815
816 $from_user_info = get_userdata($from_id);
817
818 $to_user_info = get_userdata($to_id);
819 $to_user_email = $to_user_info->user_email;
820
821 $title = $from_user_info->first_name;
822 $message = __( 'Sent you a friend request', 'wpappninja' );
823 $link = $to_user->get_profileurl() . 'friends/requests';
824 $image = " ";
825
826 if (is_email($to_user_email) && strlen($title) > 2) {
827 wpmobileapp_push( $title, $message, $image, $link, 'all', '', $to_user_email );
828 }
829 }
830
831 }, 10, 2);
832
833 if ( ! function_exists( 'wpmobileapp_fc_get_permalink_from_object' ) ) {
834 function wpmobileapp_fc_get_permalink_from_object( $object ) {
835 if ( ! is_object( $object ) ) {
836 return '';
837 }
838
839 if ( method_exists( $object, 'getPermalink' ) ) {
840 try {
841 $link = $object->getPermalink();
842 if ( ! empty( $link ) ) {
843 return $link;
844 }
845 } catch ( Exception $e ) {
846 }
847 }
848
849 if ( isset( $object->permalink ) && ! empty( $object->permalink ) ) {
850 return $object->permalink;
851 }
852
853 if ( isset( $object->url ) && ! empty( $object->url ) ) {
854 return $object->url;
855 }
856
857 if ( isset( $object->ID ) && $object->ID ) {
858 $post_link = get_permalink( $object->ID );
859 if ( $post_link ) {
860 return $post_link;
861 }
862 }
863
864 if ( isset( $object->id ) && $object->id ) {
865 $post_link = get_permalink( $object->id );
866 if ( $post_link ) {
867 return $post_link;
868 }
869 }
870
871 return '';
872 }
873 }
874
875 if ( ! function_exists( 'wpmobileapp_fc_push' ) ) {
876 function wpmobileapp_fc_push( $user_id, $title, $message, $link = '' ) {
877
878 if ( get_wpappninja_option( 'wpmobile_auto_fc' ) != '1' ) {
879 return;
880 }
881
882 $user_id = (int) $user_id;
883 if ( ! $user_id ) {
884 return;
885 }
886
887 $user = get_user_by( 'id', $user_id );
888 if ( ! $user || empty( $user->user_email ) || empty( $title ) ) {
889 return;
890 }
891
892 $image = ' ';
893
894 wpmobileapp_push(
895 $title,
896 $message,
897 $image,
898 $link,
899 $lang_2letters = 'all',
900 $send_timestamp = '',
901 $user->user_email
902 );
903 }
904 }
905
906 add_action( 'fluent_community/space/joined', 'wpmobileapp_fc_space_joined', 10, 3 );
907 function wpmobileapp_fc_space_joined( $space, $user_id, $by ) {
908
909 $space_name = '';
910 if ( is_object( $space ) ) {
911 if ( isset( $space->title ) ) {
912 $space_name = $space->title;
913 } elseif ( isset( $space->name ) ) {
914 $space_name = $space->name;
915 }
916 }
917
918 $title = __( 'New space joined', 'wpappninja' );
919 $message = $space_name
920 ? sprintf( __( 'You joined the space "%s".', 'wpappninja' ), $space_name )
921 : __( 'You joined a new space.', 'wpappninja' );
922
923 $link = wpmobileapp_fc_get_permalink_from_object( $space );
924
925 wpmobileapp_fc_push( $user_id, $title, $message, $link );
926 }
927
928 /**
929 * SPACE: join requested
930 */
931 add_action( 'fluent_community/space/join_requested', 'wpmobileapp_fc_space_join_requested', 10, 2 );
932 function wpmobileapp_fc_space_join_requested( $space, $user_id ) {
933
934 $space_name = '';
935 if ( is_object( $space ) ) {
936 if ( isset( $space->title ) ) {
937 $space_name = $space->title;
938 } elseif ( isset( $space->name ) ) {
939 $space_name = $space->name;
940 }
941 }
942
943 $title = __( 'Join request sent', 'wpappninja' );
944 $message = $space_name
945 ? sprintf( __( 'Your request to join the space "%s" has been sent.', 'wpappninja' ), $space_name )
946 : __( 'Your request to join the space has been sent.', 'wpappninja' );
947
948 $link = wpmobileapp_fc_get_permalink_from_object( $space );
949
950 wpmobileapp_fc_push( $user_id, $title, $message, $link );
951 }
952
953 /**
954 * COMMENTS: new comment + mentions in a comment
955 */
956 add_action( 'fluent_community/comment_added', 'wpmobileapp_fc_comment_added', 10, 3 );
957 function wpmobileapp_fc_comment_added( $comment, $feed, $mentioned_users ) {
958
959 if ( is_object( $feed ) && isset( $feed->user_id ) ) {
960
961 $author_id = (int) $feed->user_id;
962 $comment_author_id = 0;
963
964 if ( is_object( $comment ) && isset( $comment->user_id ) ) {
965 $comment_author_id = (int) $comment->user_id;
966 }
967
968 if ( $author_id && $author_id !== $comment_author_id ) {
969 $title = __( 'New comment', 'wpappninja' );
970 $message = __( 'Someone commented on your post.', 'wpappninja' );
971
972 $link = wpmobileapp_fc_get_permalink_from_object( $feed );
973
974 wpmobileapp_fc_push( $author_id, $title, $message, $link );
975 }
976 }
977
978 if ( ! empty( $mentioned_users ) && is_iterable( $mentioned_users ) ) {
979
980 foreach ( $mentioned_users as $mentioned ) {
981
982 $mentioned_id = 0;
983
984 if ( is_object( $mentioned ) ) {
985 if ( isset( $mentioned->user_id ) ) {
986 $mentioned_id = (int) $mentioned->user_id;
987 } elseif ( isset( $mentioned->ID ) ) {
988 $mentioned_id = (int) $mentioned->ID;
989 }
990 } elseif ( is_numeric( $mentioned ) ) {
991 $mentioned_id = (int) $mentioned;
992 }
993
994 if ( $mentioned_id ) {
995 $title = __( 'You were mentioned', 'wpappninja' );
996 $message = __( 'You were mentioned in a comment.', 'wpappninja' );
997
998 $link = wpmobileapp_fc_get_permalink_from_object( $feed );
999
1000 wpmobileapp_fc_push( $mentioned_id, $title, $message, $link );
1001 }
1002 }
1003 }
1004 }
1005
1006 /**
1007 * FEED: mention in a feed
1008 */
1009 add_action( 'fluent_community/feed_mentioned', 'wpmobileapp_fc_feed_mentioned', 10, 2 );
1010 function wpmobileapp_fc_feed_mentioned( $feed, $mentioned_users ) {
1011
1012 if ( empty( $mentioned_users ) || ! is_iterable( $mentioned_users ) ) {
1013 return;
1014 }
1015
1016 $link = wpmobileapp_fc_get_permalink_from_object( $feed );
1017
1018 foreach ( $mentioned_users as $mentioned ) {
1019
1020 $mentioned_id = 0;
1021
1022 if ( is_object( $mentioned ) ) {
1023 if ( isset( $mentioned->user_id ) ) {
1024 $mentioned_id = (int) $mentioned->user_id;
1025 } elseif ( isset( $mentioned->ID ) ) {
1026 $mentioned_id = (int) $mentioned->ID;
1027 }
1028 } elseif ( is_numeric( $mentioned ) ) {
1029 $mentioned_id = (int) $mentioned;
1030 }
1031
1032 if ( $mentioned_id ) {
1033 $title = __( 'You were mentioned', 'wpappninja' );
1034 $message = __( 'You were mentioned in a post.', 'wpappninja' );
1035
1036 wpmobileapp_fc_push( $mentioned_id, $title, $message, $link );
1037 }
1038 }
1039 }
1040
1041 /**
1042 * COURSE: user enrolled in a course
1043 */
1044 add_action( 'fluent_community/course/enrolled', 'wpmobileapp_fc_course_enrolled', 10, 3 );
1045 function wpmobileapp_fc_course_enrolled( $course, $user_id, $by ) {
1046
1047 $course_title = '';
1048 if ( is_object( $course ) ) {
1049 if ( isset( $course->title ) ) {
1050 $course_title = $course->title;
1051 } elseif ( isset( $course->post_title ) ) {
1052 $course_title = $course->post_title;
1053 }
1054 }
1055
1056 $title = __( 'Course enrolled', 'wpappninja' );
1057 $message = $course_title
1058 ? sprintf( __( 'You are now enrolled in the course "%s".', 'wpappninja' ), $course_title )
1059 : __( 'You are now enrolled in a course.', 'wpappninja' );
1060
1061 $link = wpmobileapp_fc_get_permalink_from_object( $course );
1062
1063 wpmobileapp_fc_push( $user_id, $title, $message, $link );
1064 }
1065
1066 /**
1067 * COURSE: course completed
1068 */
1069 add_action( 'fluent_community/course/completed', 'wpmobileapp_fc_course_completed', 10, 2 );
1070 function wpmobileapp_fc_course_completed( $course, $user_id ) {
1071
1072 $course_title = '';
1073 if ( is_object( $course ) ) {
1074 if ( isset( $course->title ) ) {
1075 $course_title = $course->title;
1076 } elseif ( isset( $course->post_title ) ) {
1077 $course_title = $course->post_title;
1078 }
1079 }
1080
1081 $title = __( 'Course completed', 'wpappninja' );
1082 $message = $course_title
1083 ? sprintf( __( 'Congratulations, you have completed the course "%s".', 'wpappninja' ), $course_title )
1084 : __( 'Congratulations, you have completed a course.', 'wpappninja' );
1085
1086 $link = wpmobileapp_fc_get_permalink_from_object( $course );
1087
1088 wpmobileapp_fc_push( $user_id, $title, $message, $link );
1089 }
1090
1091 /**
1092 * COURSE: lesson completed
1093 */
1094 add_action( 'fluent_community/course/lesson_completed', 'wpmobileapp_fc_lesson_completed', 10, 2 );
1095 function wpmobileapp_fc_lesson_completed( $lesson, $user_id ) {
1096
1097 $lesson_title = '';
1098 if ( is_object( $lesson ) ) {
1099 if ( isset( $lesson->title ) ) {
1100 $lesson_title = $lesson->title;
1101 } elseif ( isset( $lesson->post_title ) ) {
1102 $lesson_title = $lesson->post_title;
1103 }
1104 }
1105
1106 $title = __( 'Lesson completed', 'wpappninja' );
1107 $message = $lesson_title
1108 ? sprintf( __( 'You have completed the lesson "%s".', 'wpappninja' ), $lesson_title )
1109 : __( 'You have completed a lesson.', 'wpappninja' );
1110
1111 $link = wpmobileapp_fc_get_permalink_from_object( $lesson );
1112
1113 wpmobileapp_fc_push( $user_id, $title, $message, $link );
1114 }
1115
1116 /**
1117 * LEADERBOARD: user level upgraded
1118 */
1119 add_action( 'fluent_community/user_level_upgraded', 'wpmobileapp_fc_user_level_upgraded', 10, 3 );
1120 function wpmobileapp_fc_user_level_upgraded( $xprofile, $new_level, $old_level ) {
1121
1122 if ( ! is_object( $xprofile ) || ! isset( $xprofile->user_id ) ) {
1123 return;
1124 }
1125
1126 $user_id = (int) $xprofile->user_id;
1127
1128 $new_level_name = '';
1129 if ( is_array( $new_level ) && isset( $new_level['title'] ) ) {
1130 $new_level_name = $new_level['title'];
1131 }
1132
1133 $title = __( 'Level up', 'wpappninja' );
1134 $message = $new_level_name
1135 ? sprintf( __( 'Congratulations, you have reached level "%s".', 'wpappninja' ), $new_level_name )
1136 : __( 'Congratulations, you have reached a new level.', 'wpappninja' );
1137
1138 $link = '';
1139
1140 wpmobileapp_fc_push( $user_id, $title, $message, $link );
1141 }
1142