Skip to content

Commit

Permalink
Use wp_admin_notice function in WP 6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
chesio committed Oct 19, 2023
1 parent 22da218 commit 538e209
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 17 additions & 10 deletions classes/BlueChip/Security/Helpers/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
*/
abstract class AdminNotices
{
public const ERROR = 'notice-error';
public const WARNING = 'notice-warning';
public const SUCCESS = 'notice-success';
public const INFO = 'notice-info';
public const ERROR = 'error';
public const WARNING = 'warning';
public const SUCCESS = 'success';
public const INFO = 'info';

/**
* Add dismissible admin notice with given $message of given $type.
*
* @link https://make.wordpress.org/core/2023/10/16/introducing-admin-notice-functions-in-wordpress-6-4/
* @link https://make.wordpress.org/core/2015/04/23/spinners-and-dismissible-admin-notices-in-4-2/
*
* @param string $message Message to display in admin notice.
Expand All @@ -26,11 +27,17 @@ abstract class AdminNotices
*/
public static function add(string $message, string $type = self::INFO, bool $is_dismissible = true, bool $escape_html = true): void
{
$classes = \implode(' ', \array_filter(['notice', $type, $is_dismissible ? 'is-dismissible' : '']));
add_action('admin_notices', function () use ($message, $classes, $escape_html) {
echo '<div class="' . $classes . '">';
echo '<p>' . ($escape_html ? esc_html($message) : $message) . '</p>';
echo '</div>';
});
if (is_wp_version_compatible('6.4')) {
add_action('admin_notices', function () use ($message, $type, $is_dismissible) {
wp_admin_notice($message, ['type' => $type, 'dismissible' => $is_dismissible,]);
});
} else {
$classes = \implode(' ', \array_filter(['notice', 'notice-' . $type, $is_dismissible ? 'is-dismissible' : '']));
add_action('admin_notices', function () use ($message, $classes, $escape_html) {
echo '<div class="' . $classes . '">';
echo '<p>' . ($escape_html ? esc_html($message) : $message) . '</p>';
echo '</div>';
});
}
}
}
3 changes: 3 additions & 0 deletions tests/static-analysis/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
define('AUTH_COOKIE', '');
define('SECURE_AUTH_COOKIE', '');
define('LOGGED_IN_COOKIE', '');

// Upcoming in WordPress 6.4:
function wp_admin_notice (string $message, array $args): void {}

0 comments on commit 538e209

Please sign in to comment.