forked from emilio/CookieTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cookietool.php
438 lines (386 loc) · 17.1 KB
/
wp-cookietool.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
<?php
/*
Plugin Name: WP Cookie Tool
Plugin URI: http://cmorales.es/descargas/wordpress-cookie-tool/
Description: Easy way to follow the EU Cookie directive, especially for Spain.
Version: 1.1
Author: Carlos Morales & Emilio Cobos
Author URI: http://cmorales.es/
*/
/**
* Copyright (c) 2013 Carlos Morales & Emilio Cobos. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
/*
Javascript work based on the work by Emilio Cobos: https://github.com/ecoal95/CookieTool
*/
define( 'WP_COOKIE_TOOL_PATH', plugin_dir_path(__FILE__) ); //Includes trailing slash!!!
class wp_cookie_tool {
protected $textdomain = "wp-cookie-tool";
protected $prefix = "wp_ct";
protected $longname = "wp_cookie_tool";
/**
* Returns the options array
*
* @since WP Cookie Tool 1.0
*/
function get_options() {
$saved = (array) get_option($this->longname . '_options');
$defaults = array(
'load_css' => 'on',
'link' => 'http://example.com/cookies',
'link_name' => 'Cookie Policy',
'panel_class' => 'cookietool-message-top',
'message' => __('The message you are going to show', $this->textdomain),
'agreetext' => __('I agree', $this->textdomain),
'declinetext' => __('I disagree', $this->textdomain),
'google_analytics' => false
);
$defaults = apply_filters($this->prefix. '_default_options', $defaults);
$options = wp_parse_args($saved, $defaults);
$options = array_intersect_key($options, $defaults);
return $options;
}
}
class wp_cookie_tool_options extends wp_cookie_tool {
function __construct() {
add_action('admin_init', array(&$this, 'init'));
add_action('admin_menu', array(&$this, 'add_menu_page'));
}
function add_menu_page() {
$options_page = add_options_page(
__('WP Cookie Tool', $this->textdomain), // Name of page (html title)
__('WP Cookie Tool', $this->textdomain), // Label in menu
'manage_options', // Capability required
$this->longname . '_options_page', // Menu slug, used to uniquely identify the page
array(&$this, 'render_page')
);
}
function init() {
register_setting(
$this->longname . '_options', //Options name
$this->longname . '_options', //DB entry
array($this, 'validate') //Validate callback
);
// Register our settings field group
add_settings_section(
$this->longname . '_general', // Unique identifier for the settings section
__('General', $this->textdomain), // Section title
'__return_false', // Section callback (we don't want anything)
$this->longname . '_options_page' // Menu slug, used to uniquely identify the page; see add_menu_page()
);
add_settings_section(
$this->longname . '_scripts', // Unique identifier for the settings section
__('Scripts', $this->textdomain), // Section title
array(&$this, 'render_scripts_section'), // Section callback
$this->longname . '_options_page' // Menu slug, used to uniquely identify the page; see add_menu_page()
);
//General fields
add_settings_field(
$this->longname.'_load_css',
__('Load css?', $this->textdomain),
array($this, 'render_load_css_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_link',
__('Cookie policy page link', $this->textdomain),
array($this, 'render_link_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_link_name',
__('Cookie policy page title', $this->textdomain),
array($this, 'render_link_name_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_panel_class',
__('Panel class', $this->textdomain),
array($this, 'render_panel_class_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_message',
__('Message', $this->textdomain),
array($this, 'render_message_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_agreetext',
__('Agree text', $this->textdomain),
array($this, 'render_agreetext_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
add_settings_field(
$this->longname.'_declinetext',
__('Decline text', $this->textdomain),
array($this, 'render_declinetext_field'),
$this->longname . '_options_page',
$this->longname . '_general'
);
/*** Scripts section ***/
add_settings_field(
$this->longname.'_analytics_code',
__('Google Analytics Code', $this->textdomain),
array($this, 'render_google_analytics_code'),
$this->longname . '_options_page',
$this->longname . '_scripts'
);
}
/**
* Renders the options page.
*
* @since WP Cookie Tool 1.0
*/
function render_scripts_section() {
?>
<p><?php _e('Some code, such as Google Analytics, needs to be modified to prevent it to store cookies before the user consent.', $this->textdomain) ?> <?php _e('The plugin can insert a modified version for you, or you can choose to do it for yourself.', $this->textdomain) ?></p>
<p><?php _e('Currently, Google Analytics and AdSense are supported.', $this->textdomain) ?></p>
<p><?php _e('For Google Analytics, you have to avoid loading the script, leaving just this:', $this->textdomain) ?></p>
<pre>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
</script>
</pre>
<p><?php _e('You can do it by yourself, or insert your code below and the plugin will do it for you.', $this->textdomain) ?></p>
<p><?php _e('Regarding Google AdSense, you need to use the asynchronous version.', $this->textdomain) ?></p>
<?php
}
/**
* Renders the options page.
*
* @since WP Cookie Tool 1.0
*/
function render_page() {
?>
<div class="wrap">
<h2><?php _e('WP Cookie Tool Settings', $this->textdomain) ?></h2>
<form method="post" action="options.php">
<?php
settings_fields($this->longname . '_options');
do_settings_sections($this->longname . '_options_page');
submit_button();
?>
</form>
</div>
<?php
}
/**
* Renders the load css checkbox
*
* @since WP Cookie Tool 1.0
*/
function render_load_css_field() {
$options = $this->get_options();
?>
<label for="load_css" class="description">
<input type="checkbox" name="<?php echo $this->longname?>_options[load_css]" id="load_css" <?php checked('on', $options['load_css']); ?> />
<?php _e('Uncheck if you don\'t want to include the default styles.', $this->textdomain); ?>
</label>
<?php
}
/**
* Renders the input field for the link
*
* @since WP Cookie Tool 1.0
*/
function render_link_field() {
$options = $this->get_options();
?>
<input type="url" name="<?php echo $this->longname?>_options[link]" id="link" value="<?php echo esc_attr($options['link']); ?>" />
<p class="description"><?php _e('Place here your privacy/cookie policy page URL.', $this->textdomain); ?></p>
<p class="description"><?php _e('You will need to place the [wp_ct-cookie-div] shortcode in that page.', $this->textdomain); ?></p>
<?php
}
/**
* Renders the input field for the link name
*
* @since WP Cookie Tool 1.0
*/
function render_link_name_field() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[link_name]" id="link" value="<?php echo esc_attr($options['link_name']); ?>" />
<p class="description"><?php _e('Place here your privacy/cookie policy title', $this->textdomain); ?></p>
<?php
}
/**
* Renders the input field for the panel class
*
* @since WP Cookie Tool 1.0
*/
function render_panel_class_field() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[panel_class]" id="panel_class" value="<?php echo esc_attr($options['panel_class']); ?>" />
<p class="description"><?php _e('Panel\'s CSS class name.', $this->textdomain); ?></p>
<?php
}
/**
* Renders the input field for the message
*
* @since WP Cookie Tool 1.0
*/
function render_message_field() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[message]" id="message" value="<?php echo esc_attr($options['message']); ?>" />
<p class="description"><?php _e('This is the text you show to your visitors.', $this->textdomain); ?></p>
<?php
}
/**
* Renders the input field for the agreetext
*
* @since WP Cookie Tool 1.0
*/
function render_agreetext_field() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[agreetext]" id="agreetext" value="<?php echo esc_attr($options['agreetext']); ?>" />
<p class="description"><?php _e('Text to agree.', $this->textdomain); ?></p>
<?php
}
/**
* Renders the input field for the declinetext
*
* @since WP Cookie Tool 1.0
*/
function render_declinetext_field() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[declinetext]" id="declinetext" value="<?php echo esc_attr($options['declinetext']); ?>" />
<p class="description"><?php _e('Text to decline.', $this->textdomain); ?></p>
<?php
}
/**
* Renders the Google Analytics input setting field.
*/
function render_google_analytics_code() {
$options = $this->get_options();
?>
<input type="text" name="<?php echo $this->longname?>_options[google_analytics]" id="google-analytics" value="<?php echo esc_attr($options['google_analytics']); ?>" />
<label class="description" for="google-analytics"><?php _e('Your account code', $this->textdomain)?></label>
<p class="description"><?php _e('Your account code looks like this: UA-12345678-9', $this->textdomain) ?></p>
<p class="description"><?php _e('Leave blank if you don\'t use Google Analytics or if you will insert the code on your own.', $this->textdomain) ?></p>
<?php
}
function validate($input) {
$output = array();
if (isset($input['load_css'])) {
$output['load_css'] = 'on';
} else {
$output['load_css'] = 'off';
}
if (isset($input['link'])) {
$output['link'] = esc_url_raw($input['link']);
}
if (isset($input['link_name'])) {
$output['link_name'] = esc_html($input['link_name']);
}
if (isset($input['panel_class'])) {
$output['panel_class'] = esc_attr($input['panel_class']);
}
if (isset($input['message'])) {
$output['message'] = esc_html($input['message']);
}
if (isset($input['agreetext'])) {
$output['agreetext'] = esc_html($input['agreetext']);
}
if (isset($input['declinetext'])) {
$output['declinetext'] = esc_html($input['declinetext']);
}
if (isset($input['google_analytics'])) {
$output['google_analytics'] = esc_html($input['google_analytics']);
}
return apply_filters($this->longname . '_options_validate', $output, $input);
}
}
class wp_cookie_tool_init extends wp_cookie_tool {
function render_cookie_div() {
return '<div id="cookietool-settings"></div>';
}
function __construct() {
add_action('wp_enqueue_scripts', array(&$this, 'add_styles'));
add_action('wp_footer', array(&$this, 'config_script'), 100);
load_plugin_textdomain( $this->textdomain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
add_shortcode('wp_ct-cookie-div', array(&$this, 'render_cookie_div'));
add_action( 'wp_head', array(&$this, 'google_analytics'), 20);
}
function add_styles() {
$options = $this->get_options();
if ($options['load_css'] == "on") {
wp_enqueue_style($this->prefix . 'css', plugins_url('/cookietool.css', __FILE__));
}
wp_enqueue_script( $this->prefix . 'js', plugins_url('/cookietool.js', __FILE__), false, false, true );
}
function config_script() {
$options = $this->get_options();
$config_array = array(
"link" => $options['link'],
"linkName" => $options['link_name'],
"panelClass" => $options['panel_class'],
"message" => $options['message'],
"agreetext" => $options['agreetext'],
"declinetext" => $options['declinetext'],
"agreeStatusText" => __('You currently <strong>allow</strong> cookies in this site. <button type="button" class="button-basic" data-action="decline">Click here to disallow cookies</button>', $this->textdomain),
"disagreeStatusText" => __('You currently <strong>disallow</strong> cookies in this site. <button type="button" class="button-basic" data-action="agree">Click here to allow cookies</button>', $this->textdomain),
"notSetText" => __('You haven\'t yet established your configuration. <button type="button" class="button-basic" data-action="agree">Click here to allow cookies</button> or <button type="button" class="button-basic" data-action="decline">click here to disallow cookies</button>', $this->textdomain)
);
?>
<script type="text/javascript">
/* <![CDATA[ */
CookieTool.Config.set(<?php echo json_encode($config_array) ?>);
CookieTool.API.ask();
if( document.getElementById('cookietool-settings') ) {
CookieTool.API.displaySettings(document.getElementById('cookietool-settings'));
}
/* ]]> */
</script>
<?php
}
/*
Displays Google Analytics tracking code if it's set in the options, main script is not included, so we can delay cookie insertion
*/
function google_analytics() {
$options = $this->get_options();
if (!empty($options['google_analytics'])):
if (!is_user_logged_in()):
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?php echo esc_js($options["google_analytics"]) ?>']);
_gaq.push(['_trackPageview']);
</script>
<?php
endif;
endif;
}
}
new wp_cookie_tool_init;
new wp_cookie_tool_options;