PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 2.12
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v2.12
5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 3.0.70 3.0.71 3.0.72 All 35 releases
double-opt-in / core / OptIn.class.php

OptIn.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 2.12, at core/OptIn.class.php

865 lines 24.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace forge12\contactform7\CF7DoubleOptIn {
4 if (!defined('ABSPATH')) {
5 exit;
6 }
7
8 /**
9 * Class Frontend
10 * Responsible to handle the frontend of the Double OptIn
11 *
12 * @package forge12\contactform7\CF7DoubleOptIn
13 */
14 class OptIn
15 {
16 /**
17 * Stores the DB ID for the entry
18 *
19 * @var int
20 */
21 private $id = 0;
22
23 /**
24 * Stores the ID of the contact form 7 form.
25 *
26 * @var int
27 */
28 private $cf_form_id = 0;
29 /**
30 * Stores if the optin has been confirmed (1) or not (0)
31 *
32 * @var int
33 */
34 private $doubleoptin = 0;
35 /**
36 * Stores the content of the contact form 7 form content as a serialized string.
37 *
38 * @var string
39 */
40 private $content = "";
41 /**
42 * Stores the unique hash used to identify the opt-ins
43 *
44 * @var string
45 */
46 private $hash = "";
47 /**
48 * Stores the timestamp the opt-in mail has been sent to the user.
49 *
50 * @var string
51 */
52 private $createtime = "";
53 /**
54 * Stores the timestamp the opt-in has been confirmed by the user.
55 */
56 private $updatetime = "";
57 /**
58 * IP Address used to trigger the opt-in mail.
59 *
60 * @var string
61 */
62 private $ipaddr_register = "";
63 /**
64 * IP Address used to confirm the opt-in mail.
65 */
66 private $ipaddr_confirmation = "";
67 /**
68 * Stores the files used within this form as a serialized string.
69 */
70 private $files = "";
71 /**
72 * Store the Category ID
73 */
74 private $category = 0;
75 /**
76 * Stores the form as html
77 */
78 private $form = '';
79 /**
80 * Stores the Mail send for the optin
81 *
82 * @var string
83 */
84 private $mail_optin = '';
85 /**
86 * Stores the E-Mail
87 *
88 * @var string
89 */
90 private $email = '';
91
92 /**
93 * The constructor
94 */
95 public function __construct(array $properties)
96 {
97 foreach ($properties as $name => $value) {
98 if (isset($this->{$name})) {
99 $this->{$name} = $value;
100 }
101 }
102 }
103
104 /**
105 * @param int $cf_form_id
106 */
107 public function set_cf_form_id($cf_form_id)
108 {
109 $this->cf_form_id = (int)$cf_form_id;
110 }
111
112 /**
113 * @return int
114 */
115 public function get_cf_form_id()
116 {
117 return $this->cf_form_id;
118 }
119
120 /**
121 * Return the ID of the Optin
122 *
123 * @return int
124 */
125 public function get_id()
126 {
127 return $this->id;
128 }
129
130 /**
131 * Return the hash of the optin.
132 *
133 * @return string
134 */
135 public function get_hash()
136 {
137 return $this->hash;
138 }
139
140 /**
141 * Return true|false depending on confirm status
142 *
143 * @return bool
144 */
145 public function is_confirmed()
146 {
147 return (bool)$this->get_doubleoptin();
148 }
149
150 /**
151 * update the optin value
152 *
153 * @param int $confirmed
154 */
155 public function set_doubleoptin($confirmed)
156 {
157 $this->doubleoptin = (int)$confirmed;
158 }
159
160 /**
161 * Return the optin value for the form.
162 *
163 * @return int
164 */
165 public function get_doubleoptin()
166 {
167 return $this->doubleoptin;
168 }
169
170 /**
171 * @param string $timestamp
172 */
173 public function set_createtime($timestamp)
174 {
175 $this->createtime = $timestamp;
176 }
177
178 /**
179 * Return the Date how long the opt in will be valid
180 * @return string
181 */
182 public function get_valid_until(){
183 $settings = CF7DoubleOptIn::getInstance()->getSettings();
184
185 $createtime = $this->get_createtime();
186 $dt = new \DateTime();
187 $dt->setTimestamp($createtime);
188
189 if($this->is_confirmed()){
190 $month = (int)$settings['delete'];
191 $period = $settings['delete_period'];
192 }else{
193 $month = (int)$settings['delete_unconfirmed'];
194 $period = $settings['delete_unconfirmed_period'];
195 }
196
197 $dt->modify('+'.(int)$month.' '.$period);
198 $dt->modify('+1 day');
199
200 return $dt->format('d.m.Y');
201 }
202
203 /**
204 * Return a timestamp
205 *
206 * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a
207 * formatted string.
208 *
209 * @return string
210 */
211 public function get_createtime($view = "raw")
212 {
213 if ($view != "raw") {
214 if (!is_numeric($this->createtime)) {
215 $date = date('d.m.Y', strtotime($this->createtime));
216 $time = date('H:i:s', strtotime($this->createtime));
217 } else {
218 $date = date('d.m.Y', $this->createtime);
219 $time = date('H:i:s', $this->createtime);
220 }
221
222 return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time;
223 } else {
224 return $this->createtime;
225 }
226 }
227
228 /**
229 * @param string $timestamp
230 */
231 public function set_updatetime($timestamp)
232 {
233 $this->updatetime = $timestamp;
234 }
235
236 /**
237 * Return a timestamp
238 *
239 * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a
240 * formatted string.
241 *
242 * @return string
243 */
244 public function get_updatetime($view = 'raw')
245 {
246 if ($view != "raw") {
247 if (!is_numeric($this->updatetime)) {
248 $date = date('d.m.Y', strtotime($this->updatetime));
249 $time = date('H:i:s', strtotime($this->updatetime));
250 } else {
251 $date = date('d.m.Y', $this->updatetime);
252 $time = date('H:i:s', $this->updatetime);
253 }
254 return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time;
255 } else {
256 return $this->updatetime;
257 }
258 }
259
260 /**
261 * @param string $ip_addr
262 */
263 public function set_ipaddr_register($ip_addr)
264 {
265 $this->ipaddr_register = $ip_addr;
266 }
267
268 /**
269 * @return string
270 */
271 public function get_ipaddr_register()
272 {
273 return $this->ipaddr_register;
274 }
275
276 /**
277 * @param string $ip_addr
278 */
279 public function set_ipaddr_confirmation($ip_addr)
280 {
281 $this->ipaddr_confirmation = $ip_addr;
282 }
283
284 /**
285 * @return string
286 */
287 public function get_ipaddr_confirmation()
288 {
289 return $this->ipaddr_confirmation;
290 }
291
292 /**
293 * Return a serialized string
294 *
295 * @return mixed
296 */
297 public function get_content()
298 {
299 return $this->content;
300 }
301
302 /**
303 * @param string - a serialized string
304 */
305 public function set_content($content)
306 {
307 $this->content = $content;
308 }
309
310 /**
311 * @param string - a serialized string
312 */
313 public function set_files($files)
314 {
315 $this->files = $files;
316 }
317
318 /**
319 * @return string use maybe_unserialize to deserialize the string
320 */
321 public function get_files()
322 {
323 return $this->files;
324 }
325
326 /**
327 * Return the assigned Category ID
328 *
329 * @return int
330 */
331 public function get_category()
332 {
333 if (!is_numeric($this->category)) {
334 return 0;
335 }
336 return $this->category;
337 }
338
339 /**
340 * Assign a ID to the category
341 *
342 * @param int $id
343 *
344 * @return void
345 */
346 public function set_category($id)
347 {
348 $this->category = $id;
349 }
350
351 /**
352 * Check if the given Opt In was created by Contact Form 7
353 *
354 * @return bool
355 */
356 public function isTypeCF7()
357 {
358 $form_id = $this->get_cf_form_id();
359 if (class_exists('\WPCF7_ContactForm')) {
360 $form = \WPCF7_ContactForm::get_instance($form_id);
361
362 if ($form) {
363 return true;
364 }
365 }
366 return false;
367 }
368
369 /**
370 * Check if the given Opt In was created by Avada
371 *
372 * @return bool|void
373 */
374 public function isTypeAvada()
375 {
376 $form_id = $this->get_cf_form_id();
377 if (function_exists('Avada')) {
378 $form = get_post($form_id);
379 if ($form) {
380 return true;
381 }
382 }
383 }
384
385 /**
386 * Check if the Opt In was created by 'cf7' or 'avada'.
387 *
388 * @param string $type use either 'cf7' or 'avada'
389 *
390 * @return bool
391 */
392 public function isType($type)
393 {
394 if ($type == 'cf7') {
395 return $this->isTypeCF7();
396 } else {
397 return (!$this->isTypeCF7() && $this->isTypeAvada());
398 }
399 }
400
401 /**
402 * Load an OptIn by the given hash code. Returns either an Object of Type OptIn or null if not found.
403 *
404 * @param string $hash
405 *
406 * @return OptIn|null
407 */
408 public static function get_by_hash(string $hash)
409 {
410 global $wpdb;
411 $table = $wpdb->prefix . 'f12_cf7_doubleoptin';
412
413 if (null == $wpdb) {
414 return null;
415 }
416
417 $row = $wpdb->get_row('SELECT * FROM ' . $table . ' WHERE hash="' . $hash . '"', ARRAY_A);
418
419 if (null == $row) {
420 return null;
421 }
422
423 return new OptIn($row);
424 }
425
426 /**
427 * Get number of optins for the given post.
428 *
429 * @param $post_id
430 *
431 * @return int
432 */
433 public static function get_count($post_id)
434 {
435 global $wpdb;
436
437 if (!$wpdb) {
438 return 0;
439 }
440
441 $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin';
442 $result = $wpdb->get_results($wpdb->prepare('SELECT count(*) AS counter FROM ' . $tableName . ' WHERE cf_form_id=%d ', $post_id));
443
444 if (is_array($result)) {
445 return $result[0]->counter;
446 }
447 return 0;
448 }
449
450 /**
451 * Return a list of opt ins stored in the database.
452 *
453 * @param int $category_id
454 *
455 * @param array $atts array (
456 * 'perPage' => 10, // Posts per page
457 * 'page' => 0, // Current page
458 * 'order' => DESC, // Order (ASC/DESC)
459 * );
460 *
461 * @param null $numberOfPages - Stores the number of pages found if limited
462 *
463 * @return array
464 */
465 public static function get_list_by_category_id($category_id, $atts = array(), &$numberOfPages = null)
466 {
467 global $wpdb;
468
469 $attr = array(
470 'perPage' => 10,
471 'page' => 1,
472 'order' => 'DESC',
473 'keyword' => ''
474 );
475
476 /**
477 * Parameter
478 */
479 foreach ($atts as $key => $value) {
480 if (isset($attr[$key])) {
481 $attr[$key] = $atts[$key];
482 }
483 }
484
485 /*
486 * Update keyword
487 */
488 $where = '';
489 if (!empty($attr['keyword'])) {
490 $where = ' AND content LIKE "%%' . $attr['keyword'] . '%%"';
491 }
492
493
494 $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin';
495 $pageNum = (int)$attr['page'] - 1;
496
497 if ($numberOfPages !== null) {
498 $result = $wpdb->get_results($wpdb->prepare('SELECT count(*) AS counter FROM ' . $tableName . ' WHERE category=%d ' . $where, $category_id));
499
500 $itemCounter = 0;
501
502 if (is_array($result)) {
503 $itemCounter = $result[0]->counter;
504 }
505
506 $numberOfPages = 0;
507
508 if ($itemCounter != 0) {
509 $numberOfPages = ceil($itemCounter / (int)$attr['perPage']);
510 }
511 }
512
513 $list = array();
514
515 if ($numberOfPages == 0) {
516 return $list;
517 }
518
519 $offset = $pageNum * (int)$attr['perPage'];
520
521 $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . ' WHERE category=%d ' . $where . ' ORDER BY ID ' . $attr['order'] . ' LIMIT ' . $attr['perPage'] . ' OFFSET %d', $category_id, $offset), ARRAY_A);
522
523 foreach ($rows as $row) {
524 $list[] = new OptIn($row);
525 }
526 return $list;
527 }
528
529 /**
530 * Return a list of opt ins stored in the database.
531 *
532 * @param string $email
533 * @param int $confirmed Either recieve confirmed or unconfirmed only
534 *
535 * @return array
536 */
537 public static function get_list_by_email($email)
538 {
539 global $wpdb;
540
541 $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin';
542
543 $list = array();
544
545 $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . ' WHERE email=%s', $email), ARRAY_A);
546
547 foreach ($rows as $row) {
548 $list[] = new OptIn($row);
549 }
550 return $list;
551 }
552
553 /**
554 * Return a list of opt ins stored in the database.
555 *
556 * @param $atts array (
557 * 'perPage' => 10, // Posts per page
558 * 'page' => 0, // Current page
559 * 'order' => DESC, // Order (ASC/DESC)
560 * );
561 *
562 * @param null $numberOfPages - Stores the number of pages found if limited
563 *
564 * @return array
565 */
566 public static function get_list($atts = array(), &$numberOfPages = null)
567 {
568 global $wpdb;
569
570 $attr = array(
571 'perPage' => 10,
572 'page' => 1,
573 'order' => 'DESC',
574 'keyword' => '',
575 'cf_form_id' => '',
576 );
577
578 /*
579 * Update atts
580 */
581 foreach ($atts as $key => $value) {
582 if (isset($attr[$key])) {
583 $attr[$key] = $atts[$key];
584 }
585 }
586
587 /*
588 * Update WHERE for Query
589 */
590 $where = '';
591 $where_condition = [];
592
593 /*
594 * Add Keyword
595 */
596 if (!empty($attr['keyword'])) {
597 $where_condition[] = 'content LIKE "%%' . $attr['keyword'] . '%%"';
598 }
599
600 /*
601 * Add Form ID
602 */
603 if (!empty($attr['cf_form_id'])) {
604 $where_condition[] = 'cf_form_id="' . $attr['cf_form_id'] . '"';
605 }
606
607 /*
608 * build where string
609 */
610 if (!empty($where_condition)) {
611 $where = ' WHERE ' . implode(' AND ', $where_condition);
612 }
613
614 /*
615 * set table
616 */
617 $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin';
618 $pageNum = (int)$attr['page'] - 1;
619
620 if ($numberOfPages !== null) {
621 $result = $wpdb->get_results('SELECT count(*) as counter FROM ' . $tableName . $where);
622
623 $itemCounter = 0;
624
625 if (is_array($result)) {
626 $itemCounter = $result[0]->counter;
627 }
628
629 $numberOfPages = 0;
630
631 if ($itemCounter != 0) {
632 $numberOfPages = ceil($itemCounter / (int)$attr['perPage']);
633 }
634 }
635
636 $list = array();
637
638 if ($numberOfPages == 0) {
639 return $list;
640 }
641
642 $offset = $pageNum * (int)$attr['perPage'];
643
644 $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . $where . ' ORDER BY ID ' . $attr['order'] . ' LIMIT ' . $attr['perPage'] . ' OFFSET % d', $offset), ARRAY_A);
645
646 foreach ($rows as $row) {
647 $list[] = new OptIn($row);
648 }
649 return $list;
650 }
651
652 /**
653 * This will update all categories from A to B, e.g.: All Opt-Ins with id 1 to category id 2.
654 *
655 * @param int $from_id The origin Category ID
656 * @param int $to_id The new Category ID
657 *
658 * @return bool|int|\mysqli_result|resource|null Return the number of rows affected by the query.
659 */
660 public static function bulk_update_category($from_id, $to_id)
661 {
662 global $wpdb;
663 $table = $wpdb->prefix . 'f12_cf7_doubleoptin';
664
665 return $wpdb->query($wpdb->prepare('UPDATE ' . $table . ' SET category =%d WHERE category =%d', $to_id, $from_id));
666 }
667
668 /**
669 * Update the category of the given opt in id.
670 *
671 * @param int $optin_id The ID of the OptIn
672 * @param int $category_id The ID of the Category
673 *
674 * @return bool|int|\mysqli_result|resource|null
675 */
676 public static function update_category_by_id($optin_id, $category_id)
677 {
678 global $wpdb;
679 $table = $wpdb->prefix . 'f12_cf7_doubleoptin';
680
681 return $wpdb->query($wpdb->prepare('UPDATE ' . $table . ' SET category =%d WHERE id =%d', $category_id, $optin_id));
682 }
683
684 /**
685 * Update or create the given Object
686 */
687 public function save()
688 {
689 global $wpdb;
690 $table = $wpdb->prefix . 'f12_cf7_doubleoptin';
691
692 if (!empty($this->get_hash())) {
693 return $wpdb->update($table, array(
694 'doubleoptin' => $this->get_doubleoptin(),
695 'createtime' => $this->get_createtime(),
696 'updatetime' => $this->get_updatetime(),
697 'ipaddr_confirmation' => $this->get_ipaddr_confirmation(),
698 'ipaddr_register' => $this->get_ipaddr_register(),
699 'cf_form_id' => $this->get_cf_form_id(),
700 'content' => $this->get_content(),
701 'files' => $this->get_files(),
702 'category' => $this->get_category(),
703 'mail_optin' => $this->get_mail_optin(),
704 ), array(
705 'hash' => $this->get_hash()
706 ));
707 } else {
708 $result = $wpdb->insert($table, array(
709 'cf_form_id' => $this->get_cf_form_id(),
710 'doubleoptin' => $this->get_doubleoptin(),
711 'createtime' => $this->get_createtime(),
712 'updatetime' => $this->get_updatetime(),
713 'ipaddr_confirmation' => $this->get_ipaddr_confirmation(),
714 'ipaddr_register' => $this->get_ipaddr_register(),
715 'content' => $this->get_content(),
716 'files' => $this->get_files(),
717 'category' => $this->get_category(),
718 'form' => $this->get_form(),
719 'mail_optin' => $this->get_mail_optin(),
720 'email' => $this->get_email()
721 ));
722
723 if ($result > 0) {
724 $this->id = $wpdb->insert_id;
725 return $this->createHash();
726 }
727 }
728 return false;
729 }
730
731 /**
732 * Creates a unique hash code and adds it to the current OptIn Object
733 */
734 private function createHash()
735 {
736 global $wpdb;
737
738 if (null == $wpdb) {
739 return false;
740 }
741
742 $table = $wpdb->prefix . 'f12_cf7_doubleoptin';
743
744 // add the hash to the element
745
746 $this->hash = base64_encode($this->get_createtime() . $this->get_id() . $this->get_cf_form_id());
747
748 return $wpdb->update($table, array(
749 'hash' => $this->hash,
750 ), array(
751 'id' => $this->get_id()
752 ));
753 }
754
755 /**
756 * Return the Form
757 *
758 * @return string
759 */
760 public function get_form()
761 {
762 return $this->form;
763 }
764
765 /**
766 * Set the Form as HTML
767 *
768 * @param string $form
769 *
770 * @return void
771 */
772 public function set_form($form)
773 {
774 $this->form = $form;
775 }
776
777 /**
778 * Return the Form
779 *
780 * @return string
781 */
782 public function get_mail_optin()
783 {
784 return $this->mail_optin;
785 }
786
787 /**
788 * Return the E-mail
789 *
790 * @return string
791 */
792 public function get_email()
793 {
794 return $this->email;
795 }
796
797 /**
798 * Store the email
799 *
800 * @param string $email
801 *
802 * @return void
803 */
804 public function set_email($email)
805 {
806 $this->email = $email;
807 }
808
809 /**
810 * Set the Form as HTML
811 *
812 * @param string $form
813 *
814 * @return void
815 */
816 public function set_mail_optin($mail_html)
817 {
818 $this->mail_optin = $mail_html;
819 }
820
821 /**
822 * Return the link to the detail view
823 *
824 * @return string
825 */
826 public function get_link_ui()
827 {
828 return admin_url('admin.php?page=f12-cf7-doubleoptin_optin_view&hash=' . $this->get_hash());
829 }
830
831 /**
832 * Return the link to delete
833 *
834 * @return string
835 */
836 public function get_link_delete()
837 {
838 return admin_url('admin.php?page=f12-cf7-doubleoptin&option=delete&hash=' . $this->get_hash());
839 }
840
841 /**
842 * Return the link to the optin
843 *
844 * @return string
845 */
846 public function get_link_optin()
847 {
848 if ($this->get_cf_form_id() == 0) {
849 return get_home_url() . '?optin=' . $this->get_hash();
850 }
851
852 $parameter = CF7DoubleOptIn::getInstance()->getParameter($this->get_cf_form_id());
853
854 $page_id = $parameter['page'];
855
856 if ($page_id == -1 || !$page_id || $page_id == 0) {
857 $link = get_home_url() . '?optin=' . $this->get_hash();
858 } else {
859 $link = get_permalink($page_id) . '?optin=' . $this->get_hash();
860 }
861
862 return $link;
863 }
864 }
865 }