Skip to content

Commit 630a2e5

Browse files
committed
Implement static Notices::add() method with hybrid storage system
- Add static storage property for notices added via static method - Maintain add() method as static while preserving instance rendering - Merge static notices into instance notices during construction - Update README with proper initialization and usage examples - Reorganize documentation for better developer workflow This enables calling Notices::add() statically while maintaining WordPress admin notice rendering through instance-based approach. Fixes #23
1 parent 8c8c2c2 commit 630a2e5

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

readme.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,42 @@ 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. To use it, create an instance and add notices:
114+
The SDK includes a `Notices` class for displaying admin notices.
115+
116+
### Initialization
117+
118+
First, initialize the notices system in your plugin (typically during `admin_init`):
115119

116120
```php
117121
use EasyDigitalDownloads\Updater\Admin\Notices;
118122

119-
// Create notices instance
120-
$notices = new Notices();
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
121130

122-
// Add a notice
123-
$notices->add( array(
131+
Once initialized, you can add notices statically from anywhere in your code:
132+
133+
```php
134+
// Add a notice (static method)
135+
Notices::add( array(
124136
'id' => 'my-notice-id',
125137
'type' => 'success', // 'success', 'error', 'warning', 'info'
126138
'message' => 'Your license has been activated successfully!',
127139
'classes' => array( 'my-custom-class' ) // Optional additional CSS classes
128140
) );
141+
142+
// Example: Add notice from an action
143+
add_action( 'some_action', function() {
144+
Notices::add( array(
145+
'id' => 'license-activated',
146+
'type' => 'success',
147+
'message' => 'License activated successfully!',
148+
) );
149+
} );
129150
```
130151

131-
The notice will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards.
152+
The notices will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards.

src/Admin/Notices.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ class Notices {
2525
*/
2626
private $notices = array();
2727

28+
/**
29+
* Static notices storage.
30+
*
31+
* @var array
32+
*/
33+
private static $static_notices = array();
34+
2835
/**
2936
* Notices constructor.
3037
*/
3138
public function __construct() {
3239
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 );
3342
}
3443

3544
/**
@@ -38,7 +47,7 @@ public function __construct() {
3847
* @since <next-version>
3948
* @param array $args The notice arguments.
4049
*/
41-
public function add( array $args ) {
50+
public static function add( array $args ) {
4251
$args = wp_parse_args(
4352
$args,
4453
array(
@@ -62,7 +71,7 @@ public function add( array $args ) {
6271
$classes = array_merge( $classes, $args['classes'] );
6372
}
6473

65-
$this->notices[ $args['id'] ] = array(
74+
self::$static_notices[ $args['id'] ] = array(
6675
'message' => $args['message'],
6776
'classes' => $classes,
6877
);

0 commit comments

Comments
 (0)