Skip to content

Commit 142a047

Browse files
authored
Merge pull request #7 from simplemediacode/unwordpress
Breaking Changes. V2.0.0
2 parents 52fb045 + a445496 commit 142a047

File tree

9 files changed

+1032
-1454
lines changed

9 files changed

+1032
-1454
lines changed

CHANGELOG.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
# Changelog
2-
3-
## 1.0.6 (unpublished)
4-
5-
- `composer.json` update: authors.
6-
- updated [README.md](./README.md): removing "repositories" note
7-
8-
## 1.0.5 - 1.0.2
9-
10-
- composer tags, validation
11-
- do not load helper file. Implementation is up to Developer.
12-
13-
## 1.0.0 - 1.0.1
14-
15-
**initial**
16-
17-
- remove dependency from internal WordPress filesystem;
18-
- renaming `$wp_`* to `$wphook_`* to avoid conflict wirth WordPress;
19-
- renaming `_*()` functions to "noraml".
1+
# Changelog
2+
3+
## 2.0.0 (unreleased)
4+
5+
### Breaking Changes
6+
- Complete refactoring to modern PHP architecture
7+
- Renamed `WP_Hook` to `Hook`
8+
- Introduced `HooksInterface` and `HooksManager`
9+
- Removed global variables and functions
10+
- Added static `Hooks` facade for backward compatibility
11+
- Improved type safety with PHP 8+ return types
12+
13+
### Bug Fixes
14+
- Added missing method definitions to `HooksInterface`: `getHookCount()`, `isExecutingHook()`, and `getCurrentHook()`
15+
- Fixed consistency between interface methods and their implementations
16+
17+
## 1.0.6 (unpublished)
18+
19+
- `composer.json` update: authors.
20+
- updated [README.md](./README.md): removing "repositories" note
21+
22+
## 1.0.5 - 1.0.2
23+
24+
- composer tags, validation
25+
- do not load helper file. Implementation is up to Developer.
26+
27+
## 1.0.0 - 1.0.1
28+
29+
**initial**
30+
31+
- remove dependency from internal WordPress filesystem
32+
- renaming `$wp_`* to `$wphook_`* to avoid conflict with WordPress
33+
- renaming `_*()` functions to "normal"
2034
- Example: `_wp_filter_build_unique_id()` => `wp_filter_build_unique_id()`

README.md

Lines changed: 100 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,100 @@
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.

composer.json

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
{
2-
"name": "simplemediacode/hooks",
3-
"description": "Decoupled WordPress Hooks (filters and actions)",
4-
"type": "library",
5-
"version": "1.0.8",
6-
"require": {
7-
"php": "~8.0|~8.1|~8.2|~8.3|~8.4"
8-
},
9-
"license": "GPL-2.0-only",
10-
"authors": [
11-
{
12-
"name": "Republa",
13-
"homepage": "https://republa.com",
14-
"role": "Developer"
15-
},
16-
{
17-
"name": "Rolands Umbrovskis",
18-
"homepage": "https://umbrovskis.com",
19-
"role": "Developer"
20-
},
21-
{
22-
"name": "WordPress"
23-
}
24-
],
25-
"autoload": {
26-
"psr-4": {
27-
"SimpleMediaCode\\Hooks\\": "src/"
28-
}
29-
},
30-
"minimum-stability": "stable",
31-
"keywords": [
32-
"hooks",
33-
"phphook",
34-
"phphooks",
35-
"wordpress",
36-
"php",
37-
"php7",
38-
"php8"
39-
],
40-
"support": {
41-
"issues": "https://github.com/simplemediacode/hooks/issues"
42-
}
1+
{
2+
"name": "simplemediacode/hooks",
3+
"description": "Modern PHP Hooks System for PHP applications.",
4+
"type": "library",
5+
"version": "2.0.0",
6+
"require": {
7+
"php": "~8.0|~8.1|~8.2|~8.3|~8.4"
8+
},
9+
"license": "GPL-2.0-only",
10+
"authors": [
11+
{
12+
"name": "Republa",
13+
"homepage": "https://republa.com",
14+
"role": "Developer"
15+
},
16+
{
17+
"name": "Rolands Umbrovskis",
18+
"homepage": "https://umbrovskis.com",
19+
"role": "Developer"
20+
}
21+
],
22+
"autoload": {
23+
"psr-4": {
24+
"SimpleMediaCode\\Hooks\\": "src/"
25+
}
26+
},
27+
"minimum-stability": "stable",
28+
"keywords": [
29+
"hooks",
30+
"phphook",
31+
"phphooks",
32+
"wordpress",
33+
"php",
34+
"php7",
35+
"php8"
36+
],
37+
"support": {
38+
"issues": "https://github.com/simplemediacode/hooks/issues"
39+
}
4340
}

0 commit comments

Comments
 (0)