| @@ -1,99 +1,83 @@ | ||
| 1 | -/** | |
| 2 | - * Adds a flag to the widgets filtered by a language. | |
| 3 | - * | |
| 4 | - * @package Polylang | |
| 5 | - */ | |
| 1 | +jQuery( function( $ ) { | |
| 2 | + var widgets_container, widgets_selector, flags; | |
| 6 | 3 | |
| 7 | -jQuery( | |
| 8 | - function( $ ) { | |
| 9 | - var widgets_container, widgets_selector, flags; | |
| 4 | + if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) { | |
| 5 | + flags = pll_widgets.flags; | |
| 6 | + } | |
| 10 | 7 | |
| 11 | - if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) { | |
| 12 | - flags = pll_widgets.flags; | |
| 8 | + /** | |
| 9 | + * Prepend widget titles with a flag once a language is selected. | |
| 10 | + * @param {object} widget The widget element. | |
| 11 | + * @return {void} Nothing. | |
| 12 | + */ | |
| 13 | + function add_flag( widget ) { | |
| 14 | + if ( ! flags ) { | |
| 15 | + return; | |
| 13 | 16 | } |
| 17 | + widget = $( widget ); | |
| 18 | + var title = $( '.widget-top .widget-title h3', widget ), | |
| 19 | + locale = $( '.pll-lang-choice option:selected', widget ).val(), | |
| 20 | + icon = ( locale && flags.hasOwnProperty( locale ) ) ? flags[ locale ] : null; | |
| 14 | 21 | |
| 22 | + if ( icon ) { | |
| 23 | + icon += ' '; | |
| 24 | + var current = $( '.pll-lang', title ); | |
| 25 | + if ( current.length ) { | |
| 26 | + current.html( icon ); | |
| 27 | + } else { | |
| 28 | + flag = '<span class="pll-lang">' + icon + '</span>'; | |
| 29 | + title.prepend( flag ); | |
| 30 | + } | |
| 31 | + } else { | |
| 32 | + $( '.pll-lang', title ).remove(); | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + if ( 'undefined' !== typeof wp.customize ) { | |
| 37 | + | |
| 38 | + widgets_container = $( '#customize-controls' ); | |
| 39 | + widgets_selector = '.customize-control .widget'; | |
| 40 | + | |
| 15 | 41 | /** |
| 16 | - * Prepend widget titles with a flag once a language is selected. | |
| 42 | + * WP Customizer add control listener. | |
| 17 | 43 | * |
| 18 | - * @param {object} widget The widget element. | |
| 44 | + * @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading | |
| 45 | + * | |
| 46 | + * @param {object} control The control type. | |
| 19 | 47 | * @return {void} Nothing. |
| 20 | 48 | */ |
| 21 | - function add_flag( widget ) { | |
| 22 | - if ( ! flags ) { | |
| 49 | + function customize_add_flag( control ) { | |
| 50 | + if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) { | |
| 23 | 51 | return; |
| 24 | 52 | } |
| 25 | - widget = $( widget ); | |
| 26 | - var title = $( '.widget-top .widget-title h3', widget ), | |
| 27 | - locale = $( '.pll-lang-choice option:selected', widget ).val(), | |
| 28 | - // Icon is HTML built and come from server side and is well escaped when necessary | |
| 29 | - icon = ( locale && flags.hasOwnProperty( locale ) ) ? flags[ locale ] : null; | |
| 30 | 53 | |
| 31 | - if ( icon ) { | |
| 32 | - icon += ' '; | |
| 33 | - var current = $( '.pll-lang', title ); | |
| 34 | - if ( current.length ) { | |
| 35 | - current.html( icon ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 36 | - } else { | |
| 37 | - flag = $( '<span />' ).addClass( 'pll-lang' ).html( icon ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 38 | - title.prepend( flag ); | |
| 39 | - } | |
| 40 | - } else { | |
| 41 | - $( '.pll-lang', title ).remove(); | |
| 42 | - } | |
| 43 | - } | |
| 44 | - | |
| 45 | - if ( 'undefined' !== typeof wp.customize ) { | |
| 46 | - | |
| 47 | - widgets_container = $( '#customize-controls' ); | |
| 48 | - widgets_selector = '.customize-control .widget'; | |
| 49 | - | |
| 50 | - /** | |
| 51 | - * WP Customizer add control listener. | |
| 52 | - * | |
| 53 | - * @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading | |
| 54 | - * | |
| 55 | - * @param {object} control The control type. | |
| 56 | - * @return {void} Nothing. | |
| 54 | + /* | |
| 55 | + * Make sure the widget's contents are embedded; normally this is done | |
| 56 | + * when the control is expanded, for DOM performance reasons. | |
| 57 | 57 | */ |
| 58 | - function customize_add_flag( control ) { | |
| 59 | - if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) { | |
| 60 | - return; | |
| 61 | - } | |
| 58 | + control.embedWidgetContent(); | |
| 62 | 59 | |
| 63 | - /* | |
| 64 | - * Make sure the widget's contents are embedded; normally this is done | |
| 65 | - * when the control is expanded, for DOM performance reasons. | |
| 66 | - */ | |
| 67 | - control.embedWidgetContent(); | |
| 60 | + // Now we know for sure the widget is fully embedded. | |
| 61 | + add_flag( control.container.find( '.widget' ) ); | |
| 62 | + } | |
| 63 | + wp.customize.control.each( customize_add_flag ); | |
| 64 | + wp.customize.control.bind( 'add', customize_add_flag ); | |
| 68 | 65 | |
| 69 | - // Now we know for sure the widget is fully embedded. | |
| 70 | - add_flag( control.container.find( '.widget' ) ); | |
| 71 | - } | |
| 72 | - wp.customize.control.each( customize_add_flag ); | |
| 73 | - wp.customize.control.bind( 'add', customize_add_flag ); | |
| 66 | + } else { | |
| 74 | 67 | |
| 75 | - } else { | |
| 68 | + widgets_container = $( '#widgets-right' ); | |
| 69 | + widgets_selector = '.widget'; | |
| 76 | 70 | |
| 77 | - widgets_container = $( '#widgets-right' ); | |
| 78 | - widgets_selector = '.widget'; | |
| 71 | + } | |
| 79 | 72 | |
| 80 | - } | |
| 73 | + // Add flags on load. | |
| 74 | + $( widgets_selector, widgets_container ).each( function() { | |
| 75 | + add_flag( this ); | |
| 76 | + } ); | |
| 81 | 77 | |
| 82 | - // Add flags on load. | |
| 83 | - $( widgets_selector, widgets_container ).each( | |
| 84 | - function() { | |
| 85 | - add_flag( this ); | |
| 86 | - } | |
| 87 | - ); | |
| 78 | + // Update flags. | |
| 79 | + widgets_container.on( 'change', '.pll-lang-choice', function() { | |
| 80 | + add_flag( $( this ).parents( '.widget' ) ); | |
| 81 | + } ); | |
| 88 | 82 | |
| 89 | - // Update flags. | |
| 90 | - widgets_container.on( | |
| 91 | - 'change', | |
| 92 | - '.pll-lang-choice', | |
| 93 | - function() { | |
| 94 | - add_flag( $( this ).parents( '.widget' ) ); | |
| 95 | - } | |
| 96 | - ); | |
| 97 | - | |
| 98 | - } | |
| 99 | -); | |
| 83 | +} ); | |