Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Google_Translate_Widget   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 21.74 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 25
loc 115
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Jetpack_Google_Translate_Widget::__construct() 14 14 1
A Jetpack_Google_Translate_Widget::enqueue_scripts() 0 7 1
B Jetpack_Google_Translate_Widget::widget() 3 31 4
A Jetpack_Google_Translate_Widget::form() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Plugin Name: Google Translate Widget for WordPress.com
4
 * Plugin URI: http://automattic.com
5
 * Description: Add a widget for automatic translation
6
 * Author: Artur Piszek
7
 * Version: 0.1
8
 * Author URI: http://automattic.com
9
 * Text Domain: jetpack
10
 */
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
class Jetpack_Google_Translate_Widget extends WP_Widget {
16
	static $instance = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	/**
19
	 * Default widget title.
20
	 *
21
	 * @var string $default_title
22
	 */
23
	var $default_title;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $default_title.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
24
25
	/**
26
	 * Register widget with WordPress.
27
	 */
28 View Code Duplication
	function __construct() {
29
		parent::__construct(
30
			'google_translate_widget',
31
			/** This filter is documented in modules/widgets/facebook-likebox.php */
32
			apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),
33
			array(
34
				'description' => __( 'Provide your readers with the option to translate your site into their preferred language.', 'jetpack' ),
35
				'customize_selective_refresh' => true
36
			)
37
		);
38
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
39
40
		$this->default_title = esc_html__( 'Translate', 'jetpack' );
41
	}
42
43
	/**
44
	 * Enqueue frontend JS scripts.
45
	 */
46
	public function enqueue_scripts() {
47
		wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) );
48
		wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) );
49
		// Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
50
		// This is a hack to show google translate bar a bit lower.
51
		wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' );
52
	}
53
54
	/**
55
	 * Display the Widget.
56
	 *
57
	 * @see WP_Widget::widget()
58
	 *
59
	 * @param array $args     Display arguments.
60
	 * @param array $instance The settings for the particular instance of the widget.
61
	 */
62
	public function widget( $args, $instance ) {
63
		// We never should show more than 1 instance of this.
64
		if ( null === self::$instance ) {
65
			$instance = wp_parse_args( $instance, array(
66
				'title' => $this->default_title,
67
			) );
68
69
			wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
70
			wp_enqueue_script( 'google-translate-init' );
71
			wp_enqueue_script( 'google-translate' );
72
73
			$title = $instance['title'];
74
75
			if ( ! isset( $title ) ) {
76
				$title = $this->default_title;
77
			}
78
79
			/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
80
			$title = apply_filters( 'widget_title', $title );
81
82
			echo $args['before_widget'];
83 View Code Duplication
			if ( ! empty( $title ) ) {
84
				echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
85
			}
86
			echo '<div id="google_translate_element"></div>';
87
			echo $args['after_widget'];
88
			self::$instance = $instance;
89
			/** This action is documented in modules/widgets/gravatar-profile.php */
90
			do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
91
		}
92
	}
93
94
	/**
95
	 * Widget form in the dashboard.
96
	 *
97
	 * @see WP_Widget::form()
98
	 *
99
	 * @param array $instance Previously saved values from database.
100
	 */
101
	public function form( $instance ) {
102
		$title = isset( $instance['title'] ) ? $instance['title'] : false;
103
		if ( false === $title ) {
104
			$title = $this->default_title;
105
		}
106
		?>
107
<p>
108
	<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
109
	<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
110
</p>
111
		<?php
112
	}
113
114
	/**
115
	 * Sanitize widget form values as they are saved.
116
	 *
117
	 * @see WP_Widget::update()
118
	 *
119
	 * @param array $new_instance Values just sent to be saved.
120
	 * @param array $old_instance Previously saved values from database.
121
	 *
122
	 * @return array $instance Updated safe values to be saved.
123
	 */
124 View Code Duplication
	public function update( $new_instance, $old_instance ) {
125
		$instance = array();
126
		$instance['title'] = wp_kses( $new_instance['title'], array() );
127
		if ( $instance['title'] === $this->default_title ) {
128
			$instance['title'] = false; // Store as false in case of language change
129
		}
130
		return $instance;
131
	}
132
133
}
134
135
/**
136
 * Register the widget for use in Appearance -> Widgets.
137
 */
138
function jetpack_google_translate_widget_init() {
139
	register_widget( 'Jetpack_Google_Translate_Widget' );
140
}
141
add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
142