| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Action_Definition |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var callable |
| 16 |
*/ |
| 17 |
private $callback; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $options; |
| 23 |
|
| 24 |
private static $defaultOptions = array( |
| 25 |
'hook_name' => null, |
| 26 |
'hook_priority' => 10, |
| 27 |
); |
| 28 |
|
| 29 |
/** |
| 30 |
* First parameter is callback to be executed. |
| 31 |
* Second parameter accepts the following option names: |
| 32 |
* - 'hook_name' - WordPress hook to attach the action to. |
| 33 |
* - 'hook_priority' - WordPress hook priority; used only when 'hook_name' is set. |
| 34 |
* |
| 35 |
* @param callable $callback |
| 36 |
* @param array $options |
| 37 |
*/ |
| 38 |
public function __construct($callback, array $options = array()) |
| 39 |
{ |
| 40 |
$this->validateOptions($options); |
| 41 |
$options += self::$defaultOptions; |
| 42 |
|
| 43 |
$this->callback = $callback; |
| 44 |
$this->options = $options; |
| 45 |
} |
| 46 |
|
| 47 |
private function validateOptions(array $options) |
| 48 |
{ |
| 49 |
foreach ($options as $optionName => $optionDefault) { |
| 50 |
if (!array_key_exists($optionName, self::$defaultOptions)) { |
| 51 |
throw new InvalidArgumentException(sprintf('Option "%s" is not registered, valid options are "%s"', $optionName, implode('", "', array_keys(self::$defaultOptions)))); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return callable |
| 58 |
*/ |
| 59 |
public function getCallback() |
| 60 |
{ |
| 61 |
return $this->callback; |
| 62 |
} |
| 63 |
|
| 64 |
public function getOption($name) |
| 65 |
{ |
| 66 |
if (!array_key_exists($name, $this->options)) { |
| 67 |
throw new InvalidArgumentException(sprintf('Option "%s" is not recognized', $name)); |
| 68 |
} |
| 69 |
|
| 70 |
return $this->options[$name]; |
| 71 |
} |
| 72 |
} |
| 73 |
|