Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit bc3a851

Browse files
author
calvinalkan
committed
- Added release notes.
- Added new documentation for Release [0.1.4]
1 parent 4cbd5f5 commit bc3a851

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Release Notes
2+
3+
## [0.1.5](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.4...0.1.5)
4+
5+
### Added
6+
7+
- Added release notes.
8+
- Added new documentation for Release [0.1.4](#014httpsgithubcomcalvinalkanbetter-wordpress-hookscompare013014)
9+
10+
## [0.1.4](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.3...0.1.4)
11+
12+
### Added
13+
14+
- Smart default values - You can now typehint the return value on your event's `default()` method. If the typehint does not match with the filtered value the default() method will get called with the original and filtered value. ([Commit](https://github.com/calvinalkan/better-wordpress-hooks/commit/8d564babae2f448f607ceb1aea73edae487d2bfc#diff-6f76b222b1d42b154e0ca5f9cca9c766227cb56a75f7bff262e412a5f85a9378R182))
15+
- It's now possible to resolve mapped events from the service container ([Commit](https://github.com/calvinalkan/better-wordpress-hooks/commit/3b48f0b7951c28e1f1c8ff7ce94ce7e842e89ef6)). See example under [Bootstrapping](https://github.com/calvinalkan/better-wordpress-hooks/blob/master/README.md#bootstrapping).
16+
17+
### Changed
18+
- It's now possible to dispatch event objects without having to pass an empty array for events without constructor arguments.([Commit](https://github.com/calvinalkan/better-wordpress-hooks/commit/6165c5b3b0c810839fa02c43ebec87e78d91c6f1))
19+
- the default() method now receives the original payload, and the filtered value. ([Commit](https://github.com/calvinalkan/better-wordpress-hooks/commit/8d564babae2f448f607ceb1aea73edae487d2bfc))
20+
21+
### Fixed
22+
23+
- Some docblocks mistakes that led to false positive errors in code editors.
24+
- Logic error. Removed code that attempted to map several custom events to a WordPress Hook.
25+
This should have never been possible. Only one custom event should be mapped to a WordPress Hook.

README.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,21 @@ AcmeEvents::make($custom_container_adapter);
271271
**3. Map core and 3rd party hooks to custom event objects ( optional but recommended )**.
272272

273273
All that this does is dispatching your custom event object when the WP Hooks is fired. We'll see why we do this in a
274-
minute.
274+
minute. By default, mapped events are not resolved from the service container as they should only be data classes.
275+
However, if you pass `'resolve'` as the first key your mapped event will be resolved from the container and dispatched afterwards.
275276

276277
```php
277278
$mapped = [
278279

279-
'init' => [
280+
// Will fire on priority 10
281+
'init' => RegisterJobListingPostType::class,
280282

281-
RegisterJobListingPostType::class,
282-
283-
// more events
284-
],
285-
286-
'save_post_job_listing' => [
283+
// Will fire on priority 99
284+
'save_post_job_listing' => [JobListingCreated::class, 99],
287285

288-
JobListingCreated::class
289-
290-
]
286+
// Will be resolved from the service container
287+
// and fire on priority 99.
288+
'booking_created' => ['resolve' ,BookingCreated::class, 99],
291289

292290
];
293291

@@ -698,6 +696,43 @@ Default return values are evaluated in the following order:
698696

699697
3. If #1 and #2 are not possible the first parameter passed into the ````dispatch()```` method will be returned.
700698

699+
#### Return values for invalid callback
700+
701+
A common pain point when offering filters is that users don't respect the API of your filter and return incompatible values.
702+
If you are using object events you can typehint the expected return value on the `default()` method.
703+
If the value returned from a filter is either:
704+
1. The same as the original payload or
705+
2. NOT of the same type as the typehint on `default()` the filtered value will be discarded, and your event`s default method will be called with both the payload, and the filtered value, giving you the option to fix things.
706+
707+
**Example**
708+
````php
709+
class MyEvent extends AcmeEvents {
710+
711+
public function default($original_payload, $filtered_value) :array {
712+
713+
return [$filtered_value];
714+
715+
}
716+
717+
}
718+
````
719+
Assuming a third party developer would hook into your filter like this:
720+
```php
721+
add_filter(MyEvent::class, function ( $event ) {
722+
return 'foo';
723+
});
724+
```
725+
726+
```php
727+
$value = MyEvent::dispatch();
728+
729+
// without array typehint $value = 'foo', whoops.
730+
$do_stuff = $value[0];
731+
732+
// with typehint on default()
733+
// $value = ['foo'];
734+
```
735+
701736
***
702737

703738
### Valid Listeners

src/Mappers/WordpressEventMapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class WordpressEventMapper implements EventMapper {
2626
*/
2727
private $container;
2828

29-
/** @var Dispatcher */
29+
/**
30+
* @var Dispatcher
31+
*/
3032
private $dispatcher;
3133

3234
public function __construct( ContainerAdapter $container, Dispatcher $dispatcher, WordpressApi $wp_api = null ) {

0 commit comments

Comments
 (0)