| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * E2pdf Notification Helper | |
| 5 | - * | |
| 6 | - * @copyright Copyright 2017 https://e2pdf.com | |
| 7 | - * @license GPL v2 | |
| 8 | - * @version 1 | |
| 9 | - * @link https://e2pdf.com | |
| 10 | - * @since 0.00.01 | |
| 4 | + * File: /model/e2pdf-notification.php | |
| 5 | + * | |
| 6 | + * @package E2Pdf | |
| 7 | + * @license GPLv3 | |
| 8 | + * @link https://e2pdf.com | |
| 11 | 9 | */ |
| 12 | 10 | if (!defined('ABSPATH')) { |
| 13 | 11 | die('Access denied.'); |
| 14 | 12 | } |
| @@ -14,63 +12,130 @@ | ||
| 14 | 12 | } |
| 15 | 13 | |
| 16 | 14 | class Model_E2pdf_Notification extends Model_E2pdf_Model { |
| 17 | 15 | |
| 18 | - protected static $instance = NULL; | |
| 16 | + private static $instance = null; | |
| 17 | + private $notifications = []; | |
| 19 | 18 | |
| 20 | - const UPGRADE_URL = 'https://e2pdf.com'; | |
| 19 | + public static function instance() { | |
| 20 | + if (self::$instance === null) { | |
| 21 | + self::$instance = new self(); | |
| 22 | + } | |
| 23 | + return self::$instance; | |
| 24 | + } | |
| 21 | 25 | |
| 22 | - /** | |
| 23 | - * Add notification | |
| 24 | - * | |
| 25 | - * @param string $type - Notification type | |
| 26 | - * @param string $text - Notification text | |
| 27 | - */ | |
| 26 | + // load | |
| 27 | + public function load() { | |
| 28 | + $notifications = get_transient('e2pdf_notifications'); | |
| 29 | + return (is_array($notifications) && !empty($notifications)) ? $notifications : []; | |
| 30 | + } | |
| 31 | + | |
| 32 | + // add notification | |
| 28 | 33 | public function add_notification($type, $text) { |
| 29 | - $notifications = get_transient('e2pdf_notifications'); | |
| 30 | - $notifications[] = array( | |
| 34 | + | |
| 35 | + $notification_id = (int) round(microtime(true) * 1000) . '-' . bin2hex(random_bytes(3)); | |
| 36 | + $notifications = $this->load(); | |
| 37 | + | |
| 38 | + $notifications[$notification_id] = [ | |
| 31 | 39 | 'type' => $type, |
| 32 | 40 | 'text' => $text, |
| 33 | - ); | |
| 34 | - set_transient('e2pdf_notifications', $notifications); | |
| 41 | + 'expires' => time() + 10, | |
| 42 | + ]; | |
| 43 | + set_transient('e2pdf_notifications', $notifications, DAY_IN_SECONDS); | |
| 44 | + | |
| 45 | + $this->notifications[$notification_id] = [ | |
| 46 | + 'type' => $type, | |
| 47 | + 'text' => $text, | |
| 48 | + ]; | |
| 49 | + return $notification_id; | |
| 35 | 50 | } |
| 36 | 51 | |
| 37 | - /** | |
| 38 | - * Get notifications | |
| 39 | - * | |
| 40 | - * @return array - List of notifications | |
| 41 | - */ | |
| 42 | - public function get_notifications() { | |
| 43 | - $notifications = get_transient('e2pdf_notifications'); | |
| 44 | - if (!get_option('e2pdf_hide_warnings')) { | |
| 52 | + // get notifications | |
| 53 | + public function get_notifications($notification_id = '') { | |
| 45 | 54 | |
| 46 | - if (!is_array($notifications)) { | |
| 47 | - $notifications = array(); | |
| 55 | + $notifications = $this->load(); | |
| 56 | + $now = time(); | |
| 57 | + | |
| 58 | + if ($notification_id && isset($notifications[$notification_id])) { | |
| 59 | + $this->notifications[$notification_id] = [ | |
| 60 | + 'type' => $notifications[$notification_id]['type'], | |
| 61 | + 'text' => $notifications[$notification_id]['text'], | |
| 62 | + ]; | |
| 63 | + unset($notifications[$notification_id]); | |
| 64 | + } | |
| 65 | + | |
| 66 | + foreach ($notifications as $id => $entry) { | |
| 67 | + if (isset($this->notifications[$id]) || $now >= $entry['expires']) { | |
| 68 | + unset($notifications[$id]); | |
| 48 | 69 | } |
| 70 | + } | |
| 49 | 71 | |
| 72 | + set_transient('e2pdf_notifications', $notifications, DAY_IN_SECONDS); | |
| 73 | + | |
| 74 | + if (!get_option('e2pdf_hide_warnings', '0')) { | |
| 50 | 75 | if ($this->helper->get('license')->get('status') == 'pre_expired') { |
| 51 | - array_unshift($notifications, array( | |
| 52 | - 'type' => 'notice', | |
| 53 | - 'text' => sprintf(__("Your E2Pdf License Key will expire at <strong>%s</strong>. Please renew it at <a target='_blank' href='%s'>%s</a>.", 'e2pdf'), $this->helper->get('license')->get('expire'), self::UPGRADE_URL, self::UPGRADE_URL) | |
| 54 | - )); | |
| 76 | + /* translators: %s: License Key Expire Date */ | |
| 77 | + $message = sprintf(__('Your E2Pdf License Key will expire at <strong>%s</strong>', 'e2pdf'), $this->helper->get('license')->get('expire')); | |
| 78 | + if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { | |
| 79 | + $message .= ' | ' . sprintf('<a class="e2pdf-link" target="_blank" href="%s">%s »</a>', esc_url('https://e2pdf.com/checkout/license/renew/' . get_option('e2pdf_license')), __('Renew License Key', 'e2pdf')); | |
| 80 | + } | |
| 81 | + array_unshift( | |
| 82 | + $this->notifications, | |
| 83 | + [ | |
| 84 | + 'type' => 'notice', | |
| 85 | + 'text' => $message, | |
| 86 | + ] | |
| 87 | + ); | |
| 55 | 88 | } |
| 56 | 89 | |
| 57 | 90 | if ($this->helper->get('license')->get('status') == 'expired') { |
| 58 | - array_unshift($notifications, array( | |
| 59 | - 'type' => 'error', | |
| 60 | - 'text' => sprintf(__("Your E2Pdf License Key has expired. Please renew it at <a target='_blank' href='%s'>%s</a>.", 'e2pdf'), self::UPGRADE_URL, self::UPGRADE_URL) | |
| 61 | - )); | |
| 91 | + $message = __('Your E2Pdf License Key has expired', 'e2pdf'); | |
| 92 | + if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { | |
| 93 | + $message .= ' | ' . sprintf('<a class="e2pdf-link" target="_blank" href="%s">%s »</a>', esc_url('https://e2pdf.com/checkout/license/renew/' . get_option('e2pdf_license')), __('Renew License Key', 'e2pdf')); | |
| 94 | + } | |
| 95 | + array_unshift( | |
| 96 | + $this->notifications, | |
| 97 | + [ | |
| 98 | + 'type' => 'error', | |
| 99 | + 'text' => $message, | |
| 100 | + ] | |
| 101 | + ); | |
| 62 | 102 | } |
| 63 | 103 | |
| 64 | - if ($this->helper->get('license')->get('type') == 'FREE') { | |
| 65 | - array_unshift($notifications, array( | |
| 66 | - 'type' => 'notice', | |
| 67 | - 'text' => sprintf(__("You are using FREE license type. Up to 1 page and up to 1 template allowed. Please check <a target='_blank' href='%s'>%s</a> for upgrade options.", 'e2pdf'), self::UPGRADE_URL, self::UPGRADE_URL) | |
| 68 | - )); | |
| 104 | + if ($this->helper->get('license')->get('type') == 'FREE' && $this->helper->get('page') == 'e2pdf-templates') { | |
| 105 | + array_unshift( | |
| 106 | + $this->notifications, | |
| 107 | + [ | |
| 108 | + 'type' => 'notice', | |
| 109 | + 'text' => sprintf(__('You are using E2Pdf FREE License Type', 'e2pdf') . ' | ' . __('Up to 1 Page and up to 1 Template allowed', 'e2pdf') . ' | <a class="e2pdf-link" target="_blank" href="%s">%s »</a>', esc_url('https://e2pdf.com/price'), __('Upgrade License Key', 'e2pdf')), | |
| 110 | + ] | |
| 111 | + ); | |
| 69 | 112 | } |
| 70 | 113 | } |
| 71 | 114 | |
| 72 | - set_transient('e2pdf_notifications', array()); | |
| 73 | - return $notifications; | |
| 115 | + if ($this->helper->get('license')->get('error')) { | |
| 116 | + foreach ($this->notifications as $key => $notify) { | |
| 117 | + if ($notify['type'] === 'error' && $notify['text'] === $this->helper->get('license')->get('error')) { | |
| 118 | + unset($this->notifications[$key]); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + if ($this->helper->get('license')->get('error') === 'License Key does not match this site. Please correct License Key to continue usage.') { | |
| 122 | + $message = __('E2Pdf License Key does not match this site', 'e2pdf'); | |
| 123 | + if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { | |
| 124 | + /* translators: 1: Support URL 2: Support URL */ | |
| 125 | + $message .= ' | ' . sprintf(__('Failed to Restore License Key. Contact Support at <a target="_blank" href="%1$s">%2$s</a>', 'e2pdf'), 'https://e2pdf.com/support/contact', 'https://e2pdf.com/support/contact'); | |
| 126 | + } | |
| 127 | + } else { | |
| 128 | + $message = $this->helper->get('license')->get('error'); | |
| 129 | + } | |
| 130 | + array_unshift( | |
| 131 | + $this->notifications, | |
| 132 | + [ | |
| 133 | + 'type' => 'error', | |
| 134 | + 'text' => $message, | |
| 135 | + ] | |
| 136 | + ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + return array_values($this->notifications); | |
| 74 | 140 | } |
| 75 | - | |
| 76 | 141 | } |