| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
use Forge12\Shared\LoggerInterface; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class AvadaFormOptions |
| 14 |
* Add the OptIn Options to the Avada Form |
| 15 |
* |
| 16 |
* @package forge12\contactform7\CF7OptIn |
| 17 |
*/ |
| 18 |
class AvadaFormOptions { |
| 19 |
private LoggerInterface $logger; |
| 20 |
|
| 21 |
/** |
| 22 |
* Admin constructor. |
| 23 |
*/ |
| 24 |
public function __construct( Logger $logger ) { |
| 25 |
$this->logger = $logger; |
| 26 |
|
| 27 |
add_action( 'admin_init', array( $this, 'addHooks' ) ); |
| 28 |
$this->get_logger()->debug( 'Hook admin_init registered', [ |
| 29 |
'plugin' => 'double-opt-in', |
| 30 |
] ); |
| 31 |
|
| 32 |
add_action( 'admin_enqueue_scripts', array( $this, 'addStyles' ) ); |
| 33 |
$this->get_logger()->debug( 'Hook admin_enqueue_scripts registered', [ |
| 34 |
'plugin' => 'double-opt-in', |
| 35 |
] ); |
| 36 |
|
| 37 |
add_action( 'save_post', array( $this, 'save' ), 20, 1 ); |
| 38 |
$this->get_logger()->debug( 'Hook save_post registered', [ |
| 39 |
'plugin' => 'double-opt-in', |
| 40 |
] ); |
| 41 |
|
| 42 |
add_filter( 'awb_po_get_value', array( $this, 'get_value' ), 10, 2 ); |
| 43 |
$this->get_logger()->debug( 'Filter awb_po_get_value registered', [ |
| 44 |
'plugin' => 'double-opt-in', |
| 45 |
] ); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_logger() { |
| 49 |
return $this->logger; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_value( $ret, $field_id ) { |
| 53 |
global $post; |
| 54 |
|
| 55 |
$this->get_logger()->debug( 'get_value called', [ |
| 56 |
'plugin' => 'double-opt-in', |
| 57 |
'class' => __CLASS__, |
| 58 |
'method' => __METHOD__, |
| 59 |
'field_id' => $field_id, |
| 60 |
'post_id' => $post->ID ?? null, |
| 61 |
] ); |
| 62 |
|
| 63 |
if ( ! $post || ! isset( $post->ID ) ) { |
| 64 |
$this->get_logger()->debug( 'No post context available, returning default', [ |
| 65 |
'plugin' => 'double-opt-in', |
| 66 |
] ); |
| 67 |
return $ret; |
| 68 |
} |
| 69 |
|
| 70 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->ID ); |
| 71 |
|
| 72 |
foreach ( $metadata as $key => $value ) { |
| 73 |
$metadata[ 'doubleoptin[' . $key . ']' ] = $value; |
| 74 |
} |
| 75 |
|
| 76 |
if ( isset( $metadata[ $field_id ] ) ) { |
| 77 |
$this->get_logger()->debug( 'Value found for field_id', [ |
| 78 |
'plugin' => 'double-opt-in', |
| 79 |
'field_id' => $field_id, |
| 80 |
'value' => $metadata[ $field_id ], |
| 81 |
] ); |
| 82 |
$ret = $metadata[ $field_id ]; |
| 83 |
} else { |
| 84 |
$this->get_logger()->debug( 'No value found for field_id, returning default', [ |
| 85 |
'plugin' => 'double-opt-in', |
| 86 |
'field_id' => $field_id, |
| 87 |
] ); |
| 88 |
} |
| 89 |
|
| 90 |
return $ret; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Add the styles for the form |
| 96 |
*/ |
| 97 |
public function addStyles( $hook ) { |
| 98 |
$this->get_logger()->debug( 'Enqueuing admin styles and scripts', [ |
| 99 |
'plugin' => 'double-opt-in', |
| 100 |
'class' => __CLASS__, |
| 101 |
'method' => __METHOD__, |
| 102 |
'hook' => $hook, |
| 103 |
] ); |
| 104 |
|
| 105 |
wp_enqueue_style( |
| 106 |
'f12-avada-doubleoptin-admin-avada', |
| 107 |
plugins_url( 'assets/admin-avada-style.css', __FILE__ ) |
| 108 |
); |
| 109 |
$this->get_logger()->debug( 'Admin stylesheet enqueued', [ |
| 110 |
'plugin' => 'double-opt-in', |
| 111 |
] ); |
| 112 |
|
| 113 |
wp_enqueue_script( |
| 114 |
'f12-avada-doubleoptin-templateloader', |
| 115 |
plugins_url( 'assets/f12-avada-templateloader.js', __FILE__ ), |
| 116 |
array( 'jquery' ) |
| 117 |
); |
| 118 |
$this->get_logger()->debug( 'Template loader script enqueued', [ |
| 119 |
'plugin' => 'double-opt-in', |
| 120 |
] ); |
| 121 |
|
| 122 |
wp_localize_script( |
| 123 |
'f12-avada-doubleoptin-templateloader', |
| 124 |
'templateloader', |
| 125 |
array( |
| 126 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 127 |
'nonce' => wp_create_nonce( 'f12_doi_templateloader' ), |
| 128 |
'label_placeholder' => __( 'Please wait while we load the template...', 'double-opt-in' ) |
| 129 |
) |
| 130 |
); |
| 131 |
$this->get_logger()->debug( 'Template loader script localized', [ |
| 132 |
'plugin' => 'double-opt-in', |
| 133 |
] ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Add the hooks responsible to handle wordpress functions |
| 139 |
*/ |
| 140 |
public function addHooks() { |
| 141 |
$this->get_logger()->debug( 'Registering admin hooks', [ |
| 142 |
'plugin' => 'double-opt-in', |
| 143 |
'class' => __CLASS__, |
| 144 |
'method' => __METHOD__, |
| 145 |
] ); |
| 146 |
|
| 147 |
add_filter( 'avada_metabox_tabs', array( $this, 'addPanel' ), 10, 2 ); |
| 148 |
$this->get_logger()->debug( 'Filter avada_metabox_tabs registered', [ |
| 149 |
'plugin' => 'double-opt-in', |
| 150 |
] ); |
| 151 |
|
| 152 |
add_filter( 'f12_cf7_doubleoptin_avada_form_panel', array( $this, 'render' ), 10, 1 ); |
| 153 |
$this->get_logger()->debug( 'Filter f12_cf7_doubleoptin_avada_form_panel registered', [ |
| 154 |
'plugin' => 'double-opt-in', |
| 155 |
] ); |
| 156 |
|
| 157 |
add_action( 'wpcf7_save_contact_form', array( $this, 'save' ), 10, 3 ); |
| 158 |
$this->get_logger()->debug( 'Action wpcf7_save_contact_form registered', [ |
| 159 |
'plugin' => 'double-opt-in', |
| 160 |
] ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Add a custom panel to the contact form 7 options |
| 166 |
*/ |
| 167 |
public function addPanel( array $panels, $post_type ) { |
| 168 |
global $post; |
| 169 |
|
| 170 |
$this->get_logger()->debug( 'addPanel called', [ |
| 171 |
'plugin' => 'double-opt-in', |
| 172 |
'class' => __CLASS__, |
| 173 |
'method' => __METHOD__, |
| 174 |
'post_type' => $post_type, |
| 175 |
'post_id' => $post->ID ?? null, |
| 176 |
] ); |
| 177 |
|
| 178 |
if ( $post && $post->post_type === 'fusion_form' ) { |
| 179 |
if ( $post_type === 'default' ) { |
| 180 |
$panels['tabs_names']['f12cf7doubleoptin'] = __( 'Double-Opt-in', 'double-opt-in' ); |
| 181 |
$panels['requested_tabs'][] = 'f12cf7doubleoptin'; |
| 182 |
|
| 183 |
$this->get_logger()->debug( 'Double-Opt-in panel added to Avada metabox', [ |
| 184 |
'plugin' => 'double-opt-in', |
| 185 |
'post_id' => $post->ID, |
| 186 |
] ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $panels; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* On Contact Form save store the information in the database |
| 196 |
* |
| 197 |
* @param $postID |
| 198 |
*/ |
| 199 |
public function save( $postID ) { |
| 200 |
$this->get_logger()->debug( 'Save called', [ |
| 201 |
'plugin' => 'double-opt-in', |
| 202 |
'class' => __CLASS__, |
| 203 |
'method' => __METHOD__, |
| 204 |
'post_id' => $postID, |
| 205 |
] ); |
| 206 |
|
| 207 |
if ( ! class_exists( '\Fusion_Data_PostMeta' ) ) { |
| 208 |
$this->get_logger()->debug( 'Fusion_Data_PostMeta not available, aborting save', [ |
| 209 |
'plugin' => 'double-opt-in', |
| 210 |
] ); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! isset( $_POST[ \Fusion_Data_PostMeta::ROOT ] ) || ! isset( $_POST[ \Fusion_Data_PostMeta::ROOT ]['doubleoptin'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 215 |
$this->get_logger()->debug( 'No doubleoptin data found in POST, aborting save', [ |
| 216 |
'plugin' => 'double-opt-in', |
| 217 |
] ); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
$parameter = SanitizeHelper::sanitize_array( $_POST[ \Fusion_Data_PostMeta::ROOT ]['doubleoptin'] ); |
| 222 |
$parameter = array_merge( |
| 223 |
$parameter, |
| 224 |
SanitizeHelper::sanitize_array( $_POST[ \Fusion_Data_PostMeta::ROOT ]['_doubleoptin'] ) |
| 225 |
); |
| 226 |
|
| 227 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $postID ); |
| 228 |
|
| 229 |
foreach ( $metadata as $key => $value ) { |
| 230 |
if ( isset( $parameter[ $key ] ) ) { |
| 231 |
if ( $key === 'body' ) { |
| 232 |
$metadata[ $key ] = $parameter[ $key ]; |
| 233 |
} elseif ( $key === 'sender' ) { |
| 234 |
$metadata[ $key ] = $parameter[ $key ]; |
| 235 |
} elseif ( $key === 'page' || $key === 'enable' ) { |
| 236 |
$metadata[ $key ] = (int) $parameter[ $key ]; |
| 237 |
} else { |
| 238 |
$metadata[ $key ] = sanitize_text_field( $parameter[ $key ] ); |
| 239 |
} |
| 240 |
} elseif ( $key === 'enable' ) { |
| 241 |
$metadata[ $key ] = 0; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
$metadata = apply_filters( 'f12_cf7_doubleoptin_metadata_avada', $metadata ); |
| 246 |
$metadata = apply_filters( 'f12_cf7_doubleoptin_save_form', $metadata ); |
| 247 |
|
| 248 |
update_post_meta( $postID, 'f12-cf7-doubleoptin', $metadata ); |
| 249 |
|
| 250 |
$this->get_logger()->debug( 'Metadata saved for post', [ |
| 251 |
'plugin' => 'double-opt-in', |
| 252 |
'post_id' => $postID, |
| 253 |
'data' => $metadata, |
| 254 |
] ); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* @return array |
| 260 |
*/ |
| 261 |
private function getPages() { |
| 262 |
$this->get_logger()->debug( 'Fetching pages', [ |
| 263 |
'plugin' => 'double-opt-in', |
| 264 |
'class' => __CLASS__, |
| 265 |
'method' => __METHOD__, |
| 266 |
] ); |
| 267 |
|
| 268 |
$value = array(); |
| 269 |
$pages = get_pages(); |
| 270 |
|
| 271 |
foreach ( $pages as $page ) { |
| 272 |
$value[ $page->ID ] = $page->post_title; |
| 273 |
} |
| 274 |
|
| 275 |
$this->get_logger()->debug( 'Pages fetched', [ |
| 276 |
'plugin' => 'double-opt-in', |
| 277 |
'count' => count( $value ), |
| 278 |
] ); |
| 279 |
|
| 280 |
return $value; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* @return array |
| 286 |
*/ |
| 287 |
private function getCategories() { |
| 288 |
$this->get_logger()->debug( 'Fetching categories', [ |
| 289 |
'plugin' => 'double-opt-in', |
| 290 |
'class' => __CLASS__, |
| 291 |
'method' => __METHOD__, |
| 292 |
] ); |
| 293 |
|
| 294 |
$atts = array( |
| 295 |
'perPage' => -1, |
| 296 |
'orderBy' => 'name', |
| 297 |
'order' => 'ASC' |
| 298 |
); |
| 299 |
|
| 300 |
$listOfCategories = Category::get_list( $atts, $numberOfPages ); |
| 301 |
|
| 302 |
$value = array( |
| 303 |
0 => '---' |
| 304 |
); |
| 305 |
|
| 306 |
foreach ( $listOfCategories as $Category /** @var Category $Category */ ) { |
| 307 |
$value[ $Category->get_id() ] = $Category->get_name(); |
| 308 |
} |
| 309 |
|
| 310 |
$this->get_logger()->debug( 'Categories fetched', [ |
| 311 |
'plugin' => 'double-opt-in', |
| 312 |
'count' => count( $value ), |
| 313 |
] ); |
| 314 |
|
| 315 |
return $value; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
/** |
| 320 |
* Show the backend double opt in options |
| 321 |
* |
| 322 |
* @param array $tab_data |
| 323 |
* |
| 324 |
* @return array |
| 325 |
*/ |
| 326 |
public function render( $tab_data ) { |
| 327 |
global $post; |
| 328 |
|
| 329 |
$this->get_logger()->debug( 'Rendering Avada settings panel', [ |
| 330 |
'plugin' => 'double-opt-in', |
| 331 |
'class' => __CLASS__, |
| 332 |
'method' => __METHOD__, |
| 333 |
'post_id' => $post->ID ?? null, |
| 334 |
] ); |
| 335 |
|
| 336 |
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->ID ); |
| 337 |
|
| 338 |
$checked = ''; |
| 339 |
if ( $metadata['enable'] == 1 ) { |
| 340 |
$checked = 'checked="checked"'; |
| 341 |
} |
| 342 |
|
| 343 |
$TemplateList = new HTMLSelect( |
| 344 |
'_fusion[doubleoptin][template]', |
| 345 |
[ |
| 346 |
'blank' => 'blank', |
| 347 |
'newsletter_en' => 'newsletter_en', |
| 348 |
'newsletter_en_2' => 'newsletter_en_2', |
| 349 |
'newsletter_en_3' => 'newsletter_en_3' |
| 350 |
], |
| 351 |
[ |
| 352 |
'class' => [ 'f12-cf7-templateloader' ], |
| 353 |
'style' => [ 'display:none;' ] |
| 354 |
] |
| 355 |
); |
| 356 |
$TemplateList->setOptionSelectedByKey( $metadata['template'] ); |
| 357 |
|
| 358 |
$fields = [ |
| 359 |
[ |
| 360 |
'id' => 'doubleoptin[enable]', |
| 361 |
'label' => __( 'Enable', 'double-opt-in' ), |
| 362 |
'value' => [ 1 => __( 'Enable', 'double-opt-in' ) ], |
| 363 |
'description' => __( 'Enable this checkbox 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 . '>', |
| 364 |
'default' => 0, |
| 365 |
'dependency' => '', |
| 366 |
'type' => 'custom' |
| 367 |
], |
| 368 |
[ |
| 369 |
'id' => 'doubleoptin[category]', |
| 370 |
'label' => __( 'Category', 'double-opt-in' ), |
| 371 |
'choices' => $this->getCategories(), |
| 372 |
'description' => __( 'Select the category for the Opt-In.', 'double-opt-in' ), |
| 373 |
'default' => 0, |
| 374 |
'dependency' => '', |
| 375 |
'type' => 'select' |
| 376 |
], |
| 377 |
[ |
| 378 |
'id' => 'doubleoptin[page]', |
| 379 |
'label' => __( 'Confirmation Page', 'double-opt-in' ), |
| 380 |
'choices' => $this->getPages(), |
| 381 |
'description' => __( 'Select the page that should be displayed after the user clicks on the confirmation link', 'double-opt-in' ), |
| 382 |
'default' => 0, |
| 383 |
'dependency' => '', |
| 384 |
'type' => 'select' |
| 385 |
], |
| 386 |
[ |
| 387 |
'id' => 'doubleoptin[recipient]', |
| 388 |
'label' => __( 'To', 'double-opt-in' ), |
| 389 |
'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' ), |
| 390 |
'default' => '', |
| 391 |
'dependency' => '', |
| 392 |
'type' => 'text' |
| 393 |
], |
| 394 |
[ |
| 395 |
'id' => 'doubleoptin[sender]', |
| 396 |
'label' => __( 'From E-Mail', 'double-opt-in' ), |
| 397 |
'choices' => $metadata['sender'], |
| 398 |
'description' => __( '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' ), |
| 399 |
'default' => '', |
| 400 |
'dependency' => '', |
| 401 |
'type' => 'text' |
| 402 |
], |
| 403 |
[ |
| 404 |
'id' => 'doubleoptin[sender_name]', |
| 405 |
'label' => __( 'From Name', 'double-opt-in' ), |
| 406 |
'choices' => $metadata['sender_name'], |
| 407 |
'description' => __( 'This field defines the name for outgoing mails.', 'double-opt-in' ), |
| 408 |
'default' => '', |
| 409 |
'dependency' => '', |
| 410 |
'type' => 'text' |
| 411 |
], |
| 412 |
[ |
| 413 |
'id' => 'doubleoptin[subject]', |
| 414 |
'label' => __( 'Subject', 'double-opt-in' ), |
| 415 |
'choices' => $metadata['subject'], |
| 416 |
'description' => __( 'Enter a custom subject for the Double-Opt-In mail (e.g.: Please confirm your registration).', 'double-opt-in' ), |
| 417 |
'default' => '', |
| 418 |
'dependency' => '', |
| 419 |
'type' => 'text' |
| 420 |
], |
| 421 |
[ |
| 422 |
'id' => 'doubleoptin[template]', |
| 423 |
'label' => __( 'Template', 'double-opt-in' ), |
| 424 |
'description' => __( 'Pick the Template for your double opt-in Mail.', 'double-opt-in' ) . '</p></div><div class="pyre_field"><p>' . $TemplateList->get(), |
| 425 |
'default' => 0, |
| 426 |
'dependency' => '', |
| 427 |
'type' => 'custom' |
| 428 |
], |
| 429 |
[ |
| 430 |
'id' => 'doubleoptin[body]', |
| 431 |
'label' => __( 'Message body', 'double-opt-in' ), |
| 432 |
'choices' => $metadata['body'], |
| 433 |
'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' ), |
| 434 |
'default' => '', |
| 435 |
'dependency' => '', |
| 436 |
'type' => 'textarea' |
| 437 |
], |
| 438 |
]; |
| 439 |
|
| 440 |
$tab_data['f12cf7doubleoptin']['fields'] = apply_filters( |
| 441 |
'f12_cf7_formular_settings_after_avada', |
| 442 |
$fields, |
| 443 |
$metadata |
| 444 |
); |
| 445 |
|
| 446 |
$this->get_logger()->debug( 'Avada settings panel rendered', [ |
| 447 |
'plugin' => 'double-opt-in', |
| 448 |
'post_id' => $post->ID ?? null, |
| 449 |
] ); |
| 450 |
|
| 451 |
return $tab_data; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Extra Function to use the Avada From Options |
| 458 |
*/ |
| 459 |
|
| 460 |
namespace { |
| 461 |
function avada_page_options_tab_f12cf7doubleoptin() { |
| 462 |
return apply_filters( 'f12_cf7_doubleoptin_avada_form_panel', [] ); |
| 463 |
} |
| 464 |
} |