| 1 |
<?php |
| 2 |
|
| 3 |
namespace WordProof\SDK\Controllers; |
| 4 |
|
| 5 |
use WordProof\SDK\Helpers\ClassicNoticeHelper; |
| 6 |
use WordProof\SDK\Helpers\TransientHelper; |
| 7 |
use WordProof\SDK\Translations\TranslationsInterface; |
| 8 |
|
| 9 |
class NoticeController |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var string[] The screens on which notices should be rendered. |
| 13 |
*/ |
| 14 |
private $screens = ['post']; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var TranslationsInterface The translations objects, |
| 18 |
*/ |
| 19 |
private $translations; |
| 20 |
|
| 21 |
public function __construct(TranslationsInterface $translations) |
| 22 |
{ |
| 23 |
$this->translations = $translations; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Showing notices for the classic editor and delete them so they are only shown once. |
| 28 |
* |
| 29 |
* @action admin_notices |
| 30 |
*/ |
| 31 |
public function show() |
| 32 |
{ |
| 33 |
$screen = get_current_screen(); |
| 34 |
|
| 35 |
if (!in_array($screen->base, $this->screens, true)) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$notice = TransientHelper::getOnce(ClassicNoticeHelper::$transientKey); |
| 40 |
|
| 41 |
if (!isset($notice) || !$notice) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
switch ($notice) { |
| 46 |
case 'no_balance': |
| 47 |
$type = 'error'; |
| 48 |
$message = $this->translations->getNoBalanceNotice(); |
| 49 |
$buttonText = $this->translations->getOpenSettingsButtonText(); |
| 50 |
$buttonEventName = 'wordproof:open_settings'; |
| 51 |
break; |
| 52 |
case 'timestamp_success': |
| 53 |
$type = 'success'; |
| 54 |
$message = $this->translations->getTimestampSuccessNotice(); |
| 55 |
break; |
| 56 |
case 'timestamp_failed': |
| 57 |
$type = 'error'; |
| 58 |
$message = $this->translations->getTimestampFailedNotice(); |
| 59 |
break; |
| 60 |
case 'not_authenticated': |
| 61 |
$type = 'error'; |
| 62 |
$message = $this->translations->getNotAuthenticatedNotice(); |
| 63 |
$buttonText = $this->translations->getOpenAuthenticationButtonText(); |
| 64 |
$buttonEventName = 'wordproof:open_authentication'; |
| 65 |
break; |
| 66 |
default: |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
if (isset($message) && isset($type)) { |
| 71 |
$noticeClass = 'notice-' . $type; |
| 72 |
echo \sprintf( |
| 73 |
'<div class="notice %1$s is-dismissible"><p>%2$s</p>', |
| 74 |
esc_attr($noticeClass), |
| 75 |
esc_html($message) |
| 76 |
); |
| 77 |
|
| 78 |
if (isset($buttonText) && isset($buttonEventName)) { |
| 79 |
echo \sprintf( |
| 80 |
'<button class="button button-primary" onclick="window.dispatchEvent( new window.CustomEvent( \'%2$s\' ) )">%1$s</button>', |
| 81 |
esc_html($buttonText), |
| 82 |
esc_attr($buttonEventName) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
echo '</div>'; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|