Skip to content

Commit 9669654

Browse files
committed
Simplify Notices class by converting to single static property
- Remove hybrid storage system with separate $static_notices property - Convert existing $notices property to static for cleaner implementation - Eliminate merging logic from constructor - Update add() and render() methods to use self::$notices directly - Update README with comprehensive priority examples and clearer usage - Registry instantiation makes manual initialization unnecessary Fixes #23
1 parent 630a2e5 commit 9669654

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

readme.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,42 +111,49 @@ if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sd
111111

112112
## Admin Notices
113113

114-
The SDK includes a `Notices` class for displaying admin notices.
114+
The SDK includes a `Notices` class for displaying admin notices. The registry automatically handles instantiation, so you can use the static `add()` method directly.
115115

116-
### Initialization
116+
### Adding Notices
117117

118-
First, initialize the notices system in your plugin (typically during `admin_init`):
118+
You can add notices statically from anywhere in your code before the `admin_notices` hook fires at priority 100:
119119

120120
```php
121121
use EasyDigitalDownloads\Updater\Admin\Notices;
122122

123-
// Initialize notices rendering (typically in your plugin's main file or during admin_init)
124-
add_action( 'admin_init', function() {
125-
new Notices();
126-
} );
127-
```
128-
129-
### Adding Notices
130-
131-
Once initialized, you can add notices statically from anywhere in your code:
132-
133-
```php
134-
// Add a notice (static method)
123+
// Basic usage - can be called from anywhere
135124
Notices::add( array(
136125
'id' => 'my-notice-id',
137126
'type' => 'success', // 'success', 'error', 'warning', 'info'
138127
'message' => 'Your license has been activated successfully!',
139128
'classes' => array( 'my-custom-class' ) // Optional additional CSS classes
140129
) );
141130

142-
// Example: Add notice from an action
143-
add_action( 'some_action', function() {
131+
// Example: Add notice from an early hook (this works)
132+
add_action( 'admin_init', function() {
144133
Notices::add( array(
145-
'id' => 'license-activated',
146-
'type' => 'success',
147-
'message' => 'License activated successfully!',
134+
'id' => 'early-notice',
135+
'type' => 'info',
136+
'message' => 'Notice added during admin_init',
148137
) );
149138
} );
139+
140+
// Example: Add notice from admin_notices at lower priority (this works)
141+
add_action( 'admin_notices', function() {
142+
Notices::add( array(
143+
'id' => 'priority-notice',
144+
'type' => 'warning',
145+
'message' => 'Notice added at default priority (10)',
146+
) );
147+
}, 10 ); // Priority 10 runs before our render at priority 100
148+
149+
// This would NOT work - priority 200 runs after our render at priority 100
150+
add_action( 'admin_notices', function() {
151+
Notices::add( array(
152+
'id' => 'too-late-notice',
153+
'type' => 'error',
154+
'message' => 'This notice will not display!',
155+
) );
156+
}, 200 ); // Too late - render already happened at priority 100
150157
```
151158

152-
The notices will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards.
159+
The notices will be automatically displayed on admin pages. The registry takes care of instantiating the `Notices` class, and the `Notices` class handles rendering and styling according to WordPress admin notice standards.

src/Admin/Notices.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,13 @@ class Notices {
2323
*
2424
* @var array
2525
*/
26-
private $notices = array();
27-
28-
/**
29-
* Static notices storage.
30-
*
31-
* @var array
32-
*/
33-
private static $static_notices = array();
26+
private static $notices = array();
3427

3528
/**
3629
* Notices constructor.
3730
*/
3831
public function __construct() {
3932
add_action( 'admin_notices', array( $this, 'render' ), 100 );
40-
// Merge static notices into instance notices for rendering
41-
$this->notices = array_merge( $this->notices, self::$static_notices );
4233
}
4334

4435
/**
@@ -71,7 +62,7 @@ public static function add( array $args ) {
7162
$classes = array_merge( $classes, $args['classes'] );
7263
}
7364

74-
self::$static_notices[ $args['id'] ] = array(
65+
self::$notices[ $args['id'] ] = array(
7566
'message' => $args['message'],
7667
'classes' => $classes,
7768
);
@@ -81,11 +72,11 @@ public static function add( array $args ) {
8172
* Render the notices.
8273
*/
8374
public function render() {
84-
if ( empty( $this->notices ) ) {
75+
if ( empty( self::$notices ) ) {
8576
return;
8677
}
8778

88-
foreach ( $this->notices as $id => $args ) {
79+
foreach ( self::$notices as $id => $args ) {
8980
?>
9081
<div id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( implode( ' ', $args['classes'] ) ); ?>">
9182
<p><?php echo wp_kses_post( $args['message'] ); ?></p>

0 commit comments

Comments
 (0)