AutocompletePostListLoader.php
2 months ago
DateTime.php
1 year ago
Emoji.php
2 months ago
Functions.php
1 week ago
Notice.php
3 years ago
Posts.php
3 years ago
Readme.php
3 years ago
index.php
3 years ago
Notice.php
91 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\WP; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class Notice { |
| 11 | |
| 12 | const TYPE_ERROR = 'error'; |
| 13 | const TYPE_WARNING = 'warning'; |
| 14 | const TYPE_SUCCESS = 'success'; |
| 15 | const TYPE_INFO = 'info'; |
| 16 | |
| 17 | private $type; |
| 18 | private $message; |
| 19 | private $classes; |
| 20 | private $dataNoticeName; |
| 21 | private $renderInParagraph; |
| 22 | |
| 23 | public function __construct( |
| 24 | $type, |
| 25 | $message, |
| 26 | $classes = '', |
| 27 | $dataNoticeName = '', |
| 28 | $renderInParagraph = true |
| 29 | ) { |
| 30 | $this->type = $type; |
| 31 | $this->message = $message; |
| 32 | $this->classes = $classes; |
| 33 | $this->dataNoticeName = $dataNoticeName; |
| 34 | $this->renderInParagraph = $renderInParagraph; |
| 35 | } |
| 36 | |
| 37 | public function getMessage() { |
| 38 | return $this->message; |
| 39 | } |
| 40 | |
| 41 | public static function displayError($message, $classes = '', $dataNoticeName = '', $renderInParagraph = true, $showErrorTitle = true) { |
| 42 | if ($showErrorTitle) { |
| 43 | $message = sprintf( |
| 44 | "<b>%s </b> %s", |
| 45 | __('MailPoet Error:', 'mailpoet'), |
| 46 | $message |
| 47 | ); |
| 48 | } |
| 49 | return self::createNotice(self::TYPE_ERROR, $message, $classes, $dataNoticeName, $renderInParagraph); |
| 50 | } |
| 51 | |
| 52 | public static function displayWarning($message, $classes = '', $dataNoticeName = '', $renderInParagraph = true) { |
| 53 | return self::createNotice(self::TYPE_WARNING, $message, $classes, $dataNoticeName, $renderInParagraph); |
| 54 | } |
| 55 | |
| 56 | public static function displaySuccess($message, $classes = '', $dataNoticeName = '', $renderInParagraph = true) { |
| 57 | return self::createNotice(self::TYPE_SUCCESS, $message, $classes, $dataNoticeName, $renderInParagraph); |
| 58 | } |
| 59 | |
| 60 | public static function displayInfo($message, $classes = '', $dataNoticeName = '', $renderInParagraph = true) { |
| 61 | return self::createNotice(self::TYPE_INFO, $message, $classes, $dataNoticeName, $renderInParagraph); |
| 62 | } |
| 63 | |
| 64 | protected static function createNotice($type, $message, $classes, $dataNoticeName, $renderInParagraph) { |
| 65 | $notice = new Notice($type, $message, $classes, $dataNoticeName, $renderInParagraph); |
| 66 | WPFunctions::get()->addAction('admin_notices', [$notice, 'displayWPNotice']); |
| 67 | return $notice; |
| 68 | } |
| 69 | |
| 70 | public function displayWPNotice() { |
| 71 | $class = sprintf('notice notice-%s mailpoet_notice_server %s', $this->type, $this->classes); |
| 72 | $message = nl2br($this->message); |
| 73 | |
| 74 | if ($this->renderInParagraph) { |
| 75 | printf( |
| 76 | '<div class="%1$s" %3$s><p>%2$s</p></div>', |
| 77 | esc_attr($class), |
| 78 | wp_kses_post($message), |
| 79 | !empty($this->dataNoticeName) ? sprintf('data-notice="%s"', esc_attr($this->dataNoticeName)) : '' |
| 80 | ); |
| 81 | } else { |
| 82 | printf( |
| 83 | '<div class="%1$s" %3$s>%2$s</div>', |
| 84 | esc_attr($class), |
| 85 | wp_kses_post($message), |
| 86 | !empty($this->dataNoticeName) ? sprintf('data-notice="%s"', esc_attr($this->dataNoticeName)) : '' |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |