google-translate.php 5.7 KB

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