Completed
Push — add/gdpr-ads-compliance ( cda5c5...7a3b5d )
by
unknown
30:46 queued 12:51
created

Jetpack_EU_Cookie_Law_Widget::widget()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 2
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
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 the bottom part of the screen.
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
		 * Default hide options.
26
		 *
27
		 * @var array
28
		 */
29
		private $hide_options = array(
30
			'button',
31
			'scroll',
32
			'time',
33
		);
34
35
		/**
36
		 * Default text options.
37
		 *
38
		 * @var array
39
		 */
40
		private $text_options = array(
41
			'default',
42
			'custom',
43
		);
44
45
		/**
46
		 * Default color scheme options.
47
		 *
48
		 * @var array
49
		 */
50
		private $color_scheme_options = array(
51
			'default',
52
			'negative',
53
		);
54
55
		/**
56
		 * Default policy URL options.
57
		 *
58
		 * @var array
59
		 */
60
		private $policy_url_options = array(
61
			'default',
62
			'custom',
63
		);
64
65
		/**
66
		 * Constructor.
67
		 */
68
		function __construct() {
69
			parent::__construct(
70
				'eu_cookie_law_widget',
71
				/** This filter is documented in modules/widgets/facebook-likebox.php */
72
				apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ),
73
				array(
74
					'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ),
75
					'customize_selective_refresh' => true,
76
				),
77
				array()
78
			);
79
80 View Code Duplication
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
81
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
82
			}
83
		}
84
85
		/**
86
		 * Enqueue scripts and styles.
87
		 */
88
		function enqueue_frontend_scripts() {
89
			wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' );
90
			wp_enqueue_script(
91
				'eu-cookie-law-script',
92
				Jetpack::get_file_url_for_environment(
93
					'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
94
					'modules/widgets/eu-cookie-law/eu-cookie-law.js'
95
				),
96
				array( 'jquery' ),
97
				'20170404',
98
				true
99
			);
100
		}
101
102
		/**
103
		 * Return an associative array of default values.
104
		 *
105
		 * These values are used in new widgets.
106
		 *
107
		 * @return array Default values for the widget options.
108
		 */
109
		public function defaults() {
110
			return array(
111
				'hide'               => $this->hide_options[0],
112
				'hide-timeout'       => 30,
113
				'consent-expiration' => 180,
114
				'text'               => $this->text_options[0],
115
				'customtext'         => '',
116
				'color-scheme'       => $this->color_scheme_options[0],
117
				'policy-url'         => $this->policy_url_options[0],
118
				'default-policy-url' => 'https://jetpack.com/support/cookies/',
119
				'custom-policy-url'  => '',
120
				'policy-link-text'   => esc_html__( 'Our Cookie Policy', 'jetpack' ),
121
				'button'             => esc_html__( 'Close and accept', 'jetpack' ),
122
				'default-text'       => esc_html__( 'Privacy & Cookies: This site uses cookies.', 'jetpack' ),
123
			);
124
		}
125
126
		/**
127
		 * Front-end display of the widget.
128
		 *
129
		 * @param array $args     Widget arguments.
130
		 * @param array $instance Saved values from database.
131
		 */
132
		public function widget( $args, $instance ) {
133
			/**
134
			 * Filters the display of the EU Cookie Law widget.
135
			 *
136
			 * @since 6.1.1
137
			 *
138
			 * @param bool true Should the EU Cookie Law widget be disabled. Default to false.
139
			 */
140
			if ( apply_filters( 'jetpack_disable_eu_cookie_law_widget', false ) ) {
141
				return;
142
			}
143
144
			$instance = wp_parse_args( $instance, $this->defaults() );
145
146
			$classes = array();
147
			$classes['hide'] = 'hide-on-' . esc_attr( $instance['hide'] );
148
			if ( 'negative' === $instance['color-scheme'] ) {
149
				$classes['negative'] = 'negative';
150
			}
151
152
			if ( Jetpack::is_module_active( 'wordads' ) ) {
153
				$classes['ads'] = 'ads-active';
154
				$classes['hide'] = 'hide-on-button';
155
			}
156
157
			echo $args['before_widget'];
158
			require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' );
159
			echo $args['after_widget'];
160
			/** This action is already documented in modules/widgets/gravatar-profile.php */
161
			do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' );
162
		}
163
164
		/**
165
		 * Back-end widget form.
166
		 *
167
		 * @param array $instance Previously saved values from database.
168
		 */
169
		public function form( $instance ) {
170
			$instance = wp_parse_args( $instance, $this->defaults() );
171
			if ( Jetpack::is_module_active( 'wordads' ) ) {
172
				$instance['hide'] = 'button';
173
			}
174
			require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' );
175
		}
176
177
		/**
178
		 * Sanitize widget form values as they are saved.
179
		 *
180
		 * @param  array $new_instance Values just sent to be saved.
181
		 * @param  array $old_instance Previously saved values from database.
182
		 * @return array Updated safe values to be saved.
183
		 */
184
		public function update( $new_instance, $old_instance ) {
185
			$instance = array();
186
			$defaults = $this->defaults();
187
188
			$instance['hide']           = $this->filter_value( $new_instance['hide'], $this->hide_options );
189
			$instance['text']           = $this->filter_value( $new_instance['text'], $this->text_options );
190
			$instance['color-scheme']   = $this->filter_value( $new_instance['color-scheme'], $this->color_scheme_options );
191
			$instance['policy-url']     = $this->filter_value( $new_instance['policy-url'], $this->policy_url_options );
192
193 View Code Duplication
			if ( isset( $new_instance['hide-timeout'] ) ) {
194
				// Time can be a value between 3 and 1000 seconds.
195
				$instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) );
196
			}
197
198 View Code Duplication
			if ( isset( $new_instance['consent-expiration'] ) ) {
199
				// Time can be a value between 1 and 365 days.
200
				$instance['consent-expiration'] = min( 365, max( 1, intval( $new_instance['consent-expiration'] ) ) );
201
			}
202
203
			if ( isset( $new_instance['customtext'] ) ) {
204
				$instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 );
205
			} else {
206
				$instance['text'] = $this->text_options[0];
207
			}
208
209
			if ( isset( $new_instance['policy-url'] ) ) {
210
				$instance['policy-url'] = 'custom' === $new_instance['policy-url']
211
					? 'custom'
212
					: 'default';
213
			} else {
214
				$instance['policy-url'] = $this->policy_url_options[0];
215
			}
216
217
			if ( 'custom' === $instance['policy-url'] && isset( $new_instance['custom-policy-url'] ) ) {
218
				$instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) );
219
220
				if ( strlen( $instance['custom-policy-url'] ) < 10 ) {
221
					unset( $instance['custom-policy-url'] );
222
					global $wp_customize;
223
					if ( ! isset( $wp_customize ) ) {
224
						$instance['policy-url'] = $this->policy_url_options[0];
225
					}
226
				}
227
			}
228
229
			if ( isset( $new_instance['policy-link-text'] ) ) {
230
				$instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) );
231
			}
232
233
			if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) {
234
				unset( $instance['policy-link-text'] );
235
			}
236
237
			if ( isset( $new_instance['button'] ) ) {
238
				$instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) );
239
			}
240
241
			if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) {
242
				unset( $instance['button'] );
243
			}
244
245
			// Show the banner again if a setting has been changed.
246
			setcookie( self::$cookie_name, '', time() - 86400, '/' );
247
248
			return $instance;
249
		}
250
251
		/**
252
		 * Check if the value is allowed and not empty.
253
		 *
254
		 * @param  string $value Value to check.
255
		 * @param  array  $allowed Array of allowed values.
256
		 *
257
		 * @return string $value if pass the check or first value from allowed values.
258
		 */
259
		function filter_value( $value, $allowed = array() ) {
260
			$allowed = (array) $allowed;
261
			if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
262
				$value = $allowed[0];
263
			}
264
			return $value;
265
		}
266
	}
267
268
	// Register Jetpack_EU_Cookie_Law_Widget widget.
269
	function jetpack_register_eu_cookie_law_widget() {
270
		register_widget( 'Jetpack_EU_Cookie_Law_Widget' );
271
	};
272
273
	add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' );
274
}
275