| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author Alessio Gaggii - E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2022 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Registry for admin widgets watch-data to check for new browser notifications. |
| 16 |
* |
| 17 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBONotificationWatchdata extends JObject |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
* |
| 24 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 25 |
*/ |
| 26 |
private $pushed_data = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Proxy for immediately getting the object and bind data. |
| 30 |
* |
| 31 |
* @param array|object $data the watch-data payload to bind. |
| 32 |
*/ |
| 33 |
public static function getInstance($data = null) |
| 34 |
{ |
| 35 |
return new static($data); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Check if the given activity (guest message) matches the pushed data information. |
| 40 |
* If it does, it means a Push notification for the same event was clicked already. |
| 41 |
* |
| 42 |
* @param object $activity the latest activity (guest message) found. |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
* |
| 46 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 47 |
*/ |
| 48 |
public function matchPushedGuestMessage($activity) |
| 49 |
{ |
| 50 |
if (!$this->pushed_data || !is_object($activity) || !isset($activity->idorder) || !isset($activity->content)) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
foreach ($this->pushed_data as $pushed) { |
| 55 |
// make sure objects are converted into arrays |
| 56 |
$pushed = (array)$pushed; |
| 57 |
|
| 58 |
if (!$pushed || empty($pushed['id']) || empty($pushed['preview'])) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
if ($pushed['id'] != $activity->idorder) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
if (strpos($activity->content, $pushed['preview']) === 0) { |
| 67 |
// match found, the same guest message was clicked already from a Push notification |
| 68 |
return true; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Filters a list of latest events worth a notification by looking at what |
| 77 |
* Push notifications were dispatched already for some reservations. |
| 78 |
* |
| 79 |
* @param array $events the latest worthy events found. |
| 80 |
* |
| 81 |
* @return array the filtered latest events with keys reset. |
| 82 |
* |
| 83 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 84 |
*/ |
| 85 |
public function filterPushedReservations(array $events) |
| 86 |
{ |
| 87 |
if (!$this->pushed_data) { |
| 88 |
return $events; |
| 89 |
} |
| 90 |
|
| 91 |
foreach ($events as $k => $event) { |
| 92 |
if (!is_object($event) || !isset($event->idorder)) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
foreach ($this->pushed_data as $pushed) { |
| 97 |
// make sure objects are converted into arrays |
| 98 |
$pushed = (array)$pushed; |
| 99 |
|
| 100 |
if (!$pushed || empty($pushed['id'])) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
if ($pushed['id'] == $event->idorder) { |
| 105 |
// match found |
| 106 |
unset($events[$k]); |
| 107 |
continue 2; |
| 108 |
} |
| 109 |
|
| 110 |
if (isset($event->idorderota) && $pushed['id'] == $event->idorderota) { |
| 111 |
// match found |
| 112 |
unset($events[$k]); |
| 113 |
continue 2; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return array_values($events); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Gets the list of the Push notifications data clicked. |
| 123 |
* |
| 124 |
* @return array |
| 125 |
* |
| 126 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 127 |
*/ |
| 128 |
public function getPushedData() |
| 129 |
{ |
| 130 |
return $this->pushed_data; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Sets the information about the Push notifications data clicked. |
| 135 |
* |
| 136 |
* @param array $data list of push notification payloads clicked. |
| 137 |
* |
| 138 |
* @return self |
| 139 |
* |
| 140 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 141 |
*/ |
| 142 |
public function setPushedData(array $data) |
| 143 |
{ |
| 144 |
$this->pushed_data = $data; |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
} |
| 149 |
|