Completed
Push — add/react-videopress-settings ( 1a2dcb...a2519f )
by
unknown
275:56 queued 263:52
created

Google_Translate_Widget   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A form() 0 13 2
A update() 0 5 2
B widget() 0 24 4
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
12
class Google_Translate_Widget extends WP_Widget {
13
	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...
14
15
	/**
16
	 * Register widget with WordPress.
17
	 */
18
	function __construct() {
19
		parent::__construct(
20
			'google_translate_widget',
21
			/** This filter is documented in modules/widgets/facebook-likebox.php */
22
			apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),
23
			array( 'description' => __( 'Automatic translation of your site content', 'jetpack' ) )
24
		);
25
		wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) );
26
		wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', [ 'google-translate-init' ] );
27
	}
28
29
	/**
30
	 * Display the Widget.
31
	 *
32
	 * @see WP_Widget::widget()
33
	 *
34
	 * @param array $args     Display arguments.
35
	 * @param array $instance The settings for the particular instance of the widget.
36
	 */
37
	public function widget( $args, $instance ) {
38
		// We never should show more than 1 instance of this.
39
		if ( null === self::$instance ) {
40
			/** This filter is documented in core/src/wp-includes/default-widgets.php */
41
			$title = apply_filters( 'widget_title', $instance['title'] );
42
			echo $args['before_widget'];
43
			if ( ! empty( $title ) ) {
44
				echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
45
			}
46
			wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
47
			wp_enqueue_script( 'google-translate-init' );
48
			wp_enqueue_script( 'google-translate' );
49
			echo '<div id="google_translate_element"></div>';
50
			echo $args['after_widget'];
51
			self::$instance = $instance;
52
			// Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
53
			// This is a hack to show google translate bar a bit lower.
54
			if ( is_admin_bar_showing() ) {
55
				echo '<style>.goog-te-banner-frame { top:32px !important } </style>';
56
			}
57
			/** This action is documented in modules/widgets/gravatar-profile.php */
58
			do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
59
		}
60
	}
61
62
	/**
63
	 * Widget form in the dashboard.
64
	 *
65
	 * @see WP_Widget::form()
66
	 *
67
	 * @param array $instance Previously saved values from database.
68
	 */
69
	public function form( $instance ) {
70
		if ( isset( $instance['title'] ) ) {
71
			$title = $instance['title'];
72
		} else {
73
			$title = '';
74
		}
75
		?>
76
<p>
77
	<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
78
	<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 ); ?>" />
79
</p>
80
		<?php
81
	}
82
83
	/**
84
	 * Sanitize widget form values as they are saved.
85
	 *
86
	 * @see WP_Widget::update()
87
	 *
88
	 * @param array $new_instance Values just sent to be saved.
89
	 * @param array $old_instance Previously saved values from database.
90
	 *
91
	 * @return array $instance Updated safe values to be saved.
92
	 */
93
	public function update( $new_instance, $old_instance ) {
94
		$instance = array();
95
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
96
		return $instance;
97
	}
98
99
}
100
101
/**
102
 * Register the widget for use in Appearance -> Widgets.
103
 */
104
function jetpack_google_translate_widget_init() {
105
	register_widget( 'Google_Translate_Widget' );
106
}
107
add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
108