Skip to content

Commit

Permalink
Merge pull request #5440 from Automattic/fix/widgets-translate-undefi…
Browse files Browse the repository at this point in the history
…ned-title-array-syntax

Widgets - Google Translate: change a PHP 5.4 array syntax, check if title is defined
  • Loading branch information
dereksmart committed Nov 8, 2016
2 parents 9d4b869 + ab5f6f3 commit 05c3cca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
36 changes: 24 additions & 12 deletions modules/widgets/google-translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Author URI: http://automattic.com
* Text Domain: jetpack
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class Google_Translate_Widget extends WP_Widget {
static $instance = null;
Expand All @@ -20,10 +23,23 @@ function __construct() {
'google_translate_widget',
/** This filter is documented in modules/widgets/facebook-likebox.php */
apply_filters( 'jetpack_widget_name', __( 'Google Translate', 'jetpack' ) ),
array( 'description' => __( 'Automatic translation of your site content', 'jetpack' ) )
array(
'description' => __( 'Automatic translation of your site content', 'jetpack' ),
'customize_selective_refresh' => true
)
);
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

/**
* Enqueue frontend JS scripts.
*/
public function enqueue_scripts() {
wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) );
wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', [ 'google-translate-init' ] );
wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) );
// Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
// This is a hack to show google translate bar a bit lower.
wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' );
}

/**
Expand All @@ -37,23 +53,19 @@ function __construct() {
public function widget( $args, $instance ) {
// We never should show more than 1 instance of this.
if ( null === self::$instance ) {
wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
wp_enqueue_script( 'google-translate-init' );
wp_enqueue_script( 'google-translate' );

/** This filter is documented in core/src/wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $instance['title'] );
$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
}
wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) );
wp_enqueue_script( 'google-translate-init' );
wp_enqueue_script( 'google-translate' );
echo '<div id="google_translate_element"></div>';
echo $args['after_widget'];
self::$instance = $instance;
// Admin bar is also displayed on top of the site which causes google translate bar to hide beneath.
// This is a hack to show google translate bar a bit lower.
if ( is_admin_bar_showing() ) {
echo '<style>.goog-te-banner-frame { top:32px !important } </style>';
}
/** This action is documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' );
}
Expand Down Expand Up @@ -92,7 +104,7 @@ public function form( $instance ) {
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
return $instance;
}

Expand Down
4 changes: 2 additions & 2 deletions modules/widgets/google-translate/google-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
function googleTranslateElementInit() {
var lang = 'en';
var langParam;
var langRegex = /[?&#]lang=([a-z]+)/;
var langRegex = /[?&#]lang=([a-zA-Z\-_]+)/;
if ( typeof _wp_google_translate_widget === 'object' && typeof _wp_google_translate_widget.lang === 'string' ) {
lang = _wp_google_translate_widget.lang;
}
langParam = window.location.href.match( langRegex );
if ( langParam ) {
window.location.href = window.location.href.replace( langRegex, '' ).replace( /#googtrans\([a-zA-Z|]+\)/, '' ) + '#googtrans(' + lang + '|' + langParam[ 1 ] + ')';
window.location.href = window.location.href.replace( langRegex, '' ).replace( /#googtrans\([a-zA-Z\-_|]+\)/, '' ) + '#googtrans(' + lang + '|' + langParam[ 1 ] + ')';
}
new google.translate.TranslateElement( {
pageLanguage: lang,
Expand Down

0 comments on commit 05c3cca

Please sign in to comment.