|
1 | | -# Decoupled WordPress Hooks (filters and actions) |
2 | | - |
3 | | -Simple copy of WordPress' [WP_Hook class](https://github.com/WordPress/WordPress/blob/3cee52b3622cd6eab054db09074f220270a09243/wp-includes/class-wp-hook.php) with some adjustments to use outside of [WordPress](https://wordpress.org). |
4 | | - |
5 | | -**No dependencies**! |
6 | | -## Why? |
7 | | - |
8 | | -Because I like these WordPress "filters" and they "cost" almost nothing. And sine I use them in other projects it's easier to make them public anyways. |
9 | | - |
10 | | -## Usage |
11 | | - |
12 | | -Via **composer** |
13 | | - |
14 | | -`composer require simplemediacode/hooks` |
15 | | - |
16 | | -and then |
17 | | - |
18 | | -`use SimpleMediaCode\Hooks\WP_Hook;` |
19 | | - |
20 | | -See [ActionHooks.php](./example/ActionHooks.php) in `example` folder (which is autoloaded too). Or wrap in your own solution. |
21 | | -I use them inside classes and/or in helper functions. |
22 | | -_Should_ be compatible with WordPress (works on my machine). "Tested" with PHP 8.2.22. |
23 | | - |
24 | | -## Changelog |
25 | | - |
26 | | -Read at [CHANGELOG.md](./CHANGELOG.md). |
27 | | - |
28 | | -## Links |
29 | | - |
30 | | -More about how to use WordPress hooks (filters and actions) read at [wordpress.org: "WP_Hook: Next Generation Actions and Filters"](https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/). Instead of `$wp_*` here use `$wphook_*` for compatiblity. |
31 | | - |
32 | | ---- |
33 | | - |
34 | | -## Thanks to WordPress team and collaborators |
35 | | - |
36 | | -Most of job done by [WordPress team and collaborators](https://github.com/WordPress/WordPress) |
37 | | - |
38 | | -## License |
39 | | -This library is released under the GLP-2 license. See the complete license in the bundled [LICENSE](./LICENSE) file. |
| 1 | +# Modern PHP Hooks System |
| 2 | + |
| 3 | +A lightweight, dependency-free implementation of an action and filter hook system inspired by WordPress hooks but designed for modern PHP applications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Modern PHP Support**: Fully typed, supports PHP 8.0+ |
| 8 | +- **Dependency Injection Ready**: Use the `HooksInterface` in your classes |
| 9 | +- **Static Facade**: Convenient static methods for quick integration |
| 10 | +- **No Dependencies**: Lightweight implementation with zero dependencies |
| 11 | +- **WordPress Inspired**: Familiar API if you're coming from WordPress |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Via **composer**: |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require simplemediacode/hooks |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Using the Static Facade |
| 24 | + |
| 25 | +```php |
| 26 | +use SimpleMediaCode\Hooks\Hooks; |
| 27 | + |
| 28 | +// Add a filter |
| 29 | +Hooks::addFilter('content', function($content) { |
| 30 | + return strtoupper($content); |
| 31 | +}); |
| 32 | + |
| 33 | +// Apply a filter |
| 34 | +$content = Hooks::applyFilters('content', 'Hello World'); // Returns "HELLO WORLD" |
| 35 | + |
| 36 | +// Add an action |
| 37 | +Hooks::addAction('save_post', function($postId) { |
| 38 | + // Do something when a post is saved |
| 39 | + echo "Post {$postId} was saved!"; |
| 40 | +}); |
| 41 | + |
| 42 | +// Execute an action |
| 43 | +Hooks::doAction('save_post', 123); |
| 44 | +``` |
| 45 | + |
| 46 | +### Using Dependency Injection |
| 47 | + |
| 48 | +```php |
| 49 | +use SimpleMediaCode\Hooks\HooksInterface; |
| 50 | + |
| 51 | +class MyClass |
| 52 | +{ |
| 53 | + private array $config; |
| 54 | + private ?HooksInterface $hooks; |
| 55 | + |
| 56 | + public function __construct( |
| 57 | + array $config, |
| 58 | + ?HooksInterface $hooks = null |
| 59 | + ) { |
| 60 | + $this->config = $config; |
| 61 | + $this->hooks = $hooks; |
| 62 | + } |
| 63 | + |
| 64 | + public function processContent(string $content): string |
| 65 | + { |
| 66 | + // If hooks are available, filter the content |
| 67 | + if ($this->hooks) { |
| 68 | + $content = $this->hooks->executeHook('content', $content); |
| 69 | + } |
| 70 | + |
| 71 | + return $content; |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Extending with Custom Implementations |
| 77 | + |
| 78 | +You can implement your own version of `HooksInterface` to provide custom hook functionality: |
| 79 | + |
| 80 | +```php |
| 81 | +$customHooks = new MyCustomHooksImplementation(); |
| 82 | +Hooks::setInstance($customHooks); |
| 83 | +``` |
| 84 | + |
| 85 | +## Migration from WP_Hook |
| 86 | + |
| 87 | +This package is a complete rewrite of the original WordPress hook system. Key differences: |
| 88 | + |
| 89 | +- Renamed `WP_Hook` to `Hook` |
| 90 | +- Introduced proper interfaces for better type safety |
| 91 | +- Added dependency injection support |
| 92 | +- Replaced global variables with proper class properties |
| 93 | +- Improved naming conventions and method signatures |
| 94 | + |
| 95 | +## Changelog |
| 96 | + |
| 97 | +Read at [CHANGELOG.md](./CHANGELOG.md). |
| 98 | + |
| 99 | +## License |
| 100 | +This library is released under the GPL-2.0 license. See the complete license in the bundled [LICENSE](./LICENSE) file. |
0 commit comments