PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 3.0.72
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v3.0.72
5.12.0 5.13.0 5.13.1 5.11.0 5.10.0 5.9.0 5.8.0 5.8.1 5.7.0 5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 All 47 releases
double-opt-in / compatibility / cf7 / CF7Frontend.class.php

CF7Frontend.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 3.0.72, at compatibility/cf7/CF7Frontend.class.php

309 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace forge12\contactform7\CF7DoubleOptIn {
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use forge12\plugins\ContactForm7;
9
10 /**
11 * Class Frontend
12 * Responsible to handle the frontend of the Double OptIn
13 *
14 * @package forge12\contactform7\CF7DoubleOptIn
15 */
16 class CF7Frontend extends OptInFrontend {
17 protected $OptIn = null;
18
19 /**
20 * Admin constructor.
21 */
22 public function __construct() {
23
24 parent::__construct( 'cf7' );
25 add_action( 'wpcf7_before_send_mail', array( $this, 'onSubmit' ), 5, 3 );
26 }
27
28 /**
29 * Get the recipient email address
30 *
31 * @param string $recipient The recipient email address
32 * @param array $formParameter The form parameter array
33 * @param array $postParameter The post parameter array
34 *
35 * @return string The sanitized recipient email address
36 */
37 public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string {
38 /**
39 * Ensure the recipient has been defined in the settings
40 */
41 if ( ! isset( $formParameter['recipient'] ) ) {
42 return $recipient;
43 }
44
45 $recipient = $formParameter['recipient'];
46
47 $recipient = str_replace( '[', '', $recipient );
48 $recipient = str_replace( ']', '', $recipient );
49
50 /**
51 * Get the Recipient from the post parameter
52 */
53 if ( isset( $postParameter[ $recipient ] ) ) {
54 $recipient = sanitize_email( $_POST[ $recipient ] );
55 }
56
57 return $recipient;
58 }
59
60 /**
61 * Add File after submission and confirming opt in.
62 * @param $form
63 * @param $abort
64 * @param $submission
65 *
66 * @return void
67 */
68 public function attachExtraAttachments( $form, $abort, $submission ) {
69 if ( $this->OptIn != null && $this->OptIn->get_files() != null ) {
70 $fileWrapper = maybe_unserialize($this->OptIn->get_files());
71 if(is_array($fileWrapper)) {
72 foreach ( $fileWrapper as $file ) {
73 $submission->add_extra_attachments( $file );
74 }
75 }
76 }
77 }
78
79 /**
80 * Send the default email for the given OptIn. (Original Mail after Opt-In Confirmation)
81 *
82 * @param OptIn $OptIn The OptIn object containing the email content and form ID.
83 *
84 * @return void
85 */
86 public function sendDefaultMail( OptIn $OptIn ): void {
87 if ( ! function_exists( 'wpcf7' ) ) {
88 return;
89 }
90
91 $this->OptIn = $OptIn;
92 /**
93 * Prepare the Post Data
94 */
95 $data = maybe_unserialize( $OptIn->get_content() );
96
97 /**
98 * Sanitize the Data
99 */
100 $_POST = SanitizeHelper::sanitize_array( $data );
101
102 /**
103 * Prepare the Contact Form
104 */
105 $ContactForm = \WPCF7_ContactForm::get_instance( $OptIn->get_cf_form_id() );
106
107 /**
108 * Attach Files before send mail
109 */
110 add_action( 'wpcf7_before_send_mail', [ $this, 'attachExtraAttachments' ], 10, 3 );
111
112 /**
113 * Prepare the Submission Instance
114 */
115 $submission = \WPCF7_Submission::get_instance( $ContactForm );
116 }
117
118 /**
119 * On Form Submit add the double optin if enabled
120 *
121 * @param $form \WPCF7_ContactForm
122 * @param bool
123 * @param $submission \WPCF7_Submission
124 */
125 public function onSubmit( $form, &$abort, $submission ) {
126 $parameter = CF7DoubleOptIn::getInstance()->getParameter( $form->id() );
127
128 if ( $this->isOptinEnabled( $form->id() ) ) {
129 // Remove Contact Form 7 DB hook to ensure the optin mail is not saved
130 remove_action( 'wpcf7_before_send_mail', 'cfdb7_before_send_mail' );
131
132 if ( apply_filters( 'f12_cf7_doubleoptin_skip_option', false, $form->id(), $submission->get_posted_data(), $this->type ) ) {
133 return; // Skip if requested
134 }
135 // Get the OptIn Object
136 $OptIn = $this->maybeCreateOptIn( $form->id(), $form->form_html(), $submission->get_posted_data(), $submission->uploaded_files() );
137
138 /**
139 * Skip if OptIn not created
140 */
141 if ( ! $OptIn ) {
142 return;
143 }
144
145 // Add the Form URL to the parameters
146 $parameter['formUrl'] = $submission->get_meta( 'url' );
147
148 // Get the Body content
149 $body = $parameter['body'];
150
151 // Replace the Placeholders within the body content.
152 $body = $this->addPlaceholders( $body, $OptIn, $parameter );
153
154 /**
155 * Body
156 *
157 * Filters the Body of the OptIn Mail. Allows developers to adjust the content
158 * before transmission.
159 *
160 * @param string $body The content of the OptIn Mail.
161 *
162 * @since 2.3.3
163 */
164 $body = apply_filters( 'f12_cf7_doubleoptin_body', $body );
165
166 /**
167 * Set the OptIn Mail Content to the OptIn Object.
168 */
169 $OptIn->set_mail_optin( $body );
170 $OptIn->save();
171
172 /**
173 * Recipient
174 */
175 $additional_headers = '';
176
177 /**
178 * OptIn Mail Arguments
179 *
180 * Filters the Arguments for the OptIn Mail. This hook allows developers
181 * to add additional content to the Mail.
182 *
183 * @formatter:off
184 *
185 * @param $args {
186 * @type string $subject The Subject of the OptIn Mail
187 * @type string $body The Body of the OptIn Mail,
188 * @type string $sender The Sender of the OptIn Mail
189 * @type string $recipient The recipient of the OptIn Mail
190 * @type bool $use_html Enable/Disable HTML for the OptIn Mail. Default: true
191 * @type string $additional_headers Additional Header Information for the Mail.
192 * }
193 *
194 * @formatter:on
195 *
196 * @since 2.3.3
197 */
198 $args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [
199 'subject' => $parameter['subject'],
200 'body' => $body,
201 'sender' => $parameter['sender'],
202 'sender_name' => $parameter['sender_name'],
203 'recipient' => $parameter['recipient'],
204 'use_html' => true,
205 'additional_headers' => $additional_headers
206 ] );
207
208 /**
209 * Set the Header
210 */
211 if ( ! empty( $parameter['sender_name'] ) ) {
212 $args['additional_headers'] .= 'From: ' . $args['sender_name'] . ' <' . $args['sender'] . '>';
213 }
214
215 // Prepare the Opt-In Mail
216 \WPCF7_Mail::send( $args, 'mail' );
217
218 // Skip all mails following
219 add_filter( 'wpcf7_skip_mail', '__return_true' );
220
221 /**
222 * Action after the OptIn Mail has been sent.
223 *
224 * Allows other developers to do additional tasks after the OptIn Mail has been submitted.
225 *
226 * @formatter:off
227 *
228 * @param \WPCF7_ContactForm $form The Contact Form 7 Form Object
229 * @param int $formId The Post ID of the CF7 Form.
230 *
231 * @formatter:on
232 *
233 * @since 2.3.3
234 */
235 do_action( 'f12_cf7_doubleoptin_sent', $form, $form->id() );
236
237 } else if ( isset( $_GET['optin'] ) ) {
238 /**
239 * Add Files / Attachments to the OptIn Mail.
240 */
241 $hash = sanitize_text_field( $_GET['optin'] );
242
243 /**
244 * Get the OptIn Object
245 */
246 $OptIn = OptIn::get_by_hash( $hash );
247
248 /**
249 * Skip if the OptIn has not been found.
250 */
251 if ( null == $OptIn ) {
252 return;
253 }
254
255 /**
256 * Get the Files from the OptIn Object
257 */
258 $files = maybe_unserialize( $OptIn->get_files() );
259
260 /**
261 * Skip if no files has been found
262 */
263 if ( empty( $files ) ) {
264 return;
265 }
266
267 /**
268 * Loop through all attachments/files.
269 */
270 foreach ( $files as $file ) {
271 /**
272 * Skip file if the name is empty
273 */
274 if ( ! empty( $file ) ) {
275 continue;
276 }
277
278 /**
279 * Enable / Disable the OptIn Mail Attachment for Mail 1.
280 *
281 * Allows Developers to Enable/Disable Mail attachments dynamically.
282 *
283 * @param bool $status Either add attachments (true) or not (false). Default: true
284 * @param OptIn $OptIn The OptIn Object.
285 *
286 * @since 2.3.3
287 */
288 if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_1', true, $OptIn ) ) {
289 $submission->add_extra_attachments( $file );
290 }
291
292 /**
293 * Enable / Disable the OptIn Mail Attachment for Mail 2.
294 *
295 * Allows Developers to Enable/Disable Mail attachments dynamically.
296 *
297 * @param bool $status Either add attachments (true) or not (false). Default: true
298 * @param OptIn $OptIn The OptIn Object.
299 *
300 * @since 2.3.3
301 */
302 if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_2', true, $OptIn ) ) {
303 $submission->add_extra_attachments( $file, 'mail_2' );
304 }
305 }
306 }
307 }
308 }
309 }