From d8228035016d1063a924fd47afb2aaa003b2b67e Mon Sep 17 00:00:00 2001 From: alhoseany Date: Wed, 17 Sep 2025 22:36:07 +0800 Subject: [PATCH 1/5] Fix static method context error in Notices::add() - Remove static keyword from add() method to allow $this usage - Resolves "Cannot use '$this' in non-object context" error - Method now requires instantiation to call: $notices = new Notices(); $notices->add($args); Fixes #23 --- src/Admin/Notices.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/Notices.php b/src/Admin/Notices.php index 2e147e9..8317350 100644 --- a/src/Admin/Notices.php +++ b/src/Admin/Notices.php @@ -38,7 +38,7 @@ public function __construct() { * @since * @param array $args The notice arguments. */ - public static function add( array $args ) { + public function add( array $args ) { $args = wp_parse_args( $args, array( From 8c8c2c2810b496ba8102de33eab008489606e95a Mon Sep 17 00:00:00 2001 From: alhoseany Date: Wed, 17 Sep 2025 22:40:30 +0800 Subject: [PATCH 2/5] Add Admin Notices documentation to README - Document how to use the Notices class for displaying admin notifications - Include code examples showing proper instantiation and usage - Explain available notice types and configuration options - Reflects the non-static implementation of the add() method Closes #23 --- readme.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/readme.md b/readme.md index b7f6917..8cc3cf9 100644 --- a/readme.md +++ b/readme.md @@ -108,3 +108,24 @@ 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. To use it, create an instance and add notices: + +```php +use EasyDigitalDownloads\Updater\Admin\Notices; + +// Create notices instance +$notices = new Notices(); + +// Add a notice +$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 +) ); +``` + +The notice will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards. From 630a2e5ea622adf1957f90d5a1f1f0ebdfede66b Mon Sep 17 00:00:00 2001 From: alhoseany Date: Wed, 17 Sep 2025 23:47:35 +0800 Subject: [PATCH 3/5] 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 --- readme.md | 33 +++++++++++++++++++++++++++------ src/Admin/Notices.php | 13 +++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 8cc3cf9..d374fee 100644 --- a/readme.md +++ b/readme.md @@ -111,21 +111,42 @@ if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sd ## Admin Notices -The SDK includes a `Notices` class for displaying admin notices. To use it, create an instance and add notices: +The SDK includes a `Notices` class for displaying admin notices. + +### Initialization + +First, initialize the notices system in your plugin (typically during `admin_init`): ```php use EasyDigitalDownloads\Updater\Admin\Notices; -// Create notices instance -$notices = new Notices(); +// Initialize notices rendering (typically in your plugin's main file or during admin_init) +add_action( 'admin_init', function() { + new Notices(); +} ); +``` + +### Adding Notices -// Add a notice -$notices->add( array( +Once initialized, you can add notices statically from anywhere in your code: + +```php +// Add a notice (static method) +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 action +add_action( 'some_action', function() { + Notices::add( array( + 'id' => 'license-activated', + 'type' => 'success', + 'message' => 'License activated successfully!', + ) ); +} ); ``` -The notice will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards. +The notices will be automatically displayed on admin pages. The `Notices` class handles rendering and styling according to WordPress admin notice standards. diff --git a/src/Admin/Notices.php b/src/Admin/Notices.php index 8317350..61adc9a 100644 --- a/src/Admin/Notices.php +++ b/src/Admin/Notices.php @@ -25,11 +25,20 @@ class Notices { */ private $notices = array(); + /** + * Static notices storage. + * + * @var array + */ + private static $static_notices = array(); + /** * Notices constructor. */ public function __construct() { add_action( 'admin_notices', array( $this, 'render' ), 100 ); + // Merge static notices into instance notices for rendering + $this->notices = array_merge( $this->notices, self::$static_notices ); } /** @@ -38,7 +47,7 @@ public function __construct() { * @since * @param array $args The notice arguments. */ - public function add( array $args ) { + public static function add( array $args ) { $args = wp_parse_args( $args, array( @@ -62,7 +71,7 @@ public function add( array $args ) { $classes = array_merge( $classes, $args['classes'] ); } - $this->notices[ $args['id'] ] = array( + self::$static_notices[ $args['id'] ] = array( 'message' => $args['message'], 'classes' => $classes, ); From 966965418e0237bade92620e588643f72c11b65c Mon Sep 17 00:00:00 2001 From: alhoseany Date: Thu, 18 Sep 2025 21:25:34 +0800 Subject: [PATCH 4/5] 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 --- readme.md | 49 ++++++++++++++++++++++++------------------- src/Admin/Notices.php | 17 ++++----------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/readme.md b/readme.md index d374fee..819c37a 100644 --- a/readme.md +++ b/readme.md @@ -111,27 +111,16 @@ if ( file_exists( __DIR__ . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sd ## Admin Notices -The SDK includes a `Notices` class for displaying 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. -### Initialization +### Adding Notices -First, initialize the notices system in your plugin (typically during `admin_init`): +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; -// Initialize notices rendering (typically in your plugin's main file or during admin_init) -add_action( 'admin_init', function() { - new Notices(); -} ); -``` - -### Adding Notices - -Once initialized, you can add notices statically from anywhere in your code: - -```php -// Add a notice (static method) +// Basic usage - can be called from anywhere Notices::add( array( 'id' => 'my-notice-id', 'type' => 'success', // 'success', 'error', 'warning', 'info' @@ -139,14 +128,32 @@ Notices::add( array( 'classes' => array( 'my-custom-class' ) // Optional additional CSS classes ) ); -// Example: Add notice from an action -add_action( 'some_action', function() { +// Example: Add notice from an early hook (this works) +add_action( 'admin_init', function() { Notices::add( array( - 'id' => 'license-activated', - 'type' => 'success', - 'message' => 'License activated successfully!', + '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 `Notices` class handles rendering and styling according to WordPress admin notice standards. +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. diff --git a/src/Admin/Notices.php b/src/Admin/Notices.php index 61adc9a..1da0705 100644 --- a/src/Admin/Notices.php +++ b/src/Admin/Notices.php @@ -23,22 +23,13 @@ class Notices { * * @var array */ - private $notices = array(); - - /** - * Static notices storage. - * - * @var array - */ - private static $static_notices = array(); + private static $notices = array(); /** * Notices constructor. */ public function __construct() { add_action( 'admin_notices', array( $this, 'render' ), 100 ); - // Merge static notices into instance notices for rendering - $this->notices = array_merge( $this->notices, self::$static_notices ); } /** @@ -71,7 +62,7 @@ public static function add( array $args ) { $classes = array_merge( $classes, $args['classes'] ); } - self::$static_notices[ $args['id'] ] = array( + self::$notices[ $args['id'] ] = array( 'message' => $args['message'], 'classes' => $classes, ); @@ -81,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 ) { ?>

From debe92303071e339c228805dbc85e1ac1de8d163 Mon Sep 17 00:00:00 2001 From: alhoseany Date: Mon, 22 Sep 2025 23:29:34 +0800 Subject: [PATCH 5/5] Refactor README examples for Notices class to enhance clarity and translation support - Remove outdated examples and consolidate notice usage into a single example - Update notice ID and message to include translation-ready strings - Simplify documentation for adding notices using the admin_notices hook This improves the usability of the README for developers implementing admin notifications. --- readme.md | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/readme.md b/readme.md index 819c37a..95498f1 100644 --- a/readme.md +++ b/readme.md @@ -120,40 +120,15 @@ You can add notices statically from anywhere in your code before the `admin_noti ```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 notice using admin_notices hook with translation-ready strings add_action( 'admin_notices', function() { Notices::add( array( - 'id' => 'priority-notice', - 'type' => 'warning', - 'message' => 'Notice added at default priority (10)', + 'id' => 'my-plugin-license-activated', + 'type' => 'success', // 'success', 'error', 'warning', 'info' + 'message' => __( 'Your license has been activated successfully!', 'my-plugin-textdomain' ), + 'classes' => array( 'my-custom-class' ) // Optional additional CSS classes ) ); }, 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.