twilio ) { $this->twilio = new TwilioClient(); } return $this->twilio; } /** * {@inheritdoc} */ public function validate( array $step ): bool { $message = trim( $step['message'] ?? '' ); if ( '' === $message ) { $this->errors[] = __( 'SMS message is required.', 'afroit-messaging-workflows' ); return false; } if ( ! $this->getTwilio()->is_configured() ) { $this->errors[] = __( 'Twilio is not configured. Add Account SID, Auth Token, and From number in settings.', 'afroit-messaging-workflows' ); return false; } return true; } /** * {@inheritdoc} */ public function getValidationErrors(): array { return $this->errors; } /** * {@inheritdoc} */ 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'] ) : '', 'message' => isset( $step['message'] ) ? sanitize_textarea_field( $step['message'] ) : '', '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, [], 'message' ); } /** * {@inheritdoc} */ public function execute( array $step, array $context, ?WorkflowJob $workflowJob = null ): ?bool { Log::info( 'SendSmsAction executing', array( 'step' => $step, 'context' => $context ) ); $order = $this->getOrderFromContext( $context ); if ( null === $order ) { $this->errors[] = __( 'No valid order in context.', 'afroit-messaging-workflows' ); Log::warning( 'SendSmsAction skipped: no valid order in context', array( 'context' => $context ) ); return false; } $phone = $this->resolveStepField( $step, $context, 'phone', '{{billing_phone}}' ); $message = $this->resolveMessage( $step, $context, 'message' ); if ( '' === $phone || '' === $message ) { $this->errors[] = __( 'SMS phone or message is required.', 'afroit-messaging-workflows' ); Log::warning( 'SendSmsAction skipped: missing phone or message', array( 'context' => $context ) ); return false; } $result = $this->getTwilio()->sendSms( $phone, $message ); //$result = array( 'success' => true, 'sid' => '1234567890' ); if ( ! $result['success'] ) { $this->errors[] = $result['error']; Log::error( 'SendSmsAction failed', array( 'phone' => $phone, 'error' => $result['error'] ?? '' ) ); return false; } return true; } }