| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class ABJ_404_Solution_WPNotices { |
| 9 |
|
| 10 |
/** @var array<ABJ_404_Solution_WPNotice> */ |
| 11 |
private static $adminNotices = array(); |
| 12 |
|
| 13 |
/** |
| 14 |
* Test seam: clear the accumulated admin-notice queue without private-field |
| 15 |
* reflection. Resets to the empty-array default (M105 singleton-reset seam). |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function resetForTests(): void { |
| 20 |
self::$adminNotices = array(); |
| 21 |
} |
| 22 |
|
| 23 |
/** Display a message with the specified importance level. |
| 24 |
* @param string $noticeLevel see ABJ_404_Solution_WPNotice for notice levels. |
| 25 |
* @param string $message |
| 26 |
*/ |
| 27 |
/** |
| 28 |
* @param string $noticeLevel |
| 29 |
* @param string $message |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function registerAdminMessage(string $noticeLevel, string $message): void { |
| 33 |
$notice = new ABJ_404_Solution_WPNotice($noticeLevel, $message); |
| 34 |
|
| 35 |
self::registerAdminNotice($notice); |
| 36 |
} |
| 37 |
|
| 38 |
/** Display a message with the specified importance level. |
| 39 |
* @param ABJ_404_Solution_WPNotice $adminNotice |
| 40 |
*/ |
| 41 |
/** |
| 42 |
* @param ABJ_404_Solution_WPNotice $adminNotice |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public static function registerAdminNotice(ABJ_404_Solution_WPNotice $adminNotice): void { |
| 46 |
self::$adminNotices[] = $adminNotice; |
| 47 |
|
| 48 |
self::$adminNotices = array_unique(self::$adminNotices, SORT_REGULAR); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return string the messages to display. |
| 53 |
*/ |
| 54 |
static function echoAdminNotices() { |
| 55 |
$f = abj_service('functions'); |
| 56 |
|
| 57 |
$allHTML = ''; |
| 58 |
if (!abj_service('admin_access_policy')->isPluginAdmin()) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
foreach (self::$adminNotices as $oneNotice) { |
| 63 |
$html = ABJ_404_Solution_FileSystemService::readFileContents(ABJ404_PATH . "/includes/html/notice.html"); |
| 64 |
$html = $f->str_replace('{class}', 'notice is-dismissable is-dismissible ' . $oneNotice->getType(), $html); |
| 65 |
$msg = $oneNotice->getMessage(); |
| 66 |
$html = $f->str_replace('{message}', esc_html(is_string($msg) ? $msg : (is_scalar($msg) ? (string)$msg : '')), $html); |
| 67 |
|
| 68 |
$allHTML .= $html; |
| 69 |
} |
| 70 |
|
| 71 |
echo $allHTML; |
| 72 |
|
| 73 |
return $allHTML; |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|