PluginProbe
MStore API – Create Native Android & iOS Apps On The Cloud / trunk
MStore API – Create Native Android & iOS Apps On The Cloud vtrunk
4.21.3 4.21.2 4.21.1 trunk 1.1.5 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 All 192 releases
mstore-api / controllers / flutter-notification.php

flutter-notification.php in MStore API – Create Native Android & iOS Apps On The Cloud trunk, at controllers/flutter-notification.php

512 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6 require_once(__DIR__ . '/flutter-base.php');
7
8 /*
9 * Base REST Controller for flutter
10 *
11 * @since 1.4.0
12 *
13 * @package Notification
14 */
15
16 class FlutterNotification extends FlutterBaseController
17 {
18 /**
19 * Endpoint namespace
20 *
21 * @var string
22 */
23 protected $namespace = 'api/flutter_notification';
24
25 /**
26 * Register all routes related with stores
27 *
28 * @return void
29 */
30 public function __construct()
31 {
32 add_action('rest_api_init', array($this, 'register_flutter_notification_routes'));
33 }
34
35 public function register_flutter_notification_routes()
36 {
37 register_rest_route($this->namespace, '/test_push_notification', array(
38 array(
39 'methods' => 'POST',
40 'callback' => array($this, 'test_push_notification'),
41 'permission_callback' => array($this, 'get_settings_permissions_check'),
42 ),
43 ));
44
45 register_rest_route($this->namespace, '/test_push_notification_created_order', array(
46 array(
47 'methods' => 'POST',
48 'callback' => array($this, 'test_push_notification_created_order'),
49 'permission_callback' => array($this, 'get_settings_permissions_check'),
50 ),
51 ));
52
53 register_rest_route($this->namespace, '/test_booking_notification_to_owner', array(
54 array(
55 'methods' => 'POST',
56 'callback' => array($this, 'test_booking_notification_to_owner'),
57 'permission_callback' => array($this, 'get_settings_permissions_check'),
58 ),
59 ));
60
61 register_rest_route($this->namespace, '/test_booking_notification_to_customer', array(
62 array(
63 'methods' => 'POST',
64 'callback' => array($this, 'test_booking_notification_to_customer'),
65 'permission_callback' => array($this, 'get_settings_permissions_check'),
66 ),
67 ));
68
69 register_rest_route($this->namespace, '/list_bookings', array(
70 array(
71 'methods' => 'GET',
72 'callback' => array($this, 'list_bookings'),
73 'permission_callback' => array($this, 'get_settings_permissions_check'),
74 ),
75 ));
76
77 register_rest_route($this->namespace, '/check_user_notification_status', array(
78 array(
79 'methods' => 'GET',
80 'callback' => array($this, 'check_user_notification_status'),
81 'permission_callback' => array($this, 'get_settings_permissions_check'),
82 ),
83 ));
84
85 register_rest_route($this->namespace, '/settings', array(
86 array(
87 'methods' => 'PUT',
88 'callback' => array($this, 'update_settings'),
89 'permission_callback' => array($this, 'get_settings_permissions_check'),
90 ),
91 array(
92 'methods' => 'GET',
93 'callback' => array($this, 'get_settings'),
94 'permission_callback' => array($this, 'get_settings_permissions_check'),
95 ),
96 ));
97 }
98
99 public function test_push_notification()
100 {
101 try {
102 $json = file_get_contents('php://input');
103 $params = json_decode($json);
104
105 // Validate input
106 if (!isset($params->email)) {
107 return $this->sendError('missing_email', 'Email is required', 400);
108 }
109
110 $email = $params->email;
111 $is_manager = isset($params->is_manager) ? $params->is_manager : false;
112 $is_delivery = isset($params->is_delivery) ? $params->is_delivery : false;
113
114 // Get user
115 $user = get_user_by('email', $email);
116 if (!$user) {
117 return $this->sendError('notification_request_failed', 'Unable to process notification request', 404);
118 }
119
120 $user_id = $user->ID;
121 $is_onesignal = isset($params->is_onesignal) ? $params->is_onesignal : false;
122
123 // Check if OneSignal
124 if($is_onesignal){
125 // Check if function exists
126 if (!function_exists('one_signal_push_notification')) {
127 return $this->sendError('function_not_found', 'one_signal_push_notification function not found. Make sure functions/index.php is loaded.', 500);
128 }
129
130 // Check if OneSignal plugin is active
131 if (!is_plugin_active('onesignal-free-web-push-notifications/onesignal.php')) {
132 return $this->sendError('plugin_not_active', 'OneSignal plugin is not active', 400);
133 }
134
135 // Check if OneSignal is configured (using WordPress options directly)
136 $onesignal_wp_settings = get_option('OneSignalWPSetting');
137 if (empty($onesignal_wp_settings) || empty($onesignal_wp_settings['app_id']) || empty($onesignal_wp_settings['app_rest_api_key'])) {
138 return $this->sendError('onesignal_not_configured', 'OneSignal App ID or API Key is not configured in WordPress Settings', 400);
139 }
140
141 $result = one_signal_push_notification("Fluxstore", "Test push notification", array($user_id));
142
143 $response = [
144 'success' => $result['success'],
145 'message' => $result['success'] ? 'Notification sent successfully' : ($result['message'] ?: "Failed to send notification"),
146 'user_id' => $user_id,
147 'user_email' => $email,
148 'notification_id' => $result['notification_id']
149 ];
150
151 if (!empty($result['errors'])) {
152 $response['errors'] = $result['errors'];
153 }
154
155 return $response;
156 }
157
158 // Firebase notification
159 if ($is_manager) {
160 $result = pushNotificationForVendor($user_id, "Fluxstore", "Test push notification");
161 } else if ($is_delivery) {
162 $result = pushNotificationForDeliveryBoy($user_id, "Fluxstore", "Test push notification");
163 } else {
164 $result = pushNotificationForUser($user_id, "Fluxstore", "Test push notification");
165 }
166
167 if (is_wp_error($result)) {
168 return $result;
169 }
170
171 return [
172 'success' => $result === true,
173 'message' => $result === true ? 'Notification sent successfully' : 'Failed to send notification',
174 'user_id' => $user_id,
175 'type' => 'firebase'
176 ];
177
178 } catch (\Throwable $e) {
179 return $this->sendError('exception', $e->getMessage(), 500);
180 }
181 }
182
183 function test_push_notification_created_order(){
184 $json = file_get_contents('php://input');
185 $params = json_decode($json);
186 return trackNewOrder($params->order_id);
187 }
188
189 /**
190 * Test sending booking notification to owner
191 * POST: {"booking_id": 123}
192 */
193 function test_booking_notification_to_owner() {
194 try {
195 global $wpdb;
196
197 $json = file_get_contents('php://input');
198 $params = json_decode($json);
199
200 // Validate input
201 if (!isset($params->booking_id)) {
202 return $this->sendError('missing_booking_id', 'Booking ID is required', 400);
203 }
204
205 $booking_id = intval($params->booking_id);
206
207 // Get booking from custom table
208 $table_name = $wpdb->prefix . 'bookings_calendar';
209 $booking = $wpdb->get_row($wpdb->prepare(
210 "SELECT * FROM {$table_name} WHERE ID = %d",
211 $booking_id
212 ));
213
214 // Validate booking exists
215 if (!$booking) {
216 return $this->sendError('invalid_booking', 'Booking not found in custom table', 404);
217 }
218
219 // Get listing and owner info
220 $listing_id = $booking->listing_id;
221 if (!$listing_id) {
222 return $this->sendError('missing_listing', 'Listing ID not found in booking', 400);
223 }
224
225 $listing = get_post($listing_id);
226 if (!$listing) {
227 return $this->sendError('invalid_listing', 'Listing not found', 404);
228 }
229
230 $owner_id = $booking->owner_id;
231 if (!$owner_id) {
232 return $this->sendError('missing_owner', 'Owner ID not found in booking', 400);
233 }
234
235 $owner = get_userdata($owner_id);
236 if (!$owner) {
237 return $this->sendError('invalid_owner', 'Owner not found', 404);
238 }
239
240 // Send notification
241 sendNewBookingNotificationToOwner($booking_id);
242
243 return [
244 'success' => true,
245 'message' => 'Test notification sent to owner successfully',
246 'booking_id' => $booking_id,
247 'booking_status' => $booking->status,
248 'listing_id' => $listing_id,
249 'listing_title' => $listing->post_title,
250 'owner_id' => $owner_id,
251 'owner_name' => $owner->display_name,
252 'owner_email' => $owner->user_email
253 ];
254
255 } catch (Exception $e) {
256 return $this->sendError('exception', $e->getMessage(), 500);
257 }
258 }
259
260 /**
261 * Test sending booking notification to customer
262 * POST: {"booking_id": 123}
263 */
264 function test_booking_notification_to_customer() {
265 try {
266 global $wpdb;
267
268 $json = file_get_contents('php://input');
269 $params = json_decode($json);
270
271 // Validate input
272 if (!isset($params->booking_id)) {
273 return $this->sendError('missing_booking_id', 'Booking ID is required', 400);
274 }
275
276 $booking_id = intval($params->booking_id);
277
278 // Get booking from custom table
279 $table_name = $wpdb->prefix . 'bookings_calendar';
280 $booking = $wpdb->get_row($wpdb->prepare(
281 "SELECT * FROM {$table_name} WHERE ID = %d",
282 $booking_id
283 ));
284
285 // Validate booking exists
286 if (!$booking) {
287 return $this->sendError('invalid_booking', 'Booking not found in custom table', 404);
288 }
289
290 // Get customer info
291 $customer_id = $booking->bookings_author;
292 if (!$customer_id) {
293 return $this->sendError('missing_customer', 'Customer ID not found in booking', 400);
294 }
295
296 $customer = get_userdata($customer_id);
297 if (!$customer) {
298 return $this->sendError('invalid_customer', 'Customer not found', 404);
299 }
300
301 // Get listing info
302 $listing_id = $booking->listing_id;
303 $listing_title = 'Unknown';
304 if ($listing_id) {
305 $listing = get_post($listing_id);
306 if ($listing) {
307 $listing_title = $listing->post_title;
308 }
309 }
310
311 // Send notification with current booking status
312 sendBookingStatusChangedNotificationToCustomer($booking_id, $booking->status);
313
314 return [
315 'success' => true,
316 'message' => 'Test notification sent to customer successfully',
317 'booking_id' => $booking_id,
318 'booking_status' => $booking->status,
319 'listing_id' => $listing_id,
320 'listing_title' => $listing_title,
321 'customer_id' => $customer_id,
322 'customer_name' => $customer->display_name,
323 'customer_email' => $customer->user_email
324 ];
325
326 } catch (Exception $e) {
327 return $this->sendError('exception', $e->getMessage(), 500);
328 }
329 }
330
331 /**
332 * List available bookings for testing
333 * GET: /list_bookings?limit=10
334 */
335 function list_bookings($request) {
336 try {
337 global $wpdb;
338
339 $limit = isset($request['limit']) ? intval($request['limit']) : 10;
340 $limit = min($limit, 50); // Max 50 bookings
341
342 // Query from Listeo custom table
343 $table_name = $wpdb->prefix . 'bookings_calendar';
344
345 $bookings = $wpdb->get_results($wpdb->prepare(
346 "SELECT * FROM {$table_name} ORDER BY ID DESC LIMIT %d",
347 $limit
348 ));
349
350 if (empty($bookings)) {
351 return [
352 'success' => true,
353 'message' => 'No bookings found in custom table: ' . $table_name,
354 'total' => 0,
355 'bookings' => []
356 ];
357 }
358
359 $result = array();
360 foreach ($bookings as $booking) {
361 $customer_id = $booking->bookings_author;
362 $owner_id = $booking->owner_id;
363 $listing_id = $booking->listing_id;
364
365 // Get customer info
366 $customer_name = '';
367 $customer_email = '';
368 if ($customer_id) {
369 $customer = get_userdata($customer_id);
370 if ($customer) {
371 $customer_name = $customer->display_name;
372 $customer_email = $customer->user_email;
373 }
374 }
375
376 // Get owner info
377 $owner_name = '';
378 $owner_email = '';
379 if ($owner_id) {
380 $owner = get_userdata($owner_id);
381 if ($owner) {
382 $owner_name = $owner->display_name;
383 $owner_email = $owner->user_email;
384 }
385 }
386
387 // Get listing info
388 $listing_title = '';
389 if ($listing_id) {
390 $listing = get_post($listing_id);
391 if ($listing) {
392 $listing_title = $listing->post_title;
393 }
394 }
395
396 $result[] = array(
397 'booking_id' => $booking->ID,
398 'booking_status' => $booking->status,
399 'booking_type' => $booking->type,
400 'date_start' => $booking->date_start,
401 'date_end' => $booking->date_end,
402 'price' => $booking->price,
403 'comment' => $booking->comment,
404 'order_id' => $booking->order_id,
405 'listing_id' => $listing_id,
406 'listing_title' => $listing_title,
407 'customer_id' => $customer_id,
408 'customer_name' => $customer_name,
409 'customer_email' => $customer_email,
410 'owner_id' => $owner_id,
411 'owner_name' => $owner_name,
412 'owner_email' => $owner_email,
413 'created_date' => $booking->created
414 );
415 }
416
417 return [
418 'success' => true,
419 'message' => 'Bookings retrieved successfully from custom table',
420 'total' => count($result),
421 'bookings' => $result
422 ];
423
424 } catch (Exception $e) {
425 return $this->sendError('exception', $e->getMessage(), 500);
426 }
427 }
428
429 /**
430 * Check user notification status and settings
431 * GET: /check_user_notification_status?user_id=1&email=user@example.com
432 */
433 function check_user_notification_status($request) {
434 try {
435 $user_id = isset($request['user_id']) ? intval($request['user_id']) : null;
436 $email = isset($request['email']) ? sanitize_email($request['email']) : null;
437
438 // Get user by ID or email
439 if ($user_id) {
440 $user = get_userdata($user_id);
441 } elseif ($email) {
442 $user = get_user_by('email', $email);
443 } else {
444 return $this->sendError('missing_param', 'Either user_id or email is required', 400);
445 }
446
447 if (!$user) {
448 return $this->sendError('user_not_found', 'User not found', 404);
449 }
450
451 // Check notification enabled status
452 $is_notification_enabled = isNotificationEnabled($user->ID);
453
454 // Get user meta for debugging
455 $notification_status_meta = get_user_meta($user->ID, 'mstore_notification_status', true);
456
457 // Check OneSignal settings
458 $onesignal_wp_settings = get_option('OneSignalWPSetting');
459 $onesignal_configured = !empty($onesignal_wp_settings) &&
460 !empty($onesignal_wp_settings['app_id']) &&
461 !empty($onesignal_wp_settings['app_rest_api_key']);
462
463 return [
464 'success' => true,
465 'user_id' => $user->ID,
466 'user_email' => $user->user_email,
467 'user_name' => $user->display_name,
468 'user_roles' => $user->roles,
469 'notification_enabled' => $is_notification_enabled,
470 'notification_status_meta' => $notification_status_meta ?: 'not set (defaults to on)',
471 'onesignal_configured' => $onesignal_configured,
472 'onesignal_app_id' => $onesignal_configured ? substr($onesignal_wp_settings['app_id'], 0, 10) . '...' : null,
473 'note' => 'If notification_enabled is true, user should receive notifications if they have set External User ID in the app via OneSignal.login(externalId)'
474 ];
475
476 } catch (Exception $e) {
477 return $this->sendError('exception', $e->getMessage(), 500);
478 }
479 }
480
481 function get_settings_permissions_check($request)
482 {
483 $cookie = get_header_user_cookie($request->get_header("User-Cookie"));
484 if (isset($cookie) && $cookie != null) {
485 $user_id = validateCookieLogin($cookie);
486 if (is_wp_error($user_id)) {
487 return false;
488 }
489 $request["user_id"] = $user_id;
490 return true;
491 } else {
492 return false;
493 }
494 }
495
496 function update_settings($request){
497 $json = file_get_contents('php://input');
498 $params = json_decode($json, TRUE);
499 $user_id = $request["user_id"];
500 update_user_meta($user_id, "mstore_notification_status", $params['is_on'] == true ? 'on' : 'off');
501
502 return ['is_on' => isNotificationEnabled($user_id)];
503 }
504
505 function get_settings($request){
506 $user_id = $request["user_id"];
507 return ['is_on' => isNotificationEnabled($user_id)];
508 }
509 }
510
511 new FlutterNotification;
512