| 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 |
public function __construct( $hook, array $args = array(), ActionScheduler_Schedule $schedule = NULL, $group = '' ) { |
| 14 |
$schedule = empty( $schedule ) ? new ActionScheduler_NullSchedule() : $schedule; |
| 15 |
$this->set_hook($hook); |
| 16 |
$this->set_schedule($schedule); |
| 17 |
$this->set_args($args); |
| 18 |
$this->set_group($group); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Executes the action. |
| 23 |
* |
| 24 |
* If no callbacks are registered, an exception will be thrown and the action will not be |
| 25 |
* fired. This is useful to help detect cases where the code responsible for setting up |
| 26 |
* a scheduled action no longer exists. |
| 27 |
* |
| 28 |
* @throws Exception If no callbacks are registered for this action. |
| 29 |
*/ |
| 30 |
public function execute() { |
| 31 |
$hook = $this->get_hook(); |
| 32 |
|
| 33 |
if ( ! has_action( $hook ) ) { |
| 34 |
throw new Exception( |
| 35 |
sprintf( |
| 36 |
/* translators: 1: action hook. */ |
| 37 |
__( 'Scheduled action for %1$s will not be executed as no callbacks are registered.', 'action-scheduler' ), |
| 38 |
$hook |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
do_action_ref_array( $hook, array_values( $this->get_args() ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param string $hook |
| 48 |
*/ |
| 49 |
protected function set_hook( $hook ) { |
| 50 |
$this->hook = $hook; |
| 51 |
} |
| 52 |
|
| 53 |
public function get_hook() { |
| 54 |
return $this->hook; |
| 55 |
} |
| 56 |
|
| 57 |
protected function set_schedule( ActionScheduler_Schedule $schedule ) { |
| 58 |
$this->schedule = $schedule; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return ActionScheduler_Schedule |
| 63 |
*/ |
| 64 |
public function get_schedule() { |
| 65 |
return $this->schedule; |
| 66 |
} |
| 67 |
|
| 68 |
protected function set_args( array $args ) { |
| 69 |
$this->args = $args; |
| 70 |
} |
| 71 |
|
| 72 |
public function get_args() { |
| 73 |
return $this->args; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param string $group |
| 78 |
*/ |
| 79 |
protected function set_group( $group ) { |
| 80 |
$this->group = $group; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function get_group() { |
| 87 |
return $this->group; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @return bool If the action has been finished |
| 92 |
*/ |
| 93 |
public function is_finished() { |
| 94 |
return FALSE; |
| 95 |
} |
| 96 |
} |
| 97 |
|