You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
-[Do you have an example plugin that use this package?](#do-you-have-an-example-plugin-that-use-this-package)
29
+
-[It looks awesome. Where can I find some more goodies like this?](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)
30
+
-[This plugin isn't on wp.org. Where can I give a :star::star::star::star::star: review?](#this-plugin-isnt-on-wporg-where-can-i-give-a-starstarstarstarstar-review)
31
+
-[This plugin isn't on wp.org. Where can I make a complaint?](#this-plugin-isnt-on-wporg-where-can-i-make-a-complaint)
30
32
-[Support!](#support)
31
33
-[Donate via PayPal *](#donate-via-paypal-)
32
34
-[Why don't you hire me?](#why-dont-you-hire-me)
@@ -42,62 +44,85 @@ Lazily instantiate objects from dependency injection container to WordPress hook
42
44
43
45
## The Goals, or What This Package Does?
44
46
45
-
Lazily instantiate objects from dependency injection container to WordPress hooks (actions and filters).
47
+
Using [PSR-11 container implementation](https://www.php-fig.org/psr/psr-11/) in WordPress plugins, themes and packages during WordPress action/filter callbacks.
46
48
47
-
Using dependency container in WordPress plugins or themes. Dependencies are lazy loaded, not instantiated until the first time they are used.
49
+
Dependencies are usually lazy loaded(depends on your container implementation), not instantiated until the first time they are used (during WordPress action/filter callbacks).
48
50
49
51
## Install
50
52
51
53
Installation should be done via composer, details of how to install composer can be found at [https://getcomposer.org/](https://getcomposer.org/).
52
54
55
+
You need a [`psr/container-implementation` package](https://packagist.org/providers/psr/container-implementation) as well. This readme uses `league/container` as an example (any `psr/container-implementation` works similarly).
56
+
53
57
```bash
54
-
$ composer require typisttech/wp-contained-hook
58
+
# league/container is an example, any psr/container-implementation package works
In WordPress plus container, the above is similar to:
102
+
103
+
```php
104
+
add_action('admin_init', function ($arg) use ($container): void {
105
+
$bar = $container->get('bar');
106
+
$bar->doSomething($arg);
107
+
})
108
+
109
+
add_filter('the_content', function ($arg) use ($container) {
110
+
$foo = $container->get('foo');
111
+
return $foo->filterSomething($arg);
112
+
})
113
+
```
114
+
115
+
## API
91
116
92
-
Register all actions and filters for the plugin.
117
+
### TypistTech\WPContainedHook\Loader
93
118
94
-
Maintain a list of all hooks that are registered throughout the plugin, and register them with the WordPress API. Call the `run` function to execute the list of actions and filters.
119
+
Register all actions and filters for the plugin/package/theme.
95
120
96
-
#### Loader::__construct(Container $container)
121
+
Maintain a list of all hooks that are registered throughout the plugin, and register them with the WordPress API. Call the run function to execute the list of actions and filters.
97
122
98
-
Loader constructor.
123
+
#### Loader Constructor
99
124
100
-
*@paramLeague\Container\Container\Container $container The container.
125
+
*@paramPsr\Container\ContainerInterface $container The container.
101
126
102
127
Example:
103
128
@@ -106,11 +131,11 @@ $container = new Container;
106
131
$loader = new Loader($container);
107
132
```
108
133
109
-
#### Loader::add(AbstractHook ...$hooks)
134
+
#### Loader::add(HookInterface ...$hooks)
110
135
111
136
Add new hooks to the collection to be registered with WordPress.
112
137
113
-
*@paramAbstractHook|AbstractHook[] ...$hooks Hooks to be registered.
138
+
*@paramHookInterface|HookInterface[] ...$hooks Hooks to be registered.
114
139
115
140
Example:
116
141
@@ -135,36 +160,61 @@ Example:
135
160
$loader->run();
136
161
```
137
162
138
-
### Action and Filter
163
+
### Hooks: Action and Filter
139
164
140
165
Holds necessary information for an action or a filter.
141
166
142
-
Both `Action` and `Filter` are subclasses of `AbstractHook`.
167
+
Both `Action` and `Filter` are subclasses of `AbstractHook` and implements `HookInterface`.
143
168
144
-
AbstractHook constructor.
145
-
146
-
```php
147
-
Action::__construct(string $hook, string $classIdentifier, string $callbackMethod, int $priority = null, int $acceptedArgs = null)
148
-
Filter::__construct(string $hook, string $classIdentifier, string $callbackMethod, int $priority = null, int $acceptedArgs = null)
149
-
```
169
+
#### AbstractHook Constructor.
150
170
151
171
*@param string $hook The name of the WordPress hook that is being registered.
152
172
*@param string $classIdentifier Identifier of the entry to look for from container.
153
173
*@param string $callbackMethod The callback method name.
154
174
*@param int|null $priority Optional.The priority at which the function should be fired. Default is 10.
155
175
*@param int|null $acceptedArgs Optional. The number of arguments that should be passed to the $callback. Default is 1.
156
176
177
+
Example:
178
+
179
+
```php
180
+
$action = new Action('bar', 'admin_init', 'doSomething', 20, 2);
181
+
182
+
$filter = new Filter('foo', 'the_content', 'filterSomething', 20, 2);
183
+
```
184
+
157
185
## Frequently Asked Questions
158
186
159
187
### Do you have an example plugin that use this package?
* Hire [Tang Rufus](https://typist.tech/contact) to build your next awesome site
205
+
206
+
### This plugin isn't on wp.org. Where can I give a :star::star::star::star::star: review?
207
+
208
+
Thanks!
209
+
210
+
Consider writing a blog post, submitting pull requests, [donating](https://typist.tech/donation/) or [hiring me](https://typist.tech/contact/) instead.
211
+
212
+
### This plugin isn't on wp.org. Where can I make a complaint?
213
+
214
+
To be honest, I don't care.
215
+
216
+
If you really want to share your 1-star review, send me an email - in the first paragraph, state why didn't invest your time reading the [source code](./src) and making pull requests.
217
+
168
218
## Support!
169
219
170
220
### Donate via PayPal [](https://typist.tech/donate/wp-conatined-hook/)
0 commit comments