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 | ||
| 17 | class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { | ||
| 18 | |||
| 19 | private $default_height = 580; | ||
| 20 | private $default_width = 340; | ||
| 21 | private $max_width = 500; | ||
| 22 | private $min_width = 180; | ||
| 23 | private $max_height = 9999; | ||
| 24 | private $min_height = 130; | ||
| 25 | |||
| 26 | View Code Duplication | 	function __construct() { | |
| 50 | |||
| 51 | /** | ||
| 52 | * Enqueue scripts. | ||
| 53 | */ | ||
| 54 | 	public function enqueue_scripts() { | ||
| 59 | |||
| 60 | 	function widget( $args, $instance ) { | ||
| 61 | extract( $args ); | ||
| 62 | |||
| 63 | $like_args = $this->normalize_facebook_args( $instance['like_args'] ); | ||
| 64 | |||
| 65 | View Code Duplication | 		if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) { | |
| 66 | 			if ( current_user_can( 'edit_theme_options' ) ) { | ||
| 67 | echo $before_widget; | ||
| 68 | echo '<p>' . sprintf( __( 'It looks like your Facebook URL is incorrectly configured. Please check it in your <a href="%s">widget settings</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; | ||
| 69 | echo $after_widget; | ||
| 70 | } | ||
| 71 | echo '<!-- Invalid Facebook Page URL -->'; | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ | ||
| 76 | $title = apply_filters( 'widget_title', $instance['title'] ); | ||
| 77 | $page_url = set_url_scheme( $like_args['href'], 'https' ); | ||
| 78 | |||
| 79 | $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false'; | ||
| 80 | $like_args['stream'] = (bool) $like_args['stream'] ? 'timeline' : 'false'; | ||
| 81 | $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true'; | ||
| 82 | |||
| 83 | echo $before_widget; | ||
| 84 | |||
| 85 | if ( ! empty( $title ) ) : | ||
| 86 | echo $before_title; | ||
| 87 | |||
| 88 | $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>'; | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Filter Facebook Likebox's widget title. | ||
| 92 | * | ||
| 93 | * @module widgets | ||
| 94 | * | ||
| 95 | * @since 3.3.0 | ||
| 96 | * | ||
| 97 | * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL). | ||
| 98 | * @param string $title Widget title as set in the widget settings. | ||
| 99 | * @param string $page_url Facebook Page URL. | ||
| 100 | */ | ||
| 101 | echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url ); | ||
| 102 | |||
| 103 | echo $after_title; | ||
| 104 | endif; | ||
| 105 | |||
| 106 | ?> | ||
| 107 | <div id="fb-root"></div> | ||
| 108 | <div class="fb-page" data-href="<?php echo esc_url( $page_url ); ?>" data-width="<?php echo intval( $like_args['width'] ); ?>" data-height="<?php echo intval( $like_args['height'] ); ?>" data-hide-cover="<?php echo esc_attr( $like_args['cover'] ); ?>" data-show-facepile="<?php echo esc_attr( $like_args['show_faces'] ); ?>" data-tabs="<?php echo esc_attr( $like_args['stream'] ); ?>"> | ||
| 109 | <div class="fb-xfbml-parse-ignore"><blockquote cite="<?php echo esc_url( $page_url ); ?>"><a href="<?php echo esc_url( $page_url ); ?>"><?php echo esc_html( $title ); ?></a></blockquote></div> | ||
| 110 | </div> | ||
| 111 | <?php | ||
| 112 | wp_enqueue_script( 'jetpack-facebook-embed' ); | ||
| 113 | echo $after_widget; | ||
| 114 | |||
| 115 | /** This action is documented in modules/widgets/gravatar-profile.php */ | ||
| 116 | do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' ); | ||
| 117 | } | ||
| 118 | |||
| 119 | 	function update( $new_instance, $old_instance ) { | ||
| 141 | |||
| 142 | 	function form( $instance ) { | ||
| 212 | |||
| 213 | 	function get_default_args() { | ||
| 234 | |||
| 235 | 	function normalize_facebook_args( $args ) { | ||
| 236 | $args = wp_parse_args( (array) $args, $this->get_default_args() ); | ||
| 237 | |||
| 238 | // Validate the Facebook Page URL | ||
| 239 | 		if ( $this->is_valid_facebook_url( $args['href'] ) ) { | ||
| 240 | $temp = explode( '?', $args['href'] ); | ||
| 241 | $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); | ||
| 242 | 		} else { | ||
| 243 | $args['href'] = ''; | ||
| 244 | } | ||
| 245 | |||
| 246 | $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width ); | ||
| 247 | $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height ); | ||
| 248 | $args['show_faces'] = (bool) $args['show_faces']; | ||
| 249 | $args['stream'] = (bool) $args['stream']; | ||
| 250 | $args['cover'] = (bool) $args['cover']; | ||
| 251 | |||
| 252 | // The height used to be dependent on other widget settings | ||
| 253 | // If the user changes those settings but doesn't customize the height, | ||
| 254 | // let's intelligently assign a new height. | ||
| 255 | 		if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) { | ||
| 256 | 			if ( $args['show_faces'] && $args['stream'] ) { | ||
| 257 | $args['height'] = 580; | ||
| 258 | 			} elseif ( ! $args['show_faces'] && ! $args['stream'] ) { | ||
| 259 | $args['height'] = 130; | ||
| 260 | 			} else { | ||
| 261 | $args['height'] = 432; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | return $args; | ||
| 266 | } | ||
| 267 | |||
| 268 | 	function is_valid_facebook_url( $url ) { | ||
| 271 | |||
| 272 | 	function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { | ||
| 283 | |||
| 284 | View Code Duplication | 	function normalize_text_value( $value, $default = '', $allowed = array() ) { | |
| 285 | $allowed = (array) $allowed; | ||
| 286 | |||
| 287 | 		if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { | ||
| 288 | $value = $default; | ||
| 289 | } | ||
| 290 | |||
| 291 | return $value; | ||
| 292 | } | ||
| 293 | |||
| 294 | /** | ||
| 295 | * @deprecated | ||
| 296 | */ | ||
| 297 | 	function guess_locale_from_lang( $lang ) { | ||
| 301 | |||
| 302 | /** | ||
| 303 | * @deprecated | ||
| 304 | */ | ||
| 305 | 	function get_locale() { | ||
| 309 | } | ||
| 310 | 
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.