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 | View Code Duplication | function __construct() { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Enqueue scripts and styles. |
||
| 94 | */ |
||
| 95 | View Code Duplication | 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() { |
||
| 117 | return array( |
||
| 118 | 'hide' => $this->hide_options[0], |
||
| 119 | 'hide-timeout' => 30, |
||
| 120 | 'text' => $this->text_options[0], |
||
| 121 | 'customtext' => '', |
||
| 122 | 'color-scheme' => $this->color_scheme_options[0], |
||
| 123 | 'policy-url' => $this->policy_url_options[0], |
||
| 124 | 'default-policy-url' => 'https://jetpack.com/support/cookies/', |
||
| 125 | 'custom-policy-url' => '', |
||
| 126 | 'policy-link-text' => esc_html__( 'Our Cookie Policy', 'jetpack' ), |
||
| 127 | 'button' => esc_html__( 'Close and accept', 'jetpack' ), |
||
| 128 | 'default-text' => esc_html__( 'Privacy & Cookies: This site uses cookies.', 'jetpack' ), |
||
| 129 | ); |
||
| 130 | } |
||
| 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 ) { |
||
| 139 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
| 140 | echo $args['before_widget']; |
||
| 141 | require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' ); |
||
| 142 | echo $args['after_widget']; |
||
| 143 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 144 | do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Back-end widget form. |
||
| 149 | * |
||
| 150 | * @param array $instance Previously saved values from database. |
||
| 151 | */ |
||
| 152 | public function form( $instance ) { |
||
| 153 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
| 154 | require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); |
||
| 155 | } |
||
| 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 ) { |
||
| 165 | $instance = array(); |
||
| 166 | $defaults = $this->defaults(); |
||
| 167 | |||
| 168 | $instance['hide'] = $this->filter_value( $new_instance['hide'], $this->hide_options ); |
||
| 169 | $instance['text'] = $this->filter_value( $new_instance['text'], $this->text_options ); |
||
| 170 | $instance['color-scheme'] = $this->filter_value( $new_instance['color-scheme'], $this->color_scheme_options ); |
||
| 171 | $instance['policy-url'] = $this->filter_value( $new_instance['policy-url'], $this->policy_url_options ); |
||
| 172 | |||
| 173 | if ( isset( $new_instance['hide-timeout'] ) ) { |
||
| 174 | // Time can be a value between 3 and 1000 seconds. |
||
| 175 | $instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( isset( $new_instance['customtext'] ) ) { |
||
| 179 | $instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 ); |
||
| 180 | } else { |
||
| 181 | $instance['text'] = $this->text_options[0]; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ( isset( $new_instance['policy-url'] ) ) { |
||
| 185 | $instance['policy-url'] = 'custom' === $new_instance['policy-url'] |
||
| 186 | ? 'custom' |
||
| 187 | : 'default'; |
||
| 188 | } else { |
||
| 189 | $instance['policy-url'] = $this->policy_url_options[0]; |
||
| 190 | } |
||
| 191 | |||
| 192 | if ( 'custom' === $instance['policy-url'] && isset( $new_instance['custom-policy-url'] ) ) { |
||
| 193 | $instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) ); |
||
| 194 | |||
| 195 | if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
||
| 196 | unset( $instance['custom-policy-url'] ); |
||
| 197 | global $wp_customize; |
||
| 198 | if ( ! isset( $wp_customize ) ) { |
||
| 199 | $instance['policy-url'] = $this->policy_url_options[0]; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | View Code Duplication | if ( isset( $new_instance['policy-link-text'] ) ) { |
|
| 205 | $instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { |
||
| 209 | unset( $instance['policy-link-text'] ); |
||
| 210 | } |
||
| 211 | |||
| 212 | View Code Duplication | if ( isset( $new_instance['button'] ) ) { |
|
| 213 | $instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); |
||
| 214 | } |
||
| 215 | |||
| 216 | if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { |
||
| 217 | unset( $instance['button'] ); |
||
| 218 | } |
||
| 219 | |||
| 220 | // Show the banner again if a setting has been changed. |
||
| 221 | setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
||
| 222 | |||
| 223 | return $instance; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Check if the value is allowed and not empty. |
||
| 228 | * |
||
| 229 | * @param string $value Value to check. |
||
| 230 | * @param array $allowed Array of allowed values. |
||
| 231 | * |
||
| 232 | * @return string $value if pass the check or first value from allowed values. |
||
| 233 | */ |
||
| 234 | View Code Duplication | function filter_value( $value, $allowed = array() ) { |
|
| 241 | } |
||
| 242 | |||
| 243 | // Register Jetpack_EU_Cookie_Law_Widget widget. |
||
| 244 | function jetpack_register_eu_cookie_law_widget() { |
||
| 245 | register_widget( 'Jetpack_EU_Cookie_Law_Widget' ); |
||
| 246 | }; |
||
| 250 |