errors[] = __( 'Email template is required.', 'afroit-messaging-workflows' ); return false; } $subject = trim( $step['subject'] ?? '' ); if ( '' === $subject ) { $this->errors[] = __( 'Email subject is required.', 'afroit-messaging-workflows' ); return false; } $to = trim( $step['to'] ?? '' ); if ( '' === $to ) { $this->errors[] = __( 'Email recipient (To) is required.', 'afroit-messaging-workflows' ); return false; } $cc = trim( $step['cc'] ?? '' ); if ( '' !== $cc && ! $this->containsPlaceholders( $cc ) && ! $this->validateCommaSeparatedEmails( $cc ) ) { $this->errors[] = __( 'CC must be valid comma-separated email addresses.', 'afroit-messaging-workflows' ); return false; } $bcc = trim( $step['bcc'] ?? '' ); if ( '' !== $bcc && ! $this->containsPlaceholders( $bcc ) && ! $this->validateCommaSeparatedEmails( $bcc ) ) { $this->errors[] = __( 'BCC must be valid comma-separated email addresses.', 'afroit-messaging-workflows' ); return false; } $emails = $this->getWooCommerceEmails(); if ( ! isset( $emails[ $template_id ] ) ) { /* translators: %s: WooCommerce email template ID. */ $this->errors[] = sprintf( __( 'Email template "%s" not found.', 'afroit-messaging-workflows' ), $template_id ); 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'] ) : '', 'email_template_id' => isset( $step['email_template_id'] ) ? sanitize_text_field( $step['email_template_id'] ) : '', 'subject' => isset( $step['subject'] ) ? sanitize_text_field( $step['subject'] ) : '', 'to' => isset( $step['to'] ) ? sanitize_text_field( $step['to'] ) : '{{user_email}}', 'cc' => isset( $step['cc'] ) ? sanitize_text_field( $step['cc'] ) : '', 'bcc' => isset( $step['bcc'] ) ? sanitize_text_field( $step['bcc'] ) : '', '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, $context = []) { return $this->resolveStepField( $step, $context, 'to', '{{user_email}}' ); } public function getSubject(array $step, $context = []) { return $this->resolveStepField( $step, $context, 'subject', '[No Subject]' ); } public function getMessage(array $step, $context = []) { return $this->resolveMessage( $step, $context, 'email_template_id' ); } public function getMetadata(array $step, $context = []): array { return [ 'subject' => $this->getSubject( $step, $context ), 'cc' => $this->resolveStepField( $step, $context, 'cc', '' ), 'bcc' => $this->resolveStepField( $step, $context, 'bcc', '' ), ]; } /** * Check if string contains variable placeholders. * * @param string $input Input string. * @return bool True if placeholders present. */ private function containsPlaceholders( string $input ): bool { return false !== strpos( $input, '{{' ); } /** * Validate comma-separated email addresses. * * @param string $input Comma-separated emails. * @return bool True if all addresses are valid. */ private function validateCommaSeparatedEmails( string $input ): bool { $addresses = array_map( 'trim', explode( ',', $input ) ); $addresses = array_filter( $addresses ); foreach ( $addresses as $addr ) { if ( ! is_email( $addr ) ) { return false; } } return true; } /** * {@inheritdoc} */ public function execute( array $step, array $context, ?WorkflowJob $workflowJob = null ): ?bool { Log::info( 'SendEmailAction 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( 'SendEmailAction skipped: no valid order in context', array( 'context' => $context ) ); return false; } $to = $this->getTo( $step, $context ); $subject = $this->getSubject( $step, $context ); $cc = $this->resolveStepField( $step, $context, 'cc', '' ); $bcc = $this->resolveStepField( $step, $context, 'bcc', '' ); $template_id = $step['email_template_id'] ?? $step['template_id'] ?? ''; if ( '' === $to || '' === $subject ) { $this->errors[] = __( 'Email To or Subject is required.', 'afroit-messaging-workflows' ); Log::warning( 'SendEmailAction skipped: missing To or Subject', array( 'context' => $context ) ); return false; } $emails = $this->getWooCommerceEmails(); $email = $emails[ $template_id ] ?? null; if ( null === $email ) { $this->errors[] = __( 'Email template not found.', 'afroit-messaging-workflows' ); Log::error( 'SendEmailAction failed: template not found', array( 'template_id' => $template_id ) ); return false; } try { $email->setup_locale(); $email->object = $order; $email->recipient = $to; //$email->placeholders['{order_date}'] = wc_format_datetime( $order->get_date_created() ); //$email->placeholders['{order_number}'] = $order->get_order_number(); $content = $email->get_content(); $headers = $email->get_headers(); if ( '' !== $cc ) { $headers .= "\r\nCc: " . $cc; } if ( '' !== $bcc ) { $headers .= "\r\nBcc: " . $bcc; } $attachments = $email->get_attachments(); $result = $email->send( $to, $subject, $content, $headers, $attachments ); $email->restore_locale(); if ( ! $result ) { $this->errors[] = __( 'Email sending failed. Check logs if enabled for details.', 'afroit-messaging-workflows' ); Log::error( 'SendEmailAction failed', array( 'recipient' => $to, 'error' => 'wc_email returned false', 'subject' => $subject) ); return false; } return true; } catch ( \Throwable $e ) { $this->errors[] = __( 'Email sending failed. Check logs if enabled for details.', 'afroit-messaging-workflows' ); Log::error( 'SendEmailAction failed', array( 'recipient' => $to, 'error' => $e->getMessage() ) ); return false; } } /** * Get WooCommerce email templates (customer-facing, order-based). * * @return array */ public static function getWooCommerceEmails(): array { if ( ! function_exists( 'WC' ) || ! WC()->mailer() ) { return array(); } $all = WC()->mailer()->get_emails(); $out = array(); foreach ( $all as $class => $email ) { if ( ! $email instanceof \WC_Email ) { continue; } $email_id = is_string( $email->id ) ? $email->id : ''; if ( '' !== $email_id ) { $out[ $email_id ] = $email; } } return $out; } /** * Get email templates for dropdown (id => title). * * @return array */ public static function getEmailTemplatesForDropdown(): array { $emails = self::getWooCommerceEmails(); $out = array(); foreach ( $emails as $id => $email ) { $out[] = array( 'id' => $id, 'title' => $email->title ?? $id ); } return $out; } }