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() { |
|
| 27 | parent::__construct( |
||
| 28 | 'facebook-likebox', |
||
| 29 | /** |
||
| 30 | * Filter the name of a widget included in the Extra Sidebar Widgets module. |
||
| 31 | * |
||
| 32 | * @module widgets |
||
| 33 | * |
||
| 34 | * @since 2.1.2 |
||
| 35 | * |
||
| 36 | * @param string $widget_name Widget title. |
||
| 37 | */ |
||
| 38 | apply_filters( 'jetpack_widget_name', __( 'Facebook Page Plugin', 'jetpack' ) ), |
||
| 39 | array( |
||
| 40 | 'classname' => 'widget_facebook_likebox', |
||
| 41 | 'description' => __( 'Use the Facebook Page Plugin to connect visitors to your Facebook Page', 'jetpack' ), |
||
| 42 | 'customize_selective_refresh' => true, |
||
| 43 | ) |
||
| 44 | ); |
||
| 45 | |||
| 46 | if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) { |
||
| 47 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Enqueue scripts. |
||
| 53 | */ |
||
| 54 | public function enqueue_scripts() { |
||
| 55 | wp_enqueue_script( 'jetpack-facebook-embed' ); |
||
| 56 | wp_enqueue_style( 'jetpack_facebook_likebox', plugins_url( 'facebook-likebox/style.css', __FILE__ ) ); |
||
| 57 | wp_style_add_data( 'jetpack_facebook_likebox', 'jetpack-inline', true ); |
||
| 58 | } |
||
| 59 | |||
| 60 | function widget( $args, $instance ) { |
||
| 61 | |||
| 62 | extract( $args ); |
||
| 63 | |||
| 64 | $like_args = $this->normalize_facebook_args( $instance['like_args'] ); |
||
| 65 | |||
| 66 | if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) { |
||
| 67 | if ( current_user_can('edit_theme_options') ) { |
||
| 68 | echo $before_widget; |
||
| 69 | 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>'; |
||
| 70 | echo $after_widget; |
||
| 71 | } |
||
| 72 | echo '<!-- Invalid Facebook Page URL -->'; |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 77 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
| 78 | $page_url = set_url_scheme( $like_args['href'], 'https' ); |
||
| 79 | |||
| 80 | $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false'; |
||
| 81 | $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false'; |
||
| 82 | $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true'; |
||
| 83 | |||
| 84 | echo $before_widget; |
||
| 85 | |||
| 86 | if ( ! empty( $title ) ) : |
||
| 87 | echo $before_title; |
||
| 88 | |||
| 89 | $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Filter Facebook Likebox's widget title. |
||
| 93 | * |
||
| 94 | * @module widgets |
||
| 95 | * |
||
| 96 | * @since 3.3.0 |
||
| 97 | * |
||
| 98 | * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL). |
||
| 99 | * @param string $title Widget title as set in the widget settings. |
||
| 100 | * @param string $page_url Facebook Page URL. |
||
| 101 | */ |
||
| 102 | echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url ); |
||
| 103 | |||
| 104 | echo $after_title; |
||
| 105 | endif; |
||
| 106 | |||
| 107 | ?> |
||
| 108 | <div id="fb-root"></div> |
||
| 109 | <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-show-posts="<?php echo esc_attr( $like_args['stream'] ); ?>"> |
||
| 110 | <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> |
||
| 111 | </div> |
||
| 112 | <?php |
||
| 113 | wp_enqueue_script( 'jetpack-facebook-embed' ); |
||
| 114 | echo $after_widget; |
||
| 115 | |||
| 116 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 117 | do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' ); |
||
| 118 | } |
||
| 119 | |||
| 120 | function update( $new_instance, $old_instance ) { |
||
| 142 | |||
| 143 | function form( $instance ) { |
||
| 209 | |||
| 210 | function get_default_args() { |
||
| 231 | |||
| 232 | function normalize_facebook_args( $args ) { |
||
| 264 | |||
| 265 | function is_valid_facebook_url( $url ) { |
||
| 268 | |||
| 269 | function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { |
||
| 277 | |||
| 278 | View Code Duplication | function normalize_text_value( $value, $default = '', $allowed = array() ) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @deprecated |
||
| 289 | */ |
||
| 290 | function guess_locale_from_lang( $lang ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @deprecated |
||
| 297 | */ |
||
| 298 | function get_locale() { |
||
| 302 | } |
||
| 303 | |||
| 305 |
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.