1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Disable direct access/execution to/of the widget code. |
5
|
|
|
*/ |
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { |
11
|
|
|
public static $cookie_name = 'eucookielaw'; |
12
|
|
|
public static $cookie_validity = 2592000; // 30 days |
13
|
|
|
|
14
|
|
|
public $defaults = array(), $instance; |
|
|
|
|
15
|
|
|
|
16
|
|
|
function __construct() { |
17
|
|
|
parent::__construct( |
18
|
|
|
'eu_cookie_law_widget', |
19
|
|
|
apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ), |
20
|
|
|
array( |
21
|
|
|
'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ), |
22
|
|
|
), |
23
|
|
|
array() |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->defaults = array( |
27
|
|
|
'hide' => 'button', |
28
|
|
|
'hide-timeout' => 30, |
29
|
|
|
'text' => 'default', |
30
|
|
|
'customtext' => '', |
31
|
|
|
'color-scheme' => 'default', |
32
|
|
|
'policy-url' => 'default', |
33
|
|
|
'default-policy-url' => 'https://en.support.wordpress.com/cookies', |
34
|
|
|
'custom-policy-url' => '', |
35
|
|
|
'policy-link-text' => __( 'Our Cookie Policy', 'jetpack' ), |
36
|
|
|
'button' => __( 'Close and accept', 'jetpack' ), |
37
|
|
|
'default-text' => __( 'Privacy & Cookies: This site uses cookies from WordPress.com and selected partners. ', 'jetpack' ), |
38
|
|
|
); |
39
|
|
|
|
40
|
|
View Code Duplication |
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
41
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function widget( $args, $instance ) { |
46
|
|
|
$this->instance = wp_parse_args( $instance, $this->defaults ); |
47
|
|
|
|
48
|
|
|
do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
49
|
|
|
|
50
|
|
|
echo 'CIAO'; |
51
|
|
|
|
52
|
|
|
add_action( 'wp_footer', array( $this, 'footer' ) ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function footer() { |
56
|
|
|
$blog_url = get_bloginfo( 'url' ); |
57
|
|
|
$instance = $this->instance; |
58
|
|
|
$defaults = $this->defaults; |
59
|
|
|
$cookie_name = self::$cookie_name; |
60
|
|
|
$cookie_validity = self::$cookie_validity; |
61
|
|
|
|
62
|
|
|
require( dirname( __FILE__ ) . '/eu-cookie-law/footer.php' ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function form( $instance ) { |
66
|
|
|
$instance = wp_parse_args( $instance, $this->defaults ); |
67
|
|
|
|
68
|
|
|
require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function update( $new_instance, $old_instance ) { |
72
|
|
|
$instance = array(); |
73
|
|
|
|
74
|
|
|
if ( in_array( $new_instance['hide'], array( 'button', 'scroll', 'time' ) ) ) { |
75
|
|
|
$instance['hide'] = $new_instance['hide']; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( isset( $new_instance['hide-timeout'] ) ) { |
79
|
|
|
// time can be a value between 5 and 1000 seconds |
80
|
|
|
$instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if ( in_array( $new_instance['text'], array( 'default', 'custom' ) ) ) { |
84
|
|
|
$instance['text'] = $new_instance['text']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
if ( isset( $new_instance['customtext'] ) ) { |
88
|
|
|
$instance['customtext'] = mb_substr( $new_instance['customtext'], 0, 4096 ); |
89
|
|
|
} else { |
90
|
|
|
$instance['text'] = 'default'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if ( isset( $new_instance['color-scheme'] ) ) { |
94
|
|
|
$instance['color-scheme'] = 'negative' === $new_instance['color-scheme'] ? 'negative' : 'default'; |
95
|
|
|
} else { |
96
|
|
|
$instance['color-scheme'] = 'default'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if ( in_array( $new_instance['policy-url'], array( 'default', 'custom' ) ) ) { |
100
|
|
|
$instance['policy-url'] = $new_instance['policy-url']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ( isset( $new_instance['custom-policy-url'] ) ) { |
104
|
|
|
$instance['custom-policy-url'] = esc_url( $new_instance, array( 'http', 'https' ) ); |
105
|
|
|
|
106
|
|
|
if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
107
|
|
|
unset( $instance['custom-policy-url'] ); |
108
|
|
|
$instance['policy-url'] = 'default'; |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
$instance['policy-url'] = 'default'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
if ( isset( $new_instance['policy-link-text'] ) ) { |
115
|
|
|
$instance['policy-link-text'] = trim( mb_substr( $new_instance['policy-link-text'], 0, 100 ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $this->defaults['policy-link-text'] ) { |
119
|
|
|
unset( $instance['policy-link-text'] ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
if ( isset( $new_instance['button'] ) ) { |
123
|
|
|
$instance['button'] = trim( mb_substr( $new_instance['button'], 0, 100 ) ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ( empty( $instance['button'] ) || $instance['button'] == $this->defaults['button'] ) { |
127
|
|
|
unset( $instance['button'] ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// show the banner again if a setting has been changed |
131
|
|
|
setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
132
|
|
|
|
133
|
|
|
return $instance; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function add_consent_cookie() { |
137
|
|
|
if ( ! isset( $_POST['eucookielaw'] ) || 'accept' !== $_POST['eucookielaw'] ) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'eucookielaw' ) ) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Cookie is valid for 30 days, so the user will be shown the banner again after 30 days |
146
|
|
|
setcookie( self::$cookie_name, current_time( 'timestamp' ), time() + self::$cookie_validity, '/' ); |
147
|
|
|
|
148
|
|
|
wp_safe_redirect( $_POST['redirect_url'] ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
function enqueue_style() { |
152
|
|
|
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Only load the widget if we're inside the admin |
157
|
|
|
// or the user has not given their consent to accept cookies |
158
|
|
|
if ( is_admin() || empty( $_COOKIE[ Jetpack_EU_Cookie_Law_Widget::$cookie_name ] ) ) { |
159
|
|
|
add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' ); |
160
|
|
|
//add_action( 'init', array( 'Jetpack_EU_Cookie_Law_Widget', 'add_consent_cookie' ) ); |
161
|
|
|
} |
162
|
|
|
function jetpack_register_eu_cookie_law_widget() { |
163
|
|
|
register_widget( 'Jetpack_EU_Cookie_Law_Widget' ); |
164
|
|
|
}; |
165
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.