| @@ -4,10 +4,13 @@ | ||
| 4 | 4 | * @package Polylang |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | jQuery( |
| 8 | - function( $ ) { | |
| 9 | - var widgets_container, widgets_selector, flags; | |
| 8 | + function ( $ ) { | |
| 9 | + var widgets_container, | |
| 10 | + widgets_selector, | |
| 11 | + flags, | |
| 12 | + isBlockEditor = 'undefined' !== typeof wp.blockEditor; | |
| 10 | 13 | |
| 11 | 14 | if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) { |
| 12 | 15 | flags = pll_widgets.flags; |
| 13 | 16 | } |
| @@ -22,9 +25,9 @@ | ||
| 22 | 25 | if ( ! flags ) { |
| 23 | 26 | return; |
| 24 | 27 | } |
| 25 | 28 | widget = $( widget ); |
| 26 | - var title = $( '.widget-top .widget-title h3', widget ), | |
| 29 | + var title = isBlockEditor ? widget.prev('h3') : $( '.widget-top .widget-title h3', widget ), | |
| 27 | 30 | locale = $( '.pll-lang-choice option:selected', widget ).val(), |
| 28 | 31 | // Icon is HTML built and come from server side and is well escaped when necessary |
| 29 | 32 | icon = ( locale && flags.hasOwnProperty( locale ) ) ? flags[ locale ] : null; |
| 30 | 33 | |
| @@ -42,57 +45,73 @@ | ||
| 42 | 45 | $( '.pll-lang', title ).remove(); |
| 43 | 46 | } |
| 44 | 47 | } |
| 45 | 48 | |
| 46 | - if ( 'undefined' !== typeof wp.customize ) { | |
| 49 | + if ( isBlockEditor ) { | |
| 47 | 50 | |
| 48 | - widgets_container = $( '#customize-controls' ); | |
| 49 | - widgets_selector = '.customize-control .widget'; | |
| 51 | + widgets_container = $( '.edit-widgets-main-block-list' ); | |
| 52 | + widgets_selector = '.widget'; | |
| 50 | 53 | |
| 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; | |
| 54 | + // Update flags when we click on the legacy widget to display its form. | |
| 55 | + widgets_container.on( | |
| 56 | + 'click', | |
| 57 | + '.wp-block-legacy-widget', | |
| 58 | + function () { | |
| 59 | + add_flag( $( this ).find( '.widget' ) ); | |
| 62 | 60 | } |
| 61 | + ); | |
| 63 | 62 | |
| 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(); | |
| 63 | + } else { | |
| 64 | + if ( 'undefined' !== typeof wp.customize ) { | |
| 69 | 65 | |
| 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 ); | |
| 66 | + widgets_container = $( '#customize-controls' ); | |
| 67 | + widgets_selector = '.customize-control .widget'; | |
| 75 | 68 | |
| 76 | - } else { | |
| 69 | + /** | |
| 70 | + * WP Customizer add control listener. | |
| 71 | + * | |
| 72 | + * @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading | |
| 73 | + * | |
| 74 | + * @param {object} control The control type. | |
| 75 | + * @return {void} Nothing. | |
| 76 | + */ | |
| 77 | + function customize_add_flag( control ) { | |
| 78 | + if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) { | |
| 79 | + return; | |
| 80 | + } | |
| 77 | 81 | |
| 78 | - widgets_container = $( '#widgets-right' ); | |
| 79 | - widgets_selector = '.widget'; | |
| 82 | + /* | |
| 83 | + * Make sure the widget's contents are embedded; normally this is done | |
| 84 | + * when the control is expanded, for DOM performance reasons. | |
| 85 | + */ | |
| 86 | + control.embedWidgetContent(); | |
| 80 | 87 | |
| 81 | - } | |
| 88 | + // Now we know for sure the widget is fully embedded. | |
| 89 | + add_flag( control.container.find( '.widget' ) ); | |
| 90 | + } | |
| 91 | + wp.customize.control.each( customize_add_flag ); | |
| 92 | + wp.customize.control.bind( 'add', customize_add_flag ); | |
| 82 | 93 | |
| 83 | - // Add flags on load. | |
| 84 | - $( widgets_selector, widgets_container ).each( | |
| 85 | - function() { | |
| 86 | - add_flag( this ); | |
| 94 | + } else { | |
| 95 | + | |
| 96 | + widgets_container = $( '#widgets-right' ); | |
| 97 | + widgets_selector = '.widget'; | |
| 98 | + | |
| 87 | 99 | } |
| 88 | - ); | |
| 89 | 100 | |
| 101 | + // Add flags on load. | |
| 102 | + $( widgets_selector, widgets_container ).each( | |
| 103 | + function () { | |
| 104 | + add_flag( this ); | |
| 105 | + } | |
| 106 | + ); | |
| 107 | + } | |
| 108 | + | |
| 90 | 109 | // Update flags. |
| 91 | 110 | widgets_container.on( |
| 92 | 111 | 'change', |
| 93 | 112 | '.pll-lang-choice', |
| 94 | - function() { | |
| 113 | + function () { | |
| 95 | 114 | add_flag( $( this ).parents( '.widget' ) ); |
| 96 | 115 | } |
| 97 | 116 | ); |
| 98 | 117 | |
| @@ -100,12 +119,12 @@ | ||
| 100 | 119 | test ? a.show() : a.hide(); |
| 101 | 120 | } |
| 102 | 121 | |
| 103 | 122 | // Remove all options if dropdown is checked. |
| 104 | - $( '.widgets-sortables,.control-section-sidebar' ).on( | |
| 123 | + $( '.widgets-sortables,.control-section-sidebar,.edit-widgets-main-block-list' ).on( | |
| 105 | 124 | 'change', |
| 106 | 125 | '.pll-dropdown', |
| 107 | - function() { | |
| 126 | + function () { | |
| 108 | 127 | var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 109 | 128 | pll_toggle( $( '.no-dropdown-' + this_id ), true != $( this ).prop( 'checked' ) ); |
| 110 | 129 | } |
| 111 | 130 | ); |
| @@ -113,13 +132,13 @@ | ||
| 113 | 132 | // Disallow unchecking both show names and show flags. |
| 114 | 133 | var options = ['-show_flags', '-show_names']; |
| 115 | 134 | $.each( |
| 116 | 135 | options, |
| 117 | - function( i, v ) { | |
| 118 | - $( '.widgets-sortables,.control-section-sidebar' ).on( | |
| 136 | + function ( i, v ) { | |
| 137 | + $( '.widgets-sortables,.control-section-sidebar,.edit-widgets-main-block-list' ).on( | |
| 119 | 138 | 'change', |
| 120 | 139 | '.pll' + v, |
| 121 | - function() { | |
| 140 | + function () { | |
| 122 | 141 | var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 123 | 142 | if ( true != $( this ).prop( 'checked' ) ) { |
| 124 | 143 | $( '#widget-' + this_id + options[ 1 - i ] ).prop( 'checked', true ); |
| 125 | 144 | } |