| 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 |
if ( $this->isOptinEnabled( $post_id ) ) { |
| 109 |
|
| 110 |
// Get the Post Object |
| 111 |
$form = get_post( $post_id ); |
| 112 |
|
| 113 |
if ( ! $form ) { |
| 114 |
return $form_parameter; |
| 115 |
} |
| 116 |
|
| 117 |
$form_data['form_parameter'] = $form_parameter; |
| 118 |
|
| 119 |
if(apply_filters('f12_cf7_doubleoptin_skip_option', false, $post_id, $form_data, $this->type)){ |
| 120 |
/** |
| 121 |
* Die after sending the form |
| 122 |
*/ |
| 123 |
die( wp_json_encode( [ 'status' => 'error', 'info' => 'opt-in skipped' ] ) ); |
| 124 |
} |
| 125 |
|
| 126 |
// Get the OptIn Object |
| 127 |
$OptIn = $this->maybeCreateOptIn( $post_id, do_shortcode( $form->post_content ), $form_data, $form_parameter['attachments'] ); |
| 128 |
|
| 129 |
/** |
| 130 |
* Skip if OptIn not created |
| 131 |
*/ |
| 132 |
if ( ! $OptIn ) { |
| 133 |
return $form_parameter; |
| 134 |
} |
| 135 |
|
| 136 |
// Add the Form URL |
| 137 |
$parameter['formUrl'] = $form_data['submission']['source_url']; |
| 138 |
|
| 139 |
// Get the Body content |
| 140 |
$body = $parameter['body']; |
| 141 |
|
| 142 |
// Replace the Placeholders within the body content. |
| 143 |
$body = $this->addPlaceholders( $body, $OptIn, $parameter ); |
| 144 |
|
| 145 |
/** |
| 146 |
* Body |
| 147 |
* |
| 148 |
* Filters the Body of the OptIn Mail. Allows developers to adjust the content |
| 149 |
* before transmission. |
| 150 |
* |
| 151 |
* @param string $body The content of the OptIn Mail. |
| 152 |
* |
| 153 |
* @since 2.3.3 |
| 154 |
*/ |
| 155 |
$body = apply_filters( 'f12_cf7_doubleoptin_body', $body ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Set the OptIn Mail Content to the OptIn Object. |
| 159 |
*/ |
| 160 |
$OptIn->set_mail_optin( $body ); |
| 161 |
$OptIn->save(); |
| 162 |
|
| 163 |
/** |
| 164 |
* Recipient |
| 165 |
*/ |
| 166 |
$headers = "MIME-Version: 1.0\r\n"; |
| 167 |
$headers .= "Content-type: text/html; charset=UTF-8\r\n"; |
| 168 |
|
| 169 |
/** |
| 170 |
* OptIn Mail Arguments |
| 171 |
* |
| 172 |
* Filters the Arguments for the OptIn Mail. This hook allows developers |
| 173 |
* to add additional content to the Mail. |
| 174 |
* |
| 175 |
* @formatter:off |
| 176 |
* |
| 177 |
* @param $args { |
| 178 |
* @type string $subject The Subject of the OptIn Mail |
| 179 |
* @type string $body The Body of the OptIn Mail, |
| 180 |
* @type string $sender The Sender of the OptIn Mail |
| 181 |
* @type string $recipient The recipient of the OptIn Mail |
| 182 |
* @type bool $use_html Enable/Disable HTML for the OptIn Mail. Default: true |
| 183 |
* @type string $additional_headers Additional Header Information for the Mail. |
| 184 |
* } |
| 185 |
* |
| 186 |
* @formatter:on |
| 187 |
* |
| 188 |
* @since 2.3.3 |
| 189 |
*/ |
| 190 |
$args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [ |
| 191 |
'subject' => $parameter['subject'], |
| 192 |
'body' => $body, |
| 193 |
'sender' => $parameter['sender'], |
| 194 |
'sender_name' => $parameter['sender_name'], |
| 195 |
'recipient' => $form_data['data'][ $parameter['recipient'] ], |
| 196 |
'use_html' => true, |
| 197 |
'additional_headers' => $headers |
| 198 |
] ); |
| 199 |
|
| 200 |
/* |
| 201 |
* Adjust the header |
| 202 |
*/ |
| 203 |
$args['additional_headers'] .= sprintf( 'From: %s <%s>' . "\r\n", $args['sender_name'], $args['sender'] ); |
| 204 |
|
| 205 |
\wp_mail( |
| 206 |
$args['recipient'], |
| 207 |
$args['subject'], |
| 208 |
$args['body'], |
| 209 |
$args['additional_headers'] |
| 210 |
); |
| 211 |
|
| 212 |
/** |
| 213 |
* Action after the OptIn Mail has been sent. |
| 214 |
* |
| 215 |
* Allows other developers to do additional tasks after the OptIn Mail has been submitted. |
| 216 |
* |
| 217 |
* @formatter:off |
| 218 |
* |
| 219 |
* @param \WPCF7_ContactForm $form The Contact Form 7 Form Object |
| 220 |
* @param int $formId The Post ID of the CF7 Form. |
| 221 |
* |
| 222 |
* @formatter:on |
| 223 |
* |
| 224 |
* @since 2.3.3 |
| 225 |
*/ |
| 226 |
do_action( 'f12_cf7_doubleoptin_sent', $form, $post_id ); |
| 227 |
|
| 228 |
/** |
| 229 |
* Die after sending the form |
| 230 |
*/ |
| 231 |
die( wp_json_encode( [ 'status' => 'success', 'info' => 'opt-in send' ] ) ); |
| 232 |
} |
| 233 |
|
| 234 |
return $form_parameter; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Retrieves the recipient value from the given parameters. |
| 239 |
* |
| 240 |
* @param string $recipient The recipient parameter. |
| 241 |
* @param array $formParameter The form parameters. |
| 242 |
* @param array $postParameter The post parameters. |
| 243 |
* |
| 244 |
* @return string The recipient value if it exists in the post parameters, or an empty string if it doesn't. |
| 245 |
*/ |
| 246 |
public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string { |
| 247 |
if ( ! isset( $postParameter['data'][ $formParameter['recipient'] ] ) ) { |
| 248 |
return ''; |
| 249 |
} |
| 250 |
|
| 251 |
return $postParameter['data'][ $formParameter['recipient'] ]; |
| 252 |
} |
| 253 |
} |
| 254 |
} |