Action
1 month ago
ActionScheduler_WPCLI_Clean_Command.php
1 year ago
ActionScheduler_WPCLI_QueueRunner.php
1 year ago
ActionScheduler_WPCLI_Scheduler_command.php
1 year ago
Action_Command.php
1 year ago
Migration_Command.php
1 year ago
ProgressBar.php
1 year ago
System_Command.php
1 month ago
Action_Command.php
354 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Action_Scheduler\WP_CLI; |
| 4 | |
| 5 | /** |
| 6 | * Action command for Action Scheduler. |
| 7 | */ |
| 8 | class Action_Command extends \WP_CLI_Command { |
| 9 | |
| 10 | /** |
| 11 | * Cancel the next occurrence or all occurrences of a scheduled action. |
| 12 | * |
| 13 | * ## OPTIONS |
| 14 | * |
| 15 | * [<hook>] |
| 16 | * : Name of the action hook. |
| 17 | * |
| 18 | * [--group=<group>] |
| 19 | * : The group the job is assigned to. |
| 20 | * |
| 21 | * [--args=<args>] |
| 22 | * : JSON object of arguments assigned to the job. |
| 23 | * --- |
| 24 | * default: [] |
| 25 | * --- |
| 26 | * |
| 27 | * [--all] |
| 28 | * : Cancel all occurrences of a scheduled action. |
| 29 | * |
| 30 | * @param array $args Positional arguments. |
| 31 | * @param array $assoc_args Keyed arguments. |
| 32 | * @return void |
| 33 | */ |
| 34 | public function cancel( array $args, array $assoc_args ) { |
| 35 | require_once 'Action/Cancel_Command.php'; |
| 36 | $command = new Action\Cancel_Command( $args, $assoc_args ); |
| 37 | $command->execute(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a new scheduled action. |
| 42 | * |
| 43 | * ## OPTIONS |
| 44 | * |
| 45 | * <hook> |
| 46 | * : Name of the action hook. |
| 47 | * |
| 48 | * <start> |
| 49 | * : A unix timestamp representing the date you want the action to start. Also 'async' or 'now' to enqueue an async action. |
| 50 | * |
| 51 | * [--args=<args>] |
| 52 | * : JSON object of arguments to pass to callbacks when the hook triggers. |
| 53 | * --- |
| 54 | * default: [] |
| 55 | * --- |
| 56 | * |
| 57 | * [--cron=<cron>] |
| 58 | * : A cron-like schedule string (https://crontab.guru/). |
| 59 | * --- |
| 60 | * default: '' |
| 61 | * --- |
| 62 | * |
| 63 | * [--group=<group>] |
| 64 | * : The group to assign this job to. |
| 65 | * --- |
| 66 | * default: '' |
| 67 | * --- |
| 68 | * |
| 69 | * [--interval=<interval>] |
| 70 | * : Number of seconds to wait between runs. |
| 71 | * --- |
| 72 | * default: 0 |
| 73 | * --- |
| 74 | * |
| 75 | * ## EXAMPLES |
| 76 | * |
| 77 | * wp action-scheduler action create hook_async async |
| 78 | * wp action-scheduler action create hook_single 1627147598 |
| 79 | * wp action-scheduler action create hook_recurring 1627148188 --interval=5 |
| 80 | * wp action-scheduler action create hook_cron 1627147655 --cron='5 4 * * *' |
| 81 | * |
| 82 | * @param array $args Positional arguments. |
| 83 | * @param array $assoc_args Keyed arguments. |
| 84 | * @return void |
| 85 | */ |
| 86 | public function create( array $args, array $assoc_args ) { |
| 87 | require_once 'Action/Create_Command.php'; |
| 88 | $command = new Action\Create_Command( $args, $assoc_args ); |
| 89 | $command->execute(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Delete existing scheduled action(s). |
| 94 | * |
| 95 | * ## OPTIONS |
| 96 | * |
| 97 | * <id>... |
| 98 | * : One or more IDs of actions to delete. |
| 99 | * --- |
| 100 | * default: 0 |
| 101 | * --- |
| 102 | * |
| 103 | * ## EXAMPLES |
| 104 | * |
| 105 | * # Delete the action with id 100 |
| 106 | * $ wp action-scheduler action delete 100 |
| 107 | * |
| 108 | * # Delete the actions with ids 100 and 200 |
| 109 | * $ wp action-scheduler action delete 100 200 |
| 110 | * |
| 111 | * # Delete the first five pending actions in 'action-scheduler' group |
| 112 | * $ wp action-scheduler action delete $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) |
| 113 | * |
| 114 | * @param array $args Positional arguments. |
| 115 | * @param array $assoc_args Keyed arguments. |
| 116 | * @return void |
| 117 | */ |
| 118 | public function delete( array $args, array $assoc_args ) { |
| 119 | require_once 'Action/Delete_Command.php'; |
| 120 | $command = new Action\Delete_Command( $args, $assoc_args ); |
| 121 | $command->execute(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generates some scheduled actions. |
| 126 | * |
| 127 | * ## OPTIONS |
| 128 | * |
| 129 | * <hook> |
| 130 | * : Name of the action hook. |
| 131 | * |
| 132 | * <start> |
| 133 | * : The Unix timestamp representing the date you want the action to start. |
| 134 | * |
| 135 | * [--count=<count>] |
| 136 | * : Number of actions to create. |
| 137 | * --- |
| 138 | * default: 1 |
| 139 | * --- |
| 140 | * |
| 141 | * [--interval=<interval>] |
| 142 | * : Number of seconds to wait between runs. |
| 143 | * --- |
| 144 | * default: 0 |
| 145 | * --- |
| 146 | * |
| 147 | * [--args=<args>] |
| 148 | * : JSON object of arguments to pass to callbacks when the hook triggers. |
| 149 | * --- |
| 150 | * default: [] |
| 151 | * --- |
| 152 | * |
| 153 | * [--group=<group>] |
| 154 | * : The group to assign this job to. |
| 155 | * --- |
| 156 | * default: '' |
| 157 | * --- |
| 158 | * |
| 159 | * ## EXAMPLES |
| 160 | * |
| 161 | * wp action-scheduler action generate test_multiple 1627147598 --count=5 --interval=5 |
| 162 | * |
| 163 | * @param array $args Positional arguments. |
| 164 | * @param array $assoc_args Keyed arguments. |
| 165 | * @return void |
| 166 | */ |
| 167 | public function generate( array $args, array $assoc_args ) { |
| 168 | require_once 'Action/Generate_Command.php'; |
| 169 | $command = new Action\Generate_Command( $args, $assoc_args ); |
| 170 | $command->execute(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get details about a scheduled action. |
| 175 | * |
| 176 | * ## OPTIONS |
| 177 | * |
| 178 | * <id> |
| 179 | * : The ID of the action to get. |
| 180 | * --- |
| 181 | * default: 0 |
| 182 | * --- |
| 183 | * |
| 184 | * [--field=<field>] |
| 185 | * : Instead of returning the whole action, returns the value of a single field. |
| 186 | * |
| 187 | * [--fields=<fields>] |
| 188 | * : Limit the output to specific fields (comma-separated). Defaults to all fields. |
| 189 | * |
| 190 | * [--format=<format>] |
| 191 | * : Render output in a particular format. |
| 192 | * --- |
| 193 | * default: table |
| 194 | * options: |
| 195 | * - table |
| 196 | * - csv |
| 197 | * - json |
| 198 | * - yaml |
| 199 | * --- |
| 200 | * |
| 201 | * @param array $args Positional arguments. |
| 202 | * @param array $assoc_args Keyed arguments. |
| 203 | * @return void |
| 204 | */ |
| 205 | public function get( array $args, array $assoc_args ) { |
| 206 | require_once 'Action/Get_Command.php'; |
| 207 | $command = new Action\Get_Command( $args, $assoc_args ); |
| 208 | $command->execute(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get a list of scheduled actions. |
| 213 | * |
| 214 | * Display actions based on all arguments supported by |
| 215 | * [as_get_scheduled_actions()](https://actionscheduler.org/api/#function-reference--as_get_scheduled_actions). |
| 216 | * |
| 217 | * ## OPTIONS |
| 218 | * |
| 219 | * [--<field>=<value>] |
| 220 | * : One or more arguments to pass to as_get_scheduled_actions(). |
| 221 | * |
| 222 | * [--field=<field>] |
| 223 | * : Prints the value of a single property for each action. |
| 224 | * |
| 225 | * [--fields=<fields>] |
| 226 | * : Limit the output to specific object properties. |
| 227 | * |
| 228 | * [--format=<format>] |
| 229 | * : Render output in a particular format. |
| 230 | * --- |
| 231 | * default: table |
| 232 | * options: |
| 233 | * - table |
| 234 | * - csv |
| 235 | * - ids |
| 236 | * - json |
| 237 | * - count |
| 238 | * - yaml |
| 239 | * --- |
| 240 | * |
| 241 | * ## AVAILABLE FIELDS |
| 242 | * |
| 243 | * These fields will be displayed by default for each action: |
| 244 | * |
| 245 | * * id |
| 246 | * * hook |
| 247 | * * status |
| 248 | * * group |
| 249 | * * recurring |
| 250 | * * scheduled_date |
| 251 | * |
| 252 | * These fields are optionally available: |
| 253 | * |
| 254 | * * args |
| 255 | * * log_entries |
| 256 | * |
| 257 | * @param array $args Positional arguments. |
| 258 | * @param array $assoc_args Keyed arguments. |
| 259 | * @return void |
| 260 | * |
| 261 | * @subcommand list |
| 262 | */ |
| 263 | public function subcommand_list( array $args, array $assoc_args ) { |
| 264 | require_once 'Action/List_Command.php'; |
| 265 | $command = new Action\List_Command( $args, $assoc_args ); |
| 266 | $command->execute(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Get logs for a scheduled action. |
| 271 | * |
| 272 | * ## OPTIONS |
| 273 | * |
| 274 | * <id> |
| 275 | * : The ID of the action to get. |
| 276 | * --- |
| 277 | * default: 0 |
| 278 | * --- |
| 279 | * |
| 280 | * @param array $args Positional arguments. |
| 281 | * @return void |
| 282 | */ |
| 283 | public function logs( array $args ) { |
| 284 | $command = sprintf( 'action-scheduler action get %d --field=log_entries', $args[0] ); |
| 285 | WP_CLI::runcommand( $command ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Get the ID or timestamp of the next scheduled action. |
| 290 | * |
| 291 | * ## OPTIONS |
| 292 | * |
| 293 | * <hook> |
| 294 | * : The hook of the next scheduled action. |
| 295 | * |
| 296 | * [--args=<args>] |
| 297 | * : JSON object of arguments to search for next scheduled action. |
| 298 | * --- |
| 299 | * default: [] |
| 300 | * --- |
| 301 | * |
| 302 | * [--group=<group>] |
| 303 | * : The group to which the next scheduled action is assigned. |
| 304 | * --- |
| 305 | * default: '' |
| 306 | * --- |
| 307 | * |
| 308 | * [--raw] |
| 309 | * : Display the raw output of as_next_scheduled_action() (timestamp or boolean). |
| 310 | * |
| 311 | * @param array $args Positional arguments. |
| 312 | * @param array $assoc_args Keyed arguments. |
| 313 | * @return void |
| 314 | */ |
| 315 | public function next( array $args, array $assoc_args ) { |
| 316 | require_once 'Action/Next_Command.php'; |
| 317 | $command = new Action\Next_Command( $args, $assoc_args ); |
| 318 | $command->execute(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Run existing scheduled action(s). |
| 323 | * |
| 324 | * ## OPTIONS |
| 325 | * |
| 326 | * <id>... |
| 327 | * : One or more IDs of actions to run. |
| 328 | * --- |
| 329 | * default: 0 |
| 330 | * --- |
| 331 | * |
| 332 | * ## EXAMPLES |
| 333 | * |
| 334 | * # Run the action with id 100 |
| 335 | * $ wp action-scheduler action run 100 |
| 336 | * |
| 337 | * # Run the actions with ids 100 and 200 |
| 338 | * $ wp action-scheduler action run 100 200 |
| 339 | * |
| 340 | * # Run the first five pending actions in 'action-scheduler' group |
| 341 | * $ wp action-scheduler action run $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) |
| 342 | * |
| 343 | * @param array $args Positional arguments. |
| 344 | * @param array $assoc_args Keyed arguments. |
| 345 | * @return void |
| 346 | */ |
| 347 | public function run( array $args, array $assoc_args ) { |
| 348 | require_once 'Action/Run_Command.php'; |
| 349 | $command = new Action\Run_Command( $args, $assoc_args ); |
| 350 | $command->execute(); |
| 351 | } |
| 352 | |
| 353 | } |
| 354 |