Completed
Push — add/gdpr-ads-compliance-2 ( 2d78bf )
by
unknown
09:47
created

Jetpack_EU_Cookie_Law_Widget   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 299
Duplicated Lines 3.68 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 299
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 16 3
A enqueue_frontend_scripts() 0 13 1
A defaults() 0 17 1
B widget() 0 28 6
A form() 0 4 1
F update() 8 67 16
A filter_value() 0 7 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
		 * Array of two-letter country codes where GDPR applies.
26
		 *
27
		 * @var array
28
		 */
29
		public static $gdpr_zone = array(
30
			// European Member countries
31
			'AT', // Austria
32
			'BE', // Belgium
33
			'BG', // Bulgaria
34
			'CY', // Cyprus
35
			'CZ', // Czech Republic
36
			'DE', // Germany
37
			'DK', // Denmark
38
			'EE', // Estonia
39
			'ES', // Spain
40
			'FI', // Finland
41
			'FR', // France
42
			'GR', // Greece
43
			'HR', // Croatia
44
			'HU', // Hungary
45
			'IE', // Ireland
46
			'IT', // Italy
47
			'LT', // Lithuania
48
			'LU', // Luxembourg
49
			'LV', // Latvia
50
			'MT', // Malta
51
			'NL', // Netherlands
52
			'PL', // Poland
53
			'PT', // Portugal
54
			'RO', // Romania
55
			'SE', // Sweden
56
			'SI', // Slovenia
57
			'SK', // Slovakia
58
			'GB', // United Kingdom
59
			// Single Market Countries that GDPR applies to
60
			'CH', // Switzerland
61
			'IS', // Iceland
62
			'LI', // Liechtenstein
63
			'NO', // Norway
64
		);
65
66
		/**
67
		 * Default display options.
68
		 *
69
		 * @var array
70
		 */
71
		private $display_options = array(
72
			'all',
73
			'eu',
74
		);
75
76
		/**
77
		 * Default hide options.
78
		 *
79
		 * @var array
80
		 */
81
		private $hide_options = array(
82
			'button',
83
			'scroll',
84
			'time',
85
		);
86
87
		/**
88
		 * Default text options.
89
		 *
90
		 * @var array
91
		 */
92
		private $text_options = array(
93
			'default',
94
			'custom',
95
		);
96
97
		/**
98
		 * Default color scheme options.
99
		 *
100
		 * @var array
101
		 */
102
		private $color_scheme_options = array(
103
			'default',
104
			'negative',
105
		);
106
107
		/**
108
		 * Default policy URL options.
109
		 *
110
		 * @var array
111
		 */
112
		private $policy_url_options = array(
113
			'default',
114
			'custom',
115
		);
116
117
		/**
118
		 * Constructor.
119
		 */
120
		function __construct() {
121
			parent::__construct(
122
				'eu_cookie_law_widget',
123
				/** This filter is documented in modules/widgets/facebook-likebox.php */
124
				apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ),
125
				array(
126
					'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ),
127
					'customize_selective_refresh' => true,
128
				),
129
				array()
130
			);
131
132 View Code Duplication
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
133
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
134
			}
135
		}
136
137
		/**
138
		 * Enqueue scripts and styles.
139
		 */
140
		function enqueue_frontend_scripts() {
141
			wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' );
142
			wp_enqueue_script(
143
				'eu-cookie-law-script',
144
				Jetpack::get_file_url_for_environment(
145
					'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js',
146
					'modules/widgets/eu-cookie-law/eu-cookie-law.js'
147
				),
148
				array( 'jquery' ),
149
				'20170404',
150
				true
151
			);
152
		}
153
154
		/**
155
		 * Return an associative array of default values.
156
		 *
157
		 * These values are used in new widgets.
158
		 *
159
		 * @return array Default values for the widget options.
160
		 */
161
		public function defaults() {
162
			return array(
163
				'display'            => $this->display_options[0],
164
				'hide'               => $this->hide_options[0],
165
				'hide-timeout'       => 30,
166
				'consent-expiration' => 180,
167
				'text'               => $this->text_options[0],
168
				'customtext'         => '',
169
				'color-scheme'       => $this->color_scheme_options[0],
170
				'policy-url'         => $this->policy_url_options[0],
171
				'default-policy-url' => 'https://jetpack.com/support/cookies/',
172
				'custom-policy-url'  => '',
173
				'policy-link-text'   => esc_html__( 'Our Cookie Policy', 'jetpack' ),
174
				'button'             => esc_html__( 'Close and accept', 'jetpack' ),
175
				'default-text'       => esc_html__( 'Privacy & Cookies: This site uses cookies.', 'jetpack' ),
176
			);
177
		}
178
179
		/**
180
		 * Front-end display of the widget.
181
		 *
182
		 * @param array $args     Widget arguments.
183
		 * @param array $instance Saved values from database.
184
		 */
185
		public function widget( $args, $instance ) {
186
			$instance = wp_parse_args( $instance, $this->defaults() );
187
188
			if ( 'eu' === $instance['display'] ) {
189
				// Hide if we can detect this is a non-EU visitor.
190
				if (
191
					! isset( $_SERVER['GEOIP_COUNTRY_CODE'] )
192
					|| ! in_array( $_SERVER['GEOIP_COUNTRY_CODE'], self::$gdpr_zone )
193
				) {
194
					return;
195
				}
196
			}
197
198
			$classes = array();
199
			$classes[] = 'hide-on-' . esc_attr( $instance['hide'] );
200
			if ( 'negative' === $instance['color-scheme'] ) {
201
				$classes[] = 'negative';
202
			}
203
			if ( Jetpack::is_module_active( 'wordads' ) ) {
204
				$classes[] = 'ads-active';
205
			}
206
207
			echo $args['before_widget'];
208
			require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' );
209
			echo $args['after_widget'];
210
			/** This action is already documented in modules/widgets/gravatar-profile.php */
211
			do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' );
212
		}
213
214
		/**
215
		 * Back-end widget form.
216
		 *
217
		 * @param array $instance Previously saved values from database.
218
		 */
219
		public function form( $instance ) {
220
			$instance = wp_parse_args( $instance, $this->defaults() );
221
			require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' );
222
		}
223
224
		/**
225
		 * Sanitize widget form values as they are saved.
226
		 *
227
		 * @param  array $new_instance Values just sent to be saved.
228
		 * @param  array $old_instance Previously saved values from database.
229
		 * @return array Updated safe values to be saved.
230
		 */
231
		public function update( $new_instance, $old_instance ) {
232
			$instance = array();
233
			$defaults = $this->defaults();
234
235
			$instance['display']        = $this->filter_value( $new_instance['display'], $this->display_options );
236
			$instance['hide']           = $this->filter_value( $new_instance['hide'], $this->hide_options );
237
			$instance['text']           = $this->filter_value( $new_instance['text'], $this->text_options );
238
			$instance['color-scheme']   = $this->filter_value( $new_instance['color-scheme'], $this->color_scheme_options );
239
			$instance['policy-url']     = $this->filter_value( $new_instance['policy-url'], $this->policy_url_options );
240
241 View Code Duplication
			if ( isset( $new_instance['hide-timeout'] ) ) {
242
				// Time can be a value between 3 and 1000 seconds.
243
				$instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) );
244
			}
245
246 View Code Duplication
			if ( isset( $new_instance['consent-expiration'] ) ) {
247
				// Time can be a value between 1 and 365 days.
248
				$instance['consent-expiration'] = min( 365, max( 1, intval( $new_instance['consent-expiration'] ) ) );
249
			}
250
251
			if ( isset( $new_instance['customtext'] ) ) {
252
				$instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 );
253
			} else {
254
				$instance['text'] = $this->text_options[0];
255
			}
256
257
			if ( isset( $new_instance['policy-url'] ) ) {
258
				$instance['policy-url'] = 'custom' === $new_instance['policy-url']
259
					? 'custom'
260
					: 'default';
261
			} else {
262
				$instance['policy-url'] = $this->policy_url_options[0];
263
			}
264
265
			if ( 'custom' === $instance['policy-url'] && isset( $new_instance['custom-policy-url'] ) ) {
266
				$instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) );
267
268
				if ( strlen( $instance['custom-policy-url'] ) < 10 ) {
269
					unset( $instance['custom-policy-url'] );
270
					global $wp_customize;
271
					if ( ! isset( $wp_customize ) ) {
272
						$instance['policy-url'] = $this->policy_url_options[0];
273
					}
274
				}
275
			}
276
277
			if ( isset( $new_instance['policy-link-text'] ) ) {
278
				$instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) );
279
			}
280
281
			if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) {
282
				unset( $instance['policy-link-text'] );
283
			}
284
285
			if ( isset( $new_instance['button'] ) ) {
286
				$instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) );
287
			}
288
289
			if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) {
290
				unset( $instance['button'] );
291
			}
292
293
			// Show the banner again if a setting has been changed.
294
			setcookie( self::$cookie_name, '', time() - 86400, '/' );
295
296
			return $instance;
297
		}
298
299
		/**
300
		 * Check if the value is allowed and not empty.
301
		 *
302
		 * @param  string $value Value to check.
303
		 * @param  array  $allowed Array of allowed values.
304
		 *
305
		 * @return string $value if pass the check or first value from allowed values.
306
		 */
307
		function filter_value( $value, $allowed = array() ) {
308
			$allowed = (array) $allowed;
309
			if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
310
				$value = $allowed[0];
311
			}
312
			return $value;
313
		}
314
	}
315
316
	// Register Jetpack_EU_Cookie_Law_Widget widget.
317
	function jetpack_register_eu_cookie_law_widget() {
318
		register_widget( 'Jetpack_EU_Cookie_Law_Widget' );
319
	};
320
321
	add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' );
322
}
323