| 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 |
if(apply_filters('f12_cf7_doubleoptin_skip_option', false, $form->id(), $submission->get_posted_data(), $this->type)){ |
| 105 |
return; // Skip if requested |
| 106 |
} |
| 107 |
|
| 108 |
// Get the OptIn Object |
| 109 |
$OptIn = $this->maybeCreateOptIn( $form->id(), $form->form_html(), $submission->get_posted_data(), $submission->uploaded_files() ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Skip if OptIn not created |
| 113 |
*/ |
| 114 |
if ( ! $OptIn ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Add the Form URL to the parameters |
| 119 |
$parameter['formUrl'] = $submission->get_meta( 'url' ); |
| 120 |
|
| 121 |
// Get the Body content |
| 122 |
$body = $parameter['body']; |
| 123 |
|
| 124 |
// Replace the Placeholders within the body content. |
| 125 |
$body = $this->addPlaceholders( $body, $OptIn, $parameter ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Body |
| 129 |
* |
| 130 |
* Filters the Body of the OptIn Mail. Allows developers to adjust the content |
| 131 |
* before transmission. |
| 132 |
* |
| 133 |
* @param string $body The content of the OptIn Mail. |
| 134 |
* |
| 135 |
* @since 2.3.3 |
| 136 |
*/ |
| 137 |
$body = apply_filters( 'f12_cf7_doubleoptin_body', $body ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Set the OptIn Mail Content to the OptIn Object. |
| 141 |
*/ |
| 142 |
$OptIn->set_mail_optin( $body ); |
| 143 |
$OptIn->save(); |
| 144 |
|
| 145 |
/** |
| 146 |
* Recipient |
| 147 |
*/ |
| 148 |
$additional_headers = ''; |
| 149 |
|
| 150 |
/** |
| 151 |
* OptIn Mail Arguments |
| 152 |
* |
| 153 |
* Filters the Arguments for the OptIn Mail. This hook allows developers |
| 154 |
* to add additional content to the Mail. |
| 155 |
* |
| 156 |
* @formatter:off |
| 157 |
* |
| 158 |
* @param $args { |
| 159 |
* @type string $subject The Subject of the OptIn Mail |
| 160 |
* @type string $body The Body of the OptIn Mail, |
| 161 |
* @type string $sender The Sender of the OptIn Mail |
| 162 |
* @type string $recipient The recipient of the OptIn Mail |
| 163 |
* @type bool $use_html Enable/Disable HTML for the OptIn Mail. Default: true |
| 164 |
* @type string $additional_headers Additional Header Information for the Mail. |
| 165 |
* } |
| 166 |
* |
| 167 |
* @formatter:on |
| 168 |
* |
| 169 |
* @since 2.3.3 |
| 170 |
*/ |
| 171 |
$args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [ |
| 172 |
'subject' => $parameter['subject'], |
| 173 |
'body' => $body, |
| 174 |
'sender' => $parameter['sender'], |
| 175 |
'sender_name' => $parameter['sender_name'], |
| 176 |
'recipient' => $parameter['recipient'], |
| 177 |
'use_html' => true, |
| 178 |
'additional_headers' => $additional_headers |
| 179 |
] ); |
| 180 |
|
| 181 |
/** |
| 182 |
* Set the Header |
| 183 |
*/ |
| 184 |
if ( ! empty( $parameter['sender_name'] ) ) { |
| 185 |
$args['additional_headers'] .= 'From: ' . $args['sender_name'] . ' <' . $args['sender'] . '>'; |
| 186 |
} |
| 187 |
|
| 188 |
// Prepare the Opt-In Mail |
| 189 |
\WPCF7_Mail::send( $args, 'mail' ); |
| 190 |
|
| 191 |
// Skip all mails following |
| 192 |
add_filter( 'wpcf7_skip_mail', '__return_true' ); |
| 193 |
|
| 194 |
/** |
| 195 |
* Action after the OptIn Mail has been sent. |
| 196 |
* |
| 197 |
* Allows other developers to do additional tasks after the OptIn Mail has been submitted. |
| 198 |
* |
| 199 |
* @formatter:off |
| 200 |
* |
| 201 |
* @param \WPCF7_ContactForm $form The Contact Form 7 Form Object |
| 202 |
* @param int $formId The Post ID of the CF7 Form. |
| 203 |
* |
| 204 |
* @formatter:on |
| 205 |
* |
| 206 |
* @since 2.3.3 |
| 207 |
*/ |
| 208 |
do_action( 'f12_cf7_doubleoptin_sent', $form, $form->id() ); |
| 209 |
|
| 210 |
} else if ( isset( $_GET['optin'] ) ) { |
| 211 |
/** |
| 212 |
* Add Files / Attachments to the OptIn Mail. |
| 213 |
*/ |
| 214 |
$hash = sanitize_text_field( $_GET['optin'] ); |
| 215 |
|
| 216 |
/** |
| 217 |
* Get the OptIn Object |
| 218 |
*/ |
| 219 |
$OptIn = OptIn::get_by_hash( $hash ); |
| 220 |
|
| 221 |
/** |
| 222 |
* Skip if the OptIn has not been found. |
| 223 |
*/ |
| 224 |
if ( null == $OptIn ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the Files from the OptIn Object |
| 230 |
*/ |
| 231 |
$files = maybe_unserialize( $OptIn->get_files() ); |
| 232 |
|
| 233 |
/** |
| 234 |
* Skip if no files has been found |
| 235 |
*/ |
| 236 |
if ( empty( $files ) ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Loop through all attachments/files. |
| 242 |
*/ |
| 243 |
foreach ( $files as $file ) { |
| 244 |
/** |
| 245 |
* Skip file if the name is empty |
| 246 |
*/ |
| 247 |
if ( ! empty( $file ) ) { |
| 248 |
continue; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Enable / Disable the OptIn Mail Attachment for Mail 1. |
| 253 |
* |
| 254 |
* Allows Developers to Enable/Disable Mail attachments dynamically. |
| 255 |
* |
| 256 |
* @param bool $status Either add attachments (true) or not (false). Default: true |
| 257 |
* @param OptIn $OptIn The OptIn Object. |
| 258 |
* |
| 259 |
* @since 2.3.3 |
| 260 |
*/ |
| 261 |
if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_1', true, $OptIn ) ) { |
| 262 |
$submission->add_extra_attachments( $file ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Enable / Disable the OptIn Mail Attachment for Mail 2. |
| 267 |
* |
| 268 |
* Allows Developers to Enable/Disable Mail attachments dynamically. |
| 269 |
* |
| 270 |
* @param bool $status Either add attachments (true) or not (false). Default: true |
| 271 |
* @param OptIn $OptIn The OptIn Object. |
| 272 |
* |
| 273 |
* @since 2.3.3 |
| 274 |
*/ |
| 275 |
if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_2', true, $OptIn ) ) { |
| 276 |
$submission->add_extra_attachments( $file, 'mail_2' ); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |