1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Disable direct access/execution to/of the widget code. |
5
|
|
|
*/ |
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { |
11
|
|
|
/** |
12
|
|
|
* EU Cookie Law Widget |
13
|
|
|
* |
14
|
|
|
* Display the EU Cookie Law banner in wp_footer. |
15
|
|
|
*/ |
16
|
|
|
class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { |
17
|
|
|
/** |
18
|
|
|
* EU Cookie Law cookie name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public static $cookie_name = 'eucookielaw'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* EU Cookie Law cookie validity (30 days). |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
public static $cookie_validity = 2592000; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default hide options. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $hide_options = array( |
37
|
|
|
'button', |
38
|
|
|
'scroll', |
39
|
|
|
'time', |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Default text options. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $text_options = array( |
48
|
|
|
'default', |
49
|
|
|
'custom', |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Default color scheme options. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $color_scheme_options = array( |
58
|
|
|
'default', |
59
|
|
|
'negative', |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Default policy URL options. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private $policy_url_options = array( |
68
|
|
|
'default', |
69
|
|
|
'custom', |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Saved values from database. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
public $instance; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Constructor. |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
function __construct() { |
83
|
|
|
parent::__construct( |
84
|
|
|
'eu_cookie_law_widget', |
85
|
|
|
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
86
|
|
|
apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ), |
87
|
|
|
array( |
88
|
|
|
'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ), |
89
|
|
|
'customize_selective_refresh' => true, |
90
|
|
|
), |
91
|
|
|
array() |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
95
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Enqueue scripts and styles. |
101
|
|
|
*/ |
102
|
|
|
function enqueue_frontend_scripts() { |
103
|
|
|
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); |
104
|
|
|
wp_enqueue_script( 'eu-cookie-law-script', plugins_url( 'eu-cookie-law/eu-cookie-law.js', __FILE__ ), array( 'jquery' ), '20170404' ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return an associative array of default values. |
109
|
|
|
* |
110
|
|
|
* These values are used in new widgets. |
111
|
|
|
* |
112
|
|
|
* @return array Default values for the widget options. |
113
|
|
|
*/ |
114
|
|
|
public function defaults() { |
115
|
|
|
return array( |
116
|
|
|
'hide' => $this->hide_options[0], |
117
|
|
|
'hide-timeout' => 30, |
118
|
|
|
'text' => $this->text_options[0], |
119
|
|
|
'customtext' => '', |
120
|
|
|
'color-scheme' => $this->color_scheme_options[0], |
121
|
|
|
'policy-url' => $this->policy_url_options[0], |
122
|
|
|
'default-policy-url' => 'https://en.support.wordpress.com/cookies', |
123
|
|
|
'custom-policy-url' => '', |
124
|
|
|
'policy-link-text' => esc_html__( 'Our Cookie Policy', 'jetpack' ), |
125
|
|
|
'button' => esc_html__( 'Close and accept', 'jetpack' ), |
126
|
|
|
'default-text' => esc_html__( 'Privacy & Cookies: This site uses cookies from WordPress.com and selected partners.', 'jetpack' ), |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Front-end display of the widget. |
132
|
|
|
* |
133
|
|
|
* @param array $args Widget arguments. |
134
|
|
|
* @param array $instance Saved values from database. |
135
|
|
|
*/ |
136
|
|
|
public function widget( $args, $instance ) { |
137
|
|
|
$this->instance = wp_parse_args( $instance, $this->defaults() ); |
138
|
|
|
/** This action is already documented in modules/widgets/gravatar-profile.php */ |
139
|
|
|
do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
140
|
|
|
add_action( 'wp_footer', array( $this, 'footer' ) ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Display the widget in wp_footer. |
145
|
|
|
*/ |
146
|
|
|
public function footer() { |
147
|
|
|
$instance = $this->instance; |
148
|
|
|
require( dirname( __FILE__ ) . '/eu-cookie-law/footer.php' ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Back-end widget form. |
153
|
|
|
* |
154
|
|
|
* @param array $instance Previously saved values from database. |
155
|
|
|
*/ |
156
|
|
|
public function form( $instance ) { |
157
|
|
|
$instance = wp_parse_args( $instance, $this->defaults() ); |
158
|
|
|
require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sanitize widget form values as they are saved. |
163
|
|
|
* |
164
|
|
|
* @param array $new_instance Values just sent to be saved. |
165
|
|
|
* @param array $old_instance Previously saved values from database. |
166
|
|
|
* @return array Updated safe values to be saved. |
167
|
|
|
*/ |
168
|
|
|
public function update( $new_instance, $old_instance ) { |
169
|
|
|
$instance = array(); |
170
|
|
|
$defaults = $this->defaults(); |
171
|
|
|
|
172
|
|
|
$instance['hide'] = $this->filter_value( $new_instance['hide'], $this->hide_options ); |
173
|
|
|
$instance['text'] = $this->filter_value( $new_instance['text'], $this->text_options ); |
174
|
|
|
$instance['color-scheme'] = $this->filter_value( $new_instance['color-scheme'], $this->color_scheme_options ); |
175
|
|
|
$instance['policy-url'] = $this->filter_value( $new_instance['policy-url'], $this->policy_url_options ); |
176
|
|
|
|
177
|
|
|
if ( isset( $new_instance['hide-timeout'] ) ) { |
178
|
|
|
// Time can be a value between 3 and 1000 seconds. |
179
|
|
|
$instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ( isset( $new_instance['customtext'] ) ) { |
183
|
|
|
$instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 ); |
184
|
|
|
} else { |
185
|
|
|
$instance['text'] = $this->text_options[0]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ( isset( $new_instance['custom-policy-url'] ) ) { |
189
|
|
|
$instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) ); |
190
|
|
|
|
191
|
|
|
if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
192
|
|
|
unset( $instance['custom-policy-url'] ); |
193
|
|
|
$instance['policy-url'] = $this->policy_url_options[0]; |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$instance['policy-url'] = $this->policy_url_options[0]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
if ( isset( $new_instance['policy-link-text'] ) ) { |
200
|
|
|
$instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { |
204
|
|
|
unset( $instance['policy-link-text'] ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
View Code Duplication |
if ( isset( $new_instance['button'] ) ) { |
208
|
|
|
$instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { |
212
|
|
|
unset( $instance['button'] ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Show the banner again if a setting has been changed. |
216
|
|
|
setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
217
|
|
|
|
218
|
|
|
return $instance; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set the EU Cookie Law cookie. |
223
|
|
|
*/ |
224
|
|
|
public static function add_consent_cookie() { |
225
|
|
|
if ( ! isset( $_POST['eucookielaw'] ) || 'accept' !== $_POST['eucookielaw'] ) { |
226
|
|
|
return; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'eucookielaw' ) ) { |
230
|
|
|
return; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Cookie is valid for 30 days, so the user will be shown the banner again after 30 days. |
234
|
|
|
setcookie( self::$cookie_name, current_time( 'timestamp' ), time() + self::$cookie_validity, '/' ); |
235
|
|
|
|
236
|
|
|
wp_safe_redirect( $_POST['redirect_url'] ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Check if the value is allowed and not empty. |
241
|
|
|
* |
242
|
|
|
* @param string $value Value to check. |
243
|
|
|
* @param array $allowed Array of allowed values. |
244
|
|
|
* |
245
|
|
|
* @return string $value if pass the check or first value from allowed values. |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
function filter_value( $value, $allowed = array() ) { |
248
|
|
|
$allowed = (array) $allowed; |
249
|
|
|
if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { |
250
|
|
|
$value = $allowed[0]; |
251
|
|
|
} |
252
|
|
|
return $value; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Register Jetpack_EU_Cookie_Law_Widget widget. |
257
|
|
|
function jetpack_register_eu_cookie_law_widget() { |
258
|
|
|
register_widget( 'Jetpack_EU_Cookie_Law_Widget' ); |
259
|
|
|
}; |
260
|
|
|
|
261
|
|
|
// Only load the widget if we're inside the admin or the user has not given their consent to accept cookies. |
262
|
|
|
if ( is_admin() || empty( $_COOKIE[ Jetpack_EU_Cookie_Law_Widget::$cookie_name ] ) ) { |
263
|
|
|
add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' ); |
264
|
|
|
add_action( 'init', array( 'Jetpack_EU_Cookie_Law_Widget', 'add_consent_cookie' ) ); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|