Geen omschrijving

google-translate.php 5.8KB

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