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