Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widgets - Google Translate: change a PHP 5.4 array syntax, check if title is defined #5440

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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