Notification.php (4225B)
1 <?php 2 3 4 namespace Materialis\Notify; 5 6 7 use DateTime; 8 9 class Notification 10 { 11 const NOTIFICATION_ACTION_PREFIX = "cp_notification_notice_"; 12 13 private $name; 14 15 private $start = '*'; 16 private $end = '*'; 17 private $after = null; 18 19 private $dismissible = true; 20 private $type = "info"; 21 private $active_callback = null; 22 private $handle = null; 23 private $priority = 0; 24 25 private $data; 26 27 public function __construct($data) 28 { 29 $this->data = $data; 30 foreach ($data as $key => $value) { 31 if (property_exists($this, $key)) { 32 $this->$key = $value; 33 } 34 } 35 36 if ($this->canShow()) { 37 38 $this->addNotificationView(); 39 } 40 } 41 42 // php 5.3 compatibility 43 public function __get($name) 44 { 45 if (property_exists($this, $name)) { 46 return $this->$name; 47 } else { 48 throw new \Exception("Property {$name} does not exists in class Notification", 1); 49 50 } 51 } 52 53 public function addNotificationView() 54 { 55 $self = $this; 56 add_action('admin_notices', function () use ($self) { 57 ?> 58 <div data-cp-notification-name="<?php echo $self->name ?>" class="cp-notification notice notice-<?php echo $self->type ?> <?php echo($self->dismissible ? 'is-dismissible' : '') ?>"> 59 60 61 <?php 62 if ($self->handle) { 63 call_user_func($self->handle, $self->data); 64 } else { 65 do_action(\Materialis\Notify\Notification::NOTIFICATION_ACTION_PREFIX . $self->name, $self->data); 66 } 67 ?> 68 69 <?php if ($self->dismissible): ?> 70 <script type="text/javascript"> 71 jQuery('[data-cp-notification-name="<?php echo $self->name ?>"]').on('click', '.notice-dismiss', function () { 72 var data = { 73 'action': 'cp_dismiss_notification', 74 'notification': '<?php echo $self->name; ?>' 75 }; 76 jQuery.post(ajaxurl, data).done(function (response) { 77 78 }); 79 }) 80 </script> 81 <?php endif; ?> 82 </div> 83 <?php 84 }, 0); 85 } 86 87 public function canShow() 88 { 89 $canShow = ( 90 $this->isActive() && 91 ! $this->isDismissed() && 92 $this->inTimeBoundaries() 93 ); 94 95 return $canShow; 96 } 97 98 public function isActive() 99 { 100 if ( ! $this->active_callback) { 101 return true; 102 } else { 103 return call_user_func($this->active_callback); 104 } 105 } 106 107 public function inTimeBoundaries() 108 { 109 110 $time = new DateTime("now"); 111 112 if ($this->after) { 113 $installTime = intval(NotificationsManager::initializationTS()); 114 $showAfter = strtotime('+' . $this->after . ' days', $installTime); 115 if ($showAfter <= $time->getTimeStamp()) { 116 return true; 117 } 118 } else { 119 120 if ($this->start === "*") { 121 return true; 122 } else { 123 $start = \DateTime::createFromFormat('d-m-Y', $this->start); 124 125 126 if ($start && $start <= $time) { 127 if ($this->end === "*") { 128 return true; 129 } else { 130 $end = \DateTime::createFromFormat('d-m-Y', $this->end); 131 if ($end && $time <= $end) { 132 return true; 133 } 134 } 135 } 136 } 137 } 138 139 return false; 140 } 141 142 public function isDismissed() 143 { 144 if ( ! $this->dismissible) { 145 return false; 146 } 147 148 $notifications = get_option(NotificationsManager::DISMISSED_NOTIFICATIONS_OPTION, array()); 149 150 return in_array($this->name, $notifications); 151 } 152 }