Skip to content

Commit 5b7bc1a

Browse files
authored
Merge pull request #24 from awesomemotive/issue/23
Fix Notices Class Static Method Error and Improve Documentation #23
2 parents 439c050 + debe923 commit 5b7bc1a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,27 @@ if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sd
109109
- `file` - The main plugin file. Not needed for themes.
110110
- `type` - `plugin` or `theme`. Not needed for plugins.
111111
- `weekly_check` - Optional: whether to make a weekly request to confirm the license status. Defaults to true.
112+
113+
## Admin Notices
114+
115+
The SDK includes a `Notices` class for displaying admin notices. The registry automatically handles instantiation, so you can use the static `add()` method directly.
116+
117+
### Adding Notices
118+
119+
You can add notices statically from anywhere in your code before the `admin_notices` hook fires at priority 100:
120+
121+
```php
122+
use EasyDigitalDownloads\Updater\Admin\Notices;
123+
124+
// Add notice using admin_notices hook with translation-ready strings
125+
add_action( 'admin_notices', function() {
126+
Notices::add( array(
127+
'id' => 'my-plugin-license-activated',
128+
'type' => 'success', // 'success', 'error', 'warning', 'info'
129+
'message' => __( 'Your license has been activated successfully!', 'my-plugin-textdomain' ),
130+
'classes' => array( 'my-custom-class' ) // Optional additional CSS classes
131+
) );
132+
}, 10 ); // Priority 10 runs before our render at priority 100
133+
```
134+
135+
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Notices {
2323
*
2424
* @var array
2525
*/
26-
private $notices = array();
26+
private static $notices = array();
2727

2828
/**
2929
* Notices constructor.
@@ -62,7 +62,7 @@ public static function add( array $args ) {
6262
$classes = array_merge( $classes, $args['classes'] );
6363
}
6464

65-
$this->notices[ $args['id'] ] = array(
65+
self::$notices[ $args['id'] ] = array(
6666
'message' => $args['message'],
6767
'classes' => $classes,
6868
);
@@ -72,11 +72,11 @@ public static function add( array $args ) {
7272
* Render the notices.
7373
*/
7474
public function render() {
75-
if ( empty( $this->notices ) ) {
75+
if ( empty( self::$notices ) ) {
7676
return;
7777
}
7878

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

0 commit comments

Comments
 (0)