*/ protected array $errors = array(); /** * The workflow engine. * @var WorkflowEngine */ protected ?WorkflowEngine $engine = null; /** * The constructor. * * @param WorkflowEngine $engine The workflow engine executing the action. */ public function __construct( ?WorkflowEngine $engine = null ) { $this->engine = $engine; } /** * Validate the action. * * @param array $step Step configuration. * @return bool */ public function validate(array $step): bool { return true; } public function getValidationErrors(): array { return $this->errors; } public function getExecutionErrors() { if ( $this->errors ) { return implode( ' | ', $this->errors ); } } public function getStepData(array $step): array { return array(); } public function getMetadata(array $step, $context = []): array { return []; } /** * Execute the action. * * @param array $step Step configuration. * @param array $context Workflow context. * @param WorkflowJob|null $workflowJob Workflow job. * @return bool|null True if the action was executed successfully and workflow should continue, false if the action failed and execution would need to be stopped, null if the action executed but workflow should pause. */ public function execute(array $step, array $context, ?WorkflowJob $workflowJob = null): ?bool { // Do nothing by default. return true; // continue workflow execution } /** * Check the condition. * * @param array $step Step configuration. * @param array $context Workflow context. * @param WorkflowJob|null $workflowJob Workflow job. * @return bool */ public function evaluateCondition(array $step, $context, ?WorkflowJob $workflowJob = null): bool { $variable = trim($step['variable'] ?? ''); $operator = trim($step['operator'] ?? ''); $value = trim($step['value'] ?? ''); if ('' === $variable || '' === $operator || '' === $value) { return true; } $variableData = Variables::getVariables($variable); $handler = $variableData['condition'] ? new $variableData['condition']() : null; if (null === $handler) { return false; // condition not found } try { $condition = compact('variable', 'operator', 'value'); if (! $handler->evaluate($condition, $context)) { return false; } } catch (\Exception $e) { Log::error('BaseAction: error evaluating condition', array( 'condition' => $condition, 'context' => $context, 'error' => $e->getMessage(), )); return false; } return true; } public function getConditionString(array $step): string { $variable = trim($step['variable'] ?? ''); $operator = trim($step['operator'] ?? ''); $value = trim($step['value'] ?? ''); if ('' === $variable || '' === $operator || '' === $value) { return ''; } $operator = Conditions::getOperators()[$operator] ?? ''; return "{$variable} {$operator} {$value}"; } /** * Get WC_Order from context. * * @param array $context Context. * @return \WC_Order|AbandonedOrder|null */ protected function getOrderFromContext(array $context) { $order_id = $context['order_id'] ?? null; $order = null; if (null !== $order_id) { $order = wc_get_order($order_id); $order = $order instanceof \WC_Order ? $order : null; } if (null === $order) { $abandoned_cart_id = $context['abandoned_cart_id'] ?? null; if (null !== $abandoned_cart_id) { $order = new AbandonedOrder($abandoned_cart_id); if (empty($order->get_cart_data())) { $order = null; } } } return $order; } /** * Replace {{variable}} placeholders in string using order context. * * @param string $string Raw string. * @param array $context Context. * @param \WC_Order|null $order Order. * @return string */ protected function replaceVariables(string $string, array $context, $order = null): string { if (empty($context)) { return $string; } $order = $order ?? $this->getOrderFromContext($context); if (null === $order) { return $string; } $variables = Variables::fromOrderInstance($order); if (preg_match_all('/\{\{([a-zA-Z0-9_]+)\}\}/', $string, $matches)) { foreach ($matches[0] as $placeholder) { $value = $variables->get($placeholder); $string = str_replace($placeholder, (string) $value, $string); } } return $string; } /** * Resolve message/text from step config with variable replacement. * * @param array $step Step config. * @param array $context Context. * @param string $key Step key containing the message (default 'message'). * @return string */ protected function resolveMessage(array $step, array $context, string $key = 'message'): string { return $this->resolveStepField($step, $context, $key, ''); } /** * Resolve a step field with variable replacement. * * @param array $step Step config. * @param array $context Context. * @param string $key Step key. * @param string $default Default value if key missing. * @return string */ protected function resolveStepField(array $step, array $context, string $key, string $default = ''): string { $value = trim($step[$key] ?? $default); if ('' === $value) { return ''; } return $this->replaceVariables($value, $context); } /** * Resolve recipient phone from context. * * @param array $context Context. * @return string */ protected function resolvePhone(array $context): string { if (!empty($context['phone'])) { return (string) $context['phone']; } $order = $this->getOrderFromContext($context); if ($order instanceof \WC_Order) { return (string) $order->get_billing_phone(); } return ''; } }