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