| 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 |
/** |
| 22 |
* Triggers the specified event by passing the given argument. |
| 23 |
* No return value is expected here. |
| 24 |
* |
| 25 |
* @param string $event The event to trigger. |
| 26 |
* @param array $args The event arguments. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function trigger($event, array $args = []) |
| 31 |
{ |
| 32 |
do_action_ref_array($this->getHook($event), $args); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Triggers the specified event by passing the given argument. |
| 37 |
* At least a return value is expected here. |
| 38 |
* |
| 39 |
* @param string $event The event to trigger. |
| 40 |
* @param array $args The event arguments. |
| 41 |
* |
| 42 |
* @return array A list of returned values. |
| 43 |
*/ |
| 44 |
public function filter($event, array $args = []) |
| 45 |
{ |
| 46 |
// inject argument at the beginning of the list, which will be |
| 47 |
// used as return value by the WordPress filtering technique |
| 48 |
array_unshift($args, null); |
| 49 |
|
| 50 |
$return = apply_filters_ref_array($this->getHook($event), $args); |
| 51 |
|
| 52 |
if (is_null($return)) |
| 53 |
{ |
| 54 |
// no attached hooks |
| 55 |
return []; |
| 56 |
} |
| 57 |
|
| 58 |
// wrap returned value into an array |
| 59 |
return [$return]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Checks whether the specified event uses the Joomla notation. |
| 64 |
* In that case, rebuild the event to look more similar to |
| 65 |
* WordPress hooks. |
| 66 |
* |
| 67 |
* @param string $event The event to check. |
| 68 |
* |
| 69 |
* @return string The modified event, if needed. |
| 70 |
* |
| 71 |
* @since 1.5.11 |
| 72 |
*/ |
| 73 |
protected function getHook($event) |
| 74 |
{ |
| 75 |
// make sure it starts with "on" |
| 76 |
if (preg_match("/^on[A-Z]/", $event)) |
| 77 |
{ |
| 78 |
// remove initial "on" |
| 79 |
$event = preg_replace("/^on/", '', $event); |
| 80 |
|
| 81 |
// remove plugin name from event and prepend it at the beginning |
| 82 |
$event = 'vikbooking' . preg_replace("/vikbooking/i", '', $event); |
| 83 |
|
| 84 |
// place an underscore between each camelCase |
| 85 |
$event = strtolower(preg_replace("/([a-z])([A-Z])/", '$1_$2', $event)); |
| 86 |
} |
| 87 |
|
| 88 |
return $event; |
| 89 |
} |
| 90 |
} |
| 91 |
|