PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.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.9.1, at includes/blocks/form-builder/actions/send-email.php

247 lines 6.8 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 // if already subscribe, dont send any email
55 if (
56 $this->validateformdata->form_info['info']['type'] === 'subscription' &&
57 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data )
58 ) {
59 return;
60 }
61
62 // send email verification email to user
63 $this->send_verification_email();
64
65 if ( $this->form_type === 'subscription' ) {
66 $this->subscription_form_send_email();
67 }
68
69 $this->contact_form_send_email();
70 $this->contact_form_send_email( 'Two' );
71
72 }
73
74 private function send_verification_email() {
75 $id = $this->validateformdata->state_data['submission_id'] ?? null;
76 if (
77 ! boolval( $this->config['emailVerification'] ?? false ) ||
78 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data ) ||
79 empty( $this->user_email ) ||
80 empty( $id )
81 ) {
82 return;
83 }
84
85 try {
86
87 $verification_obj = new EmailVerification( $id, 'id' );
88 $verification_url = $verification_obj->get_signed_url();
89
90 $subject = __( 'Email Verification', 'ablocks' );
91
92 ob_start();
93 \ABlocks\Helper::get_template('email/email-verify.php', [
94 'email' => $this->user_email,
95 'url' => $verification_url,
96 ]);
97
98 $data = ob_get_clean();
99 $this->send_email( $subject, $data, $this->user_email );
100 } catch ( Exception $e ) {
101 wp_send_json_error( [ $e->getMessage() ] );
102 }
103
104 }
105
106 /**
107 * Send email to admin from contact form
108 *
109 * @param string $num
110 * @return void
111 */
112 private function contact_form_send_email( string $num = 'One' ): void {
113 // check email notification is enabled or not
114 $nums = [
115 'One' => '',
116 'Two' => 2,
117 ];
118
119 if (
120 ! array_key_exists( $num, $nums ) ||
121 ! in_array( 'email' . $nums[ $num ], $this->validateformdata->form_info['info']['actions'], true )
122 ) {
123 return;
124 }
125 $config = $this->validateformdata->form_info['info']['config'];
126
127 $to_email = sanitize_text_field( $config[ 'email' . $num . 'To' ] ?? $this->admin_email );
128
129 $subject = sanitize_text_field( $config[ 'email' . $num . 'Subject' ] ?? '' );
130 // translators: %s is email
131 $subject = $subject ? $subject : ( $this->user_email ? sprintf( __( 'You have a message from %s', 'ablocks' ), $this->user_email ) : __( 'You have a message', 'ablocks' ) );
132
133 $type = strtolower( sanitize_text_field( $config[ 'email' . $num . 'Type' ] ?? 'html' ) );
134 $message = sanitize_text_field( $config[ 'email' . $num . 'Message' ] ?? '' );
135 // check {all-fields} exists or not
136 if ( preg_match( '|\{all\-fields\}|im', $message ) ) {
137 // if exist then replace this text to data table
138 $message = str_replace( '{all-fields}', $this->get_data_as_table_format( $type ), $message );
139 } elseif ( $message ) {
140 // if message is defined by user and {all-fields} is not available
141 // then insert datatable at the end of msg
142 $message .= $this->get_data_as_table_format( $type );
143 } else {
144 // if msg is not defined by user, the add only datatable
145 $message = $this->get_data_as_table_format( $type );
146 }
147 $headers = [];
148
149 $from_email = sanitize_text_field( $config[ 'email' . $num . 'FormEmail' ] ?? '' );
150 $from_name = sanitize_text_field( $config[ 'email' . $num . 'FormName' ] ?? '' );
151
152 $reply_to = sanitize_text_field( $config[ 'email' . $num . 'ReplyTo' ] ?? '' );
153 $cc = sanitize_text_field( $config[ 'email' . $num . 'Cc' ] ?? '' );
154 $bcc = sanitize_text_field( $config[ 'email' . $num . 'Bcc' ] ?? '' );
155
156 if ( $from_email ) {
157 $headers[] = $from_name ? 'From: ' . $from_name . ' <' . $from_email . '>' : 'From: ' . $from_email;
158 }
159
160 if ( $reply_to ) {
161 $headers[] = 'Reply-To: ' . $reply_to;
162 }
163
164 if ( $cc ) {
165 $headers[] = 'CC: ' . $cc;
166 }
167 if ( $bcc ) {
168 $headers[] = 'BCC: ' . $bcc;
169 }
170
171 $template = $type === 'plain' ? 'email/plain-text/contact-email.php' : 'email/contact-email.php';
172
173 ob_start();
174 \ABlocks\Helper::get_template($template, [
175 'email' => $this->user_email,
176 'message' => $message,
177 ]);
178
179 $data = ob_get_clean();
180
181 $this->send_email( $subject, $data, $to_email, $headers );
182 }
183
184 /**
185 * Send email to admin from subscription form
186 *
187 * @return void
188 */
189 private function subscription_form_send_email(): void {
190
191 // detemine this request coming from subscription form & email is already exists
192 if (
193 $this->validateformdata->form_info['info']['type'] !== 'subscription' ||
194 array_key_exists( 'dont_send_subs_email', $this->validateformdata->state_data )
195 ) {
196 return;
197 }
198
199 // translators: %s is email
200 $subject = sprintf( __( 'You have a new Subscriber [%s]', 'ablocks' ), $this->user_email );
201
202 ob_start();
203 \ABlocks\Helper::get_template('email/new-subscriber.php', [
204 'email' => $this->user_email,
205 ]);
206
207 $data = ob_get_clean();
208 $this->send_email( $subject, $data, $this->admin_email );
209 }
210
211 /**
212 * Send email
213 *
214 * @param string $subject
215 * @param string $data
216 * @param string $to
217 * @param array $headers
218 * @return void
219 */
220 private function send_email( string $subject, string $data, string $to, array $headers = [] ): void {
221
222 $headers = array_merge( [ 'Content-Type: text/html; charset=UTF-8' ], $headers );
223 if ( wp_mail( $to, $subject, $data, $headers ) ) {
224 $this->validateformdata->set_message( __( 'Success!', 'ablocks' ) );
225 return;
226 }
227
228 $this->validateformdata->set_error_message( __( 'Failed to send email', 'ablocks' ) );
229 }
230 /**
231 * Convert data as table
232 *
233 * @param string $type
234 * @return string $type
235 */
236 private function get_data_as_table_format( string $type ): string {
237 $template = $type === 'plain' ? 'email/plain-text/email-table.php' : 'email/email-table.php';
238 ob_start();
239 \ABlocks\Helper::get_template($template, [
240 'data' => $this->validateformdata->form_info['data']
241 ]);
242
243 $data = ob_get_clean();
244 return $data;
245 }
246 }
247