| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_Action |
| 5 |
*/ |
| 6 |
class ActionScheduler_Action { |
| 7 |
/** |
| 8 |
* Action's hook. |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
protected $hook = ''; |
| 13 |
|
| 14 |
/** |
| 15 |
* Action's args. |
| 16 |
* |
| 17 |
* @var array<string, mixed> |
| 18 |
*/ |
| 19 |
protected $args = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Action's schedule. |
| 23 |
* |
| 24 |
* @var ActionScheduler_Schedule |
| 25 |
*/ |
| 26 |
protected $schedule = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Action's group. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $group = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* Priorities are conceptually similar to those used for regular WordPress actions. |
| 37 |
* Like those, a lower priority takes precedence over a higher priority and the default |
| 38 |
* is 10. |
| 39 |
* |
| 40 |
* Unlike regular WordPress actions, the priority of a scheduled action is strictly an |
| 41 |
* integer and should be kept within the bounds 0-255 (anything outside the bounds will |
| 42 |
* be brought back into the acceptable range). |
| 43 |
* |
| 44 |
* @var int |
| 45 |
*/ |
| 46 |
protected $priority = 10; |
| 47 |
|
| 48 |
/** |
| 49 |
* Construct. |
| 50 |
* |
| 51 |
* @param string $hook Action's hook. |
| 52 |
* @param mixed[] $args Action's arguments. |
| 53 |
* @param null|ActionScheduler_Schedule $schedule Action's schedule. |
| 54 |
* @param string $group Action's group. |
| 55 |
*/ |
| 56 |
public function __construct( $hook, array $args = array(), ?ActionScheduler_Schedule $schedule = null, $group = '' ) { |
| 57 |
$schedule = empty( $schedule ) ? new ActionScheduler_NullSchedule() : $schedule; |
| 58 |
$this->set_hook( $hook ); |
| 59 |
$this->set_schedule( $schedule ); |
| 60 |
$this->set_args( $args ); |
| 61 |
$this->set_group( $group ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Executes the action. |
| 66 |
* |
| 67 |
* If no callbacks are registered, an exception will be thrown and the action will not be |
| 68 |
* fired. This is useful to help detect cases where the code responsible for setting up |
| 69 |
* a scheduled action no longer exists. |
| 70 |
* |
| 71 |
* @throws Exception If no callbacks are registered for this action. |
| 72 |
*/ |
| 73 |
public function execute() { |
| 74 |
$hook = $this->get_hook(); |
| 75 |
|
| 76 |
if ( ! has_action( $hook ) ) { |
| 77 |
throw new Exception( |
| 78 |
sprintf( |
| 79 |
/* translators: 1: action hook. */ |
| 80 |
__( 'Scheduled action for %1$s will not be executed as no callbacks are registered.', 'action-scheduler' ), |
| 81 |
$hook |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
do_action_ref_array( $hook, array_values( $this->get_args() ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Set action's hook. |
| 91 |
* |
| 92 |
* @param string $hook Action's hook. |
| 93 |
*/ |
| 94 |
protected function set_hook( $hook ) { |
| 95 |
$this->hook = $hook; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get action's hook. |
| 100 |
*/ |
| 101 |
public function get_hook() { |
| 102 |
return $this->hook; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Set action's schedule. |
| 107 |
* |
| 108 |
* @param ActionScheduler_Schedule $schedule Action's schedule. |
| 109 |
*/ |
| 110 |
protected function set_schedule( ActionScheduler_Schedule $schedule ) { |
| 111 |
$this->schedule = $schedule; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Action's schedule. |
| 116 |
* |
| 117 |
* @return ActionScheduler_Schedule |
| 118 |
*/ |
| 119 |
public function get_schedule() { |
| 120 |
return $this->schedule; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set action's args. |
| 125 |
* |
| 126 |
* @param mixed[] $args Action's arguments. |
| 127 |
*/ |
| 128 |
protected function set_args( array $args ) { |
| 129 |
$this->args = $args; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get action's args. |
| 134 |
*/ |
| 135 |
public function get_args() { |
| 136 |
return $this->args; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Section action's group. |
| 141 |
* |
| 142 |
* @param string $group Action's group. |
| 143 |
*/ |
| 144 |
protected function set_group( $group ) { |
| 145 |
$this->group = $group; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Action's group. |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function get_group() { |
| 154 |
return $this->group; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Action has not finished. |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function is_finished() { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Sets the priority of the action. |
| 168 |
* |
| 169 |
* @param int $priority Priority level (lower is higher priority). Should be in the range 0-255. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function set_priority( $priority ) { |
| 174 |
if ( $priority < 0 ) { |
| 175 |
$priority = 0; |
| 176 |
} elseif ( $priority > 255 ) { |
| 177 |
$priority = 255; |
| 178 |
} |
| 179 |
|
| 180 |
$this->priority = (int) $priority; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Gets the action priority. |
| 185 |
* |
| 186 |
* @return int |
| 187 |
*/ |
| 188 |
public function get_priority() { |
| 189 |
return $this->priority; |
| 190 |
} |
| 191 |
} |
| 192 |
|