-
Notifications
You must be signed in to change notification settings - Fork 11
/
eucookielaw-wp.php
2101 lines (1800 loc) · 71.5 KB
/
eucookielaw-wp.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
/**
* EUCookieLaw: EUCookieLaw a complete solution to accomplish european law requirements about cookie consent
* @link https://github.com/diegolamonica/EUCookieLaw/
* @author Diego La Monica (diegolamonica) <diego.lamonica@gmail.com>
* @copyright 2015 Diego La Monica <http://diegolamonica.info>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
Class EUCookieLaw {
static $initialized = false;
const TEXTDOMAIN = 'EUCookieLaw';
const CUSTOMDOMAIN = 'EUCookieLawCustom';
const MENU_SLUG = 'EUCookieLaw';
const VERSION = '2.7.2';
const CSS = 'EUCookieLaw_css';
const CUSTOMCSS = 'EUCookieLaw_css_custom';
const JS = 'EUCookieLaw_js';
const WPJS = 'wpEUCookieLaw_js';
const OPT_TITLE = 'eucookie_law_title';
const OPT_MESSAGE = 'eucookie_law_description';
const OPT_AGREE = 'eucookie_law_agree';
const OPT_DISAGREE = 'eucookie_law_disagree';
const OPT_SCROLL_PX = 'eucookie_law_min_scroll';
const OPT_TITLE_TAG = 'eucookie_law_title_tag';
const OPT_3RDPDOMAINS = 'eucookie_law_3rdparty_domain';
const OPT_LOOKINSCRIPTS = 'eucookie_law_inscript';
const OPT_LOOKINTAGS = 'eucookie_law_lookintags';
const OPT_RELOAD = 'eucookie_law_reload';
const OPT_RAISE_LOAD_EVENT = 'eucookie_law_raise_load_event';
const OPT_ENABLED = 'eucookie_law_enabled';
const OPT_ENABLEDONLOGIN = 'eucookie_law_enabled_on_login';
const OPT_BOT_AS_HUMANS = 'eucookie_law_bot_as_humans';
const OPT_DEFAULT_LOOKINTAGS = 'script|iframe|img|embed|param';
const OPT_AGREEONSCROLL = 'eucookie_law_agree_on_scroll';
const OPT_AGREEONCLICK = 'eucookie_law_agree_on_click';
const OPT_FIXED_ON = 'eucookie_law_banner_fixed_on';
const OPT_BANNER_STYLE = 'eucookie_law_banner_style';
const OPT_REMEMBER_CHOICE = 'eucookie_law_remember_choice';
const OPT_LANGUAGES = 'eucookie_law_languages';
const OPT_COOKIE_EXPIRES = 'eucookie_law_banner_cookie_expires';
const OPT_WHITELIST_COOKIES = 'eucookie_law_whitelist_cookies';
const OPT_DEBUG = 'eucookie_law_debug';
const OPT_LOGFILE = 'eucookie_law_logfile';
const OPT_DEBUG_VERBOSITY = 'eucookie_law_debug_verbosity';
const OPT_ENGINE = 'eucookie_law_engine';
const OPT_DEFAULT_IFRAME_SRC = 'default_iframe_src';
const OPT_DEFAULT_SCRIPT_SRC = 'default_script_src';
const OPT_DEFAULT_IMAGE_SRC = 'default_image_src';
const OPT_UNAPPLY_ON_URL = 'eucookielaw_unapply_on_url';
const COOKIE_NAME = '__eucookielaw';
const ERR_MSG_CHECK_PERMS_OR_DIY = 'Check your permissions or put this data into the file <code>%s</code>:';
const ERR_MSG_FILE_UPDATED = 'File <code>%s</code> updated!';
const WPC_FILE_NOT_FOUND = 0; # Only for clean WP installations
const WPC_NOT_SAME_VERSION = 1;
const WPC_FILE_ORIGINAL = 2;
const WPC_FILE_THE_SAME = 3;
const CRON_SCHEDULE_HOOK = 'eucookielaw_regenerate_cache';
const CRON_TIMING = 'minutely';
const CRON_DURATION = 60;
private $PLUGIN_DIRECTORY;
private $showMergeButton = false;
public function __construct() {
self::$initialized = true;
$this->PLUGIN_DIRECTORY = dirname( __FILE__ );
add_action( 'init', array( $this, 'init' ), - 10 );
add_action( 'plugins_loaded', array( $this, 'loadTranslations' ) );
register_deactivation_hook(
EUCOOKIELAW_MAIN_FILE,
array($this, 'removeSchedule' )
);
register_activation_hook(
EUCOOKIELAW_MAIN_FILE,
array( $this, 'createSchedule' )
);
add_filter( 'cron_schedules', array($this, 'addMinutelySchedule') );
add_action( self::CRON_SCHEDULE_HOOK , array($this, 'cron'));
$this->createSchedule();
if ( is_admin() ) {
if ( ( isset( $_GET['write'] ) || count( $_POST ) > 0 ) && isset( $_GET['page'] ) && substr( $_GET['page'], 0, strlen( __CLASS__ ) ) == __CLASS__ ) {
add_action( 'admin_notices', array( $this, 'writeConfig' ) );
}
add_action( 'admin_notices', array( $this, 'notifyDifferences' ) );
add_action( 'admin_notices', array( $this, 'notifyDebug' ) );
require_once 'editor.php';
new EUCookieLawTinyMCE();
add_filter( 'admin_menu', array( $this, 'admin' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'adminScripts' ) );
} else {
add_action( 'wp_enqueue_scripts', array( $this, 'script' ) );
if ( get_option( self::OPT_ENABLEDONLOGIN, 'y' ) == 'y' && $this->isLoginPage() ) {
add_action( 'login_enqueue_scripts', array( $this, 'script' ) );
}
}
add_action('w3_pgcache_cleanup', array($this, 'cron'), 99); // Linked to w3tc cron event
add_action('w3tc_pgcache_flush', array($this, 'pgCacheFlush'));
add_action('wp_cache_gc', array($this, 'pubUpdateCacheDirectory'));
}
public function loadTranslations() {
load_plugin_textdomain( __CLASS__, false, basename( dirname( __FILE__ ) ) . '/languages/' );
load_plugin_textdomain( self::CUSTOMDOMAIN, false, 'EUCookieLawCustom/' );
}
function deactivatePlugin(){
wp_clear_scheduled_hook(self::CRON_SCHEDULE_HOOK);
/*
* TODO: I have to remove the contents from wp-config.php and the .htaccess file into the cache directory if present.
* Will do it in the version 3.0.0
*/
}
function addMinutelySchedule( $schedules ) {
if(!isset($schedules[self::CRON_TIMING])){
$schedules[self::CRON_TIMING] = array(
'interval' => self::CRON_DURATION, // 60 seconds
'display' => __( 'Every minute', self::TEXTDOMAIN )
);
}
return $schedules;
}
public function createSchedule(){
$timestamp = wp_next_scheduled( self::CRON_SCHEDULE_HOOK);
if( $timestamp == false ){
error_log("Scheduling event every minute");
if(!wp_schedule_event( time() + 60, self::CRON_TIMING, self::CRON_SCHEDULE_HOOK )){
error_log("Problem in event scheduling, trying on the next request");
}
}
}
public function removeSchedule(){
wp_clear_scheduled_hook(self::CRON_SCHEDULE_HOOK);
}
function cron(){
$this->updateCacheDirectory();
}
private function checkWPConfigFile( $wpConfigTemplate ) {
$wpConfigFile = ABSPATH . 'wp-config.php';
$response = self::WPC_FILE_THE_SAME;
if ( file_exists( WP_CONTENT_DIR . 'cache/' ) ) {
if ( ! file_exists( $wpConfigFile ) ) {
$response = self::WPC_FILE_NOT_FOUND;
} else {
$fileContent = @file_get_contents( $wpConfigFile );
if ( strpos( $fileContent, $wpConfigTemplate ) === false ) {
/*
* wp-config file does not have EUCookieLaw Cache settings
* Checking for recognizable data.
*/
$blockStartEnd = preg_quote( '--- EUCookieLaw ---', '#' );
if ( preg_match( '#' . $blockStartEnd . '(.*?)' . $blockStartEnd . '#ims', $fileContent, $items ) ) {
preg_match( '#Version:\s([0-9.]+)#', $items[1], $versionInfo );
if ( $versionInfo[1] !== self::VERSION ) {
$response = self::WPC_NOT_SAME_VERSION;
}
} else {
$response = self::WPC_FILE_ORIGINAL;
}
}
}
}
return $response;
}
private function getTemplateFile( $file, $apply = true ) {
$replacements = array(
'VERSION' => self::VERSION,
'DIR' => dirname( __FILE__ ),
'WP_CONTENT' => WP_CONTENT_DIR,
'CACHE_DIR' => WP_CONTENT_DIR . '/cache',
'W3TC_DIR' => WP_CONTENT_DIR . '/cache/page_enhanced',
'ZENC_DIR' => WP_CONTENT_DIR . '/cache/zencache',
'ABSPATH' => ABSPATH
);
if ( ! $template = @file_get_contents( dirname( __FILE__ ) . '/templates/' . $file ) ) {
error_log( "unable to read $file" );
return false;
} else {
if ( $apply ) {
foreach ( $replacements as $key => $value ) {
$template = str_replace( '%%' . $key . '%%', $value, $template );
}
}
return $template;
}
}
private function notifyMessage( $message, $type = 'updated', $code = '' ) {
?>
<div class="<?php echo $type ?>">
<p>
<?php echo $message; ?>
</p>
<?php
if ( ! empty( $code ) ) {
?>
<pre><?php echo htmlspecialchars( $code ); ?></pre>
<?php
}
?>
</div>
<?php
}
private function updateWPConfig() {
$advancedCacheTemplate = $this->getTemplateFile( 'wp-config.fragment.php' );
$contentBlockStartEnd = '# --- EUCookieLaw ---';
$addToWPConfig = array(
$contentBlockStartEnd,
"# Added by EUCookieLaw",
"# Version: " . self::VERSION,
$advancedCacheTemplate,
"# End additions by EUCookieLaw",
"# --- EUCookieLaw ---",
"" # Empty item to ensure the new line at the end
);
if(!file_exists( ABSPATH . 'wp-config.php' )){
$applyAtLine = '2';
$this->notifyMessage( sprintf(
__( self::ERR_MSG_CHECK_PERMS_OR_DIY, self::TEXTDOMAIN ),
'wp-config.php' ), 'error',
"# Insert at Line #$applyAtLine:\n" . implode( "\n", $addToWPConfig )
);
return false;
}
$config = file( ABSPATH . 'wp-config.php' );
$newWPConfig = array();
$ignore = false;
$nested = false;
# print_r($config);
$applyAtLine = 0;
foreach ( $config as $line => $row ) {
# We should put our fragment before the first require content
if ( ! $ignore && ! $nested && preg_match( '#^\s*require_once#', $row ) ) {
$newWPConfig[] = implode( "\n", $addToWPConfig );
$newWPConfig[] = $row;
$nested = true;
$applyAtLine = $line + 1;
} else {
if ( preg_match( '#' . preg_quote( $contentBlockStartEnd, '#' ) . '#', $row ) ) {
if ( $ignore ) {
# error_log("Ending Ignoring rows");
$ignore = false;
} else {
# error_log("Starting Ignoring rows");
$ignore = true;
}
} else {
if ( ! $ignore ) {
# error_log("writing original row");
$newWPConfig[] = $row;
} else {
# error_log("Ignoring rows");
}
}
}
}
if ( ! @file_put_contents( ABSPATH . 'wp-config.php', implode( "", $newWPConfig ) ) ) {
$this->notifyMessage( sprintf(
__( self::ERR_MSG_CHECK_PERMS_OR_DIY, self::TEXTDOMAIN ),
ABSPATH . 'wp-config.php' ), 'error',
"# Insert at Line #$applyAtLine:\n" . implode( "", $addToWPConfig )
);
} else {
$this->notifyMessage( sprintf(
__( self::ERR_MSG_FILE_UPDATED, self::TEXTDOMAIN ), ABSPATH . 'wp-config.php' )
);
}
}
private function updateHtaccess( $directory ) {
$template = $this->getTemplateFile( 'htaccess.fragment.txt' );
if ( $htaccess = @file_get_contents( $directory . '/.htaccess' ) ) {
if ( ( strpos( $htaccess, $template ) !== false ) || @file_put_contents( $directory . '/.htaccess', $htaccess . $template ) ) {
$this->notifyMessage( sprintf(
__( self::ERR_MSG_FILE_UPDATED, self::TEXTDOMAIN ),
$directory . '/.htaccess'
) );
$htaccess .= $template;
}
}
if ( strpos( $htaccess, $template ) === false ) {
if ( ! is_bool( $htaccess ) ) {
$template = $htaccess . $template;
}
$this->notifyMessage(
sprintf(
__( self::ERR_MSG_CHECK_PERMS_OR_DIY, self::TEXTDOMAIN ), $directory . '/.htaccess' ),
'error',
$template );
}
}
private function writeFileForCachePlugin( $directory ) {
$phpFile = $directory . '/EUCookieCache.php';
if(file_exists($phpFile.'.old')) @unlink($phpFile.'.old');
$template = $this->getTemplateFile( 'EUCookieCache.php' );
if ( ! @file_put_contents( $phpFile, $template ) ) {
$this->notifyMessage(
sprintf(
__( 'Unable to write the file <code>%s</code>', self::TEXTDOMAIN ) . '<br />' .
__( self::ERR_MSG_CHECK_PERMS_OR_DIY, self::TEXTDOMAIN ),
$phpFile
),
'error', $template );
} else {
$this->notifyMessage( sprintf(
__( self::ERR_MSG_FILE_UPDATED, self::TEXTDOMAIN ),
$phpFile
) );
}
# Updating .htaccess file
$this->updateHtaccess( $directory );
}
public function pgCacheFlush(){
add_action('w3_redirect', array($this, 'pubUpdateCacheDirectory' ));
}
public function pubUpdateCacheDirectory(){
$this->updateCacheDirectory();
}
private function updateCacheDirectory() {
# Needed only for W3TC
if ( file_exists( WP_CONTENT_DIR . '/cache/page_enhanced' ) ) {
$this->writeFileForCachePlugin( WP_CONTENT_DIR . '/cache/page_enhanced' );
}
/*
* Not needed for ZenCache and WP Super Cache
*/
}
private function hasCache() {
return ( file_exists( WP_CONTENT_DIR . '/cache' ) && is_dir( WP_CONTENT_DIR . '/cache' ) && defined('WP_CACHE') && WP_CACHE );
}
private function updateIniFile() {
if ( $this->hasCache() ) {
$domains = get_option( self::OPT_3RDPDOMAINS );
$iniFile = array(
self::OPT_3RDPDOMAINS => preg_replace( "#\r?\n#", ";", $domains ),
#self::OPT_AGREE => get_option( self::OPT_AGREE ),
self::OPT_COOKIE_EXPIRES => get_option( self::OPT_COOKIE_EXPIRES ),
self::OPT_DEBUG => ( get_option( self::OPT_DEBUG ) !== 'n' ) ? 'y' : 'n',
self::OPT_DEBUG_VERBOSITY => ( get_option( self::OPT_DEBUG_VERBOSITY, '99')),
self::OPT_LOGFILE => ( get_option( self::OPT_DEBUG ) === 'file' ) ? WP_PLUGIN_DIR . '/' . self::CUSTOMDOMAIN . '/log.txt' : '',
#self::OPT_DISAGREE => get_option( self::OPT_DISAGREE ),
self::OPT_ENABLED => get_option( self::OPT_ENABLED ),
self::OPT_FIXED_ON => get_option( self::OPT_FIXED_ON ),
self::OPT_LOOKINSCRIPTS => get_option( self::OPT_LOOKINSCRIPTS ),
self::OPT_LOOKINTAGS => get_option( self::OPT_LOOKINTAGS ),
#self::OPT_MESSAGE => get_option( self::OPT_MESSAGE ),
#self::OPT_TITLE => get_option( self::OPT_TITLE ),
self::OPT_TITLE_TAG => get_option( self::OPT_TITLE_TAG ),
self::OPT_WHITELIST_COOKIES => get_option( self::OPT_WHITELIST_COOKIES ),
self::OPT_BOT_AS_HUMANS => get_option( self::OPT_BOT_AS_HUMANS ),
self::OPT_BANNER_STYLE => get_option( self::OPT_BANNER_STYLE, '' ),
self::OPT_ENGINE => get_option( self::OPT_ENGINE, 'regexp' ),
self::OPT_DEFAULT_IFRAME_SRC => get_option( self::OPT_DEFAULT_IFRAME_SRC, 'about:blank' ),
self::OPT_DEFAULT_SCRIPT_SRC => get_option( self::OPT_DEFAULT_SCRIPT_SRC, 'about:blank' ),
self::OPT_DEFAULT_IMAGE_SRC => get_option( self::OPT_DEFAULT_IMAGE_SRC, 'about:blank' ),
self::OPT_UNAPPLY_ON_URL => get_option( self::OPT_UNAPPLY_ON_URL, ''),
self::OPT_LANGUAGES => json_encode( get_option(self::OPT_LANGUAGES, false)),
);
$file = WP_CONTENT_DIR . '/cache/eucookielaw.ini';
$config = '';
foreach ( $iniFile as $key => $value ) {
$config .= $key . '="' . str_replace( '"', '""', $value ) . "\"\n";
}
if ( @file_put_contents( $file, $config ) ) {
$this->notifyMessage( sprintf(
__( self::ERR_MSG_FILE_UPDATED, self::TEXTDOMAIN ),
$file )
);
} else {
$this->notifyMessage(
sprintf(
__( 'Error writing configuration file <code>%s</code>!', self::TEXTDOMAIN ) . '<br />' .
__( self::ERR_MSG_CHECK_PERMS_OR_DIY, self::TEXTDOMAIN ),
WP_CONTENT_DIR . '/cache/eucookielaw.ini'
),
'error', $config
);
}
}
}
public function writeConfig() {
$this->updateOptions();
if ( $this->hasCache() ) {
$this->updateWPConfig();
$this->updateCacheDirectory();
$this->updateIniFile();
}
}
public function notifyDebug() {
if ( get_option( self::OPT_DEBUG, 'n' ) !== 'n' ) {
$this->notifyMessage( __( "Warning: EUCookieLaw debug is enabled. Disable it once you've done your tests" ), "updated alert" );
}
}
public function notifyDifferences() {
$this->showMergeButton = false;
if ( in_array(
$this->checkWPConfigFile( $this->getTemplateFile( 'wp-config.fragment.php' ) ),
array( self::WPC_NOT_SAME_VERSION, self::WPC_FILE_ORIGINAL )
)
) {
$this->showMergeButton = true;
$this->notifyMessage(
sprintf(
__( "EUCookieLaw would not work with your cache until you will not <a href='%s'>go to the settings page</a> and execute the <strong>%s</strong> action (in the sidebar).", self::TEXTDOMAIN ),
admin_url( 'admin.php?page=' . __CLASS__ . '-messages' ),
__( 'Merge with cache plugin', self::TEXTDOMAIN )
), 'error'
);
}
}
private function isLoginPage() {
return in_array(
$GLOBALS['pagenow'],
array( 'wp-login.php', 'wp-register.php' )
);
}
public function init() {
$enabled = get_option( self::OPT_ENABLED, 'y' );
if ( $this->isLoginPage() && get_option( self::OPT_ENABLEDONLOGIN, 'y' ) == 'n' ) {
$enabled = 'n';
}
if ( $enabled == 'n' || is_admin() ) {
if ( ! defined( 'EUCOOKIELAW_DISABLED' ) ) {
define( 'EUCOOKIELAW_DISABLED', true );
}
} else {
define( 'EUCOOKIELAW_FORCE_AS_CACHE', true );
require $this->PLUGIN_DIRECTORY . '/eucookielaw-cache.php';
}
add_shortcode( 'EUCookieLawReconsider', array( $this, 'reconsider' ) );
add_filter( 'the_content', array( $this, 'block' ), - 1 );
}
public function reconsider( $atts ) {
$label = '';
extract( shortcode_atts( array(
'label' => 'Reconsider'
), $atts ) );
$url = preg_replace( '#(\?|&)__eucookielaw=([^&]+)(&?(.*))#', '$1$4', $_SERVER['REQUEST_URI'] );
$url = preg_replace( '#(\?|&)$#', '', $url );
$url .= ( preg_match( '#\?#', $url ) ? '&' : '?' ) . '__eucookielaw=reconsider';
return '<a class="btn btn-warning eucookielaw-reconsider-button" rel="nofollow" href="' . $url . '" onclick="(new EUCookieLaw()).reconsider(); return false;">' . __( $label, self::CUSTOMDOMAIN ) . '</a>';
}
public function block( $content ) {
$content = preg_replace( '#(\r?\n)?\[EUCookieLawBlock\](\r?\n)?#', '<!-- EUCookieLaw:start -->', $content );
$content = preg_replace( '#(\r?\n)?\[/EUCookieLawBlock\](\r?\n)?#', '<!-- EUCookieLaw:end -->', $content );
return $content;
}
public function adminScripts() {
wp_register_script( __CLASS__, plugin_dir_url( __FILE__ ) . 'EUCookieLaw-admin.js', array( 'jquery' ), self::VERSION);
wp_enqueue_script( __CLASS__ );
}
public function ignoredURL(){
$enabled = get_option( self::OPT_ENABLED, 'n' );
if($enabled == 'y') {
$urls = get_option(self::OPT_UNAPPLY_ON_URL, '');
if($urls !== ''){
$ru = $_SERVER['REQUEST_URI'];
$url = explode("\n", $urls);
foreach($url as $u){
$u = explode("*", $u);
foreach($u as $index => $value)
$u[$index] = preg_quote($value, '/');
$u = implode(".*", $u);
if( preg_match("/^" . $u . "$/", $ru)) return true;
}
};
}
return false;
}
public function script() {
$enabled = get_option( self::OPT_ENABLED, 'n' );
$hasEnabled = get_option( self::OPT_ENABLED, false );
$hasTitle = get_option( self::OPT_TITLE, false );
if( $this->ignoredURL() ){
$enabled = 'n';
}else{
if ( ! $hasEnabled && $hasTitle ) {
$enabled = 'y';
}
}
if ( $enabled == 'y' ) {
wp_register_script( self::JS, plugins_url( '/EUCookieLaw.js', __FILE__ ), array(), self::VERSION, false );
wp_register_script( self::WPJS, plugins_url( '/wpEUCookieLaw.js', __FILE__ ), array( self::JS ), self::VERSION, false );
wp_register_style( self::CSS, plugins_url( '/eucookielaw.css', __FILE__ ), array(), self::VERSION, 'screen' );
$customCSSURL = WP_PLUGIN_URL . '/' . self::CUSTOMDOMAIN . '/eucookielaw.css';
if ( file_exists( WP_PLUGIN_DIR . '/' . self::CUSTOMDOMAIN . '/eucookielaw.css' ) ) {
# error_log( "Custom CSS '$customCSSURL' correctly attacched to the page!" );
wp_register_style( self::CUSTOMCSS, $customCSSURL, array( self::CSS ), self::VERSION, 'screen' );
} else {
if ( get_option( self::OPT_DEBUG, 'n' ) == 'y' ) {
# error_log( "Custom CSS '$customCSSURL' does not exists or not reachable!" );
}
}
$bannerTitle = get_option( self::OPT_TITLE, 'Banner title' );
$bannerMessage = get_option( self::OPT_MESSAGE, 'Banner message' );
$bannerAgree = get_option( self::OPT_AGREE, 'I agree' );
$bannerDisagree = get_option( self::OPT_DISAGREE, 'I disagree' );
$titleTag = get_option( self::OPT_TITLE_TAG, 'h1' );
$agreeOnScroll = get_option( self::OPT_AGREEONSCROLL, 'n' );
$agreeOnClick = get_option( self::OPT_AGREEONCLICK, 'n' );
$fixedOn = get_option( self::OPT_FIXED_ON, 'top' );
$cookieDuration = get_option( self::OPT_COOKIE_EXPIRES, '365' );
$debug = get_option( self::OPT_DEBUG, 'n' );
$reload = get_option( self::OPT_RELOAD, 'y' );
$raiseLoadEvent = get_option( self::OPT_RAISE_LOAD_EVENT, 'y');
$rememberChoice = get_option( self::OPT_REMEMBER_CHOICE, 'y' );
$whitelist = get_option( self::OPT_WHITELIST_COOKIES, array() );
$style = get_option( self::OPT_BANNER_STYLE, '' );
$minScroll = get_option( self::OPT_SCROLL_PX, '100' );
// Localize the script with new data
$languages = get_option( self::OPT_LANGUAGES, false);
if(!$languages){
$languages = array(
'Default' => array(
'title' => $bannerTitle,
'message' => $bannerMessage,
'agreeLabel' => $bannerAgree,
'disagreeLabel' => $bannerDisagree
)
);
}
error_log($debug);
$configuration = array(
'showBanner' => true,
'raiseLoadEvent'=> ( $raiseLoadEvent == 'y'),
'reload' => ( $reload == 'y' ),
'debug' => ( $debug == 'y' ),
#'bannerTitle' => htmlspecialchars( __( $bannerTitle, self::CUSTOMDOMAIN ) ),
#'message' => htmlspecialchars( __( $bannerMessage, self::CUSTOMDOMAIN ) ),
#'agreeLabel' => htmlspecialchars( __( $bannerAgree, self::CUSTOMDOMAIN ) ),
#'disagreeLabel' => htmlspecialchars( __( $bannerDisagree, self::CUSTOMDOMAIN ) ),
'tag' => $titleTag,
'agreeOnScroll' => ( $agreeOnScroll == 'y' ),
'agreeOnClick' => ( $agreeOnClick == 'y' ),
'fixOn' => $fixedOn,
'duration' => $cookieDuration,
'remember' => ( $rememberChoice == 'y' ),
'cookieList' => $whitelist,
'classes' => $style,
'minScroll' => $minScroll,
// 'id' => 'eucookielaw-in-html',
'languages' => $languages,
);
wp_localize_script( self::JS, 'euCookieLawConfig', $configuration );
wp_enqueue_style( self::CSS );
if ( file_exists( WP_PLUGIN_DIR . '/' . self::CUSTOMDOMAIN . '/eucookielaw.css' ) ) {
wp_enqueue_style( self::CUSTOMCSS );
}
wp_enqueue_script( self::WPJS );
}
}
public function admin() {
add_menu_page(
"EU Cookie Law", "EU Cookie Law",
'activate_plugins',
self::MENU_SLUG,
array( $this, 'about' ) );
add_submenu_page( self::MENU_SLUG, __("All you need to know about EUCookieLaw", self::TEXTDOMAIN) , __( "About", self::TEXTDOMAIN ), "activate_plugins", self::MENU_SLUG, array(
$this,
'about'
) );
add_submenu_page( self::MENU_SLUG, __( "EUCookieLaw Settings", self::TEXTDOMAIN ), __( "Settings", self::TEXTDOMAIN ), "activate_plugins", self::MENU_SLUG . '-settings', array(
$this,
'settings'
) );
add_submenu_page( self::MENU_SLUG, __( "EUCookieLaw Tools", self::TEXTDOMAIN ), __( "Tools", self::TEXTDOMAIN ), "activate_plugins", self::MENU_SLUG . '-tools', array(
$this,
'tools'
) );
}
public function about() {
$screen = WP_Screen::get();
add_meta_box(
'eucookielaw-about' . $screen->id,
__( 'About EUCookieLaw', self::TEXTDOMAIN ),
array( $this, 'aboutPlugin' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-css' . $screen->id,
__( 'CSS Cookie Banner Customization', self::TEXTDOMAIN ),
array( $this, 'customizeAspect' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-donation' . $screen->id,
__( 'Donation', self::TEXTDOMAIN ),
array( $this, 'donations' ),
$screen, 'side', 'high'
);
$this->buildScreen( $screen );
}
private function buildScreen( $screen ) {
add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2 ) );
?>
<div class="wrap">
<h2>EUCookieLaw</h2>
<div id="poststuff">
<form name="post" method="post" novalidate="novalidate">
<div id="post-body"
class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes( $screen, 'side', $screen ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes( $screen, 'normal', $screen ); ?>
</div>
</div>
</form>
</div>
</div>
<?php
}
public function aboutPlugin() {
?>
<p>
EUROPA websites must follow the Commission's guidelines on <a
href="http://ec.europa.eu/ipg/basics/legal/data_protection/index_en.htm">privacy and data
protection</a>
and inform users that cookies are not being used to gather information unnecessarily.
</p>
<p>
The <a href="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=CELEX:32002L0058:EN:HTML">ePrivacy
directive</a> –
more specifically Article 5(3) – requires prior informed consent for storage for access to information
stored on a user's
terminal equipment. In other words, you must ask users if they agree to most cookies and similar
technologies (e.g. web beacons,
Flash cookies, etc.) before the site starts to use them.
</p>
<p>
For consent to be valid, it must be informed, specific, freely given and must constitute a real
indication of the individual's wishes.
</p>
<p>
In this context this plugin lives. On the client side, it alters the default document.cookie behavior to
disallow cookies to be written, until the user accept the agreement. On the server side it will block
server generated cookies until the client will accept the agreement.
</p>
<?php
}
public function customizeAspect() {
?>
<p>
<?php _e("The structure of generated banner is the following:", self::TEXTDOMAIN); ?>
</p>
<div class="highlight highlight-html"><pre>
<<span class="pl-ent">div</span> <span class="pl-e">class</span>=<span class="pl-s"><span
class="pl-pds">"</span>eucookielaw-banner<span class="pl-pds">"</span></span> <span
class="pl-e">id</span>=<span class="pl-s"><span class="pl-pds">"</span>eucookielaw-135<span
class="pl-pds">"</span></span>>
<<span class="pl-ent">div</span> <span class="pl-e">class</span>=<span class="pl-s"><span
class="pl-pds">"</span>well<span class="pl-pds">"</span></span>>
<<span class="pl-ent">h1</span> <span class="pl-e">class</span>=<span class="pl-s"><span
class="pl-pds">"</span>banner-title<span class="pl-pds">"</span></span>>The banner title</<span
class="pl-ent">h1</span>>
<<span class="pl-ent">div</span> <span class="pl-e">class</span>=<span class="pl-s"><span
class="pl-pds">"</span>banner-message<span class="pl-pds">"</span></span>>The banner message</<span
class="pl-ent">div</span>>
<<span class="pl-ent">ul</span> <span class="pl-e">id</span>=<span class="pl-s"><span
class="pl-pds">"</span>eucookielaw-language-switcher<span class="pl-pds">"</span></span>>
<<span class="pl-ent">li</span> <span class="pl-e">onclick</span>=<span class="pl-s"><span
class="pl-pds">"</span>(new EUCookieLaw()).switchLanguage('Italiano');<span
class="pl-pds">"</span></span>>Italiano</<span class="pl-ent">li</span>>
<<span class="pl-ent">li</span> <span class="pl-e">onclick</span>=<span class="pl-s"><span
class="pl-pds">"</span>(new EUCookieLaw()).switchLanguage('English');<span
class="pl-pds">"</span></span>>English</<span class="pl-ent">li</span>>
</<span class="pl-ent">ul</span>>
<<span class="pl-ent">p</span> <span class="pl-e">class</span>=<span class="pl-s"><span
class="pl-pds">"</span>banner-agreement-buttons text-right<span
class="pl-pds">"</span></span>>
<<span class="pl-ent">a</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>disagree-button btn btn-danger<span
class="pl-pds">"</span></span>
<span class="pl-e">onclick</span>=<span class="pl-s"><span
class="pl-pds">"</span>(new EUCookieLaw()).reject();<span class="pl-pds">"</span></span>>Disagree</<span
class="pl-ent">a</span>>
<<span class="pl-ent">a</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>agree-button btn btn-primary<span
class="pl-pds">"</span></span>
<span class="pl-e">onclick</span>=<span class="pl-s"><span
class="pl-pds">"</span>(new EUCookieLaw()).enableCookies();<span
class="pl-pds">"</span></span>>Agree</<span class="pl-ent">a</span>>
</<span class="pl-ent">p</span>>
</<span class="pl-ent">div</span>>
</<span class="pl-ent">div</span>></pre>
</div>
<ul class="help">
<li>
<code>.eucookielaw-banner</code>
<?php
_e(
"is the banner container it will have a random <code>id</code> attribute name that starts always with <code>eucookielaw-</code> and then followed by a number between <code>0</code> and <code>200</code>.",
self::TEXTDOMAIN
);
?>
</li>
<li>
<code>.well</code>
<?php _e("is the inner container", self::TEXTDOMAIN); ?>
</li>
<li>
<code>h1.banner-title</code>
<?php _e("is the banner title", self::TEXTDOMAIN); ?>
</li>
<li>
<code>div.banner-message</code>
<?php _e("is the banner html message", self::TEXTDOMAIN); ?>
</li>
<li>
<code>ul#eucookielaw-language-switcher</code>
<?php _e("is the box where the language buttons are located, it is visible only if more than one language is configured", self::TEXTDOMAIN); ?>
</li>
<li>
<code>p.banner-agreement-buttons.text-right</code>
<?php _e("is the buttons container for the agree/disagree buttons", self::TEXTDOMAIN); ?>
</li>
<li>
<code>a.disagree-button</code>
<?php _e("is the disagree button it implements the CSS classes <code>btn</code> and <code>btn-danger</code>", self::TEXTDOMAIN); ?>
</li>
<li>
<code>a.disagree-button</code>
<?php _e("is the agree button it implements the CSS classes <code>btn</code> and <code>btn-primary</code>" , self::TEXTDOMAIN); ?>
</li>
</ul>
<p>
<?php _e("You can make your own CSS to build a custom aspect for the banner. However, if you prefer, you can start from the bundled CSS.", self::TEXTDOMAIN); ?>
</p>
<?php
}
public function donations() {
?>
<p>
<?php echo sprintf(
__( "If you find this plugin useful, and since I've noticed that nobody did this script (as is) before of me, " .
"I'd like to receive <a href=\"%s\">a donation</a> as thankful for the time You've earned for you, your " .
"family and your hobbies! :)", self::TEXTDOMAIN ),
"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=me%40diegolamonica%2einfo&lc=IT&item_name=EU%20Cookie%20Law&no_note=0¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest" ); ?>
</p>
<p>
<?php _e( 'You can find further informations about this plugin on <a href="https://github.com/diegolamonica/EUCookieLaw/">GitHub</a>', self::TEXTDOMAIN ); ?>
</p>
<?php
$this->displayFBLike();
}
public function settings() {
$screen = WP_Screen::get();
add_meta_box(
'eucookielaw-banner' . $screen->id,
__( 'Banner', self::TEXTDOMAIN ),
array( $this, 'bannerMetabox' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-message' . $screen->id,
__( 'Messages', self::TEXTDOMAIN ),
array( $this, 'messagesMetabox' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-behavior' . $screen->id,
__( 'Behavior', self::TEXTDOMAIN ),
array( $this, 'behaviorMetabox' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-message-support' . $screen->id,
__( 'Support', self::TEXTDOMAIN ),
array( $this, 'outputMessagesSupport' ),
$screen, 'side', 'high'
);
add_meta_box(
'eucookielaw-debug'.$screen->id,
__( 'Debug', self::TEXTDOMAIN),
array($this, 'debugMetabox'),
$screen, 'side', 'high'
);
add_meta_box(
'eucookielaw-donation' . $screen->id,
__( 'Donation', self::TEXTDOMAIN ),
array( $this, 'donations' ),
$screen, 'side', 'high'
);
$this->buildScreen( $screen );
}
public function tools() {
$screen = WP_Screen::get();
add_meta_box(
'eucookielaw-tools' . $screen->id,
__( 'Tools', self::TEXTDOMAIN ),
array( $this, 'toolsMetabox' ),
$screen, 'normal', 'high'
);
add_meta_box(
'eucookielaw-donation' . $screen->id,
__( 'Donation', self::TEXTDOMAIN ),
array( $this, 'donations' ),
$screen, 'side', 'high'
);
$this->buildScreen( $screen );
}
private function generateCustomDirectory( $mode ) {
if ( $mode == 'default' ) {
$mode = 'update';
}
$customDirectory = WP_PLUGIN_DIR . '/' . self::CUSTOMDOMAIN;