Completed
Push — branch-4.5 ( 26b6a5...520ed8 )
by
unknown
326:11 queued 319:07
created

Google_Translate_Widget::widget()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 3
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 5
nop 2
dl 3
loc 27
rs 8.5806
c 0
b 0
f 0
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 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
			wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
66
			wp_enqueue_script( 'google-translate-init' );
67
			wp_enqueue_script( 'google-translate' );
68
69
			$title = $instance['title'];
70
71
			if ( false === $title ) {
72
				$title = $this->default_title;
73
			}
74
75
			/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
76
			$title = apply_filters( 'widget_title', $title );
77
78
			echo $args['before_widget'];
79 View Code Duplication
			if ( ! empty( $title ) ) {
80
				echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
81
			}
82
			echo '<div id="google_translate_element"></div>';
83
			echo $args['after_widget'];
84
			self::$instance = $instance;
85
			/** This action is documented in modules/widgets/gravatar-profile.php */
86
			do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
87
		}
88
	}
89
90
	/**
91
	 * Widget form in the dashboard.
92
	 *
93
	 * @see WP_Widget::form()
94
	 *
95
	 * @param array $instance Previously saved values from database.
96
	 */
97
	public function form( $instance ) {
98
		$title = isset( $instance['title'] ) ? $instance['title'] : false;
99
		if ( false === $title ) {
100
			$title = $this->default_title;
101
		}
102
		?>
103
<p>
104
	<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
105
	<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 ); ?>" />
106
</p>
107
		<?php
108
	}
109
110
	/**
111
	 * Sanitize widget form values as they are saved.
112
	 *
113
	 * @see WP_Widget::update()
114
	 *
115
	 * @param array $new_instance Values just sent to be saved.
116
	 * @param array $old_instance Previously saved values from database.
117
	 *
118
	 * @return array $instance Updated safe values to be saved.
119
	 */
120 View Code Duplication
	public function update( $new_instance, $old_instance ) {
121
		$instance = array();
122
		$instance['title'] = wp_kses( $new_instance['title'], array() );
123
		if ( $instance['title'] === $this->default_title ) {
124
			$instance['title'] = false; // Store as false in case of language change
125
		}
126
		return $instance;
127
	}
128
129
}
130
131
/**
132
 * Register the widget for use in Appearance -> Widgets.
133
 */
134
function jetpack_google_translate_widget_init() {
135
	register_widget( 'Google_Translate_Widget' );
136
}
137
add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
138