PluginProbe
Hostinger Tools / 3.0.78
Hostinger Tools v3.0.78
3.0.78 3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 All 109 releases
hostinger / vendor / woocommerce / action-scheduler / classes / WP_CLI / Action / Generate_Command.php

Generate_Command.php in Hostinger Tools 3.0.78, at vendor/woocommerce/action-scheduler/classes/WP_CLI/Action/Generate_Command.php

122 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Action_Scheduler\WP_CLI\Action;
4
5 use function \WP_CLI\Utils\get_flag_value;
6
7 /**
8 * WP-CLI command: action-scheduler action generate
9 */
10 class Generate_Command extends \ActionScheduler_WPCLI_Command {
11
12 /**
13 * Execute command.
14 *
15 * @return void
16 */
17 public function execute() {
18 $hook = $this->args[0];
19 $schedule_start = $this->args[1];
20 $callback_args = get_flag_value( $this->assoc_args, 'args', array() );
21 $group = get_flag_value( $this->assoc_args, 'group', '' );
22 $interval = (int) get_flag_value( $this->assoc_args, 'interval', 0 ); // avoid absint() to support negative intervals
23 $count = absint( get_flag_value( $this->assoc_args, 'count', 1 ) );
24
25 if ( ! empty( $callback_args ) ) {
26 $callback_args = json_decode( $callback_args, true );
27 }
28
29 $schedule_start = as_get_datetime_object( $schedule_start );
30
31 $function_args = array(
32 'start' => absint( $schedule_start->format( 'U' ) ),
33 'interval' => $interval,
34 'count' => $count,
35 'hook' => $hook,
36 'callback_args' => $callback_args,
37 'group' => $group,
38 );
39
40 $function_args = array_values( $function_args );
41
42 try {
43 $actions_added = $this->generate( ...$function_args );
44 } catch ( \Exception $e ) {
45 $this->print_error( $e );
46 }
47
48 $num_actions_added = count( (array) $actions_added );
49
50 $this->print_success( $num_actions_added, 'single' );
51 }
52
53 /**
54 * Schedule multiple single actions.
55 *
56 * @param int $schedule_start Starting timestamp of first action.
57 * @param int $interval How long to wait between runs.
58 * @param int $count Limit number of actions to schedule.
59 * @param string $hook The hook to trigger.
60 * @param array $args Arguments to pass when the hook triggers.
61 * @param string $group The group to assign this job to.
62 * @return int[] IDs of actions added.
63 */
64 protected function generate( $schedule_start, $interval, $count, $hook, array $args = array(), $group = '' ) {
65 $actions_added = array();
66
67 $progress_bar = \WP_CLI\Utils\make_progress_bar(
68 sprintf(
69 /* translators: %d is number of actions to create */
70 _n( 'Creating %d action', 'Creating %d actions', $count, 'action-scheduler' ),
71 number_format_i18n( $count )
72 ),
73 $count
74 );
75
76 for ( $i = 0; $i < $count; $i++ ) {
77 $actions_added[] = as_schedule_single_action( $schedule_start + ( $i * $interval ), $hook, $args, $group );
78 $progress_bar->tick();
79 }
80
81 $progress_bar->finish();
82
83 return $actions_added;
84 }
85
86 /**
87 * Print a success message with the action ID.
88 *
89 * @param int $actions_added Number of actions generated.
90 * @param string $action_type Type of actions scheduled.
91 * @return void
92 */
93 protected function print_success( $actions_added, $action_type ) {
94 \WP_CLI::success(
95 sprintf(
96 /* translators: %1$d refers to the total number of tasks added, %2$s is the action type */
97 _n( '%1$d %2$s action scheduled.', '%1$d %2$s actions scheduled.', $actions_added, 'action-scheduler' ),
98 number_format_i18n( $actions_added ),
99 $action_type
100 )
101 );
102 }
103
104 /**
105 * Convert an exception into a WP CLI error.
106 *
107 * @param \Exception $e The error object.
108 * @throws \WP_CLI\ExitException When an error occurs.
109 * @return void
110 */
111 protected function print_error( \Exception $e ) {
112 \WP_CLI::error(
113 sprintf(
114 /* translators: %s refers to the exception error message. */
115 __( 'There was an error creating the scheduled action: %s', 'action-scheduler' ),
116 $e->getMessage()
117 )
118 );
119 }
120
121 }
122