| @@ -12,40 +12,75 @@ | ||
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | class Model_E2pdf_Notification extends Model_E2pdf_Model { |
| 15 | 15 | |
| 16 | + private static $instance = null; | |
| 17 | + private $notifications = []; | |
| 18 | + | |
| 19 | + public static function instance() { | |
| 20 | + if (self::$instance === null) { | |
| 21 | + self::$instance = new self(); | |
| 22 | + } | |
| 23 | + return self::$instance; | |
| 24 | + } | |
| 25 | + | |
| 26 | + // load | |
| 27 | + public function load() { | |
| 28 | + $notifications = get_transient('e2pdf_notifications'); | |
| 29 | + return (is_array($notifications) && !empty($notifications)) ? $notifications : []; | |
| 30 | + } | |
| 31 | + | |
| 16 | 32 | // add notification |
| 17 | 33 | public function add_notification($type, $text) { |
| 18 | 34 | |
| 19 | - $notifications = get_transient('e2pdf_notifications'); | |
| 20 | - if (!is_array($notifications)) { | |
| 21 | - $notifications = []; | |
| 22 | - } | |
| 35 | + $notification_id = (int) round(microtime(true) * 1000) . '-' . bin2hex(random_bytes(3)); | |
| 36 | + $notifications = $this->load(); | |
| 23 | 37 | |
| 24 | - $notifications[] = [ | |
| 38 | + $notifications[$notification_id] = [ | |
| 25 | 39 | 'type' => $type, |
| 26 | 40 | 'text' => $text, |
| 41 | + 'expires' => time() + 10, | |
| 27 | 42 | ]; |
| 28 | - set_transient('e2pdf_notifications', $notifications); | |
| 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; | |
| 29 | 50 | } |
| 30 | 51 | |
| 31 | 52 | // get notifications |
| 32 | - public function get_notifications() { | |
| 53 | + public function get_notifications($notification_id = '') { | |
| 33 | 54 | |
| 34 | - $notifications = get_transient('e2pdf_notifications'); | |
| 35 | - if (!is_array($notifications)) { | |
| 36 | - $notifications = []; | |
| 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]); | |
| 37 | 64 | } |
| 38 | 65 | |
| 66 | + foreach ($notifications as $id => $entry) { | |
| 67 | + if (isset($this->notifications[$id]) || $now >= $entry['expires']) { | |
| 68 | + unset($notifications[$id]); | |
| 69 | + } | |
| 70 | + } | |
| 71 | + | |
| 72 | + set_transient('e2pdf_notifications', $notifications, DAY_IN_SECONDS); | |
| 73 | + | |
| 39 | 74 | if (!get_option('e2pdf_hide_warnings', '0')) { |
| 40 | 75 | if ($this->helper->get('license')->get('status') == 'pre_expired') { |
| 41 | 76 | /* translators: %s: License Key Expire Date */ |
| 42 | 77 | $message = sprintf(__('Your E2Pdf License Key will expire at <strong>%s</strong>', 'e2pdf'), $this->helper->get('license')->get('expire')); |
| 43 | 78 | if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { |
| 44 | - $message .= ' | ' . sprintf('<a class="e2pdf-link" target="_blank" href="%s">%s »</a>', 'https://e2pdf.com/checkout/license/renew/' . get_option('e2pdf_license'), __('Renew License Key', 'e2pdf')); | |
| 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')); | |
| 45 | 80 | } |
| 46 | 81 | array_unshift( |
| 47 | - $notifications, | |
| 82 | + $this->notifications, | |
| 48 | 83 | [ |
| 49 | 84 | 'type' => 'notice', |
| 50 | 85 | 'text' => $message, |
| 51 | 86 | ] |
| @@ -54,12 +89,12 @@ | ||
| 54 | 89 | |
| 55 | 90 | if ($this->helper->get('license')->get('status') == 'expired') { |
| 56 | 91 | $message = __('Your E2Pdf License Key has expired', 'e2pdf'); |
| 57 | 92 | if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { |
| 58 | - $message .= ' | ' . sprintf('<a class="e2pdf-link" target="_blank" href="%s">%s »</a>', 'https://e2pdf.com/checkout/license/renew/' . get_option('e2pdf_license'), __('Renew License Key', 'e2pdf')); | |
| 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')); | |
| 59 | 94 | } |
| 60 | 95 | array_unshift( |
| 61 | - $notifications, | |
| 96 | + $this->notifications, | |
| 62 | 97 | [ |
| 63 | 98 | 'type' => 'error', |
| 64 | 99 | 'text' => $message, |
| 65 | 100 | ] |
| @@ -67,12 +102,12 @@ | ||
| 67 | 102 | } |
| 68 | 103 | |
| 69 | 104 | if ($this->helper->get('license')->get('type') == 'FREE' && $this->helper->get('page') == 'e2pdf-templates') { |
| 70 | 105 | array_unshift( |
| 71 | - $notifications, | |
| 106 | + $this->notifications, | |
| 72 | 107 | [ |
| 73 | 108 | 'type' => 'notice', |
| 74 | - '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>', 'https://e2pdf.com/price', __('Upgrade License Key', 'e2pdf')), | |
| 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')), | |
| 75 | 110 | ] |
| 76 | 111 | ); |
| 77 | 112 | } |
| 78 | 113 | } |
| @@ -77,23 +112,24 @@ | ||
| 77 | 112 | } |
| 78 | 113 | } |
| 79 | 114 | |
| 80 | 115 | if ($this->helper->get('license')->get('error')) { |
| 81 | - foreach ($notifications as $key => $notify) { | |
| 116 | + foreach ($this->notifications as $key => $notify) { | |
| 82 | 117 | if ($notify['type'] === 'error' && $notify['text'] === $this->helper->get('license')->get('error')) { |
| 83 | - unset($notifications[$key]); | |
| 118 | + unset($this->notifications[$key]); | |
| 84 | 119 | } |
| 85 | 120 | } |
| 86 | 121 | if ($this->helper->get('license')->get('error') === 'License Key does not match this site. Please correct License Key to continue usage.') { |
| 87 | 122 | $message = __('E2Pdf License Key does not match this site', 'e2pdf'); |
| 88 | 123 | if (current_user_can('manage_options') || current_user_can('e2pdf_license')) { |
| 89 | - $message .= ' | ' . sprintf('<a class="e2pdf-link" id="e2pdf-restore-license-key" href="javascript:void(0)" _wpnonce="%s">%s »</a>', wp_create_nonce('e2pdf_license'), __('Restore License Key', 'e2pdf')); | |
| 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'); | |
| 90 | 126 | } |
| 91 | 127 | } else { |
| 92 | 128 | $message = $this->helper->get('license')->get('error'); |
| 93 | 129 | } |
| 94 | 130 | array_unshift( |
| 95 | - $notifications, | |
| 131 | + $this->notifications, | |
| 96 | 132 | [ |
| 97 | 133 | 'type' => 'error', |
| 98 | 134 | 'text' => $message, |
| 99 | 135 | ] |
| @@ -99,8 +135,7 @@ | ||
| 99 | 135 | ] |
| 100 | 136 | ); |
| 101 | 137 | } |
| 102 | 138 | |
| 103 | - set_transient('e2pdf_notifications', []); | |
| 104 | - return $notifications; | |
| 139 | + return array_values($this->notifications); | |
| 105 | 140 | } |
| 106 | 141 | } |