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( $instance, array( |
93
|
|
|
'title' => $this->default_title, |
94
|
|
|
) ); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Filter the layout of the Google Translate Widget. |
98
|
|
|
* |
99
|
|
|
* 3 different integers are accepted. |
100
|
|
|
* 0 for the vertical layout. |
101
|
|
|
* 1 for the horizontal layout. |
102
|
|
|
* 2 for the dropdown only. |
103
|
|
|
* |
104
|
|
|
* @see https://translate.google.com/manager/website/ |
105
|
|
|
* |
106
|
|
|
* @module widgets |
107
|
|
|
* |
108
|
|
|
* @since 5.5.0 |
109
|
|
|
* |
110
|
|
|
* @param string $layout layout of the Google Translate Widget. |
111
|
|
|
*/ |
112
|
|
|
$button_layout = apply_filters( 'jetpack_google_translate_widget_layout', 0 ); |
113
|
|
|
|
114
|
|
|
if ( |
115
|
|
|
! is_int( $button_layout ) |
116
|
|
|
|| 0 > $button_layout |
117
|
|
|
|| 2 < $button_layout |
118
|
|
|
) { |
119
|
|
|
$button_layout = 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
wp_localize_script( |
123
|
|
|
'google-translate-init', |
124
|
|
|
'_wp_google_translate_widget', |
125
|
|
|
array( |
126
|
|
|
'lang' => get_locale(), |
127
|
|
|
'layout' => intval( $button_layout ), |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
wp_enqueue_script( 'google-translate-init' ); |
131
|
|
|
wp_enqueue_script( 'google-translate' ); |
132
|
|
|
|
133
|
|
|
$title = $instance['title']; |
134
|
|
|
|
135
|
|
|
if ( ! isset( $title ) ) { |
136
|
|
|
$title = $this->default_title; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
140
|
|
|
$title = apply_filters( 'widget_title', $title ); |
141
|
|
|
|
142
|
|
|
echo $args['before_widget']; |
143
|
|
View Code Duplication |
if ( ! empty( $title ) ) { |
144
|
|
|
echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
145
|
|
|
} |
146
|
|
|
echo '<div id="google_translate_element"></div>'; |
147
|
|
|
echo $args['after_widget']; |
148
|
|
|
self::$instance = $instance; |
149
|
|
|
/** This action is documented in modules/widgets/gravatar-profile.php */ |
150
|
|
|
do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Widget form in the dashboard. |
156
|
|
|
* |
157
|
|
|
* @see WP_Widget::form() |
158
|
|
|
* |
159
|
|
|
* @param array $instance Previously saved values from database. |
160
|
|
|
*/ |
161
|
|
|
public function form( $instance ) { |
162
|
|
|
$title = isset( $instance['title'] ) ? $instance['title'] : false; |
163
|
|
|
if ( false === $title ) { |
164
|
|
|
$title = $this->default_title; |
165
|
|
|
} |
166
|
|
|
?> |
167
|
|
|
<p> |
168
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
169
|
|
|
<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 ); ?>" /> |
170
|
|
|
</p> |
171
|
|
|
<?php |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Sanitize widget form values as they are saved. |
176
|
|
|
* |
177
|
|
|
* @see WP_Widget::update() |
178
|
|
|
* |
179
|
|
|
* @param array $new_instance Values just sent to be saved. |
180
|
|
|
* @param array $old_instance Previously saved values from database. |
181
|
|
|
* |
182
|
|
|
* @return array $instance Updated safe values to be saved. |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function update( $new_instance, $old_instance ) { |
185
|
|
|
$instance = array(); |
186
|
|
|
$instance['title'] = wp_kses( $new_instance['title'], array() ); |
187
|
|
|
if ( $instance['title'] === $this->default_title ) { |
188
|
|
|
$instance['title'] = false; // Store as false in case of language change |
189
|
|
|
} |
190
|
|
|
return $instance; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Register the widget for use in Appearance -> Widgets. |
197
|
|
|
*/ |
198
|
|
|
function jetpack_google_translate_widget_init() { |
199
|
|
|
register_widget( 'Jetpack_Google_Translate_Widget' ); |
200
|
|
|
} |
201
|
|
|
add_action( 'widgets_init', 'jetpack_google_translate_widget_init' ); |
202
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.