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 |
||
| 11 | class Jetpack_Follow_Button_Widget extends WP_Widget { |
||
| 12 | |||
| 13 | View Code Duplication | public function __construct() { |
|
| 24 | |||
| 25 | public function widget( $args, $instance ) { |
||
| 26 | $attributes = array(); |
||
| 27 | $instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) ); |
||
| 28 | |||
| 29 | $wpcom_locale = get_locale(); |
||
| 30 | |||
| 31 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 32 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 33 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | View Code Duplication | if ( class_exists( 'GP_Locales' ) ) { |
|
| 38 | $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', $wpcom_locale ); |
||
| 39 | if ( $wpcom_locale_object instanceof GP_Locale ) { |
||
| 40 | $wpcom_locale = $wpcom_locale_object->slug; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | if ( empty( $instance['show_name'] ) ) { |
||
| 45 | $attributes[] = 'data-show-blog-name="false"'; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ( ! empty( $instance['show_count'] ) ) { |
||
| 49 | $attributes[] = 'data-show-follower-count="true"'; |
||
| 50 | } |
||
| 51 | |||
| 52 | echo $args['before_widget']; |
||
| 53 | ?> |
||
| 54 | |||
| 55 | <a |
||
| 56 | class="wordpress-follow-button" |
||
| 57 | href="<?php echo esc_url( home_url() ); ?>" |
||
| 58 | data-blog="<?php echo esc_url( home_url() ); ?>" |
||
| 59 | data-lang="<?php echo esc_attr( $wpcom_locale ); ?>" <?php if ( ! empty( $attributes ) ) echo implode( ' ', $attributes ); ?> |
||
| 60 | > |
||
| 61 | <?php sprintf( __( 'Follow %s on WordPress.com', 'jetpack' ), get_bloginfo( 'name' ) ); ?> |
||
| 62 | </a> |
||
| 63 | <script type="text/javascript">(function(d){var f = d.getElementsByTagName('SCRIPT')[0], p = d.createElement('SCRIPT');p.type = 'text/javascript';p.async = true;p.src = '//widgets.wp.com/platform.js';f.parentNode.insertBefore(p,f);}(document));</script> |
||
| 64 | |||
| 65 | <?php |
||
| 66 | echo $args['after_widget']; |
||
| 67 | |||
| 68 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 69 | do_action( 'jetpack_stats_extra', 'widget_view', 'follow_button' ); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function form( $instance ) { |
||
| 89 | |||
| 90 | public function update( $new_instance, $old_instance ) { |
||
| 95 | } |
||
| 96 |