PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.4.5
Jetpack – WP Security, Backup, Speed, & Growth v4.4.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / google-translate.php
google-translate.php
120 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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;
17
18 /**
19 * Register widget with WordPress.
20 */
21 function __construct() {
22 parent::__construct(
23 'google_translate_widget',
24 /** This filter is documented in modules/widgets/facebook-likebox.php */
25 apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),
26 array(
27 'description' => __( 'Automatic translation of your site content', 'jetpack' ),
28 'customize_selective_refresh' => true
29 )
30 );
31 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
32 }
33
34 /**
35 * Enqueue frontend JS scripts.
36 */
37 public function enqueue_scripts() {
38 wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) );
39 wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) );
40 // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
41 // This is a hack to show google translate bar a bit lower.
42 wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' );
43 }
44
45 /**
46 * Display the Widget.
47 *
48 * @see WP_Widget::widget()
49 *
50 * @param array $args Display arguments.
51 * @param array $instance The settings for the particular instance of the widget.
52 */
53 public function widget( $args, $instance ) {
54 // We never should show more than 1 instance of this.
55 if ( null === self::$instance ) {
56 wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
57 wp_enqueue_script( 'google-translate-init' );
58 wp_enqueue_script( 'google-translate' );
59
60 /** This filter is documented in core/src/wp-includes/default-widgets.php */
61 $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
62 echo $args['before_widget'];
63 if ( ! empty( $title ) ) {
64 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
65 }
66 echo '<div id="google_translate_element"></div>';
67 echo $args['after_widget'];
68 self::$instance = $instance;
69 /** This action is documented in modules/widgets/gravatar-profile.php */
70 do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
71 }
72 }
73
74 /**
75 * Widget form in the dashboard.
76 *
77 * @see WP_Widget::form()
78 *
79 * @param array $instance Previously saved values from database.
80 */
81 public function form( $instance ) {
82 if ( isset( $instance['title'] ) ) {
83 $title = $instance['title'];
84 } else {
85 $title = '';
86 }
87 ?>
88 <p>
89 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
90 <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 ); ?>" />
91 </p>
92 <?php
93 }
94
95 /**
96 * Sanitize widget form values as they are saved.
97 *
98 * @see WP_Widget::update()
99 *
100 * @param array $new_instance Values just sent to be saved.
101 * @param array $old_instance Previously saved values from database.
102 *
103 * @return array $instance Updated safe values to be saved.
104 */
105 public function update( $new_instance, $old_instance ) {
106 $instance = array();
107 $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
108 return $instance;
109 }
110
111 }
112
113 /**
114 * Register the widget for use in Appearance -> Widgets.
115 */
116 function jetpack_google_translate_widget_init() {
117 register_widget( 'Google_Translate_Widget' );
118 }
119 add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
120