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 | * EU Cookie Law cookie validity (30 days). |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | public static $cookie_validity = 2592000; |
||
30 | |||
31 | /** |
||
32 | * Default hide options. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $hide_options = array( |
||
37 | 'button', |
||
38 | 'scroll', |
||
39 | 'time', |
||
40 | ); |
||
41 | |||
42 | /** |
||
43 | * Default text options. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $text_options = array( |
||
48 | 'default', |
||
49 | 'custom', |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * Default color scheme options. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $color_scheme_options = array( |
||
58 | 'default', |
||
59 | 'negative', |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * Default policy URL options. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $policy_url_options = array( |
||
68 | 'default', |
||
69 | 'custom', |
||
70 | ); |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | */ |
||
75 | function __construct() { |
||
76 | parent::__construct( |
||
77 | 'eu_cookie_law_widget', |
||
78 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
79 | apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ), |
||
80 | array( |
||
81 | 'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ), |
||
82 | 'customize_selective_refresh' => true, |
||
83 | ), |
||
84 | array() |
||
85 | ); |
||
86 | |||
87 | View Code Duplication | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
|
88 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Enqueue scripts and styles. |
||
94 | */ |
||
95 | function enqueue_frontend_scripts() { |
||
96 | wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); |
||
97 | wp_enqueue_script( |
||
98 | 'eu-cookie-law-script', |
||
99 | Jetpack::get_file_url_for_environment( |
||
100 | '_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js', |
||
101 | 'modules/widgets/eu-cookie-law/eu-cookie-law.js' |
||
102 | ), |
||
103 | array( 'jquery' ), |
||
104 | '20170404', |
||
105 | true |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Return an associative array of default values. |
||
111 | * |
||
112 | * These values are used in new widgets. |
||
113 | * |
||
114 | * @return array Default values for the widget options. |
||
115 | */ |
||
116 | public function defaults() { |
||
131 | |||
132 | /** |
||
133 | * Front-end display of the widget. |
||
134 | * |
||
135 | * @param array $args Widget arguments. |
||
136 | * @param array $instance Saved values from database. |
||
137 | */ |
||
138 | public function widget( $args, $instance ) { |
||
146 | |||
147 | /** |
||
148 | * Back-end widget form. |
||
149 | * |
||
150 | * @param array $instance Previously saved values from database. |
||
151 | */ |
||
152 | public function form( $instance ) { |
||
156 | |||
157 | /** |
||
158 | * Sanitize widget form values as they are saved. |
||
159 | * |
||
160 | * @param array $new_instance Values just sent to be saved. |
||
161 | * @param array $old_instance Previously saved values from database. |
||
162 | * @return array Updated safe values to be saved. |
||
163 | */ |
||
164 | public function update( $new_instance, $old_instance ) { |
||
216 | |||
217 | /** |
||
218 | * Check if the value is allowed and not empty. |
||
219 | * |
||
220 | * @param string $value Value to check. |
||
221 | * @param array $allowed Array of allowed values. |
||
222 | * |
||
223 | * @return string $value if pass the check or first value from allowed values. |
||
224 | */ |
||
225 | function filter_value( $value, $allowed = array() ) { |
||
232 | } |
||
233 | |||
241 |