| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Classes\Notifications\Model; |
| 4 |
|
| 5 |
use MasterAddons\Inc\Classes\Notifications\Base\Date; |
| 6 |
use ReflectionClass; |
| 7 |
|
| 8 |
// No, Direct access Sir !!! |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Notification abstract class |
| 15 |
* |
| 16 |
* Jewel Theme <support@jeweltheme.com> |
| 17 |
*/ |
| 18 |
abstract class Notification |
| 19 |
{ |
| 20 |
|
| 21 |
use Date; |
| 22 |
|
| 23 |
public $type; |
| 24 |
public $control; |
| 25 |
public $color; |
| 26 |
public $current_interval = 0; |
| 27 |
public $intervals = array(); |
| 28 |
public $is_active = true; |
| 29 |
public $next_exec_time = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* Construct method |
| 33 |
* |
| 34 |
* @author Jewel Theme <support@jeweltheme.com> |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
$this->init(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Init method |
| 43 |
* |
| 44 |
* @author Jewel Theme <support@jeweltheme.com> |
| 45 |
*/ |
| 46 |
final public function init() |
| 47 |
{ |
| 48 |
$data = $this->get(); |
| 49 |
|
| 50 |
if (false !== $data) { // Found Data from Database. |
| 51 |
$this->is_active = isset($data['is_active']) ? $data['is_active'] : true; |
| 52 |
$this->intervals = isset($data['intervals']) ? $data['intervals'] : array(); |
| 53 |
$this->current_interval = isset($data['current_interval']) ? $data['current_interval'] : 0; |
| 54 |
} else { // No Data From Database, So Build & Save it. |
| 55 |
$this->build(); |
| 56 |
$this->save(); |
| 57 |
} |
| 58 |
|
| 59 |
if ($this->is_active && !empty($this->intervals)) { |
| 60 |
$intervals = wp_list_filter($this->intervals, array('fired' => false)); |
| 61 |
|
| 62 |
if (!empty($intervals)) { |
| 63 |
$first = array_shift($intervals); |
| 64 |
$this->next_exec_time = $first['date']; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* build method |
| 73 |
* |
| 74 |
* @author Jewel Theme <support@jeweltheme.com> |
| 75 |
*/ |
| 76 |
final public function build() |
| 77 |
{ |
| 78 |
foreach ($this->get_intervals() as $index => $day) { |
| 79 |
$data = array( |
| 80 |
'days' => $day, |
| 81 |
'fired' => false, |
| 82 |
); |
| 83 |
|
| 84 |
if (0 === $index) { |
| 85 |
$data['date'] = gmdate('Y-m-d', time() + (DAY_IN_SECONDS * $day)); |
| 86 |
} else { |
| 87 |
$data['date'] = $this->date_increment($this->intervals[$index - 1]['date'], $day); |
| 88 |
} |
| 89 |
|
| 90 |
$this->intervals[] = $data; |
| 91 |
} |
| 92 |
|
| 93 |
return $this->intervals; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Final method fire |
| 98 |
* |
| 99 |
* @param [type] $trigger_time . |
| 100 |
* @param [type] $notification_type . |
| 101 |
* |
| 102 |
* @author Jewel Theme <support@jeweltheme.com> |
| 103 |
*/ |
| 104 |
final public function fire($trigger_time, $notification_type) |
| 105 |
{ |
| 106 |
update_option("jltma_{$notification_type}_last_interact", $trigger_time); |
| 107 |
|
| 108 |
// Current Interval is completed . |
| 109 |
$this->intervals[$this->current_interval]['fired'] = true; |
| 110 |
|
| 111 |
// Set the next interval as Current Interval . |
| 112 |
$this->current_interval++; |
| 113 |
|
| 114 |
// Return if notification is inactive . |
| 115 |
if (!$this->is_active) { |
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
if ($this->current_interval >= count($this->intervals)) { |
| 120 |
// Stop the Notification if We reach the end interval . |
| 121 |
$this->is_active = false; |
| 122 |
} else { |
| 123 |
// Delay the next intervals if needed . |
| 124 |
$this->maybe_delay($trigger_time); |
| 125 |
} |
| 126 |
|
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* delay method |
| 132 |
* |
| 133 |
* @param [type] $trigger_time . |
| 134 |
* |
| 135 |
* @author Jewel Theme <support@jeweltheme.com> |
| 136 |
*/ |
| 137 |
final public function maybe_delay($trigger_time) |
| 138 |
{ |
| 139 |
$diff = abs($this->date_diff($this->next_exec_time, $trigger_time)); |
| 140 |
|
| 141 |
if (!$diff) { |
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
foreach ($this->intervals as &$interval) { |
| 146 |
if ($interval['fired']) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
$interval['date'] = $this->date_increment($interval['date'], $diff); |
| 150 |
} |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Save method |
| 157 |
* |
| 158 |
* @author Jewel Theme <support@jeweltheme.com> |
| 159 |
*/ |
| 160 |
final public function save() |
| 161 |
{ |
| 162 |
update_option( |
| 163 |
$this->get_key(), |
| 164 |
array( |
| 165 |
'is_active' => $this->is_active, |
| 166 |
'intervals' => $this->intervals, |
| 167 |
'current_interval' => $this->current_interval, |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get method |
| 174 |
* |
| 175 |
* @author Jewel Theme <support@jeweltheme.com> |
| 176 |
*/ |
| 177 |
final public function get() |
| 178 |
{ |
| 179 |
return get_option($this->get_key()); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Delete method |
| 184 |
* |
| 185 |
* @author Jewel Theme <support@jeweltheme.com> |
| 186 |
*/ |
| 187 |
final public function delete() |
| 188 |
{ |
| 189 |
delete_option($this->get_key()); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get ID |
| 194 |
* |
| 195 |
* @author Jewel Theme <support@jeweltheme.com> |
| 196 |
*/ |
| 197 |
final public function get_id() |
| 198 |
{ |
| 199 |
$calss_name = (new ReflectionClass($this))->getShortName(); |
| 200 |
|
| 201 |
return strtolower($calss_name); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get intervals method |
| 206 |
* |
| 207 |
* @author Jewel Theme <support@jeweltheme.com> |
| 208 |
*/ |
| 209 |
final public function get_intervals() |
| 210 |
{ |
| 211 |
$interval = (array) $this->intervals(); |
| 212 |
sort($interval); |
| 213 |
|
| 214 |
return $interval; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Get key method |
| 219 |
* |
| 220 |
* @author Jewel Theme <support@jeweltheme.com> |
| 221 |
*/ |
| 222 |
abstract public function get_key(); |
| 223 |
|
| 224 |
/** |
| 225 |
* Get intervals method |
| 226 |
* |
| 227 |
* @author Jewel Theme <support@jeweltheme.com> |
| 228 |
*/ |
| 229 |
abstract public function intervals(); |
| 230 |
} |
| 231 |
|