messaging ) { $this->messaging = new MetaMessaging(); } return $this->messaging; } public function validate( array $step ): bool { try { $valid = $this->getMessaging()->getTemplate( $step['template_id'] ) !== false; if ( ! $valid ) { /* translators: %s: WhatsApp template ID. */ throw new \Exception( sprintf( __( 'Template %s not found.', 'afroit-messaging-workflows' ), $step['template_id'] ) ); } return $valid; } catch ( \Exception $e ) { /* translators: %s: WhatsApp template ID. */ $this->errors[] = sprintf( __( 'Template %s not found.', 'afroit-messaging-workflows' ), $step['template_id'] ); return false; } } public function getValidationErrors(): array { return $this->errors; } public function getStepData( array $step ): array { 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'] ) : '', 'template_id' => isset( $step['template_id'] ) ? sanitize_text_field( $step['template_id'] ) : '', 'phone' => isset( $step['phone'] ) ? sanitize_text_field( $step['phone'] ) : '{{billing_phone}}', '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'] ) : '', ); } public function getTo(array $step) { return $this->resolveStepField( $step, [], 'phone', '{{billing_phone}}' ); } public function getMessage(array $step) { return $this->resolveMessage( $step, [], 'template_id' ); } /** * {@inheritdoc} */ public function execute( array $step, array $context, ?WorkflowJob $workflowJob = null ): ?bool { Log::info( 'SendWhatsAppAction executing', array('context' => $context) ); $order = $this->getOrderFromContext( $context ); if ( null === $order ) { Log::warning( 'SendWhatsAppAction skipped: no order in context', array( 'context' => $context ) ); return false; } $phone = $this->resolveStepField( $step, $context, 'phone', '{{billing_phone}}' ); $template_id = $this->resolveStepField( $step, $context, 'template_id' ); if ( '' === $phone || '' === $template_id ) { Log::warning( 'SendWhatsAppAction skipped: missing phone or template', array( 'context' => $context ) ); return false; } $result = $this->sendMessageTemplate( $phone, $template_id, $context, $order ); Log::info( 'SendWhatsAppAction sent', array( 'result' => $result ) ); return $result; } protected function sendMessageTemplate( $phone, $template_id, array $context, $order ) { $wa_template = $this->getMessaging()->getTemplate( $template_id ); if ( false === $wa_template ) { $this->errors[] = __( 'WhatsApp template not found.', 'afroit-messaging-workflows' ); Log::error( 'SendWhatsAppAction failed: template not found', array( 'template_id' => $template_id ) ); return false; } if ($wa_template['status'] !== 'APPROVED') { $this->errors[] = __( 'WhatsApp template not approved.', 'afroit-messaging-workflows' ); Log::error( 'SendWhatsAppAction failed: template not approved', array( 'template_id' => $template_id ) ); return false; } try { $components = $this->getTemplateComponentParameters($wa_template['components'], $context, $order); $sent = $this->getMessaging()->sendMessageTemplate( $phone, $wa_template['name'], $wa_template['language'], $components); return true; } catch ( \Exception $e ) { $this->errors[] = $e->getMessage(); Log::error( 'SendWhatsAppAction failed', array('phone' => $phone, 'template_id' => $template_id, 'error' => $e->getMessage() ) ); return false; } } protected function getTemplateComponentParameters($raw_components, $context, $order) { $components = array(); foreach ($raw_components as $component) { $type = strtolower(sanitize_text_field($component['type'] ?? '')); if ($type === 'header' && !empty($component['example']['header_text_named_params'])) { $raw_params = $component['example']['header_text_named_params']; $components[] = $this->getParametersValues($component, $raw_params, $context, $order); } if ($type === 'body' && !empty($component['example']['body_text_named_params'])) { $raw_params = $component['example']['body_text_named_params']; $components[] = $this->getParametersValues($component, $raw_params, $context, $order); } if ($type === 'buttons' && !empty($component['buttons'])) { $button_components = $this->getButtonParameterComponents($component['buttons'], $context, $order); $components = array_merge($components, $button_components); } } return $components; } /** * Build WhatsApp API button components for URL buttons that require parameters. * * @param array $buttons Template buttons from API. * @param array $context Workflow context. * @param \WC_Order|null $order Order. * @return array Button components for sendMessageTemplate. */ protected function getButtonParameterComponents(array $buttons, array $context, $order): array { $components = array(); foreach ($buttons as $index => $button) { $btn_type = strtoupper(sanitize_text_field($button['type'] ?? '')); if ($btn_type !== 'URL') { continue; } $url = $button['url'] ?? ''; if ('' === $url || strpos($url, '{{') === false) { continue; } $parameters = $this->resolveUrlButtonParameters($url, $context, $order); if (empty($parameters)) { continue; } $components[] = array( 'type' => 'button', 'sub_type' => 'url', 'index' => (int) $index, 'parameters' => $parameters, ); } return $components; } /** * Resolve {{variable}} placeholders in a URL and return WhatsApp API parameter format. * * Supports named vars ({{order_id}}, {{order_url}}) and indexed ({{1}}) for URL buttons. * * @param string $url URL with optional {{var}} placeholders. * @param array $context Workflow context. * @param \WC_Order|null $order Order. * @return array Array of { type: 'text', text: resolved_value }. */ protected function resolveUrlButtonParameters(string $url, array $context, $order): array { $parameters = array(); if (!preg_match_all('/\{\{([a-zA-Z0-9_]+)\}\}/', $url, $matches)) { return $parameters; } foreach ($matches[1] as $param_name) { $placeholder = '{{' . $param_name . '}}'; $resolved = $this->replaceVariables($placeholder, $context, $order); if ($resolved === $placeholder && is_numeric($param_name)) { $resolved = $this->resolveIndexedUrlParameter((int) $param_name, $context, $order); } $parameters[] = array( 'type' => 'text', 'text' => (string) $resolved, ); } return $parameters; } /** * Resolve indexed placeholder ({{1}}, {{2}}) for URL buttons when no named variable matches. * Default: {{1}} = order_url (common for "View Order" buttons). * * @param int $index Parameter index (1-based). * @param array $context Workflow context. * @param \WC_Order|null $order Order. * @return string Resolved value. */ protected function resolveIndexedUrlParameter(int $index, array $context, $order): string { $order = $order ?? $this->getOrderFromContext($context); if (null === $order) { return ''; } $vars = Variables::fromOrderInstance($order); $fallbacks = array(1 => 'order_id', 2 => 'order_url', 3 => 'store_url'); $var_name = $fallbacks[$index] ?? 'order_url'; return (string) $vars->get($var_name); } protected function getParametersValues($component, $raw_params, $context, $order) { $parameters = []; foreach ($raw_params as $param) { $parameters[] = [ 'type' => 'text', 'parameter_name' => $param['param_name'], 'text' => $this->replaceVariables("{{".$param['param_name']."}}", $context, $order), ]; } return [ 'type' => $component['type'], 'parameters' => $parameters, ]; } }