PluginProbe
Polylang / 2.8.4
Polylang v2.8.4
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.8.4, at js/widgets.js

101 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Adds a flag to the widgets filtered by a language.
3 *
4 * @package Polylang
5 */
6
7 jQuery(
8 function( $ ) {
9 var widgets_container, widgets_selector, flags;
10
11 if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) {
12 flags = pll_widgets.flags;
13 }
14
15 /**
16 * Prepend widget titles with a flag once a language is selected.
17 *
18 * @param {object} widget The widget element.
19 * @return {void} Nothing.
20 */
21 function add_flag( widget ) {
22 if ( ! flags ) {
23 return;
24 }
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
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 // See the comment above about the icon which is safe. So it is also safe to prepend flag which uses icon.
39 title.prepend( flag ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.prepend
40 }
41 } else {
42 $( '.pll-lang', title ).remove();
43 }
44 }
45
46 if ( 'undefined' !== typeof wp.customize ) {
47
48 widgets_container = $( '#customize-controls' );
49 widgets_selector = '.customize-control .widget';
50
51 /**
52 * WP Customizer add control listener.
53 *
54 * @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading
55 *
56 * @param {object} control The control type.
57 * @return {void} Nothing.
58 */
59 function customize_add_flag( control ) {
60 if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) {
61 return;
62 }
63
64 /*
65 * Make sure the widget's contents are embedded; normally this is done
66 * when the control is expanded, for DOM performance reasons.
67 */
68 control.embedWidgetContent();
69
70 // Now we know for sure the widget is fully embedded.
71 add_flag( control.container.find( '.widget' ) );
72 }
73 wp.customize.control.each( customize_add_flag );
74 wp.customize.control.bind( 'add', customize_add_flag );
75
76 } else {
77
78 widgets_container = $( '#widgets-right' );
79 widgets_selector = '.widget';
80
81 }
82
83 // Add flags on load.
84 $( widgets_selector, widgets_container ).each(
85 function() {
86 add_flag( this );
87 }
88 );
89
90 // Update flags.
91 widgets_container.on(
92 'change',
93 '.pll-lang-choice',
94 function() {
95 add_flag( $( this ).parents( '.widget' ) );
96 }
97 );
98
99 }
100 );
101