| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\LoggerInterface; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Frontend |
| 13 |
* Responsible to handle the frontend of the Double OptIn |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 16 |
*/ |
| 17 |
class AvadaFrontend extends OptInFrontend { |
| 18 |
/** |
| 19 |
* Admin constructor. |
| 20 |
*/ |
| 21 |
public function __construct( LoggerInterface $logger ) { |
| 22 |
parent::__construct( $logger, 'avada' ); |
| 23 |
|
| 24 |
$this->get_logger()->debug( 'Avada integration constructor called', [ |
| 25 |
'plugin' => 'double-opt-in', |
| 26 |
'class' => __CLASS__, |
| 27 |
'method' => __METHOD__, |
| 28 |
'optin' => $_GET['optin'] ?? null, |
| 29 |
] ); |
| 30 |
|
| 31 |
if ( ! isset( $_GET['optin'] ) ) { |
| 32 |
add_filter( 'fusion_form_send_mail_args', [ $this, 'onSubmit' ], 10, 3 ); |
| 33 |
$this->get_logger()->debug( 'Filter fusion_form_send_mail_args registered', [ |
| 34 |
'plugin' => 'double-opt-in', |
| 35 |
] ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Send the default email for the given OptIn. (Original Mail after Opt-In Confirmation) |
| 42 |
* |
| 43 |
* @param OptIn $OptIn The OptIn object containing the email content and form ID. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function sendDefaultMail( OptIn $OptIn ): void { |
| 48 |
$this->get_logger()->debug( 'sendDefaultMail called', [ |
| 49 |
'plugin' => 'double-opt-in', |
| 50 |
'class' => __CLASS__, |
| 51 |
'method' => __METHOD__, |
| 52 |
'form_id' => $OptIn->get_cf_form_id(), |
| 53 |
'optin_id' => $OptIn->get_id(), |
| 54 |
] ); |
| 55 |
|
| 56 |
$formData = maybe_unserialize( $OptIn->get_content() ); |
| 57 |
$formData = SanitizeHelper::sanitize_array( $formData ); |
| 58 |
|
| 59 |
if ( ! isset( $formData['data'] ) ) { |
| 60 |
$this->get_logger()->warning( 'No form data available in OptIn, aborting sendDefaultMail', [ |
| 61 |
'plugin' => 'double-opt-in', |
| 62 |
'form_id' => $OptIn->get_cf_form_id(), |
| 63 |
'optin_id' => $OptIn->get_id(), |
| 64 |
] ); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$data = $formData['data']; |
| 69 |
$formDataString = []; |
| 70 |
foreach ( $data as $key => $value ) { |
| 71 |
if ( is_array( $value ) ) { |
| 72 |
$value = implode( ',', $value ); |
| 73 |
} |
| 74 |
$formDataString[] = $key . '=' . $value; |
| 75 |
} |
| 76 |
$formDataString = implode( "&", $formDataString ); |
| 77 |
|
| 78 |
$_POST = [ |
| 79 |
'formData' => $formDataString, |
| 80 |
'field_labels' => json_encode( $formData['field_labels'] ), |
| 81 |
'field_types' => json_encode( $formData['field_types'] ), |
| 82 |
'hidden_field_names' => json_encode( $formData['hidden_field_names'] ), |
| 83 |
'form_id' => $OptIn->get_cf_form_id(), |
| 84 |
]; |
| 85 |
|
| 86 |
$this->get_logger()->debug( 'Prepared $_POST for Avada form submission', [ |
| 87 |
'plugin' => 'double-opt-in', |
| 88 |
'form_id' => $OptIn->get_cf_form_id(), |
| 89 |
'optin_id' => $OptIn->get_id(), |
| 90 |
] ); |
| 91 |
|
| 92 |
require_once( 'AvadaFormSubmit.class.php' ); |
| 93 |
|
| 94 |
$AvadaFormSubmit = new AvadaFormSubmit(); |
| 95 |
|
| 96 |
$AvadaFormSubmit->submit( $OptIn->get_cf_form_id(), [] ); |
| 97 |
|
| 98 |
$this->get_logger()->info( 'Avada form submitted via sendDefaultMail', [ |
| 99 |
'plugin' => 'double-opt-in', |
| 100 |
'form_id' => $OptIn->get_cf_form_id(), |
| 101 |
'optin_id' => $OptIn->get_id(), |
| 102 |
] ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* On Form Submit add the double optin if enabled |
| 107 |
* |
| 108 |
* @param array $parameter |
| 109 |
* @param int $post_id |
| 110 |
* @param array $data |
| 111 |
*/ |
| 112 |
public function onSubmit( $form_parameter, $submission_id, $form_data ) { |
| 113 |
$this->get_logger()->debug( 'onSubmit called', [ |
| 114 |
'plugin' => 'double-opt-in', |
| 115 |
'class' => __CLASS__, |
| 116 |
'method' => __METHOD__, |
| 117 |
'submission_id' => $submission_id, |
| 118 |
'post_data' => $_POST ?? [], |
| 119 |
] ); |
| 120 |
|
| 121 |
$post_id = isset( $_POST['form_id'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification |
| 122 |
|
| 123 |
if ( ! $post_id ) { |
| 124 |
$this->get_logger()->warning( 'No form_id found in POST, skipping onSubmit', [ |
| 125 |
'plugin' => 'double-opt-in', |
| 126 |
] ); |
| 127 |
return $form_parameter; |
| 128 |
} |
| 129 |
|
| 130 |
$parameter = CF7DoubleOptIn::getInstance()->getParameter( $post_id ); |
| 131 |
|
| 132 |
if ( $this->isOptinEnabled( $post_id ) ) { |
| 133 |
$form = get_post( $post_id ); |
| 134 |
|
| 135 |
if ( ! $form ) { |
| 136 |
$this->get_logger()->warning( 'Form post not found, skipping onSubmit', [ |
| 137 |
'plugin' => 'double-opt-in', |
| 138 |
'post_id' => $post_id, |
| 139 |
] ); |
| 140 |
return $form_parameter; |
| 141 |
} |
| 142 |
|
| 143 |
$form_data['form_parameter'] = $form_parameter; |
| 144 |
|
| 145 |
if ( apply_filters( 'f12_cf7_doubleoptin_skip_option', false, $post_id, $form_data, $this->type ) ) { |
| 146 |
$this->get_logger()->info( 'Opt-in skipped by filter', [ |
| 147 |
'plugin' => 'double-opt-in', |
| 148 |
'post_id' => $post_id, |
| 149 |
] ); |
| 150 |
die( wp_json_encode( [ 'status' => 'error', 'info' => 'opt-in skipped' ] ) ); |
| 151 |
} |
| 152 |
|
| 153 |
$OptIn = $this->maybeCreateOptIn( |
| 154 |
$post_id, |
| 155 |
do_shortcode( $form->post_content ), |
| 156 |
$form_data, |
| 157 |
$form_parameter['attachments'] |
| 158 |
); |
| 159 |
|
| 160 |
if ( ! $OptIn ) { |
| 161 |
$this->get_logger()->warning( 'OptIn object could not be created', [ |
| 162 |
'plugin' => 'double-opt-in', |
| 163 |
'post_id' => $post_id, |
| 164 |
] ); |
| 165 |
return $form_parameter; |
| 166 |
} |
| 167 |
|
| 168 |
$parameter['formUrl'] = $form_data['submission']['source_url']; |
| 169 |
|
| 170 |
$body = $this->addPlaceholders( $parameter['body'], $OptIn, $parameter ); |
| 171 |
$body = apply_filters( 'f12_cf7_doubleoptin_body', $body ); |
| 172 |
|
| 173 |
$OptIn->set_mail_optin( $body ); |
| 174 |
$OptIn->save(); |
| 175 |
|
| 176 |
$headers = "MIME-Version: 1.0\r\n"; |
| 177 |
$headers .= "Content-type: text/html; charset=UTF-8\r\n"; |
| 178 |
|
| 179 |
$args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [ |
| 180 |
'subject' => $parameter['subject'], |
| 181 |
'body' => $body, |
| 182 |
'sender' => $parameter['sender'], |
| 183 |
'sender_name' => $parameter['sender_name'], |
| 184 |
'recipient' => $form_data['data'][ $parameter['recipient'] ] ?? '', |
| 185 |
'use_html' => true, |
| 186 |
'additional_headers' => $headers |
| 187 |
] ); |
| 188 |
|
| 189 |
$args['additional_headers'] .= sprintf( 'From: %s <%s>' . "\r\n", $args['sender_name'], $args['sender'] ); |
| 190 |
|
| 191 |
\wp_mail( |
| 192 |
$args['recipient'], |
| 193 |
$args['subject'], |
| 194 |
$args['body'], |
| 195 |
$args['additional_headers'] |
| 196 |
); |
| 197 |
|
| 198 |
$telemetry = new Telemetry( $this->get_logger() ); |
| 199 |
$telemetry->increment( 'total_optins' ); |
| 200 |
$telemetry->increment( 'avada_optins' ); |
| 201 |
|
| 202 |
$this->get_logger()->info( 'OptIn mail sent', [ |
| 203 |
'plugin' => 'double-opt-in', |
| 204 |
'post_id' => $post_id, |
| 205 |
'recipient' => $args['recipient'], |
| 206 |
'subject' => $args['subject'], |
| 207 |
] ); |
| 208 |
|
| 209 |
do_action( 'f12_cf7_doubleoptin_sent', $form, $post_id ); |
| 210 |
|
| 211 |
die( wp_json_encode( [ 'status' => 'success', 'info' => 'opt-in send' ] ) ); |
| 212 |
} |
| 213 |
|
| 214 |
$this->get_logger()->debug( 'Opt-in not enabled for form', [ |
| 215 |
'plugin' => 'double-opt-in', |
| 216 |
'post_id' => $post_id, |
| 217 |
] ); |
| 218 |
|
| 219 |
return $form_parameter; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* Retrieves the recipient value from the given parameters. |
| 225 |
* |
| 226 |
* @param string $recipient The recipient parameter. |
| 227 |
* @param array $formParameter The form parameters. |
| 228 |
* @param array $postParameter The post parameters. |
| 229 |
* |
| 230 |
* @return string The recipient value if it exists in the post parameters, or an empty string if it doesn't. |
| 231 |
*/ |
| 232 |
public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string { |
| 233 |
$this->get_logger()->debug( 'getRecipient called', [ |
| 234 |
'plugin' => 'double-opt-in', |
| 235 |
'class' => __CLASS__, |
| 236 |
'method' => __METHOD__, |
| 237 |
'formParameter' => $formParameter, |
| 238 |
] ); |
| 239 |
|
| 240 |
if ( ! isset( $postParameter['data'][ $formParameter['recipient'] ] ) ) { |
| 241 |
$this->get_logger()->warning( 'Recipient not found in postParameter', [ |
| 242 |
'plugin' => 'double-opt-in', |
| 243 |
] ); |
| 244 |
return ''; |
| 245 |
} |
| 246 |
|
| 247 |
$value = $postParameter['data'][ $formParameter['recipient'] ]; |
| 248 |
|
| 249 |
$this->get_logger()->debug( 'Recipient resolved', [ |
| 250 |
'plugin' => 'double-opt-in', |
| 251 |
'recipient' => $value, |
| 252 |
] ); |
| 253 |
|
| 254 |
return $value; |
| 255 |
} |
| 256 |
} |
| 257 |
} |