errors[] = __('Wait must be at least 1.', 'afroit-messaging-workflows'); return false; } $valid_units = array('minutes', 'hours', 'days'); $unit = isset($step['delay_unit']) ? sanitize_text_field($step['delay_unit']) : 'minutes'; if (! in_array($unit, $valid_units, true)) { $this->errors[] = __('Delay unit must be minutes, hours, or days.', 'afroit-messaging-workflows'); return false; } return true; } public function computeDelayInSeconds(array $step): int { $multipliers = array( 'minutes' => 60, 'hours' => 3600, 'days' => 86400, ); $delay = (int) ($step['delay'] ?? 0); $unit = isset($step['delay_unit']) && in_array($step['delay_unit'], array_keys($multipliers), true) ? $step['delay_unit'] : 'minutes'; return $delay * $multipliers[ $unit ]; } public function getValidationErrors(): array { return $this->errors; } public function getStepData(array $step): array { $valid_units = array('minutes', 'hours', 'days'); $unit = isset($step['delay_unit']) ? sanitize_text_field($step['delay_unit']) : 'minutes'; if (! in_array($unit, $valid_units, true)) { $unit = 'minutes'; } return array( 'id' => isset($step['id']) ? sanitize_text_field($step['id']) : '', 'type' => sanitize_text_field($step['type']), 'label' => isset($step['label']) ? sanitize_text_field($step['label']) : '', 'delay' => max(1, absint($step['delay'])), 'delay_unit' => $unit, 'variable' => isset($step['variable']) ? sanitize_text_field($step['variable']) : '', 'operator' => isset($step['operator']) ? sanitize_text_field($step['operator']) : '', 'value' => isset($step['value']) ? sanitize_text_field($step['value']) : '', ); } /** * Execute the wait action. * Note: This is a logical "wait" -- typically, a scheduler should handle deferred execution. * * @inheritdoc */ public function execute(array $step, array $context, ?WorkflowJob $workflowJob = null): ?bool { Log::info( 'WaitAction executing', array('context' => $context) ); $delay = $this->computeDelayInSeconds($step); $delay = (int) ($step['delay'] ?? 0); $unit = isset($step['delay_unit']) && in_array($step['delay_unit'], array('minutes', 'hours', 'days'), true) ? $step['delay_unit'] : 'minutes'; $index = $context['step_index'] ?? 0; $multipliers = array( 'minutes' => 60, 'hours' => 3600, 'days' => 86400, ); $seconds = $delay * $multipliers[ $unit ]; if ($seconds > 0) { // encure this workflow again for the waut to happen $this->engine->getQueue()->enqueue( $workflowJob->getLocalWorkflowId(), array_merge($context, array('start_at_step' => $index + 1)), // in the next run, it should not execute the wait again gmdate('Y-m-d H:i:s', strtotime('now') + $seconds), $workflowJob->id ); $this->engine->getQueue()->logExecutionResult($workflowJob, $context['step_index'], 'wait', "enqueued: {$seconds} seconds"); return true; } $this->engine->getQueue()->logExecutionResult($workflowJob, $context['step_index'], 'wait', "completed"); return false; } }