PluginProbe
Polylang / 2.7.1
Polylang v2.7.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / js / widgets.js

widgets.js in Polylang 2.7.1, at js/widgets.js

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