Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 27aea7e

Browse files
committed
Merge branch 'readme'
2 parents 41dc461 + 4510574 commit 27aea7e

File tree

2 files changed

+94
-42
lines changed

2 files changed

+94
-42
lines changed

README.md

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
[![Donate via PayPal](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://typist.tech/donate/wp-contained-hook/)
1111
[![Hire Typist Tech](https://img.shields.io/badge/Hire-Typist%20Tech-ff69b4.svg)](https://typist.tech/contact/)
1212

13-
Lazily instantiate objects from dependency injection container to WordPress hooks (actions and filters).
14-
1513
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1614
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1715

@@ -20,13 +18,17 @@ Lazily instantiate objects from dependency injection container to WordPress hook
2018
- [Install](#install)
2119
- [Usage](#usage)
2220
- [API](#api)
23-
- [Loader](#loader)
24-
- [Loader::__construct(Container $container)](#loader__constructcontainer-container)
25-
- [Loader::add(AbstractHook ...$hooks)](#loaderaddabstracthook-hooks)
21+
- [TypistTech\WPContainedHook\Loader](#typisttech%5Cwpcontainedhook%5Cloader)
22+
- [Loader Constructor](#loader-constructor)
23+
- [Loader::add(HookInterface ...$hooks)](#loaderaddhookinterface-hooks)
2624
- [Loader::run()](#loaderrun)
27-
- [Action and Filter](#action-and-filter)
25+
- [Hooks: Action and Filter](#hooks-action-and-filter)
26+
- [AbstractHook Constructor.](#abstracthook-constructor)
2827
- [Frequently Asked Questions](#frequently-asked-questions)
2928
- [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)
3032
- [Support!](#support)
3133
- [Donate via PayPal *](#donate-via-paypal-)
3234
- [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
4244

4345
## The Goals, or What This Package Does?
4446

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.
4648

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).
4850

4951
## Install
5052

5153
Installation should be done via composer, details of how to install composer can be found at [https://getcomposer.org/](https://getcomposer.org/).
5254

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+
5357
``` bash
54-
$ composer require typisttech/wp-contained-hook
58+
# league/container is an example, any psr/container-implementation package works
59+
$ composer require typisttech/wp-contained-hook league/container
5560
```
5661

5762
## Usage
5863

5964
```php
6065
use League\Container\Container;
61-
use League\Container\ReflectionContainer;
62-
use TypistTech\WPContainedHook\Action;
63-
use TypistTech\WPContainedHook\Filter;
66+
use TypistTech\WPContainedHook\Hooks\Action;
67+
use TypistTech\WPContainedHook\Hooks\Filter;
6468
use TypistTech\WPContainedHook\Loader;
6569

6670
$container = new Container;
6771

68-
// Optional container config.
69-
$container->delegate(new ReflectionContainer);
70-
$someClass = new SomeClass;
71-
$this->container->add(SomeClass::class, $someClass);
72-
73-
$loader = new Loader($container);
72+
// Configure the container.
73+
// This depends on your `psr/container-implementation`.
74+
$container->add('bar', Bar::class);
75+
$container->add('foo', Foo::class);
7476

7577
// Action.
76-
$action = new Action(SomeClass::class, 'plugin_loaded', 'doSomething');
78+
$action = new Action('bar', 'admin_init', 'doSomething');
7779

7880
// Filter.
79-
$filter = new Filter(SomeClass::class, 'the_content', 'filterSomething');
81+
$filter = new Filter('foo', 'the_content', 'filterSomething');
8082

81-
// Add to loader
83+
// Add to loader.
84+
$loader = new Loader($container);
8285
$loader->add($action, $filter);
8386

84-
// Add to WordPress
87+
// Add to WordPress.
8588
$loader->run();
8689
```
8790

88-
## API
91+
In plain WordPress, the above is similar to:
8992

90-
### Loader
93+
```php
94+
$bar = new Bar();
95+
add_action('admin_init', [$bar, 'doSomething'])
96+
97+
$foo = new Foo();
98+
add_filter('the_content', [$foo, 'filterSomething'])
99+
```
100+
101+
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
91116

92-
Register all actions and filters for the plugin.
117+
### TypistTech\WPContainedHook\Loader
93118

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.
95120

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.
97122

98-
Loader constructor.
123+
#### Loader Constructor
99124

100-
* @param League\Container\Container\Container $container The container.
125+
* @param Psr\Container\ContainerInterface $container The container.
101126

102127
Example:
103128

@@ -106,11 +131,11 @@ $container = new Container;
106131
$loader = new Loader($container);
107132
```
108133

109-
#### Loader::add(AbstractHook ...$hooks)
134+
#### Loader::add(HookInterface ...$hooks)
110135

111136
Add new hooks to the collection to be registered with WordPress.
112137

113-
* @param AbstractHook|AbstractHook[] ...$hooks Hooks to be registered.
138+
* @param HookInterface|HookInterface[] ...$hooks Hooks to be registered.
114139

115140
Example:
116141

@@ -135,36 +160,61 @@ Example:
135160
$loader->run();
136161
```
137162

138-
### Action and Filter
163+
### Hooks: Action and Filter
139164

140165
Holds necessary information for an action or a filter.
141166

142-
Both `Action` and `Filter` are subclasses of `AbstractHook`.
167+
Both `Action` and `Filter` are subclasses of `AbstractHook` and implements `HookInterface`.
143168

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.
150170

151171
* @param string $hook The name of the WordPress hook that is being registered.
152172
* @param string $classIdentifier Identifier of the entry to look for from container.
153173
* @param string $callbackMethod The callback method name.
154174
* @param int|null $priority Optional.The priority at which the function should be fired. Default is 10.
155175
* @param int|null $acceptedArgs Optional. The number of arguments that should be passed to the $callback. Default is 1.
156176

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+
157185
## Frequently Asked Questions
158186

159187
### Do you have an example plugin that use this package?
160188

161-
Here you go:
189+
Here you go:
162190

163191
* [Sunny](https://github.com/TypistTech/sunny)
164192
* [WP Cloudflare Guard](https://github.com/TypistTech/wp-cloudflare-guard)
193+
* [Disallow Pwned Passwords](https://github.com/ItinerisLtd/disallow-pwned-passwords)
165194

166195
*Add your own plugin [here](https://github.com/TypistTech/wp-contained-hook/edit/master/README.md)*
167196

197+
### It looks awesome. Where can I find some more goodies like this?
198+
199+
* Articles on Typist Tech's [blog](https://typist.tech)
200+
* [Tang Rufus' WordPress plugins](https://profiles.wordpress.org/tangrufus#content-plugins) on wp.org
201+
* More projects on [Typist Tech's GitHub profile](https://github.com/TypistTech)
202+
* Stay tuned on [Typist Tech's newsletter](https://typist.tech/go/newsletter)
203+
* Follow [Tang Rufus' Twitter account](https://twitter.com/TangRufus)
204+
* 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+
168218
## Support!
169219

170220
### Donate via PayPal [![Donate via PayPal](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://typist.tech/donate/wp-conatined-hook/)

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"name": "typisttech/wp-contained-hook",
3-
"description": "Lazily instantiate objects from dependency injection container to WordPress hooks (actions and filters)",
3+
"description": "Using PSR-11 container implementation in WordPress plugins, themes and packages during WordPress action/filter callbacks.",
44
"keywords": [
55
"action",
66
"container",
7-
"dependency-injection",
7+
"dependency",
88
"di",
99
"filter",
1010
"hook",
11+
"injection",
12+
"psr11",
1113
"wordpress",
1214
"wp"
1315
],

0 commit comments

Comments
 (0)