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 Google_Translate_Widget extends WP_Widget { |
| 16 | static $instance = null; |
| 17 | |
| 18 | /** |
| 19 | * Register widget with WordPress. |
| 20 | */ |
| 21 | function __construct() { |
| 22 | parent::__construct( |
| 23 | 'google_translate_widget', |
| 24 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 25 | apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ), |
| 26 | array( |
| 27 | 'description' => __( 'Automatic translation of your site content', 'jetpack' ), |
| 28 | 'customize_selective_refresh' => true |
| 29 | ) |
| 30 | ); |
| 31 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Enqueue frontend JS scripts. |
| 36 | */ |
| 37 | public function enqueue_scripts() { |
| 38 | wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) ); |
| 39 | wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) ); |
| 40 | // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. |
| 41 | // This is a hack to show google translate bar a bit lower. |
| 42 | wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Display the Widget. |
| 47 | * |
| 48 | * @see WP_Widget::widget() |
| 49 | * |
| 50 | * @param array $args Display arguments. |
| 51 | * @param array $instance The settings for the particular instance of the widget. |
| 52 | */ |
| 53 | public function widget( $args, $instance ) { |
| 54 | // We never should show more than 1 instance of this. |
| 55 | if ( null === self::$instance ) { |
| 56 | wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) ); |
| 57 | wp_enqueue_script( 'google-translate-init' ); |
| 58 | wp_enqueue_script( 'google-translate' ); |
| 59 | |
| 60 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 61 | $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' ); |
| 62 | echo $args['before_widget']; |
| 63 | if ( ! empty( $title ) ) { |
| 64 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
| 65 | } |
| 66 | echo '<div id="google_translate_element"></div>'; |
| 67 | echo $args['after_widget']; |
| 68 | self::$instance = $instance; |
| 69 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 70 | do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Widget form in the dashboard. |
| 76 | * |
| 77 | * @see WP_Widget::form() |
| 78 | * |
| 79 | * @param array $instance Previously saved values from database. |
| 80 | */ |
| 81 | public function form( $instance ) { |
| 82 | if ( isset( $instance['title'] ) ) { |
| 83 | $title = $instance['title']; |
| 84 | } else { |
| 85 | $title = ''; |
| 86 | } |
| 87 | ?> |
| 88 | <p> |
| 89 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
| 90 | <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 ); ?>" /> |
| 91 | </p> |
| 92 | <?php |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sanitize widget form values as they are saved. |
| 97 | * |
| 98 | * @see WP_Widget::update() |
| 99 | * |
| 100 | * @param array $new_instance Values just sent to be saved. |
| 101 | * @param array $old_instance Previously saved values from database. |
| 102 | * |
| 103 | * @return array $instance Updated safe values to be saved. |
| 104 | */ |
| 105 | public function update( $new_instance, $old_instance ) { |
| 106 | $instance = array(); |
| 107 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : ''; |
| 108 | return $instance; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Register the widget for use in Appearance -> Widgets. |
| 115 | */ |
| 116 | function jetpack_google_translate_widget_init() { |
| 117 | register_widget( 'Google_Translate_Widget' ); |
| 118 | } |
| 119 | add_action( 'widgets_init', 'jetpack_google_translate_widget_init' ); |
| 120 |