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 / avada / AvadaFrontend.class.php

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

249 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 if ( is_array( $value ) ) {
63 $value = implode( ',', $value );
64 }
65 $formDataString[] = $key . '=' . $value;
66 }
67 $formDataString = implode( "&", $formDataString );
68
69 /**
70 * Reformat them to work with avada submit
71 */
72 $_POST = array(
73 'formData' => $formDataString,
74 'field_labels' => json_encode( $formData['field_labels'] ),
75 'field_types' => json_encode( $formData['field_types'] ),
76 'hidden_field_names' => json_encode( $formData['hidden_field_names'] )
77 );
78
79 $_POST['form_id'] = $OptIn->get_cf_form_id();
80
81 /**
82 * Load the Avada Form Submit Object
83 */
84 require_once( 'AvadaFormSubmit.class.php' );
85
86 $AvadaFormSubmit = new AvadaFormSubmit();
87
88 /**
89 * Submit the form
90 */
91 $AvadaFormSubmit->submit( $OptIn->get_cf_form_id(), [] );
92 }
93
94 /**
95 * On Form Submit add the double optin if enabled
96 *
97 * @param array $parameter
98 * @param int $post_id
99 * @param array $data
100 */
101 public function onSubmit( $form_parameter, $submission_id, $form_data ) {
102 $post_id = isset( $_POST['form_id'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification
103
104 if ( ! $post_id ) {
105 return $form_parameter;
106 }
107
108 $parameter = CF7DoubleOptIn::getInstance()->getParameter( $post_id );
109
110 if ( $this->isOptinEnabled( $post_id ) ) {
111
112 // Get the Post Object
113 $form = get_post( $post_id );
114
115 if ( ! $form ) {
116 return $form_parameter;
117 }
118
119 $form_data['form_parameter'] = $form_parameter;
120
121 // Get the OptIn Object
122 $OptIn = $this->maybeCreateOptIn( $post_id, do_shortcode( $form->post_content ), $form_data, $form_parameter['attachments'] );
123
124 /**
125 * Skip if OptIn not created
126 */
127 if ( ! $OptIn ) {
128 return $form_parameter;
129 }
130
131 // Add the Form URL
132 $parameter['formUrl'] = $form_data['submission']['source_url'];
133
134 // Get the Body content
135 $body = $parameter['body'];
136
137 // Replace the Placeholders within the body content.
138 $body = $this->addPlaceholders( $body, $OptIn, $parameter );
139
140 /**
141 * Body
142 *
143 * Filters the Body of the OptIn Mail. Allows developers to adjust the content
144 * before transmission.
145 *
146 * @param string $body The content of the OptIn Mail.
147 *
148 * @since 2.3.3
149 */
150 $body = apply_filters( 'f12_cf7_doubleoptin_body', $body );
151
152 /**
153 * Set the OptIn Mail Content to the OptIn Object.
154 */
155 $OptIn->set_mail_optin( $body );
156 $OptIn->save();
157
158 /**
159 * Recipient
160 */
161 $headers = "MIME-Version: 1.0\r\n";
162 $headers .= "Content-type: text/html; charset=UTF-8\r\n";
163
164 /**
165 * OptIn Mail Arguments
166 *
167 * Filters the Arguments for the OptIn Mail. This hook allows developers
168 * to add additional content to the Mail.
169 *
170 * @formatter:off
171 *
172 * @param $args {
173 * @type string $subject The Subject of the OptIn Mail
174 * @type string $body The Body of the OptIn Mail,
175 * @type string $sender The Sender of the OptIn Mail
176 * @type string $recipient The recipient of the OptIn Mail
177 * @type bool $use_html Enable/Disable HTML for the OptIn Mail. Default: true
178 * @type string $additional_headers Additional Header Information for the Mail.
179 * }
180 *
181 * @formatter:on
182 *
183 * @since 2.3.3
184 */
185 $args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [
186 'subject' => $parameter['subject'],
187 'body' => $body,
188 'sender' => $parameter['sender'],
189 'sender_name' => $parameter['sender_name'],
190 'recipient' => $form_data['data'][ $parameter['recipient'] ],
191 'use_html' => true,
192 'additional_headers' => $headers
193 ] );
194
195 /*
196 * Adjust the header
197 */
198 $args['additional_headers'] .= sprintf( 'From: %s <%s>' . "\r\n", $args['sender_name'], $args['sender'] );
199
200 \wp_mail(
201 $args['recipient'],
202 $args['subject'],
203 $args['body'],
204 $args['additional_headers']
205 );
206
207 /**
208 * Action after the OptIn Mail has been sent.
209 *
210 * Allows other developers to do additional tasks after the OptIn Mail has been submitted.
211 *
212 * @formatter:off
213 *
214 * @param \WPCF7_ContactForm $form The Contact Form 7 Form Object
215 * @param int $formId The Post ID of the CF7 Form.
216 *
217 * @formatter:on
218 *
219 * @since 2.3.3
220 */
221 do_action( 'f12_cf7_doubleoptin_sent', $form, $post_id );
222
223 /**
224 * Die after sending the form
225 */
226 die( wp_json_encode( [ 'status' => 'success', 'info' => 'opt-in send' ] ) );
227 }
228
229 return $form_parameter;
230 }
231
232 /**
233 * Retrieves the recipient value from the given parameters.
234 *
235 * @param string $recipient The recipient parameter.
236 * @param array $formParameter The form parameters.
237 * @param array $postParameter The post parameters.
238 *
239 * @return string The recipient value if it exists in the post parameters, or an empty string if it doesn't.
240 */
241 public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string {
242 if ( ! isset( $postParameter['data'][ $formParameter['recipient'] ] ) ) {
243 return '';
244 }
245
246 return $postParameter['data'][ $formParameter['recipient'] ];
247 }
248 }
249 }