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