| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
use AcyMailing\Core\AcymObject; |
| 6 |
|
| 7 |
class HeaderHelper extends AcymObject |
| 8 |
{ |
| 9 |
public function display(array $breadcrumb): void |
| 10 |
{ |
| 11 |
$this->displayLastNews(); |
| 12 |
|
| 13 |
echo '<div id="acym_header" class="grid-x margin-bottom-1">'; |
| 14 |
echo '<div class="cell large-6 xlarge-7 xxlarge-8 grid-x acym_vcenter">'; |
| 15 |
$this->displayBreadcrumb($breadcrumb); |
| 16 |
echo '</div>'; |
| 17 |
|
| 18 |
echo '<div class="cell large-6 xlarge-5 xxlarge-4 grid-x">'; |
| 19 |
|
| 20 |
echo '<div id="checkVersionArea" class="cell auto grid-x align-right acym_vcenter">'; |
| 21 |
$this->displayVersionArea(); |
| 22 |
echo '</div>'; |
| 23 |
|
| 24 |
echo '<div class="cell shrink grid-x align-right acym_vcenter">'; |
| 25 |
$this->displayCheckVersionButton(); |
| 26 |
$this->displayHelpWedButton(); |
| 27 |
$this->displayDocumentationButton(); |
| 28 |
$this->displayNotificationCenter(); |
| 29 |
echo '</div>'; |
| 30 |
|
| 31 |
echo '</div>'; |
| 32 |
echo '</div>'; |
| 33 |
} |
| 34 |
|
| 35 |
public function displayVersionArea(bool $reloading = false): void |
| 36 |
{ |
| 37 |
$currentLevel = $this->config->get('level', ''); |
| 38 |
$currentVersion = $this->config->get('version', ''); |
| 39 |
$latestVersion = $this->config->get('latestversion', ''); |
| 40 |
|
| 41 |
echo '<div id="acym_level_version_area" class="text-right">'; |
| 42 |
echo '<div id="acym_level">'.esc_html(ACYM_NAME.' '.$currentLevel.' '); |
| 43 |
|
| 44 |
if (version_compare($currentVersion, $latestVersion, '>=')) { |
| 45 |
acym_tooltip( |
| 46 |
[ |
| 47 |
'hoveredText' => '<span class="acym__color__green">'.esc_html($currentVersion).'</span>', |
| 48 |
'textShownInTooltip' => acym_translation('ACYM_UP_TO_DATE'), |
| 49 |
] |
| 50 |
); |
| 51 |
} elseif (!empty($latestVersion)) { |
| 52 |
if ('wordpress' === ACYM_CMS) { |
| 53 |
$downloadLink = admin_url().'update-core.php'; |
| 54 |
} else { |
| 55 |
$downloadLink = ACYM_ACYMAILING_WEBSITE.'account/license/" target="_blank'; |
| 56 |
} |
| 57 |
acym_tooltip( |
| 58 |
[ |
| 59 |
'hoveredText' => '<span class="acy_updateversion acym__color__red">'.esc_html($currentVersion).'</span>', |
| 60 |
'textShownInTooltip' => acym_translationSprintf('ACYM_CLICK_UPDATE', $latestVersion), |
| 61 |
'titleShownInTooltip' => acym_translation('ACYM_OLD_VERSION'), |
| 62 |
'link' => $downloadLink, |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
echo '</div></div>'; |
| 68 |
|
| 69 |
$expirationDate = $this->config->get('expirationdate', 0); |
| 70 |
if ((empty($expirationDate) || $expirationDate == -1) && empty($this->config->get('acymailer_apikey', ''))) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
echo '<div id="acym_expiration" class="cell">'; |
| 75 |
if (acym_level(ACYM_ESSENTIAL) && ACYM_PRODUCTION) { |
| 76 |
if ($expirationDate == -2) { |
| 77 |
echo '<div class="acylicence_expired"> |
| 78 |
<a class="acy_attachlicence acymbuttons acym__color__red acym_link_license_tab" |
| 79 |
href="'.esc_url(acym_completeLink('configuration', false, false, true)).'">'.esc_html(acym_translation('ACYM_ATTACH_LICENCE')).'</a> |
| 80 |
</div>'; |
| 81 |
} elseif ($expirationDate < time()) { |
| 82 |
//TODO redirect to the subscription page directly instead of the subscriptions listing |
| 83 |
// We'll need to return the subscription id when calling the API |
| 84 |
acym_tooltip( |
| 85 |
[ |
| 86 |
'hoveredText' => '<span class="acy_subscriptionexpired acym__color__red">'.esc_html(acym_translation('ACYM_SUBSCRIPTION_EXPIRED')).'</span>', |
| 87 |
'textShownInTooltip' => acym_translation('ACYM_SUBSCRIPTION_EXPIRED_LINK'), |
| 88 |
'link' => ACYM_ACYMAILING_WEBSITE.'account/license/', |
| 89 |
] |
| 90 |
); |
| 91 |
} else { |
| 92 |
echo '<div class="acylicence_valid"> |
| 93 |
<span class="acy_subscriptionok acym__color__green">'.esc_html( |
| 94 |
acym_translationSprintf( |
| 95 |
'ACYM_VALID_UNTIL', |
| 96 |
acym_getDate($expirationDate, acym_translation('ACYM_DATE_FORMAT_LC4')) |
| 97 |
) |
| 98 |
).'</span> |
| 99 |
</div>'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$creditRemainingSendingMethod = ''; |
| 104 |
acym_trigger('onAcymGetCreditRemainingSendingMethod', [&$creditRemainingSendingMethod, $reloading]); |
| 105 |
if (!empty($creditRemainingSendingMethod)) { |
| 106 |
echo '<div class="acy_sending_method_credits"> |
| 107 |
<span>'.wp_kses($creditRemainingSendingMethod, [ |
| 108 |
'span' => [ |
| 109 |
'class' => true, |
| 110 |
], |
| 111 |
'a' => [ |
| 112 |
'href' => true, |
| 113 |
'class' => true, |
| 114 |
'title' => true, |
| 115 |
'target' => true, |
| 116 |
], |
| 117 |
]).'</span> |
| 118 |
</div>'; |
| 119 |
} |
| 120 |
|
| 121 |
echo '</div>'; |
| 122 |
} |
| 123 |
|
| 124 |
public function displayNotificationCenter(): void |
| 125 |
{ |
| 126 |
$notifications = json_decode($this->config->get('notifications', '{}'), true); |
| 127 |
if (!is_array($notifications) || empty($notifications)) { |
| 128 |
$notifications = []; |
| 129 |
} |
| 130 |
$message = ''; |
| 131 |
//if we have a notifications for success |
| 132 |
$notificationLevel = 0; |
| 133 |
$successMessage = acym_getVar('string', 'acym_success', '', 'SESSION'); |
| 134 |
if (!empty($successMessage)) { |
| 135 |
$message = $successMessage; |
| 136 |
acym_setSession('acym_success', ''); |
| 137 |
$notificationLevel = 1; |
| 138 |
} |
| 139 |
|
| 140 |
if (!empty($notifications)) { |
| 141 |
foreach ($notifications as $notification) { |
| 142 |
if ($notification['read']) continue; |
| 143 |
if ($notification['level'] === 'info' && $notificationLevel < 2) $notificationLevel = 2; |
| 144 |
if ($notification['level'] === 'warning' && $notificationLevel < 3) $notificationLevel = 3; |
| 145 |
if ($notification['level'] === 'error' && $notificationLevel < 4) $notificationLevel = 4; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$iconToDisplay = ''; |
| 150 |
$tooltip = false; |
| 151 |
|
| 152 |
switch ($notificationLevel) { |
| 153 |
case 0: |
| 154 |
// default icon |
| 155 |
$iconToDisplay = 'acymicon-bell'; |
| 156 |
$notificationLevel = ''; |
| 157 |
break; |
| 158 |
case 1: |
| 159 |
// success |
| 160 |
$iconToDisplay = 'acymicon-check-circle acym__color__green'; |
| 161 |
$notificationLevel = 'acym__header__notification__button__success acym__header__notification__pulse'; |
| 162 |
$tooltip = true; |
| 163 |
break; |
| 164 |
case 2: |
| 165 |
// info |
| 166 |
$iconToDisplay = 'acymicon-bell acym__color__blue'; |
| 167 |
$notificationLevel = 'acym__header__notification__button__info'; |
| 168 |
break; |
| 169 |
case 3: |
| 170 |
// warning |
| 171 |
$iconToDisplay = 'acymicon-exclamation-triangle acym__color__orange'; |
| 172 |
$notificationLevel = 'acym__header__notification__button__warning'; |
| 173 |
break; |
| 174 |
case 4: |
| 175 |
// error |
| 176 |
$iconToDisplay = 'acymicon-exclamation-circle acym__color__red'; |
| 177 |
$notificationLevel = 'acym__header__notification__button__error'; |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
echo '<div class="cell grid-x align-center acym_vcenter medium-shrink acym__header__notification '.esc_attr($notificationLevel).' button_header cursor-pointer"'; |
| 182 |
if ($tooltip) { |
| 183 |
echo ' data-acym-tooltip="'.esc_attr($message).'" data-acym-tooltip-position="left"'; |
| 184 |
} |
| 185 |
echo '>'; |
| 186 |
echo '<i class="'.esc_attr($iconToDisplay).'"></i>'; |
| 187 |
echo '<div class="cell grid-x acym__header__notification__center align-center">'; |
| 188 |
$this->displayNotificationCenterInner($notifications); |
| 189 |
echo '</div>'; |
| 190 |
echo '</div>'; |
| 191 |
} |
| 192 |
|
| 193 |
public function displayNotificationCenterInner(array $notifications): void |
| 194 |
{ |
| 195 |
if (empty($notifications)) { |
| 196 |
echo '<div class="cell grid-x acym__header__notification__one acym__header__notification__one__empty acym_vcenter">'; |
| 197 |
echo '<h2 class="cell text-center">'.esc_html(acym_translation('ACYM_YOU_DONT_HAVE_NOTIFICATIONS')).'</h2>'; |
| 198 |
echo '</div>'; |
| 199 |
|
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
echo '<div class="cell grid-x acym__header__notification__toolbox"><p class="cell auto">'; |
| 204 |
echo esc_html(acym_translation('ACYM_NOTIFICATIONS')); |
| 205 |
echo '</p><div class="cell shrink cursor-pointer acym__header__notification__toolbox__remove text-right" data-id="all">'; |
| 206 |
echo esc_html(acym_translation('ACYM_DELETE_ALL')); |
| 207 |
echo '</div></div>'; |
| 208 |
|
| 209 |
foreach ($notifications as $key => $notif) { |
| 210 |
$fullMessageHover = $notif['message']; |
| 211 |
|
| 212 |
if (strlen($notif['message']) > 150) { |
| 213 |
$tag = new \stdClass(); |
| 214 |
$tag->wrap = 150; |
| 215 |
|
| 216 |
$pluginHelper = new PluginHelper(); |
| 217 |
$notif['message'] = $pluginHelper->wrapText($notif['message'], $tag); |
| 218 |
} |
| 219 |
|
| 220 |
$logo = $notif['level'] === 'info' ? 'acymicon-bell' : ($notif['level'] === 'warning' ? 'acymicon-exclamation-triangle' : 'acymicon-exclamation-circle'); |
| 221 |
$read = $notif['read'] ? 'acym__header__notification__one__read' : ''; |
| 222 |
echo '<div class="'.esc_attr($read).' cell grid-x acym__header__notification__one acym_vcenter acym_vcenter acym__header__notification__one__'.esc_attr( |
| 223 |
$notif['level'] |
| 224 |
).'">'; |
| 225 |
echo '<div class="cell small-3 align-center grid-x acym__header__notification__one__icon"><i class="cell '.esc_attr($logo).'"></i></div>'; |
| 226 |
echo '<div class="cell grid-x small-8">'; |
| 227 |
echo '<p class="cell acym__header__notification__message"'; |
| 228 |
if ($fullMessageHover !== $notif['message']) { |
| 229 |
echo ' data-acym-full="'.esc_attr($fullMessageHover).'"'; |
| 230 |
} |
| 231 |
echo '>'; |
| 232 |
echo $notif['message']; |
| 233 |
echo '<div class="cell acym__header__notification__one__date">'.esc_html(acym_date($notif['date'])).'</div></div>'; |
| 234 |
echo '<i class="cell small-1 acym__header__notification__one__delete acymicon-close" data-id="'.esc_attr($key).'"></i>'; |
| 235 |
echo '</div>'; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
public function addNotification(object $notif): string |
| 240 |
{ |
| 241 |
if ($notif->level === 'success') { |
| 242 |
acym_setSession('acym_success', $notif->message); |
| 243 |
|
| 244 |
return ''; |
| 245 |
} |
| 246 |
|
| 247 |
$notifications = json_decode($this->config->get('notifications', '[]'), true); |
| 248 |
if (!is_array($notifications)) { |
| 249 |
$notifications = []; |
| 250 |
} |
| 251 |
|
| 252 |
$notif->message = str_replace('<br />', "\r\n", $notif->message); |
| 253 |
$notif->message = acym_stripTags($notif->message, '<a>'); |
| 254 |
|
| 255 |
// Prevent duplicated notifications |
| 256 |
foreach ($notifications as $key => $oneNotif) { |
| 257 |
if ($oneNotif['message'] === $notif->message && $oneNotif['level'] === $notif->level) unset($notifications[$key]); |
| 258 |
} |
| 259 |
$notifications = array_values($notifications); |
| 260 |
|
| 261 |
$notif->id = uniqid(); |
| 262 |
array_unshift($notifications, $notif); |
| 263 |
|
| 264 |
if (count($notifications) > 10) unset($notifications[10]); |
| 265 |
|
| 266 |
$this->config->saveConfig(['notifications' => json_encode($notifications)]); |
| 267 |
|
| 268 |
return $notif->id; |
| 269 |
} |
| 270 |
|
| 271 |
private function displayLastNews(): void |
| 272 |
{ |
| 273 |
$lastNewsCheck = $this->config->get('last_news_check', 0); |
| 274 |
if ($lastNewsCheck < time() - 7200) { |
| 275 |
$context = stream_context_create(['http' => ['timeout' => 1]]); |
| 276 |
$news = @file_get_contents(ACYM_ACYMAILING_WEBSITE.'acymnews.xml', false, $context); |
| 277 |
$this->config->saveConfig( |
| 278 |
[ |
| 279 |
'last_news_check' => time(), |
| 280 |
'last_news' => base64_encode($news), |
| 281 |
], |
| 282 |
false |
| 283 |
); |
| 284 |
} else { |
| 285 |
$news = $this->config->get('last_news', ''); |
| 286 |
if (!empty($news)) $news = base64_decode($news); |
| 287 |
} |
| 288 |
if (empty($news)) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
$news = @simplexml_load_string($news); |
| 293 |
if (empty($news->news)) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
$currentLanguage = acym_getLanguageTag(); |
| 298 |
$latestNews = null; |
| 299 |
$doNotRemind = json_decode($this->config->get('remindme', '[]')); |
| 300 |
|
| 301 |
foreach ($news->news as $oneNews) { |
| 302 |
// If we found a news more recent, it means we found the latest available one |
| 303 |
if (!empty($latestNews) && strtotime($latestNews->date) > strtotime($oneNews->date)) break; |
| 304 |
|
| 305 |
// If the news isn't published or that the language isn't correct, leave |
| 306 |
$language = strtolower($oneNews->language); |
| 307 |
if (empty($oneNews->published) || ($language != strtolower($currentLanguage) && ($language != 'default' || !empty($latestNews)))) { |
| 308 |
continue; |
| 309 |
} |
| 310 |
|
| 311 |
// If the extension isn't correct, leave |
| 312 |
if (!empty($oneNews->extension) && strtolower($oneNews->extension) !== 'acymailing') continue; |
| 313 |
|
| 314 |
// If the CMS isn't correct, leave |
| 315 |
if (!empty($oneNews->cms) && strtolower($oneNews->cms) != strtolower('WordPress')) continue; |
| 316 |
|
| 317 |
// If the level of the extension isn't correct, leave |
| 318 |
if (!empty($oneNews->level) && strtolower($oneNews->level) != strtolower($this->config->get('level'))) continue; |
| 319 |
|
| 320 |
// If the version of the extension isn't correct, leave |
| 321 |
if (!empty($oneNews->version)) { |
| 322 |
[$version, $operator] = explode('_', $oneNews->version); |
| 323 |
if (!version_compare($this->config->get('version'), $version, $operator)) continue; |
| 324 |
} |
| 325 |
|
| 326 |
if (!empty($oneNews->mailermethod)) { |
| 327 |
$sendMethod = $this->config->get('mailer_method', ''); |
| 328 |
if ($oneNews->mailermethod !== $sendMethod) continue; |
| 329 |
} |
| 330 |
|
| 331 |
if (in_array($oneNews->name, $doNotRemind)) continue; |
| 332 |
|
| 333 |
$latestNews = $oneNews; |
| 334 |
} |
| 335 |
|
| 336 |
if (empty($latestNews)) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
echo '<div id="acym__header__banner__news" data-news="'.esc_attr($latestNews->name).'">'; |
| 341 |
echo wp_kses( |
| 342 |
$latestNews->content, |
| 343 |
[ |
| 344 |
'style' => [ |
| 345 |
'type' => true, |
| 346 |
], |
| 347 |
'div' => [ |
| 348 |
'class' => true, |
| 349 |
'title' => true, |
| 350 |
], |
| 351 |
'b' => [], |
| 352 |
'a' => [ |
| 353 |
'href' => true, |
| 354 |
'target' => true, |
| 355 |
], |
| 356 |
] |
| 357 |
); |
| 358 |
echo '</div>'; |
| 359 |
} |
| 360 |
|
| 361 |
private function displayBreadcrumb(array $breadcrumb): void |
| 362 |
{ |
| 363 |
echo '<i class="cell shrink acym-logo"></i>'; |
| 364 |
echo '<div id="acym_global_navigation" class="cell auto"> |
| 365 |
<nav aria-label="You are here:" role="navigation"> |
| 366 |
<ul class="breadcrumbs grid-x">'; |
| 367 |
$i = 0; |
| 368 |
foreach ($breadcrumb as $oneLevel => $link) { |
| 369 |
if ($i !== 0) { |
| 370 |
echo '<li class="breadcrumbs__separator"><i class="acymicon-keyboard-arrow-right"></i></li>'; |
| 371 |
} |
| 372 |
|
| 373 |
if (count($breadcrumb) > 1 && $i === count($breadcrumb) - 1) { |
| 374 |
echo '<li class="last_link cell shrink"><span class="show-for-sr">Current: </span>'; |
| 375 |
} else { |
| 376 |
echo '<li>'; |
| 377 |
} |
| 378 |
|
| 379 |
if (!empty($link)) { |
| 380 |
echo '<a href="'.esc_url($link).'">'; |
| 381 |
} |
| 382 |
echo esc_html($oneLevel); |
| 383 |
if (!empty($link)) { |
| 384 |
echo '</a>'; |
| 385 |
} |
| 386 |
echo '</li>'; |
| 387 |
|
| 388 |
$i++; |
| 389 |
} |
| 390 |
echo '</ul> |
| 391 |
</nav> |
| 392 |
</div>'; |
| 393 |
} |
| 394 |
|
| 395 |
private function displayCheckVersionButton(): void |
| 396 |
{ |
| 397 |
if (ACYM_CMS == 'wordpress' && !acym_level(ACYM_ESSENTIAL)) { |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
$lastLicenseCheck = $this->config->get('lastlicensecheck', 0); |
| 402 |
if (empty($lastLicenseCheck)) { |
| 403 |
$lastLicenseCheck = time(); |
| 404 |
} |
| 405 |
|
| 406 |
$lastDateFormatted = acym_date($lastLicenseCheck, 'ACYM_DATE_FORMAT_LC2'); |
| 407 |
|
| 408 |
acym_tooltip( |
| 409 |
[ |
| 410 |
'hoveredText' => '<a id="checkVersionButton" type="button" class="grid-x align-center button_header medium-shrink acym_vcenter"><i class="cell shrink acymicon-autorenew"></i></a>', |
| 411 |
'textShownInTooltip' => esc_html(acym_translation('ACYM_LAST_CHECK')).' <span id="acym__check__version__last__check">'.esc_html( |
| 412 |
$lastDateFormatted |
| 413 |
).'</span>', |
| 414 |
] |
| 415 |
); |
| 416 |
} |
| 417 |
|
| 418 |
private function displayHelpWedButton(): void |
| 419 |
{ |
| 420 |
if (ACYM_CMS !== 'wordpress' || acym_level(ACYM_ESSENTIAL)) { |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
echo '<a type="button" class="grid-x align-center button_header medium-shrink acym_vcenter" target="_blank" href="https://wordpress.org/support/plugin/acymailing/"> |
| 425 |
<i class="cell shrink acymicon-wordpress"></i> |
| 426 |
</a>'; |
| 427 |
} |
| 428 |
|
| 429 |
private function displayDocumentationButton(): void |
| 430 |
{ |
| 431 |
acym_tooltip( |
| 432 |
[ |
| 433 |
'hoveredText' => '<a type="button" class="grid-x align-center button_header medium-shrink acym_vcenter" target="_blank" href="'.esc_url( |
| 434 |
ACYM_DOCUMENTATION |
| 435 |
).'"><i class="cell shrink acymicon-book"></i></a>', |
| 436 |
'textShownInTooltip' => acym_translation('ACYM_DOCUMENTATION'), |
| 437 |
] |
| 438 |
); |
| 439 |
} |
| 440 |
} |
| 441 |
|