'info', // info, success, warning, error
'location' => 'dashboard', // dashboard, wp_notice, admin_bar, reactive
'link' => null,
'dismissible' => true,
'context' => 'adpresso', // 'all', 'adpresso', 'page_slug'
// A notice describes the site, so it is global unless a caller addresses one user; dismissals are per user.
'user_id' => null,
];
$data = wp_parse_args( $args, $defaults );
$data['slug'] = $slug;
$data['message'] = $message;
switch ( $data['location'] ) {
case 'admin_bar':
$this->admin_bar_notices[ $slug ] = $data;
break;
case 'reactive':
$this->reactive_notices[ $slug ] = $data;
break;
case 'wp_notice':
case 'dashboard':
default:
Notification_Center::get()->register( $data );
break;
}
}
/**
* Gathers and renders the default WP admin notices.
*
* @return void
*/
public function render_wp_notices(): void {
$user_id = get_current_user_id();
$wp_notices = Notification_Center::get()->get_active_notices_by_location( 'wp_notice', $user_id );
$dash_notices = Notification_Center::get()->get_active_notices_by_location( 'dashboard', $user_id );
$all_wp_notices = array_merge( $wp_notices, $dash_notices );
if ( empty( $all_wp_notices ) ) {
return;
}
$current_screen = get_current_screen();
// The Notifications page lists every notice itself.
if ( $current_screen && '' !== $this->page_hook && $this->page_hook === $current_screen->id ) {
return;
}
// The dashboard lists its notices itself.
$is_dashboard = $current_screen && 'toplevel_page_' . Constants::ADPRESSO_SLUG === $current_screen->id;
foreach ( $all_wp_notices as $notice ) {
if ( $is_dashboard && 'dashboard' === $notice->location ) {
continue;
}
// Check the context.
$show_on_this_page = false;
if ( $notice->context === 'all' ) {
$show_on_this_page = true;
} elseif ( $notice->context === 'adpresso' && Util::is_adpresso_admin_screen() ) {
$show_on_this_page = true;
} elseif ( $notice->context === 'dashboard' && $current_screen->id === 'dashboard' ) {
$show_on_this_page = true;
} elseif ( $notice->context === $current_screen->id ) {
$show_on_this_page = true;
}
if ( ! $show_on_this_page ) {
continue;
}
printf(
'
',
esc_attr( $notice->type ),
$notice->dismissible ? ' is-dismissible' : '',
esc_attr( $notice->slug ),
wp_kses_post( $notice->message ),
$notice->dismissible ? sprintf(
'',
esc_attr( $notice->slug ),
esc_html__( 'Dismiss this notice', 'adpresso' )
) : ''
);
}
}
/**
* Enqueue the JS for the reactive flyouts and the dismiss handler (REST, via apiFetch).
*
* @param string $hook_suffix
*
* @return void
*/
public function enqueue_assets( string $hook_suffix ): void {
$user_id = get_current_user_id();
$wp_notices = Notification_Center::get()->get_active_notices_by_location( 'wp_notice', $user_id );
$dash_notices = Notification_Center::get()->get_active_notices_by_location( 'dashboard', $user_id );
$all_wp_notices = array_merge( $wp_notices, $dash_notices );
$has_notices_to_show = false;
if ( ! empty( $all_wp_notices ) ) {
$current_screen = get_current_screen();
foreach ( $all_wp_notices as $notice ) {
if ( $notice->context === 'all' ) {
$has_notices_to_show = true;
break;
} elseif ( $notice->context === 'adpresso' && Util::is_adpresso_admin_screen() ) {
$has_notices_to_show = true;
break;
} elseif ( $notice->context === 'dashboard' && $current_screen->id === 'dashboard' ) {
$has_notices_to_show = true;
break;
} elseif ( $notice->context === $current_screen->id ) {
$has_notices_to_show = true;
break;
}
}
}
if ( ! $has_notices_to_show && ! str_contains( $hook_suffix, 'adpresso' ) ) {
return;
}
// The Notifications page carries the AdPresso header like the other admin pages.
if ( '' !== $this->page_hook && $hook_suffix === $this->page_hook ) {
Assets::get()->enqueue_script( 'header' );
Assets::get()->enqueue_style( 'header' );
wp_set_script_translations( 'adpresso-header', 'adpresso' );
}
$handle = 'adpresso-notifications';
wp_enqueue_script(
$handle,
ADPRESSO_BASE_URL . 'admin/assets/src/notifications.js',
[ 'adpresso-core-events', 'wp-a11y', 'wp-api-fetch' ],
ADPRESSO_VERSION,
true
);
wp_localize_script( $handle, 'adpressoInitialNotices', $this->reactive_notices );
}
/**
* Get the admin bar notices.
*
* @return array
*/
public function get_admin_bar_items(): array {
return $this->admin_bar_notices;
}
/**
* Add notices to the admin bar.
*
* @param WP_Admin_Bar $wp_admin_bar
*
* @return void
*/
public function render_admin_bar_notices( WP_Admin_Bar $wp_admin_bar ): void {
if ( empty( $this->admin_bar_notices ) ) {
return;
}
// Main node.
$count = count( $this->admin_bar_notices );
$wp_admin_bar->add_node( [
'id' => 'adpresso-ab-notices',
'title' => sprintf( '%d', $count ),
'href' => admin_url( 'admin.php?page=adpresso-notifications' ),
] );
foreach ( $this->admin_bar_notices as $slug => $notice ) {
$wp_admin_bar->add_node( [
'id' => 'adpresso-ab-notice-' . $slug,
'parent' => 'adpresso-ab-notices',
'title' => wp_strip_all_tags( $notice['message'] ),
'href' => $notice['link'] ?? '#',
] );
}
}
/**
* Register the admin page for the notification dashboard.
*/
public function register_menu_page(): void {
$this->page_hook = (string) add_submenu_page(
Constants::ADPRESSO_SLUG,
__( 'Notifications', 'adpresso' ),
__( 'Notifications', 'adpresso' ),
adpresso_get_capability( 'manage_settings' ),
'adpresso-notifications',
[ $this, 'render_dashboard_page' ],
999
);
}
/**
* Renders the HTML for the dashboard notification page.
*/
public function render_dashboard_page(): void {
$all_notices = Notification_Center::get()->get_dashboard_notifications();
echo '';
echo 'AdPresso
';
// Not an h1/h2: WP moves admin notices after the first .wrap h1/h2 (the hidden h2 stays that anchor).
printf( '
%s
', esc_html__( 'AdPresso Notifications', 'adpresso' ) );
?>
' . esc_html__( 'You have no active notifications. All clear!', 'adpresso' ) . '';
echo '
';
return;
}
$types = [
'info' => [ 'dashicons-info-outline', __( 'Info', 'adpresso' ) ],
'success' => [ 'dashicons-yes-alt', __( 'Success', 'adpresso' ) ],
'warning' => [ 'dashicons-warning', __( 'Warning', 'adpresso' ) ],
'error' => [ 'dashicons-dismiss', __( 'Error', 'adpresso' ) ],
];
?>
|
|
|
|
type ] ) ? $notice->type : 'info';
[ $icon_class, $type_label ] = $types[ $type ];
?>
|
|
message ); ?>
link ) ) : ?>
|
created_at ) ) ); ?>
|
dismissible ) : ?>
|
';
}
}