PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 3.0.1
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v3.0.1
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 / avada / AvadaFrontend.class.php

AvadaFrontend.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 3.0.1, at compatibility/avada/AvadaFrontend.class.php

248 lines 7.1 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 /**
9 * Class Frontend
10 * Responsible to handle the frontend of the Double OptIn
11 *
12 * @package forge12\contactform7\CF7DoubleOptIn
13 */
14 class AvadaFrontend extends OptInFrontend {
15 /**
16 * Admin constructor.
17 */
18 public function __construct() {
19 parent::__construct( 'avada' );
20
21 if ( ! isset( $_GET['optin'] ) ) {
22 add_filter( 'fusion_form_send_mail_args', array( $this, 'onSubmit' ), 10, 3 );
23 }
24 }
25
26 /**
27 * Send the default email for the given OptIn. (Original Mail after Opt-In Confirmation)
28 *
29 * @param OptIn $OptIn The OptIn object containing the email content and form ID.
30 *
31 * @return void
32 */
33 public function sendDefaultMail( OptIn $OptIn ): void {
34 /**
35 * Get the Form Data
36 */
37 $formData = maybe_unserialize( $OptIn->get_content() );
38
39 /**
40 * Sanitize the data
41 */
42 $formData = SanitizeHelper::sanitize_array( $formData );
43
44 /**
45 * check if the formdata is available
46 */
47 if ( ! isset( $formData['data'] ) ) {
48 return;
49 }
50
51 /**
52 * Transform the Data to a key=>value string.
53 *
54 * @example
55 * $data = ['e-mail' => '[email protected]', 'subject' => 'Betreff']
56 * to
57 * $output = [email protected]&subject=Betreff
58 */
59 $data = $formData['data'];
60 $formDataString = [];
61 foreach ( $data as $key => $value ) {
62 $formDataString[] = $key . '=' . $value;
63 }
64 $formDataString = implode( "&", $formDataString );
65
66 /**
67 * Reformat them to work with avada submit
68 */
69 $_POST = array(
70 'formData' => $formDataString,
71 'field_labels' => json_encode( $formData['field_labels'] ),
72 'field_types' => json_encode( $formData['field_types'] ),
73 'hidden_field_names' => json_encode( $formData['hidden_field_names'] )
74 );
75
76 $_POST['form_id'] = $OptIn->get_cf_form_id();
77
78 /**
79 * Load the Avada Form Submit Object
80 */
81 require_once( 'AvadaFormSubmit.class.php' );
82
83 $AvadaFormSubmit = new AvadaFormSubmit();
84
85 /**
86 * Submit the form
87 */
88 $AvadaFormSubmit->submit( $OptIn->get_cf_form_id(), [] );
89 }
90
91 /**
92 * On Form Submit add the double optin if enabled
93 *
94 * @param array $parameter
95 * @param int $post_id
96 * @param array $data
97 */
98 public function onSubmit( $form_parameter, $submission_id, $form_data ) {
99 $post_id = isset( $_POST['form_id'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification
100
101 if ( ! $post_id ) {
102 return $form_parameter;
103 }
104
105 $parameter = CF7DoubleOptIn::getInstance()->getParameter( $post_id );
106
107 if ( $this->isOptinEnabled( $post_id ) ) {
108
109 // Get the Post Object
110 $form = get_post( $post_id );
111
112 if ( ! $form ) {
113 return $form_parameter;
114 }
115
116 $form_data['form_parameter'] = $form_parameter;
117
118 // Get the OptIn Object
119 $OptIn = $this->maybeCreateOptIn( $post_id, do_shortcode( $form->post_content ), $form_data, $form_parameter['attachments'] );
120
121 /**
122 * Skip if OptIn not created
123 */
124 if ( ! $OptIn ) {
125 return $form_parameter;
126 }
127
128 // Add the Form URL
129 $parameter['formUrl'] = $form_data['submission']['source_url'];
130
131 // Get the Body content
132 $body = $parameter['body'];
133
134 // Replace the Placeholders within the body content.
135 $body = $this->addPlaceholders( $body, $OptIn, $parameter );
136
137 /**
138 * Body
139 *
140 * Filters the Body of the OptIn Mail. Allows developers to adjust the content
141 * before transmission.
142 *
143 * @param string $body The content of the OptIn Mail.
144 *
145 * @since 2.3.3
146 */
147 $body = apply_filters( 'f12_cf7_doubleoptin_body', $body );
148
149 /**
150 * Set the OptIn Mail Content to the OptIn Object.
151 */
152 $OptIn->set_mail_optin( $body );
153 $OptIn->save();
154
155 /**
156 * Recipient
157 */
158 $headers = 'MIME-Version: 1.0' . "\r\n";
159 $headers .= 'Content-type: text/html; charset=UTF8' . "\r\n";
160
161 /**
162 * OptIn Mail Arguments
163 *
164 * Filters the Arguments for the OptIn Mail. This hook allows developers
165 * to add additional content to the Mail.
166 *
167 * @formatter:off
168 *
169 * @param $args {
170 * @type string $subject The Subject of the OptIn Mail
171 * @type string $body The Body of the OptIn Mail,
172 * @type string $sender The Sender of the OptIn Mail
173 * @type string $recipient The recipient of the OptIn Mail
174 * @type bool $use_html Enable/Disable HTML for the OptIn Mail. Default: true
175 * @type string $additional_headers Additional Header Information for the Mail.
176 * }
177 *
178 * @formatter:on
179 *
180 * @since 2.3.3
181 */
182 $args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [
183 'subject' => $parameter['subject'],
184 'body' => $body,
185 'sender' => $parameter['sender'],
186 'sender_name' => $parameter['sender_name'],
187 'recipient' => $form_data['data'][ $parameter['recipient'] ],
188 'use_html' => true,
189 'additional_headers' => $headers
190 ] );
191
192 /*
193 * Adjust the header
194 */
195 $args['additional_headers'] .= sprintf( 'From: %s <%s>' . "\r\n", $args['sender_name'], $args['sender'] );
196
197 $args['additional_headers'] .= 'From: ' . $args['sender'] . "\r\n";
198
199 \wp_mail(
200 $args['recipient'],
201 $args['subject'],
202 $args['body'],
203 $args['additional_headers']
204 );
205
206 /**
207 * Action after the OptIn Mail has been sent.
208 *
209 * Allows other developers to do additional tasks after the OptIn Mail has been submitted.
210 *
211 * @formatter:off
212 *
213 * @param \WPCF7_ContactForm $form The Contact Form 7 Form Object
214 * @param int $formId The Post ID of the CF7 Form.
215 *
216 * @formatter:on
217 *
218 * @since 2.3.3
219 */
220 do_action( 'f12_cf7_doubleoptin_sent', $form, $post_id );
221
222 /**
223 * Die after sending the form
224 */
225 die( wp_json_encode( [ 'status' => 'success', 'info' => 'opt-in send' ] ) );
226 }
227
228 return $form_parameter;
229 }
230
231 /**
232 * Retrieves the recipient value from the given parameters.
233 *
234 * @param string $recipient The recipient parameter.
235 * @param array $formParameter The form parameters.
236 * @param array $postParameter The post parameters.
237 *
238 * @return string The recipient value if it exists in the post parameters, or an empty string if it doesn't.
239 */
240 public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string {
241 if ( ! isset( $postParameter['data'][ $formParameter['recipient'] ] ) ) {
242 return '';
243 }
244
245 return $postParameter['data'][ $formParameter['recipient'] ];
246 }
247 }
248 }