PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
← All changes | includes/blocks/form-builder/actions/send-emails.php +194 -0 2.7.7 → 2.14.0 View file →
@@ -1,0 +1,194 @@
1 +<?php
2 +namespace ABlocks\Blocks\FormBuilder\Actions;
3 +
4 +use ABlocks\Blocks\FormBuilder\Actions\Interfaces\FormSubmissionAction;
5 +use ABlocks\Blocks\FormBuilder\ValidateFormData;
6 +use ABlocks\Blocks\FormBuilder\EmailVerification;
7 +use Exception;
8 +use ABlocks\Classes\EmailTemplate;
9 +use ABlocks\Blocks\FormBuilder\Query;
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit;
12 +}
13 +/**
14 + * Send email to admin
15 + */
16 +final class SendEmails implements FormSubmissionAction {
17 +
18 + /** @var $validateformdata */
19 + private ValidateFormData $validateformdata;
20 +
21 + /** @var $admin_email */
22 + private string $admin_email;
23 +
24 + /** @var $user_email */
25 + private string $user_email;
26 +
27 + /** @var $form_type */
28 + private string $form_type;
29 +
30 + /** @var $config */
31 + private array $config;
32 +
33 + private array $lists;
34 +
35 + /**
36 + * Contructor
37 + *
38 + * @param ValidateFormData $obj
39 + */
40 + public function __construct( ValidateFormData $obj ) {
41 + $this->validateformdata = $obj;
42 +
43 + $this->admin_email = \get_option( 'admin_email' );
44 +
45 + $this->user_email = $this->validateformdata->form_info['info']['email'];
46 + $this->form_type = $this->validateformdata->form_info['info']['type'];
47 + $this->config = $this->validateformdata->form_info['info']['config'];
48 +
49 + $this->lists = EmailTemplate::ins(
50 + $this->validateformdata
51 + ->get_block_data()['parentAttributes']['email_template_id'] ?? ''
52 + )
53 + ->get_templates(
54 + 'subscription' === $this->validateformdata->form_info['info']['type']
55 + );
56 + }
57 +
58 + /**
59 + * Send email action
60 + *
61 + * @return void
62 + */
63 + public function action() {
64 + if (
65 + $this->validateformdata->form_info['info']['type'] === 'subscription' &&
66 + Query::is_email_exists( $this->validateformdata->form_info['info']['email'], 'subscription' )
67 + ) {
68 + return;
69 + }
70 +
71 + // wp_send_json($this->lists);
72 + foreach ( $this->lists ?? [] as $slug => $mail_templ ) {
73 + if (
74 + ! $mail_templ['status'] ||
75 + empty( $mail_templ['to'] )
76 + ) {
77 + continue;
78 + }
79 + $this->send_mail( $mail_templ );
80 + }
81 +
82 + }
83 +
84 + /**
85 + * Send email to admin from contact form
86 + *
87 + * @param string $num
88 + * @return void
89 + */
90 + private function send_mail( array $config ): void {
91 + $this->user_email = $this->validateformdata->apply_vars( $this->user_email );
92 +
93 + $to_email = $this->validateformdata->apply_vars( sanitize_text_field( $config['to'] ) );
94 +
95 + $subject = $this->validateformdata->apply_vars( sanitize_text_field( $config['subject'] ?? '' ) );
96 +
97 + $type = strtolower( sanitize_text_field( $config['format'] ?? 'html' ) );
98 +
99 + $message = $this->validateformdata->apply_vars( wp_kses_post( $config['body'] ?? '' ) );
100 + // check {all-fields} exists or not
101 + $message = empty( $message ) ? '{all-fields}' : $message;
102 + if ( preg_match( '|\{all\-fields\}|im', $message ) ) {
103 + // if exist then replace this text to data table
104 + $message = str_replace( '{all-fields}', $this->get_data_as_table_format( $type ), $message );
105 + }
106 +
107 + $headers = [];
108 +
109 + $from_email = $this->validateformdata->apply_vars( sanitize_text_field( $config['from'] ?? $this->admin_email ) );
110 + $from_name = $this->validateformdata->apply_vars( sanitize_text_field( $config['from_name'] ?? '' ) );
111 +
112 + $reply_to = $this->validateformdata->apply_vars( sanitize_text_field( $config['reply_to'] ?? '' ) );
113 + $cc = $this->validateformdata->apply_vars( sanitize_text_field( $config['cc'] ?? '' ) );
114 + $bcc = $this->validateformdata->apply_vars( sanitize_text_field( $config['bcc'] ?? '' ) );
115 +
116 + if ( $from_email ) {
117 + $headers[] = $from_name ? 'From: ' . $from_name . ' <' . $from_email . '>' : 'From: ' . $from_email;
118 + }
119 +
120 + if ( $reply_to ) {
121 + $headers[] = 'Reply-To: ' . $reply_to;
122 + }
123 + if ( $cc ) {
124 + $headers[] = 'CC: ' . implode( ',', array_unique(
125 + preg_split(
126 + '|[,\s]|', $this->validateformdata->apply_vars( strval( $cc ) )
127 + )
128 + ) );
129 + }
130 + if ( $bcc ) {
131 + $headers[] = 'BCC: ' . implode( ',', array_unique(
132 + preg_split(
133 + '|[,\s]|', $this->validateformdata->apply_vars( strval( $bcc ) )
134 + )
135 + ) );
136 + }
137 +
138 + if ( $type === 'plain' ) {
139 + $headers[] = 'Content-Type: text/plain; charset=UTF-8';
140 + }
141 + $template = $type === 'plain' ? 'email/plain-text/contact-email.php' : 'email/contact-email.php';
142 +
143 + ob_start();
144 + \ABlocks\Helper::get_template($template, [
145 + 'email' => $this->user_email,
146 + 'message' => $message,
147 + ]);
148 +
149 + $data = ob_get_clean();
150 + foreach ( array_unique( preg_split( '|[,\s]|', strval( $to_email ) ) ) as $email ) {
151 + $email = trim( $email );
152 + if ( ! empty( $email ) ) {
153 + $this->send_email( $subject, $data, $email, $headers );
154 + }
155 + }
156 +
157 + }
158 +
159 + /**
160 + * Send email
161 + *
162 + * @param string $subject
163 + * @param string $data
164 + * @param string $to
165 + * @param array $headers
166 + * @return void
167 + */
168 + private function send_email( string $subject, string $data, string $to, array $headers = [] ): void {
169 +
170 + $headers = array_merge( [ 'Content-Type: text/html; charset=UTF-8' ], $headers );
171 + if ( wp_mail( $to, $subject, $data, $headers ) ) {
172 + $this->validateformdata->set_message( __( 'Success!', 'ablocks' ) );
173 + return;
174 + }
175 +
176 + $this->validateformdata->set_error_message( __( 'Failed to send email', 'ablocks' ) );
177 + }
178 + /**
179 + * Convert data as table
180 + *
181 + * @param string $type
182 + * @return string $type
183 + */
184 + private function get_data_as_table_format( string $type ): string {
185 + $template = $type === 'plain' ? 'email/plain-text/email-table.php' : 'email/email-table.php';
186 + ob_start();
187 + \ABlocks\Helper::get_template($template, [
188 + 'data' => $this->validateformdata->form_info['data']
189 + ]);
190 +
191 + $data = ob_get_clean();
192 + return $data;
193 + }
194 +}