google-translate.php
| 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 | // This is a hack to show google translate bar a bit lower. |
| 57 | wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Display the Widget. |
| 62 | * |
| 63 | * @see WP_Widget::widget() |
| 64 | * |
| 65 | * @param array $args Display arguments. |
| 66 | * @param array $instance The settings for the particular instance of the widget. |
| 67 | */ |
| 68 | public function widget( $args, $instance ) { |
| 69 | // We never should show more than 1 instance of this. |
| 70 | if ( null === self::$instance ) { |
| 71 | $instance = wp_parse_args( $instance, array( |
| 72 | 'title' => $this->default_title, |
| 73 | ) ); |
| 74 | |
| 75 | /** |
| 76 | * Filter the layout of the Google Translate Widget. |
| 77 | * |
| 78 | * 3 different integers are accepted. |
| 79 | * 0 for the vertical layout. |
| 80 | * 1 for the horizontal layout. |
| 81 | * 2 for the dropdown only. |
| 82 | * |
| 83 | * @see https://translate.google.com/manager/website/ |
| 84 | * |
| 85 | * @module widgets |
| 86 | * |
| 87 | * @since 5.5.0 |
| 88 | * |
| 89 | * @param string $layout layout of the Google Translate Widget. |
| 90 | */ |
| 91 | $button_layout = apply_filters( 'jetpack_google_translate_widget_layout', 0 ); |
| 92 | |
| 93 | if ( |
| 94 | ! is_int( $button_layout ) |
| 95 | || 0 > $button_layout |
| 96 | || 2 < $button_layout |
| 97 | ) { |
| 98 | $button_layout = 0; |
| 99 | } |
| 100 | |
| 101 | wp_localize_script( |
| 102 | 'google-translate-init', |
| 103 | '_wp_google_translate_widget', |
| 104 | array( |
| 105 | 'lang' => get_locale(), |
| 106 | 'layout' => intval( $button_layout ), |
| 107 | ) |
| 108 | ); |
| 109 | wp_enqueue_script( 'google-translate-init' ); |
| 110 | wp_enqueue_script( 'google-translate' ); |
| 111 | |
| 112 | $title = $instance['title']; |
| 113 | |
| 114 | if ( ! isset( $title ) ) { |
| 115 | $title = $this->default_title; |
| 116 | } |
| 117 | |
| 118 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
| 119 | $title = apply_filters( 'widget_title', $title ); |
| 120 | |
| 121 | echo $args['before_widget']; |
| 122 | if ( ! empty( $title ) ) { |
| 123 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
| 124 | } |
| 125 | echo '<div id="google_translate_element"></div>'; |
| 126 | echo $args['after_widget']; |
| 127 | self::$instance = $instance; |
| 128 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 129 | do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Widget form in the dashboard. |
| 135 | * |
| 136 | * @see WP_Widget::form() |
| 137 | * |
| 138 | * @param array $instance Previously saved values from database. |
| 139 | */ |
| 140 | public function form( $instance ) { |
| 141 | $title = isset( $instance['title'] ) ? $instance['title'] : false; |
| 142 | if ( false === $title ) { |
| 143 | $title = $this->default_title; |
| 144 | } |
| 145 | ?> |
| 146 | <p> |
| 147 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
| 148 | <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 ); ?>" /> |
| 149 | </p> |
| 150 | <?php |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sanitize widget form values as they are saved. |
| 155 | * |
| 156 | * @see WP_Widget::update() |
| 157 | * |
| 158 | * @param array $new_instance Values just sent to be saved. |
| 159 | * @param array $old_instance Previously saved values from database. |
| 160 | * |
| 161 | * @return array $instance Updated safe values to be saved. |
| 162 | */ |
| 163 | public function update( $new_instance, $old_instance ) { |
| 164 | $instance = array(); |
| 165 | $instance['title'] = wp_kses( $new_instance['title'], array() ); |
| 166 | if ( $instance['title'] === $this->default_title ) { |
| 167 | $instance['title'] = false; // Store as false in case of language change |
| 168 | } |
| 169 | return $instance; |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Register the widget for use in Appearance -> Widgets. |
| 176 | */ |
| 177 | function jetpack_google_translate_widget_init() { |
| 178 | register_widget( 'Jetpack_Google_Translate_Widget' ); |
| 179 | } |
| 180 | add_action( 'widgets_init', 'jetpack_google_translate_widget_init' ); |
| 181 |