PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.4.3
Jetpack – WP Security, Backup, Speed, & Growth v7.4.3
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
204 lines 5.7 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 Jetpack_Google_Translate_Widget extends WP_Widget {
16 static $instance = null;
17
18 /**
19 * Default widget title.
20 *
21 * @var string $default_title
22 */
23 var $default_title;
24
25 /**
26 * Register widget with WordPress.
27 */
28 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(
48 'google-translate-init',
49 Jetpack::get_file_url_for_environment(
50 '_inc/build/widgets/google-translate/google-translate.min.js',
51 'modules/widgets/google-translate/google-translate.js'
52 )
53 );
54 wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) );
55 // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
56 // Overwrite position of body.admin-bar
57 // This is a hack to show google translate bar a bit lower.
58 $lowerTranslateBar = '
59 .admin-bar {
60 position: inherit !important;
61 top: auto !important;
62 }
63 .admin-bar .goog-te-banner-frame {
64 top: 32px !important
65 }
66 @media screen and (max-width: 782px) {
67 .admin-bar .goog-te-banner-frame {
68 top: 46px !important;
69 }
70 }
71 @media screen and (max-width: 480px) {
72 .admin-bar .goog-te-banner-frame {
73 position: absolute;
74 }
75 }
76 ';
77 wp_add_inline_style( 'admin-bar', $lowerTranslateBar );
78 wp_add_inline_style( 'wpcom-admin-bar', $lowerTranslateBar );
79 }
80
81 /**
82 * Display the Widget.
83 *
84 * @see WP_Widget::widget()
85 *
86 * @param array $args Display arguments.
87 * @param array $instance The settings for the particular instance of the widget.
88 */
89 public function widget( $args, $instance ) {
90 // We never should show more than 1 instance of this.
91 if ( null === self::$instance ) {
92 $instance = wp_parse_args(
93 $instance, array(
94 'title' => $this->default_title,
95 )
96 );
97
98 /**
99 * Filter the layout of the Google Translate Widget.
100 *
101 * 3 different integers are accepted.
102 * 0 for the vertical layout.
103 * 1 for the horizontal layout.
104 * 2 for the dropdown only.
105 *
106 * @see https://translate.google.com/manager/website/
107 *
108 * @module widgets
109 *
110 * @since 5.5.0
111 *
112 * @param string $layout layout of the Google Translate Widget.
113 */
114 $button_layout = apply_filters( 'jetpack_google_translate_widget_layout', 0 );
115
116 if (
117 ! is_int( $button_layout )
118 || 0 > $button_layout
119 || 2 < $button_layout
120 ) {
121 $button_layout = 0;
122 }
123
124 wp_localize_script(
125 'google-translate-init',
126 '_wp_google_translate_widget',
127 array(
128 'lang' => get_locale(),
129 'layout' => intval( $button_layout ),
130 )
131 );
132 wp_enqueue_script( 'google-translate-init' );
133 wp_enqueue_script( 'google-translate' );
134
135 $title = $instance['title'];
136
137 if ( ! isset( $title ) ) {
138 $title = $this->default_title;
139 }
140
141 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
142 $title = apply_filters( 'widget_title', $title );
143
144 echo $args['before_widget'];
145 if ( ! empty( $title ) ) {
146 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
147 }
148 echo '<div id="google_translate_element"></div>';
149 echo $args['after_widget'];
150 self::$instance = $instance;
151 /** This action is documented in modules/widgets/gravatar-profile.php */
152 do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
153 }
154 }
155
156 /**
157 * Widget form in the dashboard.
158 *
159 * @see WP_Widget::form()
160 *
161 * @param array $instance Previously saved values from database.
162 */
163 public function form( $instance ) {
164 $title = isset( $instance['title'] ) ? $instance['title'] : false;
165 if ( false === $title ) {
166 $title = $this->default_title;
167 }
168 ?>
169 <p>
170 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
171 <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 ); ?>" />
172 </p>
173 <?php
174 }
175
176 /**
177 * Sanitize widget form values as they are saved.
178 *
179 * @see WP_Widget::update()
180 *
181 * @param array $new_instance Values just sent to be saved.
182 * @param array $old_instance Previously saved values from database.
183 *
184 * @return array $instance Updated safe values to be saved.
185 */
186 public function update( $new_instance, $old_instance ) {
187 $instance = array();
188 $instance['title'] = wp_kses( $new_instance['title'], array() );
189 if ( $instance['title'] === $this->default_title ) {
190 $instance['title'] = false; // Store as false in case of language change
191 }
192 return $instance;
193 }
194
195 }
196
197 /**
198 * Register the widget for use in Appearance -> Widgets.
199 */
200 function jetpack_google_translate_widget_init() {
201 register_widget( 'Jetpack_Google_Translate_Widget' );
202 }
203 add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
204