Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | |||
| 12 | class Google_Translate_Widget extends WP_Widget { |
||
| 13 | static $instance = null; |
||
|
0 ignored issues
–
show
|
|||
| 14 | |||
| 15 | /** |
||
| 16 | * Register widget with WordPress. |
||
| 17 | */ |
||
| 18 | function __construct() { |
||
| 19 | parent::__construct( |
||
| 20 | 'google_translate_widget', |
||
| 21 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 22 | apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ), |
||
| 23 | array( 'description' => __( 'Automatic translation of your site content', 'jetpack' ) ) |
||
| 24 | ); |
||
| 25 | wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) ); |
||
| 26 | wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', [ 'google-translate-init' ] ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Display the Widget. |
||
| 31 | * |
||
| 32 | * @see WP_Widget::widget() |
||
| 33 | * |
||
| 34 | * @param array $args Display arguments. |
||
| 35 | * @param array $instance The settings for the particular instance of the widget. |
||
| 36 | */ |
||
| 37 | public function widget( $args, $instance ) { |
||
| 38 | // We never should show more than 1 instance of this. |
||
| 39 | if ( null === self::$instance ) { |
||
| 40 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 41 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
| 42 | echo $args['before_widget']; |
||
| 43 | if ( ! empty( $title ) ) { |
||
| 44 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
||
| 45 | } |
||
| 46 | wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) ); |
||
| 47 | wp_enqueue_script( 'google-translate-init' ); |
||
| 48 | wp_enqueue_script( 'google-translate' ); |
||
| 49 | echo '<div id="google_translate_element"></div>'; |
||
| 50 | echo $args['after_widget']; |
||
| 51 | self::$instance = $instance; |
||
| 52 | // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. |
||
| 53 | // This is a hack to show google translate bar a bit lower. |
||
| 54 | if ( is_admin_bar_showing() ) { |
||
| 55 | echo '<style>.goog-te-banner-frame { top:32px !important } </style>'; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Widget form in the dashboard. |
||
| 62 | * |
||
| 63 | * @see WP_Widget::form() |
||
| 64 | * |
||
| 65 | * @param array $instance Previously saved values from database. |
||
| 66 | */ |
||
| 67 | public function form( $instance ) { |
||
| 68 | if ( isset( $instance['title'] ) ) { |
||
| 69 | $title = $instance['title']; |
||
| 70 | } else { |
||
| 71 | $title = ''; |
||
| 72 | } |
||
| 73 | ?> |
||
| 74 | <p> |
||
| 75 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
||
| 76 | <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 ); ?>" /> |
||
| 77 | </p> |
||
| 78 | <?php |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sanitize widget form values as they are saved. |
||
| 83 | * |
||
| 84 | * @see WP_Widget::update() |
||
| 85 | * |
||
| 86 | * @param array $new_instance Values just sent to be saved. |
||
| 87 | * @param array $old_instance Previously saved values from database. |
||
| 88 | * |
||
| 89 | * @return array $instance Updated safe values to be saved. |
||
| 90 | */ |
||
| 91 | public function update( $new_instance, $old_instance ) { |
||
| 92 | $instance = array(); |
||
| 93 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
||
| 94 | return $instance; |
||
| 95 | } |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Register the widget for use in Appearance -> Widgets. |
||
| 101 | */ |
||
| 102 | function jetpack_google_translate_widget_init() { |
||
| 103 | register_widget( 'Google_Translate_Widget' ); |
||
| 104 | } |
||
| 105 | add_action( 'widgets_init', 'jetpack_google_translate_widget_init' ); |
||
| 106 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.