PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 3.0.60
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v3.0.60
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 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 All 38 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.60, at compatibility/cf7/CF7Frontend.class.php

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