Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,52 @@ if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sd
- `file` - The main plugin file. Not needed for themes.
- `type` - `plugin` or `theme`. Not needed for plugins.
- `weekly_check` - Optional: whether to make a weekly request to confirm the license status. Defaults to true.

## Admin Notices

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

### Adding Notices

You can add notices statically from anywhere in your code before the `admin_notices` hook fires at priority 100:

```php
use EasyDigitalDownloads\Updater\Admin\Notices;

// Basic usage - can be called from anywhere
Notices::add( array(
'id' => 'my-notice-id',
'type' => 'success', // 'success', 'error', 'warning', 'info'
'message' => 'Your license has been activated successfully!',
'classes' => array( 'my-custom-class' ) // Optional additional CSS classes
) );

// Example: Add notice from an early hook (this works)
add_action( 'admin_init', function() {
Notices::add( array(
'id' => 'early-notice',
'type' => 'info',
'message' => 'Notice added during admin_init',
) );
} );

// Example: Add notice from admin_notices at lower priority (this works)
add_action( 'admin_notices', function() {
Notices::add( array(
'id' => 'priority-notice',
'type' => 'warning',
'message' => 'Notice added at default priority (10)',
) );
}, 10 ); // Priority 10 runs before our render at priority 100

// This would NOT work - priority 200 runs after our render at priority 100
add_action( 'admin_notices', function() {
Notices::add( array(
'id' => 'too-late-notice',
'type' => 'error',
'message' => 'This notice will not display!',
) );
}, 200 ); // Too late - render already happened at priority 100
```

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.
8 changes: 4 additions & 4 deletions src/Admin/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Notices {
*
* @var array
*/
private $notices = array();
private static $notices = array();

/**
* Notices constructor.
Expand Down Expand Up @@ -62,7 +62,7 @@ public static function add( array $args ) {
$classes = array_merge( $classes, $args['classes'] );
}

$this->notices[ $args['id'] ] = array(
self::$notices[ $args['id'] ] = array(
'message' => $args['message'],
'classes' => $classes,
);
Expand All @@ -72,11 +72,11 @@ public static function add( array $args ) {
* Render the notices.
*/
public function render() {
if ( empty( $this->notices ) ) {
if ( empty( self::$notices ) ) {
return;
}

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