Skip to content

Conversation

alhoseany
Copy link

Fix Notices Class Static Method Error and Improve Documentation

Overview

This PR resolves a critical PHP error in the Notices class and adds comprehensive documentation for the Admin Notices functionality.

Changes Made

🐛 Bug Fix: Notices Class Static Method Error

  • File: src/Admin/Notices.php
  • Issue: The add() method was declared as static but attempted to use $this->notices, causing a "Cannot use '$this' in non-object context" error
  • Solution: Removed the static keyword from the add() method to allow proper instance method behavior

Before:

public static function add( array $args ) {
    // ... code using $this->notices
}

After:

public function add( array $args ) {
    // ... code using $this->notices
}

📚 Documentation Enhancement: Admin Notices Usage

  • File: readme.md
  • Added: Complete "Admin Notices" section with usage examples
  • Includes:
    • Proper instantiation examples
    • Available notice types (success, error, warning, info)
    • Configuration options and parameters
    • Code examples showing best practices

Impact

Breaking Change Notice

⚠️ Breaking Change: The Notices::add() method is no longer static. Code calling this method must be updated:

Old Usage (No longer works):

Notices::add( $args );

New Usage (Required):

$notices = new Notices();
$notices->add( $args );

Benefits

  • ✅ Resolves fatal PHP errors when using the Notices class
  • ✅ Provides clear documentation for developers
  • ✅ Maintains WordPress admin notice standards
  • ✅ Enables proper instance-based notice management

Files Changed

  • src/Admin/Notices.php (1 line changed)
  • readme.md (21 lines added)

Testing

  • Verified Notices class instantiation works correctly
  • Confirmed add() method functions without errors
  • Validated notice rendering in WordPress admin

Closes #23

- 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
- 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
- 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
Copy link
Contributor

@robincornett robincornett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to create a new static property; it should be fine to change the existing one to static. Additionally, the registry takes care of instantiating the class, so all a developer should have to do is to use the add method before admin_notices 100.

- 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
…nslation 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.
@robincornett robincornett merged commit 5b7bc1a into release/1.0.1 Sep 22, 2025
@robincornett robincornett deleted the issue/23 branch September 22, 2025 15:44
@robincornett robincornett linked an issue Sep 22, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot use $this in static method add() in Notices class
2 participants