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