| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use forge12\plugins\ContactForm7; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Backend |
| 13 |
* Responsible to handle the admin settings for the double opt-in field |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7OptIn |
| 16 |
*/ |
| 17 |
class CF7Backend { |
| 18 |
/** |
| 19 |
* Admin constructor. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
add_action( 'admin_init', array( $this, 'addHooks' ) ); |
| 23 |
add_action( 'admin_enqueue_scripts', array( $this, 'addStyles' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Add the styles for the form |
| 28 |
*/ |
| 29 |
public function addStyles( $hook ) { |
| 30 |
wp_enqueue_script( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/f12-cf7-popup.js', __FILE__ ), array( 'jquery' ) ); |
| 31 |
wp_localize_script( 'f12-cf7-doubleoptin-admin', 'doi', array( |
| 32 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 33 |
'nonce' => wp_create_nonce( 'f12_doi_details' ) |
| 34 |
) ); |
| 35 |
|
| 36 |
wp_enqueue_script( 'f12-cf7-doubleoptin-templateloader', plugins_url( 'assets/f12-cf7-templateloader.js', __FILE__ ), array( 'jquery' ) ); |
| 37 |
wp_localize_script( 'f12-cf7-doubleoptin-templateloader', 'templateloader', array( |
| 38 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 39 |
'nonce' => wp_create_nonce( 'f12_doi_templateloader' ), |
| 40 |
'label_placeholder' => __( 'Please wait while we load the template...', 'double-opt-in' ) |
| 41 |
) ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add the hooks responsible to handle wordpress functions |
| 47 |
*/ |
| 48 |
public function addHooks() { |
| 49 |
add_filter( 'wpcf7_editor_panels', array( $this, 'addPanel' ), 10, 1 ); |
| 50 |
add_action( 'wpcf7_save_contact_form', array( $this, 'save' ), 10, 3 ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add a custom panel to the contact form 7 options |
| 55 |
*/ |
| 56 |
public function addPanel( array $panels ) { |
| 57 |
$panels['optin'] = array( |
| 58 |
'title' => __( 'Double-Opt-in', 'double-opt-in' ), |
| 59 |
'callback' => array( $this, 'render' ) |
| 60 |
); |
| 61 |
|
| 62 |
return $panels; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* On Contact Form save store the information in the database |
| 67 |
* |
| 68 |
* @param \WPCF7_ContactForm $contact_form |
| 69 |
* @param $args |
| 70 |
* @param $context |
| 71 |
*/ |
| 72 |
public function save( $contact_form, $args, $context ) { |
| 73 |
$postID = $contact_form->id(); |
| 74 |
|
| 75 |
// Validate nonce |
| 76 |
if ( ! isset( $_POST['f12_cf7_doubleoptin_save_form_nonce'] ) || ! wp_verify_nonce( $_POST['f12_cf7_doubleoptin_save_form_nonce'], 'f12_cf7_doubleoptin_save_form_action' ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Check if the double opt in settings is set |
| 81 |
if ( ! $postID || ! isset( $_POST['doubleoptin'] ) ) { |
| 82 |
update_post_meta( $postID, 'f12-cf7-doubleoptin', array() ); |
| 83 |
|
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$parameter = SanitizeHelper::sanitize_array( $_POST['doubleoptin'] ); |
| 88 |
|
| 89 |
// load the default parameter |
| 90 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $postID ); |
| 91 |
|
| 92 |
// validated all parameter defined in the default parameter options and sanitize them. |
| 93 |
foreach ( $metadata as $key => $value ) { |
| 94 |
if ( isset( $parameter[ $key ] ) ) { |
| 95 |
if ( $key == 'body' ) { |
| 96 |
$metadata[ $key ] = $parameter[ $key ]; |
| 97 |
} else if ( $key == 'enable' ) { |
| 98 |
$metadata[ $key ] = (int) $parameter[ $key ]; |
| 99 |
} else if ( $key == 'sender' ) { |
| 100 |
$metadata[ $key ] = $parameter[ $key ]; |
| 101 |
} else { |
| 102 |
$metadata[ $key ] = $parameter[ $key ]; |
| 103 |
} |
| 104 |
} else if ( $key == 'enable' ) { |
| 105 |
$metadata[ $key ] = 0; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Adjust the Metadata before saving |
| 111 |
* |
| 112 |
* @param array $metadata |
| 113 |
*/ |
| 114 |
$metadata = apply_filters( 'f12_cf7_doubleoptin_metadata_cf7', $metadata ); |
| 115 |
|
| 116 |
/** |
| 117 |
* Adjust the Metadata before saving |
| 118 |
* |
| 119 |
* @param array $metadata |
| 120 |
*/ |
| 121 |
$metadata = apply_filters( 'f12_cf7_doubleoptin_save_form', $metadata ); |
| 122 |
|
| 123 |
update_post_meta( $postID, 'f12-cf7-doubleoptin', $metadata ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Return an option list containing all tags for the condition and a default field to disable the condition. |
| 128 |
* |
| 129 |
* @param \WPCF7_ContactForm $post |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
private function getTags( $post ) { |
| 134 |
$tags = array(); |
| 135 |
$arrayTags = $post->scan_form_tags(); |
| 136 |
foreach ( $arrayTags as $formTag /** @var \WPCF7_FormTag $formTag */ ) { |
| 137 |
if ( ! empty( $formTag->name ) ) { |
| 138 |
$tags[ $formTag->name ] = $formTag->name; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $tags; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Show the backend double opt in options |
| 147 |
* |
| 148 |
* @param \WPCF7_ContactForm $post |
| 149 |
*/ |
| 150 |
public function render( $post ) { |
| 151 |
if ( ! $post ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->id() ); |
| 156 |
$id = "doubleoptin"; |
| 157 |
?> |
| 158 |
<div class="forge12-plugin" style="padding-top:0;"> |
| 159 |
<div class="forge12-plugin-content" style="margin-top:0;"> |
| 160 |
<div class="forge12-plugin-content-main"> |
| 161 |
<div class="box" style="width:100%;"> |
| 162 |
<h2><?php _e( 'Opt-In Formular Settings', 'double-opt-in' ); ?></h2> |
| 163 |
<div class="option"> |
| 164 |
<div class="label"> |
| 165 |
<label for="doubleoptin[enable]"> |
| 166 |
<?php _e( 'Enable', 'double-opt-in' ); ?> |
| 167 |
</label> |
| 168 |
</div> |
| 169 |
<div class="input"> |
| 170 |
<input type="checkbox" name="doubleoptin[enable]" id="doubleoptin" |
| 171 |
value="1" <?php echo isset( $metadata['enable'] ) && $metadata['enable'] == 1 ? 'checked = "checked"' : ''; ?> > |
| 172 |
<span> |
| 173 |
<?php _e( 'Yes', 'double-opt-in' ); ?> |
| 174 |
</span> |
| 175 |
<p><?php _e( 'Enable this checkox to activate the Double-Opt-In Mail for this formular.', 'double-opt-in' ); ?></p> |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
|
| 179 |
<div class="option"> |
| 180 |
<div class="label"> |
| 181 |
<label for="doubleoptin[category]"> |
| 182 |
<?php _e( 'Category', 'double-opt-in' ); ?> |
| 183 |
</label> |
| 184 |
</div> |
| 185 |
<div class="input"> |
| 186 |
<?php |
| 187 |
$atts = array( |
| 188 |
'perPage' => - 1, |
| 189 |
'orderBy' => 'name', |
| 190 |
'order' => 'ASC' |
| 191 |
); |
| 192 |
|
| 193 |
$list = Category::get_list( $atts, $numberOfPages ); |
| 194 |
|
| 195 |
$response = '<option value="0">' . __( 'Please select', 'double-opt-in' ) . '</option>'; |
| 196 |
foreach ( $list as $Category /** @var Category $Category */ ) { |
| 197 |
$selected = ""; |
| 198 |
if ( $Category->get_id() == $metadata['category'] ) { |
| 199 |
$selected = 'selected="selected"'; |
| 200 |
} |
| 201 |
$response .= '<option value="' . esc_attr( $Category->get_id() ) . '" ' . $selected . '>' . esc_attr( $Category->get_name() ) . '</option>'; |
| 202 |
} |
| 203 |
?> |
| 204 |
<select name="doubleoptin[category]"><?php echo wp_kses( $response, array( |
| 205 |
'option' => array( |
| 206 |
'value' => array(), |
| 207 |
'selected' => array() |
| 208 |
) |
| 209 |
) ); ?></select> |
| 210 |
<span> |
| 211 |
<?php _e( 'Assign the Opt-In to a Category for an easier administration.', 'double-opt-in' ); ?> |
| 212 |
</span> |
| 213 |
</div> |
| 214 |
</div> |
| 215 |
|
| 216 |
<div class="option"> |
| 217 |
<div class="label"> |
| 218 |
<label for="doubleoptin[page]"> |
| 219 |
<?php _e( 'Dynamic Condition', 'double-opt-in' ); ?> |
| 220 |
</label> |
| 221 |
</div> |
| 222 |
<div class="input"> |
| 223 |
<span> |
| 224 |
<?php _e( 'Enable the Opt-In only when the field ', 'double-opt-in' ); ?> |
| 225 |
</span> |
| 226 |
<?php |
| 227 |
$ConditionList = new HTMLSelect( $id . '[conditions]', array_merge( array( 'disable' => __( 'Disabled', 'double-opt-in' ) ), $this->getTags( $post ) ), array( |
| 228 |
'id' => array( $id . '-conditions' ), |
| 229 |
'class' => array( |
| 230 |
'large-text', |
| 231 |
'code' |
| 232 |
) |
| 233 |
) ); |
| 234 |
$ConditionList->setOptionSelectedByKey( $metadata['conditions'] ); |
| 235 |
echo wp_kses( $ConditionList->get(), array( |
| 236 |
'select' => array( 'name' => array() ), |
| 237 |
'option' => array( |
| 238 |
'value' => array(), |
| 239 |
'selected' => array() |
| 240 |
) |
| 241 |
) ); |
| 242 |
?> |
| 243 |
<span> |
| 244 |
<?php _e( 'has been filled/checked by the visitor.', 'double-opt-in' ); ?> |
| 245 |
</span> |
| 246 |
</div> |
| 247 |
</div> |
| 248 |
|
| 249 |
<div class="option"> |
| 250 |
<div class="label"> |
| 251 |
<label for="doubleoptin[page]"> |
| 252 |
<?php _e( 'Confirmation Page', 'double-opt-in' ); ?> |
| 253 |
</label> |
| 254 |
</div> |
| 255 |
<div class="input"> |
| 256 |
<?php wp_dropdown_pages( array( |
| 257 |
'show_option_none' => __( 'default', 'double-opt-in' ), |
| 258 |
'name' => 'doubleoptin[page]', |
| 259 |
'selected' => $metadata['page'] |
| 260 |
) ); ?> |
| 261 |
<p> |
| 262 |
<?php _e( 'Select the page that should be displayed after the user clicks on the confirmation link', 'double-opt-in' ); ?> |
| 263 |
</p> |
| 264 |
</div> |
| 265 |
</div> |
| 266 |
|
| 267 |
<?php |
| 268 |
/** |
| 269 |
* Used to render settings for CF7 |
| 270 |
* |
| 271 |
* @param string $output |
| 272 |
* @param array $metadata |
| 273 |
* |
| 274 |
* @since 3.0.0 |
| 275 |
*/ |
| 276 |
echo apply_filters( 'f12_cf7_formular_settings_after_cf7', '', $metadata ); |
| 277 |
|
| 278 |
/** |
| 279 |
* Deprecated - Used to add settings for cf7. |
| 280 |
* |
| 281 |
* @param string $type |
| 282 |
* @param array $metadata |
| 283 |
*/ |
| 284 |
do_action( 'f12_cf7_formular_settings_after', 'cf7', $metadata ); |
| 285 |
?> |
| 286 |
</div> |
| 287 |
|
| 288 |
<div class="box" style="width:100%;"> |
| 289 |
<h2><?php _e( 'Customize your Opt-In Mail', 'double-opt-in' ); ?></h2> |
| 290 |
<div class="option"> |
| 291 |
<div class="label"> |
| 292 |
<label for="doubleoptin-recipient"> |
| 293 |
<label for="<?php echo esc_attr( $id ); ?>-recipient"><?php echo __( 'To', 'double-opt-in' ); ?></label> |
| 294 |
</label> |
| 295 |
</div> |
| 296 |
<div class="input"> |
| 297 |
<input type="text" id="<?php echo esc_attr( $id ); ?>-recipient" |
| 298 |
name="<?php echo esc_attr( $id ); ?>[recipient]" |
| 299 |
class="large-text code" size="70" |
| 300 |
value="<?php echo esc_attr( $metadata['recipient'] ); ?>"/> |
| 301 |
<p> |
| 302 |
<?php _e( 'This field defines who will receive the Double-Opt-In mail. Enter one of the following tags that represents the mail for the customer:', 'double-opt-in' ); ?> |
| 303 |
</p> |
| 304 |
<p> |
| 305 |
<code> |
| 306 |
<?php |
| 307 |
$arrayTags = $this->getTags( $post ); |
| 308 |
foreach ( $arrayTags as $key => $value ) { |
| 309 |
$arrayTags[ $key ] = "[" . esc_html( $value ) . "]"; |
| 310 |
} |
| 311 |
echo esc_html( implode( ",", array_filter( $arrayTags ) ) ); |
| 312 |
?> |
| 313 |
</code> |
| 314 |
</p> |
| 315 |
</div> |
| 316 |
</div> |
| 317 |
|
| 318 |
<div class="option"> |
| 319 |
<div class="label"> |
| 320 |
<label for="<?php echo esc_attr( $id ); ?>-sender"> |
| 321 |
<?php echo esc_html( __( 'From', 'double-opt-in' ) ); ?> |
| 322 |
</label> |
| 323 |
</div> |
| 324 |
<div class="input"> |
| 325 |
<input type="text" id="<?php echo esc_attr( $id ); ?>-sender" |
| 326 |
name="<?php echo esc_attr( $id ); ?>[sender]" |
| 327 |
class="large-text code" size="70" |
| 328 |
value="<?php echo esc_attr( $metadata['sender'] ); ?>"/> |
| 329 |
<p> |
| 330 |
<?php _e( 'This field defines who will sent the mail. You can either enter the e-Mail (e.g.: [email protected]) or use a placeholder (e.g.: [_site_admin_email]).', 'double-opt-in' ); ?> |
| 331 |
</p> |
| 332 |
</div> |
| 333 |
</div> |
| 334 |
|
| 335 |
<div class="option"> |
| 336 |
<div class="label"> |
| 337 |
<label for="<?php echo esc_attr( $id ); ?>-sender"> |
| 338 |
<?php echo esc_html( __( 'From Name', 'double-opt-in' ) ); ?> |
| 339 |
</label> |
| 340 |
</div> |
| 341 |
<div class="input"> |
| 342 |
<input type="text" id="<?php echo esc_attr( $id ); ?>-sender_name" |
| 343 |
name="<?php echo esc_attr( $id ); ?>[sender_name]" |
| 344 |
class="large-text code" size="70" |
| 345 |
value="<?php echo esc_attr( $metadata['sender_name'] ); ?>"/> |
| 346 |
<p> |
| 347 |
<?php _e( 'This field defines the name of the sender (e.g. Max Mustermann).', 'double-opt-in' ); ?> |
| 348 |
</p> |
| 349 |
</div> |
| 350 |
</div> |
| 351 |
|
| 352 |
<div class="option"> |
| 353 |
<div class="label"> |
| 354 |
<label for="<?php echo esc_attr( $id ); ?>-subject"> |
| 355 |
<?php echo esc_html( __( 'Subject', 'double-opt-in' ) ); ?> |
| 356 |
</label> |
| 357 |
</div> |
| 358 |
<div class="input"> |
| 359 |
<input type="text" id="<?php echo esc_attr( $id ); ?>-subject" |
| 360 |
name="<?php echo esc_attr( $id ); ?>[subject]" |
| 361 |
class="large-text code" size="70" |
| 362 |
value="<?php echo esc_attr( $metadata['subject'] ); ?>"/> |
| 363 |
<p> |
| 364 |
<?php _e( 'Enter a custom subject for the Double-Opt-In mail (e.g.: Please confirm your registration).', 'double-opt-in' ); ?> |
| 365 |
</p> |
| 366 |
</div> |
| 367 |
</div> |
| 368 |
|
| 369 |
<div class="option"> |
| 370 |
<div class="label"> |
| 371 |
<label for="<?php echo esc_attr( $id ); ?>-body"> |
| 372 |
<?php echo esc_html( __( 'Template', 'double-opt-in' ) ); ?> |
| 373 |
</label> |
| 374 |
</div> |
| 375 |
<div class="input"> |
| 376 |
<div class="preview"> |
| 377 |
<div class="preview-item"> |
| 378 |
<div class="preview-item-inner <?php if ( $metadata['template'] == 'blank' ) { |
| 379 |
echo 'active'; |
| 380 |
}; ?>"> |
| 381 |
<img src="<?php echo esc_url( plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'mails/blank.png' ); ?>" |
| 382 |
class="f12-cf7-templateloader-preview" data-template="blank" |
| 383 |
title="Blank"/> |
| 384 |
</div> |
| 385 |
</div> |
| 386 |
<div class="preview-item"> |
| 387 |
<div class="preview-item-inner <?php if ( $metadata['template'] == 'newsletter_en' ) { |
| 388 |
echo 'active'; |
| 389 |
}; ?>"> |
| 390 |
<img src="<?php echo esc_url( plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'mails/newsletter_en.png' ); ?>" |
| 391 |
class="f12-cf7-templateloader-preview" title="Newsletter EN" |
| 392 |
data-template="newsletter_en" alt=""/> |
| 393 |
</div> |
| 394 |
</div> |
| 395 |
<div class="preview-item"> |
| 396 |
<div class="preview-item-inner <?php if ( $metadata['template'] == 'newsletter_en_2' ) { |
| 397 |
echo 'active'; |
| 398 |
}; ?>"> |
| 399 |
<img src="<?php echo esc_url( plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'mails/newsletter_en_2.png' ); ?>" |
| 400 |
class="f12-cf7-templateloader-preview" title="Newsletter EN 2" |
| 401 |
data-template="newsletter_en_2"/> |
| 402 |
</div> |
| 403 |
</div> |
| 404 |
<div class="preview-item"> |
| 405 |
<div class="preview-item-inner <?php if ( $metadata['template'] == 'newsletter_en_3' ) { |
| 406 |
echo 'active'; |
| 407 |
}; ?>"> |
| 408 |
<img src="<?php echo esc_url( plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'mails/newsletter_en_3.png' ); ?>" |
| 409 |
class="f12-cf7-templateloader-preview" title="Newsletter EN 3" |
| 410 |
data-template="newsletter_en_3"/> |
| 411 |
</div> |
| 412 |
</div> |
| 413 |
</div> |
| 414 |
<?php |
| 415 |
$TemplateList = new HTMLSelect( $id . '[template]', array( |
| 416 |
'blank' => 'blank', |
| 417 |
'newsletter_en' => 'newsletter_en', |
| 418 |
'newsletter_en_2' => 'newsletter_en_2', |
| 419 |
'newsletter_en_3' => 'newsletter_en_3' |
| 420 |
), array( |
| 421 |
'class' => array( 'f12-cf7-templateloader' ), |
| 422 |
'style' => array( 'display:none;' ) |
| 423 |
) ); |
| 424 |
$TemplateList->setOptionSelectedByKey( $metadata['template'] ); |
| 425 |
echo wp_kses( $TemplateList->get(), array( |
| 426 |
'select' => array( |
| 427 |
'style' => array(), |
| 428 |
'class' => array(), |
| 429 |
'name' => array() |
| 430 |
), |
| 431 |
'option' => array( |
| 432 |
'value' => array(), |
| 433 |
'selected' => array() |
| 434 |
) |
| 435 |
) ); |
| 436 |
?> |
| 437 |
</div> |
| 438 |
</div> |
| 439 |
|
| 440 |
<div class="option"> |
| 441 |
<div class="label"> |
| 442 |
<label for="<?php echo esc_attr( $id ); ?>-body"> |
| 443 |
<?php echo esc_html( __( 'Message body', 'double-opt-in' ) ); ?> |
| 444 |
</label> |
| 445 |
</div> |
| 446 |
<div class="input"> |
| 447 |
<textarea id="<?php echo esc_attr( $id ); ?>-body" |
| 448 |
name="<?php echo esc_attr( $id ); ?>[body]" |
| 449 |
cols="100" rows="18" |
| 450 |
class="large-text code"><?php echo esc_textarea( $metadata['body'] ); ?></textarea> |
| 451 |
<p> |
| 452 |
<?php _e( 'Enter the Message you want to sent to your customer. Do not forget to add the Double-Opt-In tag ([doubleoptinlink]) e.g.:', 'double-opt-in' ); ?> |
| 453 |
<code> |
| 454 |
<a href="[doubleoptinlink]">Confirm the registration</a> |
| 455 |
</code> |
| 456 |
</p> |
| 457 |
<p> |
| 458 |
<?php _e( 'These placeholders are available for the opt-in Message body: ', 'double-opt-in' ); ?> |
| 459 |
</p> |
| 460 |
<p> |
| 461 |
<code>[doubleoptinlink],[doubleoptin_form_url],[doubleoptin_form_subject],[doubleoptin_form_date],[doubleoptin_form_time],[_site_admin_email],<?php |
| 462 |
$arrayTags = $this->getTags( $post ); |
| 463 |
foreach ( $arrayTags as $key => $value ) { |
| 464 |
$arrayTags[ $key ] = "[" . esc_html( $value ) . "]"; |
| 465 |
} |
| 466 |
echo implode( ",", array_filter( $arrayTags ) ); |
| 467 |
?> |
| 468 |
</code> |
| 469 |
</p> |
| 470 |
</div> |
| 471 |
</div> |
| 472 |
</div> |
| 473 |
|
| 474 |
|
| 475 |
<?php do_action( 'f12_cf7_doubleoptin_admin_panel', $post, $metadata, $id ); ?> |
| 476 |
<?php wp_nonce_field( 'f12_cf7_doubleoptin_save_form_action', 'f12_cf7_doubleoptin_save_form_nonce' ); ?> |
| 477 |
</div> |
| 478 |
</div> |
| 479 |
</div> |
| 480 |
<?php |
| 481 |
} |
| 482 |
} |
| 483 |
} |