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 | * Widget position options. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | private $position_options = array( |
||
71 | 'bottom', |
||
72 | 'top', |
||
73 | ); |
||
74 | |||
75 | /** |
||
76 | * Constructor. |
||
77 | */ |
||
78 | View Code Duplication | function __construct() { |
|
79 | parent::__construct( |
||
80 | 'eu_cookie_law_widget', |
||
81 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
82 | apply_filters( 'jetpack_widget_name', esc_html__( 'Cookies & Consents Banner', 'jetpack' ) ), |
||
83 | array( |
||
84 | 'description' => esc_html__( 'Display a banner for EU Cookie Law and GDPR compliance.', 'jetpack' ), |
||
85 | 'customize_selective_refresh' => true, |
||
86 | ), |
||
87 | array() |
||
88 | ); |
||
89 | |||
90 | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
||
91 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Enqueue scripts and styles. |
||
97 | */ |
||
98 | function enqueue_frontend_scripts() { |
||
111 | |||
112 | /** |
||
113 | * Return an associative array of default values. |
||
114 | * |
||
115 | * These values are used in new widgets. |
||
116 | * |
||
117 | * @return array Default values for the widget options. |
||
118 | */ |
||
119 | public function defaults() { |
||
136 | |||
137 | /** |
||
138 | * Front-end display of the widget. |
||
139 | * |
||
140 | * @param array $args Widget arguments. |
||
141 | * @param array $instance Saved values from database. |
||
142 | */ |
||
143 | public function widget( $args, $instance ) { |
||
178 | |||
179 | /** |
||
180 | * Back-end widget form. |
||
181 | * |
||
182 | * @param array $instance Previously saved values from database. |
||
183 | */ |
||
184 | public function form( $instance ) { |
||
202 | |||
203 | /** |
||
204 | * Sanitize widget form values as they are saved. |
||
205 | * |
||
206 | * @param array $new_instance Values just sent to be saved. |
||
207 | * @param array $old_instance Previously saved values from database. |
||
208 | * @return array Updated safe values to be saved. |
||
209 | */ |
||
210 | public function update( $new_instance, $old_instance ) { |
||
277 | |||
278 | /** |
||
279 | * Check if the value is allowed and not empty. |
||
280 | * |
||
281 | * @param string $value Value to check. |
||
282 | * @param array $allowed Array of allowed values. |
||
283 | * |
||
284 | * @return string $value if pass the check or first value from allowed values. |
||
285 | */ |
||
286 | function filter_value( $value, $allowed = array() ) { |
||
293 | } |
||
294 | |||
302 |