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 |
||
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() { |
||
84 | |||
85 | /** |
||
86 | * Enqueue scripts and styles. |
||
87 | */ |
||
88 | function enqueue_frontend_scripts() { |
||
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' => get_option( 'wp_page_for_privacy_policy' ) ? $this->policy_url_options[1] : $this->policy_url_options[0], |
||
118 | 'default-policy-url' => 'https://automattic.com/cookies/', |
||
119 | 'custom-policy-url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', |
||
120 | 'policy-link-text' => esc_html__( 'Cookie Policy', 'jetpack' ), |
||
121 | 'button' => esc_html__( 'Close and accept', 'jetpack' ), |
||
122 | 'default-text' => esc_html__( "Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. \r\nTo find out more, including how to control cookies, see here:", '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 ) { |
||
163 | |||
164 | /** |
||
165 | * Back-end widget form. |
||
166 | * |
||
167 | * @param array $instance Previously saved values from database. |
||
168 | */ |
||
169 | public function form( $instance ) { |
||
187 | |||
188 | /** |
||
189 | * Sanitize widget form values as they are saved. |
||
190 | * |
||
191 | * @param array $new_instance Values just sent to be saved. |
||
192 | * @param array $old_instance Previously saved values from database. |
||
193 | * @return array Updated safe values to be saved. |
||
194 | */ |
||
195 | public function update( $new_instance, $old_instance ) { |
||
196 | $instance = array(); |
||
197 | $defaults = $this->defaults(); |
||
198 | |||
199 | $instance['hide'] = $this->filter_value( isset( $new_instance['hide'] ) ? $new_instance['hide'] : '', $this->hide_options ); |
||
200 | $instance['text'] = $this->filter_value( isset( $new_instance['text'] ) ? $new_instance['text'] : '', $this->text_options ); |
||
201 | $instance['color-scheme'] = $this->filter_value( isset( $new_instance['color-scheme'] ) ? $new_instance['color-scheme'] : '', $this->color_scheme_options ); |
||
202 | $instance['policy-url'] = $this->filter_value( isset( $new_instance['policy-url'] ) ? $new_instance['policy-url'] : '', $this->policy_url_options ); |
||
203 | |||
204 | View Code Duplication | if ( isset( $new_instance['hide-timeout'] ) ) { |
|
205 | // Time can be a value between 3 and 1000 seconds. |
||
206 | $instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
||
207 | } |
||
208 | |||
209 | View Code Duplication | if ( isset( $new_instance['consent-expiration'] ) ) { |
|
210 | // Time can be a value between 1 and 365 days. |
||
211 | $instance['consent-expiration'] = min( 365, max( 1, intval( $new_instance['consent-expiration'] ) ) ); |
||
212 | } |
||
213 | |||
214 | if ( isset( $new_instance['customtext'] ) ) { |
||
215 | $instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 ); |
||
216 | } else { |
||
217 | $instance['text'] = $this->text_options[0]; |
||
218 | } |
||
219 | |||
220 | if ( isset( $new_instance['policy-url'] ) ) { |
||
221 | $instance['policy-url'] = 'custom' === $new_instance['policy-url'] |
||
222 | ? 'custom' |
||
223 | : 'default'; |
||
224 | } else { |
||
225 | $instance['policy-url'] = $this->policy_url_options[0]; |
||
226 | } |
||
227 | |||
228 | if ( 'custom' === $instance['policy-url'] && isset( $new_instance['custom-policy-url'] ) ) { |
||
229 | $instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) ); |
||
230 | |||
231 | if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
||
232 | unset( $instance['custom-policy-url'] ); |
||
233 | global $wp_customize; |
||
234 | if ( ! isset( $wp_customize ) ) { |
||
235 | $instance['policy-url'] = $this->policy_url_options[0]; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | |||
240 | if ( isset( $new_instance['policy-link-text'] ) ) { |
||
241 | $instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); |
||
242 | } |
||
243 | |||
244 | if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { |
||
245 | unset( $instance['policy-link-text'] ); |
||
246 | } |
||
247 | |||
248 | if ( isset( $new_instance['button'] ) ) { |
||
249 | $instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); |
||
250 | } |
||
251 | |||
252 | if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { |
||
253 | unset( $instance['button'] ); |
||
254 | } |
||
255 | |||
256 | // Show the banner again if a setting has been changed. |
||
257 | setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
||
258 | |||
259 | return $instance; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Check if the value is allowed and not empty. |
||
264 | * |
||
265 | * @param string $value Value to check. |
||
266 | * @param array $allowed Array of allowed values. |
||
267 | * |
||
268 | * @return string $value if pass the check or first value from allowed values. |
||
269 | */ |
||
270 | function filter_value( $value, $allowed = array() ) { |
||
277 | } |
||
278 | |||
279 | // Register Jetpack_EU_Cookie_Law_Widget widget. |
||
286 |