| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class AvadaFormOptions |
| 10 |
* Add the OptIn Options to the Avada Form |
| 11 |
* |
| 12 |
* @package forge12\contactform7\CF7OptIn |
| 13 |
*/ |
| 14 |
class AvadaFormOptions |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Admin constructor. |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
add_action('admin_init', array($this, 'addHooks')); |
| 22 |
add_action('admin_enqueue_scripts', array($this, 'addStyles')); |
| 23 |
add_action('save_post', array($this, 'save'), 20, 1); |
| 24 |
add_filter('awb_po_get_value', array($this, 'get_value'), 10, 2); |
| 25 |
} |
| 26 |
|
| 27 |
public function get_value($ret, $field_id) |
| 28 |
{ |
| 29 |
global $post; |
| 30 |
|
| 31 |
if(!$post || !isset($post->ID)){ |
| 32 |
return $ret; |
| 33 |
} |
| 34 |
|
| 35 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter($post->ID); |
| 36 |
|
| 37 |
foreach ($metadata as $key => $value) { |
| 38 |
$metadata['doubleoptin[' . $key . ']'] = $value; |
| 39 |
} |
| 40 |
|
| 41 |
if (isset($metadata[$field_id])) { |
| 42 |
$ret = $metadata[$field_id]; |
| 43 |
} |
| 44 |
return $ret; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Add the styles for the form |
| 49 |
*/ |
| 50 |
public function addStyles($hook) |
| 51 |
{ |
| 52 |
wp_enqueue_style('f12-avada-doubleoptin-admin-avada', plugins_url('assets/admin-avada-style.css', __FILE__)); |
| 53 |
|
| 54 |
wp_enqueue_script('f12-avada-doubleoptin-templateloader', plugins_url('assets/f12-avada-templateloader.js', __FILE__), array('jquery')); |
| 55 |
wp_localize_script('f12-avada-doubleoptin-templateloader', 'templateloader', array( |
| 56 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 57 |
'nonce' => wp_create_nonce('f12_doi_templateloader'), |
| 58 |
'label_placeholder' => __('Please wait while we load the template...', 'double-opt-in') |
| 59 |
)); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Add the hooks responsible to handle wordpress functions |
| 64 |
*/ |
| 65 |
public function addHooks() |
| 66 |
{ |
| 67 |
add_filter('avada_metabox_tabs', array($this, 'addPanel'), 10, 2); |
| 68 |
add_filter('f12_cf7_doubleoptin_avada_form_panel', array($this, 'render'), 10, 1); |
| 69 |
add_action('wpcf7_save_contact_form', array($this, 'save'), 10, 3); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Add a custom panel to the contact form 7 options |
| 74 |
*/ |
| 75 |
public function addPanel(array $panels, $post_type) |
| 76 |
{ |
| 77 |
global $post; |
| 78 |
|
| 79 |
if($post && $post->post_type == 'fusion_form') { |
| 80 |
if ($post_type == 'default') { |
| 81 |
$panels['tabs_names']['f12cf7doubleoptin'] = __('Double-Opt-in', 'double-opt-in'); |
| 82 |
$panels['requested_tabs'][] = 'f12cf7doubleoptin'; |
| 83 |
} |
| 84 |
} |
| 85 |
return $panels; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* On Contact Form save store the information in the database |
| 90 |
* |
| 91 |
* @param \WPCF7_ContactForm $contact_form |
| 92 |
* @param $args |
| 93 |
* @param $context |
| 94 |
*/ |
| 95 |
public function save($postID) |
| 96 |
{ |
| 97 |
if(!class_exists('\Fusion_Data_PostMeta')){ |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if (!isset($_POST[\Fusion_Data_PostMeta::ROOT]) || !isset($_POST[\Fusion_Data_PostMeta::ROOT]['doubleoptin'])) { // phpcs:ignore WordPress.Security.NonceVerification |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$parameter = CF7DoubleOptIn::getInstance()->sanitize_array($_POST[\Fusion_Data_PostMeta::ROOT]['doubleoptin']); |
| 106 |
$parameter = array_merge($parameter, CF7DoubleOptIn::getInstance()->sanitize_array($_POST[\Fusion_Data_PostMeta::ROOT]['_doubleoptin'])); |
| 107 |
|
| 108 |
// load the default parameter |
| 109 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter($postID); |
| 110 |
|
| 111 |
// validated all parameter defined in the default parameter options and sanitize them. |
| 112 |
foreach ($metadata as $key => $value) { |
| 113 |
if (isset($parameter[$key])) { |
| 114 |
if ($key == 'body') { |
| 115 |
$metadata[$key] = $parameter[$key]; |
| 116 |
} else if ($key == 'sender') { |
| 117 |
$metadata[$key] = $parameter[$key]; |
| 118 |
} else if ($key == 'page' || $key == 'enable') { |
| 119 |
$metadata[$key] = (int)$parameter[$key]; |
| 120 |
} else { |
| 121 |
$metadata[$key] = sanitize_text_field($parameter[$key]); |
| 122 |
} |
| 123 |
} else if ($key == 'enable') { |
| 124 |
$metadata[$key] = 0; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$metadata = apply_filters('f12_cf7_doubleoptin_save_form', $metadata); |
| 129 |
|
| 130 |
update_post_meta($postID, 'f12-cf7-doubleoptin', $metadata); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
private function getPages() |
| 137 |
{ |
| 138 |
$value = array(); |
| 139 |
$pages = get_pages(); |
| 140 |
foreach ($pages as $page) { |
| 141 |
$value[$page->ID] = $page->post_title; |
| 142 |
} |
| 143 |
return $value; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
private function getCategories() |
| 150 |
{ |
| 151 |
$atts = array( |
| 152 |
'perPage' => -1, |
| 153 |
'orderBy' => 'name', |
| 154 |
'order' => 'ASC' |
| 155 |
); |
| 156 |
|
| 157 |
$listOfCategories = Category::get_list($atts, $numberOfPages); |
| 158 |
|
| 159 |
$value = array( |
| 160 |
0 => '---' |
| 161 |
); |
| 162 |
|
| 163 |
foreach ($listOfCategories as $Category /** @var Category $Category */) { |
| 164 |
$value[$Category->get_id()] = $Category->get_name(); |
| 165 |
} |
| 166 |
return $value; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Show the backend double opt in options |
| 171 |
* |
| 172 |
* @param array $tab_data |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function render($tab_data) |
| 176 |
{ |
| 177 |
global $post; |
| 178 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter($post->ID); |
| 179 |
|
| 180 |
$checked = ''; |
| 181 |
if($metadata['enable'] == 1){ |
| 182 |
$checked = 'checked="checked"'; |
| 183 |
} |
| 184 |
|
| 185 |
$TemplateList = new HTMLSelect('_fusion[doubleoptin][template]', array( |
| 186 |
'blank' => 'blank', |
| 187 |
'newsletter_en' => 'newsletter_en', |
| 188 |
'newsletter_en_2' => 'newsletter_en_2', |
| 189 |
'newsletter_en_3' => 'newsletter_en_3' |
| 190 |
), array( |
| 191 |
'class' => array('f12-cf7-templateloader'), |
| 192 |
'style' => array('display:none;') |
| 193 |
)); |
| 194 |
$TemplateList->setOptionSelectedByKey($metadata['template']); |
| 195 |
|
| 196 |
|
| 197 |
$tab_data['f12cf7doubleoptin']['fields'] = [ |
| 198 |
[ |
| 199 |
'id' => 'doubleoptin[enable]', |
| 200 |
'label' => __('Enable', 'double-opt-in'), |
| 201 |
'value' => [1 => __('Enable', 'double-opt-in')], |
| 202 |
'description' => __('Enable this checkox to activate the Double-Opt-In Mail for this formular.', 'double-opt-in').'</p></div><div class="pyre_field"><p><input type="checkbox" id="pyre_doubleoptin[enable]" name="_fusion[doubleoptin][enable]" value="1" '.$checked.'>', |
| 203 |
'default' => 0, |
| 204 |
'dependency' => '', |
| 205 |
'type' => 'custom' |
| 206 |
], |
| 207 |
[ |
| 208 |
'id' => 'doubleoptin[category]', |
| 209 |
'label' => __('Category', 'double-opt-in'), |
| 210 |
'choices' => $this->getCategories(), |
| 211 |
'description' => __('Select the category for the Opt-In.', 'double-opt-in'), |
| 212 |
'default' => 0, |
| 213 |
'dependency' => '', |
| 214 |
'type' => 'select' |
| 215 |
], |
| 216 |
[ |
| 217 |
'id' => 'doubleoptin[page]', |
| 218 |
'label' => __('Confirmation Page', 'double-opt-in'), |
| 219 |
'choices' => $this->getPages(), |
| 220 |
'description' => __('Select the page that should be displayed after the user clicks on the confirmation link', 'double-opt-in'), |
| 221 |
'default' => 0, |
| 222 |
'dependency' => '', |
| 223 |
'type' => 'select' |
| 224 |
], |
| 225 |
[ |
| 226 |
'id' => 'doubleoptin[recipient]', |
| 227 |
'label' => __('To', 'double-opt-in'), |
| 228 |
'description' => __('This field defines who will receive the Double-Opt-In mail. Use the "Field Name" defined for the User E-Mail.', 'double-opt-in'), |
| 229 |
'default' => '', |
| 230 |
'dependency' => '', |
| 231 |
'type' => 'text' |
| 232 |
], |
| 233 |
[ |
| 234 |
'id' => 'doubleoptin[sender]', |
| 235 |
'label' => __('From', 'double-opt-in'), |
| 236 |
'choices' => $metadata['sender'], |
| 237 |
'description' => __('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'), |
| 238 |
'default' => '', |
| 239 |
'dependency' => '', |
| 240 |
'type' => 'text' |
| 241 |
], |
| 242 |
[ |
| 243 |
'id' => 'doubleoptin[subject]', |
| 244 |
'label' => __('Subject', 'double-opt-in'), |
| 245 |
'choices' => $metadata['subject'], |
| 246 |
'description' => __('Enter a custom subject for the Double-Opt-In mail (e.g.: Please confirm your registration).', 'double-opt-in'), |
| 247 |
'default' => '', |
| 248 |
'dependency' => '', |
| 249 |
'type' => 'text' |
| 250 |
], |
| 251 |
[ |
| 252 |
'id' => 'doubleoptin[template]', |
| 253 |
'label' => __('Template', 'double-opt-in'), |
| 254 |
'description' => __('Pick the Template for your double opt in Mail.', 'double-opt-in').'</p></div><div class="pyre_field"><p>'.$TemplateList->get(), |
| 255 |
'default' => 0, |
| 256 |
'dependency' => '', |
| 257 |
'type' => 'custom' |
| 258 |
], |
| 259 |
/*[ |
| 260 |
'id' => 'doubleoptin[template]', |
| 261 |
'label' => __('Template', 'double-opt-in'), |
| 262 |
'choices' => [ |
| 263 |
'blank' => 'Custom', |
| 264 |
'newsletter_en' => 'Newsletter 1', |
| 265 |
'newsletter_en_2' => 'Newsletter 2', |
| 266 |
'newsletter_en_3' => 'Newsletter 3' |
| 267 |
], |
| 268 |
'description' => __('Pick the Template for your double opt in Mail.', 'double-opt-in'), |
| 269 |
'default' => 'blank', |
| 270 |
'dependency' => '', |
| 271 |
'type' => 'select' |
| 272 |
],*/ |
| 273 |
[ |
| 274 |
'id' => 'doubleoptin[body]', |
| 275 |
'label' => __('Message body', 'double-opt-in'), |
| 276 |
'choices' => $metadata['body'], |
| 277 |
'description' => __('Enter the Message you want to sent to your customer. Do not forget to add the Double-Opt-In tag ([doubleoptinlink]) e.g.: <pre><code><a href="[doubleoptinlink]">Confirm the registration</a></code></pre>', 'double-opt-in'), |
| 278 |
'default' => '', |
| 279 |
'dependency' => '', |
| 280 |
'type' => 'textarea' |
| 281 |
], |
| 282 |
]; |
| 283 |
|
| 284 |
return $tab_data; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Extra Function to use the Avada From Options |
| 291 |
*/ |
| 292 |
|
| 293 |
namespace { |
| 294 |
function avada_page_options_tab_f12cf7doubleoptin() |
| 295 |
{ |
| 296 |
return apply_filters('f12_cf7_doubleoptin_avada_form_panel', []); |
| 297 |
} |
| 298 |
} |