Jetpack_Google_Translate_Widget

The WordPress Core Jetpack Google Translate Widget class.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/google-translate.php  
  1. class Jetpack_Google_Translate_Widget extends WP_Widget { 
  2. static $instance = null; 
  3.  
  4. /** 
  5. * Default widget title. 
  6. * @var string $default_title 
  7. */ 
  8. var $default_title; 
  9.  
  10. /** 
  11. * Register widget with WordPress. 
  12. */ 
  13. function __construct() { 
  14. parent::__construct( 
  15. 'google_translate_widget',  
  16. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  17. apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),  
  18. array( 
  19. 'description' => __( 'Provide your readers with the option to translate your site into their preferred language.', 'jetpack' ),  
  20. 'customize_selective_refresh' => true 
  21. ); 
  22. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  23.  
  24. $this->default_title = esc_html__( 'Translate', 'jetpack' ); 
  25.  
  26. /** 
  27. * Enqueue frontend JS scripts. 
  28. */ 
  29. public function enqueue_scripts() { 
  30. wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) ); 
  31. wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) ); 
  32. // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. 
  33. // This is a hack to show google translate bar a bit lower. 
  34. wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' ); 
  35.  
  36. /** 
  37. * Display the Widget. 
  38. * @see WP_Widget::widget() 
  39. * @param array $args Display arguments. 
  40. * @param array $instance The settings for the particular instance of the widget. 
  41. */ 
  42. public function widget( $args, $instance ) { 
  43. // We never should show more than 1 instance of this. 
  44. if ( null === self::$instance ) { 
  45. $instance = wp_parse_args( $instance, array( 
  46. 'title' => $this->default_title,  
  47. ) ); 
  48.  
  49. wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) ); 
  50. wp_enqueue_script( 'google-translate-init' ); 
  51. wp_enqueue_script( 'google-translate' ); 
  52.  
  53. $title = $instance['title']; 
  54.  
  55. if ( ! isset( $title ) ) { 
  56. $title = $this->default_title; 
  57.  
  58. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  59. $title = apply_filters( 'widget_title', $title ); 
  60.  
  61. echo $args['before_widget']; 
  62. if ( ! empty( $title ) ) { 
  63. echo $args['before_title'] . esc_html( $title ) . $args['after_title']; 
  64. echo '<div id="google_translate_element"></div>'; 
  65. echo $args['after_widget']; 
  66. self::$instance = $instance; 
  67. /** This action is documented in modules/widgets/gravatar-profile.php */ 
  68. do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); 
  69.  
  70. /** 
  71. * Widget form in the dashboard. 
  72. * @see WP_Widget::form() 
  73. * @param array $instance Previously saved values from database. 
  74. */ 
  75. public function form( $instance ) { 
  76. $title = isset( $instance['title'] ) ? $instance['title'] : false; 
  77. if ( false === $title ) { 
  78. $title = $this->default_title; 
  79. ?> 
  80. <p> 
  81. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> 
  82. <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 ); ?>" /> 
  83. </p> 
  84. <?php 
  85.  
  86. /** 
  87. * Sanitize widget form values as they are saved. 
  88. * @see WP_Widget::update() 
  89. * @param array $new_instance Values just sent to be saved. 
  90. * @param array $old_instance Previously saved values from database. 
  91. * @return array $instance Updated safe values to be saved. 
  92. */ 
  93. public function update( $new_instance, $old_instance ) { 
  94. $instance = array(); 
  95. $instance['title'] = wp_kses( $new_instance['title'], array() ); 
  96. if ( $instance['title'] === $this->default_title ) { 
  97. $instance['title'] = false; // Store as false in case of language change 
  98. return $instance; 
  99.