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() { |
||
| 69 | parent::__construct( |
||
| 70 | 'eu_cookie_law_widget', |
||
| 71 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 72 | apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ), |
||
| 73 | array( |
||
| 74 | 'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ), |
||
| 75 | 'customize_selective_refresh' => true, |
||
| 76 | ), |
||
| 77 | array() |
||
| 78 | ); |
||
| 79 | |||
| 80 | View Code Duplication | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
|
| 81 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Enqueue scripts and styles. |
||
| 87 | */ |
||
| 88 | function enqueue_frontend_scripts() { |
||
| 89 | wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); |
||
| 90 | wp_enqueue_script( |
||
| 91 | 'eu-cookie-law-script', |
||
| 92 | Jetpack::get_file_url_for_environment( |
||
| 93 | '_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js', |
||
| 94 | 'modules/widgets/eu-cookie-law/eu-cookie-law.js' |
||
| 95 | ), |
||
| 96 | array( 'jquery' ), |
||
| 97 | '20170404', |
||
| 98 | true |
||
| 99 | ); |
||
| 100 | } |
||
| 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' => $this->policy_url_options[0], |
||
| 118 | 'default-policy-url' => 'https://jetpack.com/support/cookies/', |
||
| 119 | 'custom-policy-url' => '', |
||
| 120 | 'policy-link-text' => esc_html__( 'Our Cookie Policy', 'jetpack' ), |
||
| 121 | 'button' => esc_html__( 'Close and accept', 'jetpack' ), |
||
| 122 | 'default-text' => esc_html__( 'Privacy & Cookies: This site uses cookies.', '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 ) { |
||
| 133 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
| 134 | |||
| 135 | $classes = array(); |
||
| 136 | $classes[] = 'hide-on-' . esc_attr( $instance['hide'] ); |
||
| 137 | if ( 'negative' === $instance['color-scheme'] ) { |
||
| 138 | $classes[] = 'negative'; |
||
| 139 | } |
||
| 140 | if ( Jetpack::is_module_active( 'wordads' ) ) { |
||
| 141 | $classes[] = 'ads-active'; |
||
| 142 | } |
||
| 143 | |||
| 144 | echo $args['before_widget']; |
||
| 145 | require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' ); |
||
| 146 | echo $args['after_widget']; |
||
| 147 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 148 | do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Back-end widget form. |
||
| 153 | * |
||
| 154 | * @param array $instance Previously saved values from database. |
||
| 155 | */ |
||
| 156 | public function form( $instance ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sanitize widget form values as they are saved. |
||
| 163 | * |
||
| 164 | * @param array $new_instance Values just sent to be saved. |
||
| 165 | * @param array $old_instance Previously saved values from database. |
||
| 166 | * @return array Updated safe values to be saved. |
||
| 167 | */ |
||
| 168 | public function update( $new_instance, $old_instance ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Check if the value is allowed and not empty. |
||
| 237 | * |
||
| 238 | * @param string $value Value to check. |
||
| 239 | * @param array $allowed Array of allowed values. |
||
| 240 | * |
||
| 241 | * @return string $value if pass the check or first value from allowed values. |
||
| 242 | */ |
||
| 243 | function filter_value( $value, $allowed = array() ) { |
||
| 250 | } |
||
| 251 | |||
| 259 |