PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.1
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 1.1.2 All 80 releases
ablocks / includes / blocks / form-builder / actions / send-email.php

send-email.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.1, at includes/blocks/form-builder/actions/send-email.php

289 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * Send email to admin
14 */
15 final class SendEmail implements FormSubmissionAction {
16
17 /** @var $validateformdata */
18 private ValidateFormData $validateformdata;
19
20 /** @var $admin_email */
21 private string $admin_email;
22
23 /** @var $user_email */
24 private string $user_email;
25
26 /** @var $form_type */
27 private string $form_type;
28
29 /** @var $config */
30 private array $config;
31
32 /**
33 * Contructor
34 *
35 * @param ValidateFormData $obj
36 */
37 public function __construct( ValidateFormData $obj ) {
38 $this->validateformdata = $obj;
39
40 $this->admin_email = \get_option( 'admin_email' );
41
42 $this->user_email = $this->validateformdata->form_info['info']['email'];
43 $this->form_type = $this->validateformdata->form_info['info']['type'];
44 $this->config = $this->validateformdata->form_info['info']['config'];
45 }
46
47 /**
48 * Send email action
49 *
50 * @return void
51 */
52 public function action() {
53
54 // wp_send_json($this->validateformdata->form_info['info']);
55 // if already subscribe, dont send any email
56 if (
57 $this->validateformdata->form_info['info']['type'] === 'subscription' &&
58 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data )
59 ) {
60 return;
61 }
62
63 // send email verification email to user
64 $this->send_verification_email();
65
66 if ( $this->form_type === 'subscription' &&
67 ( $this->validateformdata->form_info['info']['config']['adminNotify'] ?? true )
68 ) {
69 $this->subscription_form_send_email();
70 }
71
72 // $this->contact_form_send_email();
73 // $this->contact_form_send_email( 'Two' );
74 }
75
76 private function send_verification_email() {
77 $id = $this->validateformdata->state_data['submission_id'] ?? null;
78 if (
79 ! boolval( $this->config['emailVerification'] ?? false ) ||
80 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data ) ||
81 empty( $this->user_email ) ||
82 empty( $id )
83 ) {
84 return;
85 }
86
87 try {
88
89 $verification_obj = new EmailVerification( $id, 'id' );
90 $verification_url = $verification_obj->get_signed_url();
91
92 $subject = __( 'Email Verification', 'ablocks' );
93
94 ob_start();
95 \ABlocks\Helper::get_template('email/email-verify.php', [
96 'email' => $this->user_email,
97 'url' => $verification_url,
98 ]);
99
100 $data = ob_get_clean();
101 $this->send_email( $subject, $data, $this->user_email );
102 } catch ( Exception $e ) {
103 wp_send_json_error( [ $e->getMessage() ] );
104 }
105
106 }
107
108 /**
109 * Send email to admin from contact form
110 *
111 * @param string $num
112 * @return void
113 */
114 private function contact_form_send_email( string $num = 'One' ): void {
115 $this->user_email = $this->apply_vars( $this->user_email );
116 // check email notification is enabled or not
117 $nums = [
118 'One' => '',
119 'Two' => 2,
120 ];
121
122 if (
123 ! array_key_exists( $num, $nums ) ||
124 ! in_array( 'email' . $nums[ $num ], $this->validateformdata->form_info['info']['actions'], true )
125 ) {
126 return;
127 }
128 $config = $this->validateformdata->form_info['info']['config'];
129
130 $to_email = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'To' ] ?? $this->admin_email ) );
131
132 $subject = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'Subject' ] ?? '' ) );
133 // translators: %s is email
134 $subject = $subject ? $subject : ( $this->user_email ? sprintf( __( 'You have a message from %s', 'ablocks' ), $this->user_email ) : __( 'You have a message', 'ablocks' ) );
135
136 $type = strtolower( sanitize_text_field( $config[ 'email' . $num . 'Type' ] ?? 'html' ) );
137 $message = $this->apply_vars( sanitize_text_field( $config[ 'email' . $num . 'Message' ] ?? '' ) );
138 // check {all-fields} exists or not
139 $message = empty( $message ) ? '{all-fields}' : $message;
140 if ( preg_match( '|\{all\-fields\}|im', $message ) ) {
141 // if exist then replace this text to data table
142 $message = str_replace( '{all-fields}', $this->get_data_as_table_format( $type ), $message );
143 }
144 // elseif ( $message ) {
145 // if message is defined by user and {all-fields} is not available
146 // then insert datatable at the end of msg
147 // $message .= $this->get_data_as_table_format( $type );
148 // } else {
149 // if msg is not defined by user, the add only datatable
150 // $message = $this->get_data_as_table_format( $type );
151 // }
152 $headers = [];
153
154 $from_email = sanitize_text_field( $config[ 'email' . $num . 'FormEmail' ] ?? '' );
155 $from_name = sanitize_text_field( $config[ 'email' . $num . 'FormName' ] ?? '' );
156
157 $reply_to = sanitize_text_field( $config[ 'email' . $num . 'ReplyTo' ] ?? '' );
158 $cc = sanitize_text_field( $config[ 'email' . $num . 'Cc' ] ?? '' );
159 $bcc = sanitize_text_field( $config[ 'email' . $num . 'Bcc' ] ?? '' );
160
161 if ( $from_email ) {
162 $headers[] = $from_name ? 'From: ' . $from_name . ' <' . $from_email . '>' : 'From: ' . $from_email;
163 }
164
165 if ( $reply_to ) {
166 $headers[] = 'Reply-To: ' . $reply_to;
167 }
168 if ( $cc ) {
169 $headers[] = 'CC: ' . implode( ',', array_unique(
170 preg_split(
171 '|[,\s]|', $this->apply_vars( strval( $cc ) )
172 )
173 ) );
174 }
175 if ( $bcc ) {
176 $headers[] = 'BCC: ' . implode( ',', array_unique(
177 preg_split(
178 '|[,\s]|', $this->apply_vars( strval( $bcc ) )
179 )
180 ) );
181 }
182
183 if ( $type === 'plain' ) {
184 $headers[] = 'Content-Type: text/plain; charset=UTF-8';
185 }
186 $template = $type === 'plain' ? 'email/plain-text/contact-email.php' : 'email/contact-email.php';
187
188 ob_start();
189 \ABlocks\Helper::get_template($template, [
190 'email' => $this->user_email,
191 'message' => $message,
192 ]);
193
194 $data = ob_get_clean();
195 foreach ( array_unique( preg_split( '|[,\s]|', strval( $to_email ) ) ) as $email ) {
196 $email = trim( $email );
197 if ( ! empty( $email ) ) {
198 $this->send_email( $subject, $data, $email, $headers );
199 }
200 }
201
202 }
203
204 private function apply_vars( ?string $msg ): ?string {
205 $admin_email = get_option( 'admin_email' );
206 $current_user = wp_get_current_user();
207 $current_user_email = $current_user->user_email;
208
209 $fields = array_merge(
210 $this->validateformdata->form_info['data'] ?? [],
211 [
212 'admin_email' => [ 'value' => $admin_email ],
213 'user_email' => [ 'value' => $current_user_email ]
214 ]
215 );
216 // wp_send_json($fields);
217 foreach ( $fields as $key => [ 'value' => $val ] ) {
218 if ( is_array( $val ) ) {
219 $val = implode( ', ', $val );
220 }
221 $msg = str_replace( "{{$key}}", $val, $msg );
222 }
223 return $msg;
224 }
225
226 /**
227 * Send email to admin from subscription form
228 *
229 * @return void
230 */
231 private function subscription_form_send_email(): void {
232
233 // detemine this request coming from subscription form & email is already exists
234 if (
235 $this->validateformdata->form_info['info']['type'] !== 'subscription' ||
236 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data )
237 ) {
238 return;
239 }
240
241 // translators: %s is email
242 $subject = sprintf( __( 'You have a new Subscriber [%s]', 'ablocks' ), $this->user_email );
243
244 ob_start();
245 \ABlocks\Helper::get_template('email/new-subscriber.php', [
246 'email' => $this->user_email,
247 ]);
248
249 $data = ob_get_clean();
250 $this->send_email( $subject, $data, $this->admin_email );
251 }
252
253 /**
254 * Send email
255 *
256 * @param string $subject
257 * @param string $data
258 * @param string $to
259 * @param array $headers
260 * @return void
261 */
262 private function send_email( string $subject, string $data, string $to, array $headers = [] ): void {
263
264 $headers = array_merge( [ 'Content-Type: text/html; charset=UTF-8' ], $headers );
265 if ( wp_mail( $to, $subject, $data, $headers ) ) {
266 $this->validateformdata->set_message( __( 'Success!', 'ablocks' ) );
267 return;
268 }
269
270 $this->validateformdata->set_error_message( __( 'Failed to send email', 'ablocks' ) );
271 }
272 /**
273 * Convert data as table
274 *
275 * @param string $type
276 * @return string $type
277 */
278 private function get_data_as_table_format( string $type ): string {
279 $template = $type === 'plain' ? 'email/plain-text/email-table.php' : 'email/email-table.php';
280 ob_start();
281 \ABlocks\Helper::get_template($template, [
282 'data' => $this->validateformdata->form_info['data']
283 ]);
284
285 $data = ob_get_clean();
286 return $data;
287 }
288 }
289