-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcookie-notice.php
1885 lines (1620 loc) · 78.5 KB
/
cookie-notice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Cookie Notice
Description: Cookie Notice allows you to elegantly inform users that your site uses cookies and helps you comply with the EU GDPR cookie law and CCPA regulations.
Version: 1.3.2
Author: dFactory
Author URI: http://www.dfactory.eu/
Plugin URI: http://www.dfactory.eu/plugins/cookie-notice/
License: MIT License
License URI: http://opensource.org/licenses/MIT
Text Domain: cookie-notice
Domain Path: /languages
Cookie Notice
Copyright (C) 2013-2020, Digital Factory - [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice class.
*
* @class Cookie_Notice
* @version 1.3.2
*/
class Cookie_Notice {
/**
* @var $defaults
*/
private $defaults = array(
'general' => array(
'position' => 'bottom',
'message_text' => '',
'css_style' => 'bootstrap',
'css_class' => '',
'accept_text' => '',
'refuse_text' => '',
'refuse_opt' => false,
'refuse_code' => '',
'refuse_code_head' => '',
'revoke_cookies' => false,
'revoke_cookies_opt' => 'automatic',
'revoke_message_text' => '',
'revoke_text' => '',
'redirection' => false,
'see_more' => false,
'link_target' => '_blank',
'link_position' => 'banner',
'time' => 'month',
'time_rejected' => 'month',
'hide_effect' => 'fade',
'on_scroll' => false,
'on_scroll_offset' => 100,
'on_click' => false,
'colors' => array(
'text' => '#fff',
'bar' => '#000',
'bar_opacity' => 100
),
'see_more_opt' => array(
'text' => '',
'link_type' => 'page',
'id' => 0,
'link' => '',
'sync' => false
),
'script_placement' => 'header',
'coronabar' => false, // disabled by default
'coronabar_cases' => true,
'coronabar_texts' => false,
'coronabar_text_strings' => array(
'headline' => 'Spread the message. Stop the virus.',
'step_one_title' => 'Hands', // string
'step_one_desc' => 'Wash often', // string
'step_two_title' => 'Elbow', // string
'step_two_desc' => 'Cough into', // string
'step_three_title' => 'Face', // string
'step_three_desc' => 'Don\'t touch', // string
'step_four_title' => 'Space', // string
'step_four_desc' => 'Avoid crowds', // string
'step_five_title' => 'Home', // string
'step_five_desc' => 'Stay inside', // string
'confirmed' => 'Confirmed', // string
'recovered' => 'Recovered', // string
),
'translate' => true,
'deactivation_delete' => false,
'update_version' => 3,
'update_notice' => true,
'update_delay_date' => 0
),
'version' => '1.3.2'
);
private $positions = array();
private $styles = array();
private $choices = array();
private $links = array();
private $link_targets = array();
private $link_positions = array();
private $colors = array();
private $options = array();
private $effects = array();
private $times = array();
private $notices = array();
private $script_placements = array();
private static $_instance;
/**
* Disable object cloning.
*/
public function __clone() {}
/**
* Disable unserializing of the class.
*/
public function __wakeup() {}
/**
* Main plugin instance.
*
* @return object
*/
public static function instance() {
if ( self::$_instance === null ) {
self::$_instance = new self();
add_action( 'plugins_loaded', array( self::$_instance, 'load_textdomain' ) );
self::$_instance->includes();
}
return self::$_instance;
}
/**
* Constructor.
*
* @return void
*/
public function __construct() {
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
// get options
$options = get_option( 'cookie_notice_options', $this->defaults['general'] );
// check legacy parameters
$options = $this->check_legacy_params( $options, array( 'refuse_opt', 'on_scroll', 'on_click', 'deactivation_delete', 'see_more' ) );
// merge old options with new ones
$this->options = array(
'general' => $this->multi_array_merge( $this->defaults['general'], $options )
);
if ( ! isset( $this->options['general']['see_more_opt']['sync'] ) )
$this->options['general']['see_more_opt']['sync'] = $this->defaults['general']['see_more_opt']['sync'];
// actions
add_action( 'init', array( $this, 'register_shortcodes' ) );
add_action( 'init', array( $this, 'wpsc_add_cookie' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_init', array( $this, 'update_notice' ) );
add_action( 'admin_init', array( $this, 'check_version' ) );
add_action( 'admin_menu', array( $this, 'admin_menu_options' ) );
add_action( 'after_setup_theme', array( $this, 'load_defaults' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'wp_head', array( $this, 'wp_print_header_scripts' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'wp_print_footer_scripts' ) );
add_action( 'wp_footer', array( $this, 'add_cookie_notice' ), 1000 );
add_action( 'wp_footer', array( $this, 'wp_footer_corona' ), 1001 );
add_action( 'wp_ajax_cn_dismiss_notice', array( $this, 'ajax_dismiss_admin_notice' ) );
add_action( 'wp_ajax_cn_save_cases', array( $this, 'ajax_save_cases' ) );
add_action( 'wp_ajax_nopriv_cn_save_cases', array( $this, 'ajax_save_cases' ) );
// filters
add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );
add_filter( 'body_class', array( $this, 'change_body_class' ) );
}
/**
* Include required files.
*
* @return void
*/
private function includes() {
include_once( plugin_dir_path( __FILE__ ) . 'includes/functions.php' );
}
/**
* Load plugin defaults.
*/
public function load_defaults() {
$this->positions = array(
'top' => __( 'Top', 'cookie-notice' ),
'bottom' => __( 'Bottom', 'cookie-notice' )
);
$this->styles = array(
'none' => __( 'None', 'cookie-notice' ),
'wp-default' => __( 'Light', 'cookie-notice' ),
'bootstrap' => __( 'Dark', 'cookie-notice' )
);
$this->revoke_opts = array(
'automatic' => __( 'Automatic', 'cookie-notice' ),
'manual' => __( 'Manual', 'cookie-notice' )
);
$this->links = array(
'page' => __( 'Page link', 'cookie-notice' ),
'custom' => __( 'Custom link', 'cookie-notice' )
);
$this->link_targets = array(
'_blank',
'_self'
);
$this->link_positions = array(
'banner' => __( 'Banner', 'cookie-notice' ),
'message' => __( 'Message', 'cookie-notice' )
);
$this->colors = array(
'text' => __( 'Text color', 'cookie-notice' ),
'bar' => __( 'Bar color', 'cookie-notice' ),
);
$this->times = apply_filters(
'cn_cookie_expiry',
array(
'hour' => array( __( 'An hour', 'cookie-notice' ), 3600 ),
'day' => array( __( '1 day', 'cookie-notice' ), 86400 ),
'week' => array( __( '1 week', 'cookie-notice' ), 604800 ),
'month' => array( __( '1 month', 'cookie-notice' ), 2592000 ),
'3months' => array( __( '3 months', 'cookie-notice' ), 7862400 ),
'6months' => array( __( '6 months', 'cookie-notice' ), 15811200 ),
'year' => array( __( '1 year', 'cookie-notice' ), 31536000 ),
'infinity' => array( __( 'infinity', 'cookie-notice' ), 2147483647 )
)
);
$this->effects = array(
'none' => __( 'None', 'cookie-notice' ),
'fade' => __( 'Fade', 'cookie-notice' ),
'slide' => __( 'Slide', 'cookie-notice' )
);
$this->script_placements = array(
'header' => __( 'Header', 'cookie-notice' ),
'footer' => __( 'Footer', 'cookie-notice' ),
);
// set default text strings
$this->defaults['general']['message_text'] = __( 'We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.', 'cookie-notice' );
$this->defaults['general']['accept_text'] = __( 'Ok', 'cookie-notice' );
$this->defaults['general']['refuse_text'] = __( 'No', 'cookie-notice' );
$this->defaults['general']['revoke_message_text'] = __( 'You can revoke your consent any time using the Revoke consent button.', 'cookie-notice' );
$this->defaults['general']['revoke_text'] = __( 'Revoke consent', 'cookie-notice' );
$this->defaults['general']['see_more_opt']['text'] = __( 'Privacy policy', 'cookie-notice' );
// set default coronabar text strings
$this->defaults['general']['coronabar_text_strings']['headline'] = __( 'Spread the message. Stop the virus.', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_one_title'] = __( 'Hands', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_one_desc'] = __( 'Wash often', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_two_title'] = __( 'Elbow', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_two_desc'] = __( 'Cough into', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_three_title'] = __( 'Face', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_three_desc'] = __( 'Don\'t touch', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_four_title'] = __( 'Space', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_four_desc'] = __( 'Avoid crowds', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_five_title'] = __( 'Home', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['step_five_desc'] = __( 'Stay inside', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['confirmed'] = __( 'Confirmed', 'cookie-notice' );
$this->defaults['general']['coronabar_text_strings']['recovered'] = __( 'Recovered', 'cookie-notice' );
// set translation strings on plugin activation
if ( $this->options['general']['translate'] === true ) {
$this->options['general']['translate'] = false;
$this->options['general']['message_text'] = $this->defaults['general']['message_text'];
$this->options['general']['accept_text'] = $this->defaults['general']['accept_text'];
$this->options['general']['refuse_text'] = $this->defaults['general']['refuse_text'];
$this->options['general']['revoke_message_text'] = $this->defaults['general']['revoke_message_text'];
$this->options['general']['revoke_text'] = $this->defaults['general']['revoke_text'];
$this->options['general']['see_more_opt']['text'] = $this->defaults['general']['see_more_opt']['text'];
// coronabar strings
foreach ( $this->defaults['general']['coronabar_text_strings'] as $key => $label ) {
$this->options['general']['coronabar_text_strings'][$key] = $this->defaults['general']['coronabar_text_strings'][$key];
}
update_option( 'cookie_notice_options', $this->options['general'] );
}
// WPML >= 3.2
if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, '3.2', '>=' ) ) {
$this->register_wpml_strings();
// WPML and Polylang compatibility
} elseif ( function_exists( 'icl_register_string' ) ) {
icl_register_string( 'Cookie Notice', 'Message in the notice', $this->options['general']['message_text'] );
icl_register_string( 'Cookie Notice', 'Button text', $this->options['general']['accept_text'] );
icl_register_string( 'Cookie Notice', 'Refuse button text', $this->options['general']['refuse_text'] );
icl_register_string( 'Cookie Notice', 'Revoke message text', $this->options['general']['revoke_message_text'] );
icl_register_string( 'Cookie Notice', 'Revoke button text', $this->options['general']['revoke_text'] );
icl_register_string( 'Cookie Notice', 'Privacy policy text', $this->options['general']['see_more_opt']['text'] );
icl_register_string( 'Cookie Notice', 'Custom link', $this->options['general']['see_more_opt']['link'] );
// coronabar strings
foreach ( $this->defaults['general']['coronabar_text_strings'] as $key => $label ) {
icl_register_string( 'Cookie Notice', $key, $this->options['general']['coronabar_text_strings'][$key] );
}
}
}
/**
* Check plugin version.
*
* @return void
*/
public function check_version() {
if ( ! current_user_can( 'install_plugins' ) )
return;
// get current database version
$current_db_version = get_option( 'cookie_notice_version', '1.0.0' );
// new version?
if ( version_compare( $current_db_version, $this->defaults['version'], '<' ) ) {
// updates plugin version
update_option( 'cookie_notice_version', $this->defaults['version'], false );
}
}
/**
* Update notice.
*
* @return void
*/
public function update_notice() {
if ( ! current_user_can( 'install_plugins' ) )
return;
// test only
// $this->options['general'] = wp_parse_args( array( 'update_version' => 2, 'update_notice' => true ), $this->options['general'] );
// update_option( 'cookie_notice_options', $this->options['general'] );
$current_update = 4;
if ( $this->options['general']['update_version'] < $current_update ) {
// check version, if update version is lower than plugin version, set update notice to true
$this->options['general'] = wp_parse_args( array( 'update_version' => $current_update, 'update_notice' => true ), $this->options['general'] );
update_option( 'cookie_notice_options', $this->options['general'] );
}
// display current version notice
if ( $this->options['general']['update_notice'] === true && $this->options['general']['coronabar'] ) {
// include notice js and css, only if needed
wp_enqueue_script( 'cookie-notice-admin-notice', plugins_url( 'js/admin-notice' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', __FILE__ ), array( 'jquery' ), $this->defaults['version'] );
wp_localize_script(
'cookie-notice-admin-notice',
'cnArgsNotice',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'cn_dismiss_notice' )
)
);
wp_enqueue_style( 'cookie-notice-admin-notice', plugins_url( 'css/admin-notice' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', __FILE__ ) );
$notice_html =
'<div class="cn-notice-text">' .
'<p>' . __( "Thank you for using the <strong>Corona Banner</strong> and spreading the word about steps to stop the Coronavirus pandemia.", 'cookie-notice' ) . '<br />' .
sprintf( __( 'Feel free to customize or disable it anytime in the <a href="%s">Cookie Notice settings</a>.', 'cookie-notice' ), admin_url( 'options-general.php?page=cookie-notice' ) ) . '</p>' .
'</div>';
$this->add_notice( $notice_html, 'notice is-dismissible', 'div' );
}
}
/**
* Add admin notices.
*
* @param string $html
* @param string $status
* @param bool $container
* @return void
*/
private function add_notice( $html = '', $status = 'error', $container = '' ) {
$this->notices[] = array(
'html' => $html,
'status' => $status,
'container' => ( ! empty( $container ) && in_array( $container, array( 'p', 'div' ) ) ? $container : '' )
);
add_action( 'admin_notices', array( $this, 'display_notice'), 0 );
}
/**
* Print admin notices.
*
* @return void
*/
public function display_notice() {
foreach( $this->notices as $notice ) {
echo '
<div id="cn-admin-notice" class="cn-notice ' . $notice['status'] . '">
' . ( ! empty( $notice['container'] ) ? '<' . $notice['container'] . ' class="cn-notice-container">' : '' ) . '
' . $notice['html'] . '
' . ( ! empty( $notice['container'] ) ? '</' . $notice['container'] . ' class="cn-notice-container">' : '' ) . '
</div>';
}
}
/**
* Dismiss admin notice.
*
* @return void
*/
public function ajax_dismiss_admin_notice() {
if ( ! current_user_can( 'install_plugins' ) )
return;
if ( wp_verify_nonce( esc_attr( $_REQUEST['nonce'] ), 'cn_dismiss_notice' ) ) {
$notice_action = empty( $_REQUEST['notice_action'] ) || $_REQUEST['notice_action'] === 'dismiss' ? 'dismiss' : esc_attr( $_REQUEST['notice_action'] );
switch ( $notice_action ) {
// delay notice
case 'delay':
// set delay period to 1 week from now
$this->options['general'] = wp_parse_args( array( 'update_delay_date' => time() + 1209600 ), $this->options['general'] );
// update options
update_option( 'cookie_notice_options', $this->options['general'] );
break;
// approve notice
case 'approve':
// enable coronabar
$this->options['general'] = wp_parse_args( array( 'coronabar' => true), $this->options['general'] );
// hide notice
$this->options['general'] = wp_parse_args( array( 'update_notice' => false ), $this->options['general'] );
$this->options['general'] = wp_parse_args( array( 'update_delay_date' => 0 ), $this->options['general'] );
// update options
update_option( 'cookie_notice_options', $this->options['general'] );
break;
// hide notice
default:
$this->options['general'] = wp_parse_args( array( 'update_notice' => false ), $this->options['general'] );
$this->options['general'] = wp_parse_args( array( 'update_delay_date' => 0 ), $this->options['general'] );
update_option( 'cookie_notice_options', $this->options['general'] );
}
}
exit;
}
/**
* Save corona cases locally for caching.
*
* @return void
*/
public function ajax_save_cases() {
// check if the bar is enabled
if ( ! $this->options['general']['coronabar'] )
return;
if ( ! wp_verify_nonce( esc_attr( $_REQUEST['nonce'] ), 'cn_save_cases' ) )
return;
$json_data = ! empty( $_REQUEST['data'] ) ? esc_attr( $_REQUEST['data'] ) : false;
$cases_data = array();
if ( ! empty( $json_data ) )
$cases_data = json_decode( stripslashes( html_entity_decode( $json_data ) ), true );
// save data
if ( $cases_data && is_array( $cases_data ) )
set_transient( 'cookie_notice_coronadata', $cases_data, 12 * HOUR_IN_SECONDS );
}
/**
* Add new body classes.
*
* @param array $classes Body classes
* @return array
*/
public function change_body_class( $classes ) {
if ( is_admin() )
return $classes;
if ( $this->cookies_set() ) {
$classes[] = 'cookies-set';
if ( $this->cookies_accepted() )
$classes[] = 'cookies-accepted';
else
$classes[] = 'cookies-refused';
} else
$classes[] = 'cookies-not-set';
return $classes;
}
/**
* Register shortcode.
*
* @return void
*/
public function register_shortcodes() {
add_shortcode( 'cookies_accepted', array( $this, 'cookies_accepted_shortcode' ) );
add_shortcode( 'cookies_revoke', array( $this, 'cookies_revoke_shortcode' ) );
add_shortcode( 'cookies_policy_link', array( $this, 'cookies_policy_link_shortcode' ) );
}
/**
* Register cookies accepted shortcode.
*
* @param array $args
* @param mixed $content
* @return string
*/
public function cookies_accepted_shortcode( $args, $content ) {
if ( $this->cookies_accepted() ) {
$scripts = html_entity_decode( trim( wp_kses( $content, $this->get_allowed_html() ) ) );
if ( ! empty( $scripts ) ) {
if ( preg_match_all( '/' . get_shortcode_regex() . '/', $content ) )
$scripts = do_shortcode( $scripts );
return $scripts;
}
}
return '';
}
/**
* Register cookies accepted shortcode.
*
* @param array $args
* @param mixed $content
* @return string
*/
public function cookies_revoke_shortcode( $args, $content ) {
// get options
$options = $this->options['general'];
// defaults
$defaults = array(
'title' => $options['revoke_text'],
'class' => $options['css_class']
);
// combine shortcode arguments
$args = shortcode_atts( $defaults, $args );
// escape class(es)
$args['class'] = esc_attr( $args['class'] );
return '<a href="#" class="cn-revoke-cookie cn-button cn-revoke-inline' . ( $options['css_style'] !== 'none' ? ' ' . $options['css_style'] : '' ) . ( $args['class'] !== '' ? ' ' . $args['class'] : '' ) . '" title="' . esc_html( $args['title'] ) . '">' . esc_html( $args['title'] ) . '</a>';
}
/**
* Register cookies policy link shortcode.
*
* @param array $args
* @param string $content
* @return string
*/
public function cookies_policy_link_shortcode( $args, $content ) {
// get options
$options = $this->options['general'];
// defaults
$defaults = array(
'title' => esc_html( $options['see_more_opt']['text'] !== '' ? $options['see_more_opt']['text'] : '➜' ),
'link' => ( $options['see_more_opt']['link_type'] === 'custom' ? $options['see_more_opt']['link'] : get_permalink( $options['see_more_opt']['id'] ) ),
'class' => $options['css_class']
);
// combine shortcode arguments
$args = shortcode_atts( $defaults, $args );
return '<a href="' . $args['link'] . '" target="' . $options['link_target'] . '" id="cn-more-info" class="cn-privacy-policy-link cn-link' . ( $args['class'] !== '' ? ' ' . $args['class'] : '' ) . '">' . esc_html( $args['title'] ) . '</a>';
}
/**
* Register WPML (>= 3.2) strings if needed.
*
* @return void
*/
private function register_wpml_strings() {
global $wpdb;
// prepare strings
$strings = array(
'Message in the notice' => $this->options['general']['message_text'],
'Button text' => $this->options['general']['accept_text'],
'Refuse button text' => $this->options['general']['refuse_text'],
'Revoke message text' => $this->options['general']['revoke_message_text'],
'Revoke button text' => $this->options['general']['revoke_text'],
'Privacy policy text' => $this->options['general']['see_more_opt']['text'],
'Custom link' => $this->options['general']['see_more_opt']['link']
);
// coronabar strings
foreach ( $this->defaults['general']['coronabar_text_strings'] as $key => $label ) {
$strings[$key] = $this->options['general']['coronabar_text_strings'];
}
// get query results
$results = $wpdb->get_col( $wpdb->prepare( "SELECT name FROM " . $wpdb->prefix . "icl_strings WHERE context = %s", 'Cookie Notice' ) );
// check results
foreach( $strings as $string => $value ) {
// string does not exist?
if ( ! in_array( $string, $results, true ) ) {
// register string
do_action( 'wpml_register_single_string', 'Cookie Notice', $string, $value );
}
}
}
/**
* Load textdomain.
*
* @return void
*/
public function load_textdomain() {
load_plugin_textdomain( 'cookie-notice', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Add submenu.
*
* @return void
*/
public function admin_menu_options() {
add_options_page( __( 'Cookie Notice', 'cookie-notice' ), __( 'Cookie Notice', 'cookie-notice' ), apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ), 'cookie-notice', array( $this, 'options_page' ) );
}
/**
* Options page output.
*
* @return void
*/
public function options_page() {
echo '
<div class="wrap">
<h2>' . __( 'Cookie Notice', 'cookie-notice' ) . '</h2>
<div class="cookie-notice-settings">
<div class="cookie-notice-credits">
<h3 class="hndle">' . __( 'Cookie Notice', 'cookie-notice' ) . ' ' . $this->defaults['version'] . '</h3>
<div class="inside">
<h4 class="inner">' . __( 'Need support?', 'cookie-notice' ) . '</h4>
<p class="inner">' . sprintf( __( 'If you are having problems with this plugin, please browse it\'s <a href="%s" target="_blank">Documentation</a> or talk about them in the <a href="%s" target="_blank">Support forum</a>', 'cookie-notice' ), 'https://www.dfactory.eu/docs/cookie-notice/?utm_source=cookie-notice-settings&utm_medium=link&utm_campaign=docs', 'https://dfactory.eu/support/?utm_source=cookie-notice-settings&utm_medium=link&utm_campaign=support' ) . '</p>
<hr />
<h4 class="inner">' . __( 'Do you like this plugin?', 'cookie-notice' ) . '</h4>
<p class="inner">' . sprintf( __( '<a href="%s" target="_blank">Rate it 5</a> on WordPress.org', 'cookie-notice' ), 'https://wordpress.org/support/plugin/cookie-notice/reviews/?filter=5' ) . '<br />' .
sprintf( __( 'Blog about it & link to the <a href="%s" target="_blank">plugin page</a>.', 'cookie-notice' ), 'https://dfactory.eu/plugins/cookie-notice?utm_source=cookie-notice-settings&utm_medium=link&utm_campaign=blog-about' ) . '<br />' .
sprintf( __( 'Check out our other <a href="%s" target="_blank">WordPress plugins</a>.', 'cookie-notice' ), 'https://dfactory.eu/plugins/?utm_source=cookie-notice-settings&utm_medium=link&utm_campaign=other-plugins' ) . '
</p>
</div>
</div>
<form action="options.php" method="post">';
settings_fields( 'cookie_notice_options' );
do_settings_sections( 'cookie_notice_options' );
echo '
<p class="submit">';
submit_button( '', 'primary', 'save_cookie_notice_options', false );
echo ' ';
submit_button( __( 'Reset to defaults', 'cookie-notice' ), 'secondary', 'reset_cookie_notice_options', false );
echo '
</p>
</form>
</div>
<div class="clear"></div>
</div>';
}
/**
* Regiseter plugin settings.
*
* @return void
*/
public function register_settings() {
register_setting( 'cookie_notice_options', 'cookie_notice_options', array( $this, 'validate_options' ) );
// configuration
add_settings_section( 'cookie_notice_configuration', __( 'Configuration', 'cookie-notice' ), array( $this, 'cn_section_configuration' ), 'cookie_notice_options' );
add_settings_field( 'cn_message_text', __( 'Message', 'cookie-notice' ), array( $this, 'cn_message_text' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_accept_text', __( 'Button text', 'cookie-notice' ), array( $this, 'cn_accept_text' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_see_more', __( 'Privacy policy', 'cookie-notice' ), array( $this, 'cn_see_more' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_refuse_opt', __( 'Refuse consent', 'cookie-notice' ), array( $this, 'cn_refuse_opt' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_revoke_opt', __( 'Revoke consent', 'cookie-notice' ), array( $this, 'cn_revoke_opt' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_refuse_code', __( 'Script blocking', 'cookie-notice' ), array( $this, 'cn_refuse_code' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_redirection', __( 'Reloading', 'cookie-notice' ), array( $this, 'cn_redirection' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_on_scroll', __( 'On scroll', 'cookie-notice' ), array( $this, 'cn_on_scroll' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_on_click', __( 'On click', 'cookie-notice' ), array( $this, 'cn_on_click' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_time', __( 'Accepted expiry', 'cookie-notice' ), array( $this, 'cn_time' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_time_rejected', __( 'Rejected expiry', 'cookie-notice' ), array( $this, 'cn_time_rejected' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_script_placement', __( 'Script placement', 'cookie-notice' ), array( $this, 'cn_script_placement' ), 'cookie_notice_options', 'cookie_notice_configuration' );
add_settings_field( 'cn_deactivation_delete', __( 'Deactivation', 'cookie-notice' ), array( $this, 'cn_deactivation_delete' ), 'cookie_notice_options', 'cookie_notice_configuration' );
// coronabar
add_settings_section( 'cookie_notice_coronabar', __( 'Corona Banner', 'cookie-notice' ), array( $this, 'cn_section_coronabar' ), 'cookie_notice_options' );
add_settings_field( 'cn_coronabar', __( 'Display', 'cookie-notice' ), array( $this, 'cn_coronabar' ), 'cookie_notice_options', 'cookie_notice_coronabar' );
add_settings_field( 'cn_coronabar_cases', __( 'Current cases', 'cookie-notice' ), array( $this, 'cn_coronabar_cases' ), 'cookie_notice_options', 'cookie_notice_coronabar' );
add_settings_field( 'cn_coronabar_texts', __( 'Text strings', 'cookie-notice' ), array( $this, 'cn_coronabar_texts' ), 'cookie_notice_options', 'cookie_notice_coronabar' );
// design
add_settings_section( 'cookie_notice_design', __( 'Design', 'cookie-notice' ), array( $this, 'cn_section_design' ), 'cookie_notice_options' );
add_settings_field( 'cn_position', __( 'Position', 'cookie-notice' ), array( $this, 'cn_position' ), 'cookie_notice_options', 'cookie_notice_design' );
add_settings_field( 'cn_hide_effect', __( 'Animation', 'cookie-notice' ), array( $this, 'cn_hide_effect' ), 'cookie_notice_options', 'cookie_notice_design' );
add_settings_field( 'cn_css_style', __( 'Button style', 'cookie-notice' ), array( $this, 'cn_css_style' ), 'cookie_notice_options', 'cookie_notice_design' );
add_settings_field( 'cn_css_class', __( 'Button class', 'cookie-notice' ), array( $this, 'cn_css_class' ), 'cookie_notice_options', 'cookie_notice_design' );
add_settings_field( 'cn_colors', __( 'Colors', 'cookie-notice' ), array( $this, 'cn_colors' ), 'cookie_notice_options', 'cookie_notice_design' );
}
/**
* Section callback: fix for WP < 3.3
*
* @return void
*/
public function cn_section_configuration() {}
public function cn_section_design() {}
public function cn_section_coronabar() {}
/**
* Display Corona Banner option.
*
* @return void
*/
public function cn_coronabar() {
echo '
<fieldset>
<label><input id="cn_coronabar" type="checkbox" name="cookie_notice_options[coronabar]" value="1" ' . checked( true, $this->options['general']['coronabar'], false ) . '/>' . __( 'Enable to display the Corona Banner.', 'cookie-notice' ) . '</label>
<p class="description">' . __( 'The Corona Banner displays data about Coronavirus pandemia and <strong>five steps recommended by the WHO (World Health Organization)</strong> to help flatten the Coronavirus curve.', 'cookie-notice' ) . '</p>
</fieldset>';
}
/**
* Display current cases option.
*
* @return void
*/
public function cn_coronabar_cases() {
echo '
<fieldset>
<label><input id="cn_coronabar_cases" type="checkbox" name="cookie_notice_options[coronabar_cases]" value="1" ' . checked( true, $this->options['general']['coronabar_cases'], false ) . '/>' . __( 'Display information about current cases.', 'cookie-notice' ) . '</label>
<p class="description">' . __( 'Provides up-to-date data about Coronavirus confirmed and recovered cases.', 'cookie-notice' ) . '</p>
</fieldset>';
}
/**
* Coronabar text strings.
*
* @return void
*/
public function cn_coronabar_texts() {
$descriptions = array(
'headline' => __( 'Headline message', 'cookie-notice' ),
'recovered' => __( 'Current cases text strings', 'cookie-notice' ),
'step_one_desc' => __( 'Recommendation One', 'cookie-notice' ),
'step_two_desc' => __( 'Recommendation Two', 'cookie-notice' ),
'step_three_desc' => __( 'Recommendation Three', 'cookie-notice' ),
'step_four_desc' => __( 'Recommendation Four', 'cookie-notice' ),
'step_five_desc' => __( 'Recommendation Five', 'cookie-notice' )
);
echo '
<fieldset>
<label><input id="cn_coronabar_texts" type="checkbox" name="cookie_notice_options[coronabar_texts]" value="1" ' . checked( true, $this->options['general']['coronabar_texts'], false ) . ' />' . __( 'Enable if you\'d like to adjust the Corona Banner text strings.', 'cookie-notice' ) . '</label>
<div id="cn_coronabar_text_strings"' . ( $this->options['general']['coronabar_texts'] === false ? ' style="display: none;"' : '' ) . '>';
foreach ( $this->defaults['general']['coronabar_text_strings'] as $key => $label ) {
echo '
<span class="cn_coronabar_text_string">
<input type="text" class="regular-text" name="cookie_notice_options[coronabar_text_strings][' . $key .']" value="' . esc_html( $this->options['general']['coronabar_text_strings'][$key] ) . '" />
</span>';
// display description
if ( array_key_exists( $key, $descriptions ) ) {
echo '
<p class="description">' . $descriptions[$key] . '</p>';
}
}
echo '
</div>
</fieldset>';
}
/**
* Cookie message option.
*
* @return void
*/
public function cn_message_text() {
echo '
<fieldset>
<div id="cn_message_text">
<textarea name="cookie_notice_options[message_text]" class="large-text" cols="50" rows="5">' . esc_textarea( $this->options['general']['message_text'] ) . '</textarea>
<p class="description">' . __( 'Enter the cookie notice message.', 'cookie-notice' ) . '</p>
</div>
</fieldset>';
}
/**
* Accept cookie label option.
*
* @return void
*/
public function cn_accept_text() {
echo '
<fieldset>
<div id="cn_accept_text">
<input type="text" class="regular-text" name="cookie_notice_options[accept_text]" value="' . esc_attr( $this->options['general']['accept_text'] ) . '" />
<p class="description">' . __( 'The text of the option to accept the notice and make it disappear.', 'cookie-notice' ) . '</p>
</div>
</fieldset>';
}
/**
* Enable/Disable third party non functional cookies option.
*
* @return void
*/
public function cn_refuse_opt() {
echo '
<fieldset>
<label><input id="cn_refuse_opt" type="checkbox" name="cookie_notice_options[refuse_opt]" value="1" ' . checked( true, $this->options['general']['refuse_opt'], false ) . ' />' . __( 'Enable to give to the user the possibility to refuse third party non functional cookies.', 'cookie-notice' ) . '</label>
<div id="cn_refuse_opt_container"' . ( $this->options['general']['refuse_opt'] === false ? ' style="display: none;"' : '' ) . '>
<div id="cn_refuse_text">
<input type="text" class="regular-text" name="cookie_notice_options[refuse_text]" value="' . esc_attr( $this->options['general']['refuse_text'] ) . '" />
<p class="description">' . __( 'The text of the button to refuse the consent.', 'cookie-notice' ) . '</p>
</div>
</div>
</fieldset>';
}
/**
* Non functional cookies code.
*
* @return void
*/
public function cn_refuse_code() {
$allowed_html = $this->get_allowed_html();
$active = ! empty( $this->options['general']['refuse_code'] ) && empty( $this->options['general']['refuse_code_head'] ) ? 'body' : 'head';
echo '
<fieldset>
<div id="cn_refuse_code">
<div id="cn_refuse_code_fields">
<h2 class="nav-tab-wrapper">
<a id="refuse_head-tab" class="nav-tab' . ( $active === 'head' ? ' nav-tab-active' : '' ) . '" href="#refuse_head">' . __( 'Head', 'cookie-notice' ) . '</a>
<a id="refuse_body-tab" class="nav-tab' . ( $active === 'body' ? ' nav-tab-active' : '' ) . '" href="#refuse_body">' . __( 'Body', 'cookie-notice' ) . '</a>
</h2>
<div id="refuse_head" class="refuse-code-tab' . ( $active === 'head' ? ' active' : '' ) . '">
<p class="description">' . __( 'The code to be used in your site header, before the closing head tag.', 'cookie-notice' ) . '</p>
<textarea name="cookie_notice_options[refuse_code_head]" class="large-text" cols="50" rows="8">' . html_entity_decode( trim( wp_kses( $this->options['general']['refuse_code_head'], $allowed_html ) ) ) . '</textarea>
</div>
<div id="refuse_body" class="refuse-code-tab' . ( $active === 'body' ? ' active' : '' ) . '">
<p class="description">' . __( 'The code to be used in your site footer, before the closing body tag.', 'cookie-notice' ) . '</p>
<textarea name="cookie_notice_options[refuse_code]" class="large-text" cols="50" rows="8">' . html_entity_decode( trim( wp_kses( $this->options['general']['refuse_code'], $allowed_html ) ) ) . '</textarea>
</div>
</div>
<p class="description">' . __( 'Enter non functional cookies Javascript code here (for e.g. Google Analitycs) to be used after the notice is accepted.', 'cookie-notice' ) . '</br>' . __( 'To get the user consent status use the <code>cn_cookies_accepted()</code> function.', 'cookie-notice' ) . '</p>
</div>
</fieldset>';
}
/**
* Revoke cookies option.
*
* @return void
*/
public function cn_revoke_opt() {
echo '
<fieldset>
<label><input id="cn_revoke_cookies" type="checkbox" name="cookie_notice_options[revoke_cookies]" value="1" ' . checked( true, $this->options['general']['revoke_cookies'], false ) . ' />' . __( 'Enable to give to the user the possibility to revoke their consent <i>(requires "Refuse consent" option enabled)</i>.', 'cookie-notice' ) . '</label>
<div id="cn_revoke_opt_container"' . ( $this->options['general']['revoke_cookies'] ? '' : ' style="display: none;"' ) . '>
<textarea name="cookie_notice_options[revoke_message_text]" class="large-text" cols="50" rows="2">' . esc_textarea( $this->options['general']['revoke_message_text'] ) . '</textarea>
<p class="description">' . __( 'Enter the revoke message.', 'cookie-notice' ) . '</p>
<input type="text" class="regular-text" name="cookie_notice_options[revoke_text]" value="' . esc_attr( $this->options['general']['revoke_text'] ) . '" />
<p class="description">' . __( 'The text of the button to revoke the consent.', 'cookie-notice' ) . '</p>';
foreach ( $this->revoke_opts as $value => $label ) {
echo '
<label><input id="cn_revoke_cookies-' . $value . '" type="radio" name="cookie_notice_options[revoke_cookies_opt]" value="' . $value . '" ' . checked( $value, $this->options['general']['revoke_cookies_opt'], false ) . ' />' . esc_html( $label ) . '</label>';
}
echo '
<p class="description">' . __( 'Select the method for displaying the revoke button - automatic (in the banner) or manual using <code>[cookies_revoke]</code> shortcode.', 'cookie-notice' ) . '</p>
</div>
</fieldset>';
}
/**
* Redirection on cookie accept.
*
* @return void
*/
public function cn_redirection() {
echo '
<fieldset>
<label><input id="cn_redirection" type="checkbox" name="cookie_notice_options[redirection]" value="1" ' . checked( true, $this->options['general']['redirection'], false ) . ' />' . __( 'Enable to reload the page after the notice is accepted.', 'cookie-notice' ) . '</label>
</fieldset>';
}
/**
* Privacy policy link option.
*
* @return void
*/
public function cn_see_more() {
$pages = get_pages(
array(
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 0,
'child_of' => 0,
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
)
);
echo '
<fieldset>
<label><input id="cn_see_more" type="checkbox" name="cookie_notice_options[see_more]" value="1" ' . checked( true, $this->options['general']['see_more'], false ) . ' />' . __( 'Enable privacy policy link.', 'cookie-notice' ) . '</label>
<div id="cn_see_more_opt"' . ($this->options['general']['see_more'] === false ? ' style="display: none;"' : '') . '>
<input type="text" class="regular-text" name="cookie_notice_options[see_more_opt][text]" value="' . esc_attr( $this->options['general']['see_more_opt']['text'] ) . '" />
<p class="description">' . __( 'The text of the privacy policy button.', 'cookie-notice' ) . '</p>
<div id="cn_see_more_opt_custom_link">';
foreach ( $this->links as $value => $label ) {
$value = esc_attr( $value );
echo '
<label><input id="cn_see_more_link-' . $value . '" type="radio" name="cookie_notice_options[see_more_opt][link_type]" value="' . $value . '" ' . checked( $value, $this->options['general']['see_more_opt']['link_type'], false ) . ' />' . esc_html( $label ) . '</label>';
}
echo '
</div>
<p class="description">' . __( 'Select where to redirect user for more information.', 'cookie-notice' ) . '</p>
<div id="cn_see_more_opt_page"' . ($this->options['general']['see_more_opt']['link_type'] === 'custom' ? ' style="display: none;"' : '') . '>
<select name="cookie_notice_options[see_more_opt][id]">
<option value="0" ' . selected( 0, $this->options['general']['see_more_opt']['id'], false ) . '>' . __( '-- select page --', 'cookie-notice' ) . '</option>';
if ( $pages ) {
foreach ( $pages as $page ) {
echo '
<option value="' . $page->ID . '" ' . selected( $page->ID, $this->options['general']['see_more_opt']['id'], false ) . '>' . esc_html( $page->post_title ) . '</option>';
}
}
echo '
</select>
<p class="description">' . __( 'Select from one of your site\'s pages.', 'cookie-notice' ) . '</p>';
global $wp_version;
if ( version_compare( $wp_version, '4.9.6', '>=' ) ) {
echo '
<label><input id="cn_see_more_opt_sync" type="checkbox" name="cookie_notice_options[see_more_opt][sync]" value="1" ' . checked( true, $this->options['general']['see_more_opt']['sync'], false ) . ' />' . __( 'Synchronize with WordPress Privacy Policy page.', 'cookie-notice' ) . '</label>';
}
echo '
</div>
<div id="cn_see_more_opt_link"' . ($this->options['general']['see_more_opt']['link_type'] === 'page' ? ' style="display: none;"' : '') . '>
<input type="text" class="regular-text" name="cookie_notice_options[see_more_opt][link]" value="' . esc_attr( $this->options['general']['see_more_opt']['link'] ) . '" />
<p class="description">' . __( 'Enter the full URL starting with http(s)://', 'cookie-notice' ) . '</p>
</div>
<div id="cn_see_more_link_target">';
foreach ( $this->link_targets as $target ) {
echo '
<label><input id="cn_see_more_link_target-' . $target . '" type="radio" name="cookie_notice_options[link_target]" value="' . $target . '" ' . checked( $target, $this->options['general']['link_target'], false ) . ' />' . $target . '</label>';