PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.3
Jetpack – WP Security, Backup, Speed, & Growth v10.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 in Jetpack – WP Security, Backup, Speed, & Growth 10.3, at modules/widgets/google-translate.php

207 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Automattic\Jetpack\Assets;
4
5 /**
6 * Plugin Name: Google Translate Widget for WordPress.com
7 * Plugin URI: https://automattic.com
8 * Description: Add a widget for automatic translation
9 * Author: Artur Piszek
10 * Version: 0.1
11 * Author URI: https://automattic.com
12 * Text Domain: jetpack
13 */
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 class Jetpack_Google_Translate_Widget extends WP_Widget {
19 static $instance = null;
20
21 /**
22 * Default widget title.
23 *
24 * @var string $default_title
25 */
26 var $default_title;
27
28 /**
29 * Register widget with WordPress.
30 */
31 function __construct() {
32 parent::__construct(
33 'google_translate_widget',
34 /** This filter is documented in modules/widgets/facebook-likebox.php */
35 apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),
36 array(
37 'description' => __( 'Provide your readers with the option to translate your site into their preferred language.', 'jetpack' ),
38 'customize_selective_refresh' => true,
39 )
40 );
41 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
42
43 $this->default_title = esc_html__( 'Translate', 'jetpack' );
44 }
45
46 /**
47 * Enqueue frontend JS scripts.
48 */
49 public function enqueue_scripts() {
50 wp_register_script(
51 'google-translate-init',
52 Assets::get_file_url_for_environment(
53 '_inc/build/widgets/google-translate/google-translate.min.js',
54 'modules/widgets/google-translate/google-translate.js'
55 )
56 );
57 wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) );
58 // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
59 // Overwrite position of body.admin-bar
60 // This is a hack to show google translate bar a bit lower.
61 $lowerTranslateBar = '
62 .admin-bar {
63 position: inherit !important;
64 top: auto !important;
65 }
66 .admin-bar .goog-te-banner-frame {
67 top: 32px !important
68 }
69 @media screen and (max-width: 782px) {
70 .admin-bar .goog-te-banner-frame {
71 top: 46px !important;
72 }
73 }
74 @media screen and (max-width: 480px) {
75 .admin-bar .goog-te-banner-frame {
76 position: absolute;
77 }
78 }
79 ';
80 wp_add_inline_style( 'admin-bar', $lowerTranslateBar );
81 wp_add_inline_style( 'wpcom-admin-bar', $lowerTranslateBar );
82 }
83
84 /**
85 * Display the Widget.
86 *
87 * @see WP_Widget::widget()
88 *
89 * @param array $args Display arguments.
90 * @param array $instance The settings for the particular instance of the widget.
91 */
92 public function widget( $args, $instance ) {
93 // We never should show more than 1 instance of this.
94 if ( null === self::$instance ) {
95 $instance = wp_parse_args(
96 $instance, array(
97 'title' => $this->default_title,
98 )
99 );
100
101 /**
102 * Filter the layout of the Google Translate Widget.
103 *
104 * 3 different integers are accepted.
105 * 0 for the vertical layout.
106 * 1 for the horizontal layout.
107 * 2 for the dropdown only.
108 *
109 * @see https://translate.google.com/manager/website/
110 *
111 * @module widgets
112 *
113 * @since 5.5.0
114 *
115 * @param string $layout layout of the Google Translate Widget.
116 */
117 $button_layout = apply_filters( 'jetpack_google_translate_widget_layout', 0 );
118
119 if (
120 ! is_int( $button_layout )
121 || 0 > $button_layout
122 || 2 < $button_layout
123 ) {
124 $button_layout = 0;
125 }
126
127 wp_localize_script(
128 'google-translate-init',
129 '_wp_google_translate_widget',
130 array(
131 'lang' => get_locale(),
132 'layout' => (int) $button_layout,
133 )
134 );
135 wp_enqueue_script( 'google-translate-init' );
136 wp_enqueue_script( 'google-translate' );
137
138 $title = $instance['title'];
139
140 if ( ! isset( $title ) ) {
141 $title = $this->default_title;
142 }
143
144 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
145 $title = apply_filters( 'widget_title', $title );
146
147 echo $args['before_widget'];
148 if ( ! empty( $title ) ) {
149 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
150 }
151 echo '<div id="google_translate_element"></div>';
152 echo $args['after_widget'];
153 self::$instance = $instance;
154 /** This action is documented in modules/widgets/gravatar-profile.php */
155 do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
156 }
157 }
158
159 /**
160 * Widget form in the dashboard.
161 *
162 * @see WP_Widget::form()
163 *
164 * @param array $instance Previously saved values from database.
165 */
166 public function form( $instance ) {
167 $title = isset( $instance['title'] ) ? $instance['title'] : false;
168 if ( false === $title ) {
169 $title = $this->default_title;
170 }
171 ?>
172 <p>
173 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
174 <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 ); ?>" />
175 </p>
176 <?php
177 }
178
179 /**
180 * Sanitize widget form values as they are saved.
181 *
182 * @see WP_Widget::update()
183 *
184 * @param array $new_instance Values just sent to be saved.
185 * @param array $old_instance Previously saved values from database.
186 *
187 * @return array $instance Updated safe values to be saved.
188 */
189 public function update( $new_instance, $old_instance ) {
190 $instance = array();
191 $instance['title'] = wp_kses( $new_instance['title'], array() );
192 if ( $instance['title'] === $this->default_title ) {
193 $instance['title'] = false; // Store as false in case of language change
194 }
195 return $instance;
196 }
197
198 }
199
200 /**
201 * Register the widget for use in Appearance -> Widgets.
202 */
203 function jetpack_google_translate_widget_init() {
204 register_widget( 'Jetpack_Google_Translate_Widget' );
205 }
206 add_action( 'widgets_init', 'jetpack_google_translate_widget_init' );
207