| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Implements the event dispatcher interface for the WordPress platform. |
| 16 |
* |
| 17 |
* @since 1.5.10 |
| 18 |
*/ |
| 19 |
class VBOPlatformOrgWordpressDispatcher implements VBOPlatformDispatcherInterface |
| 20 |
{ |
| 21 |
use VBOEventObserver; |
| 22 |
|
| 23 |
/** |
| 24 |
* Triggers the specified event by passing the given argument. |
| 25 |
* No return value is expected here. |
| 26 |
* |
| 27 |
* @param string $event The event to trigger. |
| 28 |
* @param array $args The event arguments. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function trigger($event, array $args = []) |
| 33 |
{ |
| 34 |
/** @see Observer::notify() */ |
| 35 |
$this->notify($event, ...$args); |
| 36 |
|
| 37 |
do_action_ref_array($this->getHook($event), $args); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Triggers the specified event by passing the given argument. |
| 42 |
* At least a return value is expected here. |
| 43 |
* |
| 44 |
* @param string $event The event to trigger. |
| 45 |
* @param array $args The event arguments. |
| 46 |
* |
| 47 |
* @return array A list of returned values. |
| 48 |
*/ |
| 49 |
public function filter($event, array $args = []) |
| 50 |
{ |
| 51 |
/** @see Observer::notify() */ |
| 52 |
$internal = $this->notify($event, ...$args); |
| 53 |
|
| 54 |
// inject argument at the beginning of the list, which will be |
| 55 |
// used as return value by the WordPress filtering technique |
| 56 |
array_unshift($args, null); |
| 57 |
|
| 58 |
$return = apply_filters_ref_array($this->getHook($event), $args); |
| 59 |
|
| 60 |
if (is_null($return)) |
| 61 |
{ |
| 62 |
// no attached hooks |
| 63 |
$return = []; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
// wrap returned value into an array |
| 68 |
$return = [$return]; |
| 69 |
} |
| 70 |
|
| 71 |
// wrap returned value into an array |
| 72 |
return array_merge($internal, $return); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @inheritDoc |
| 77 |
* |
| 78 |
* @since 1.18.15 (J) - 1.8.15 (WP) |
| 79 |
*/ |
| 80 |
public function observe(string $event, $handler) |
| 81 |
{ |
| 82 |
/** @see VBOEventObserver::subscribe() */ |
| 83 |
$this->subscribe($event, $handler); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Checks whether the specified event uses the Joomla notation. |
| 88 |
* In that case, rebuild the event to look more similar to |
| 89 |
* WordPress hooks. |
| 90 |
* |
| 91 |
* @param string $event The event to check. |
| 92 |
* |
| 93 |
* @return string The modified event, if needed. |
| 94 |
* |
| 95 |
* @since 1.5.11 |
| 96 |
*/ |
| 97 |
protected function getHook($event) |
| 98 |
{ |
| 99 |
// make sure it starts with "on" |
| 100 |
if (preg_match("/^on[A-Z]/", $event)) |
| 101 |
{ |
| 102 |
// remove initial "on" |
| 103 |
$event = preg_replace("/^on/", '', $event); |
| 104 |
|
| 105 |
// remove plugin name from event and prepend it at the beginning |
| 106 |
$event = 'vikbooking' . preg_replace("/vikbooking/i", '', $event); |
| 107 |
|
| 108 |
// place an underscore between each camelCase |
| 109 |
$event = strtolower(preg_replace("/([a-z])([A-Z])/", '$1_$2', $event)); |
| 110 |
} |
| 111 |
|
| 112 |
return $event; |
| 113 |
} |
| 114 |
} |
| 115 |
|