PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Action / Definition.php

Definition.php in ManageWP Worker 4.9.25, at src/MWP/Action/Definition.php

73 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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