import { createElement, useEffect, useRef } from '@wordpress/element' const LegacyWidgetArea = ({ value, option, option: { sidebarId = 'ct-footer-sidebar-1' }, onChange, }) => { const parentEl = useRef() useEffect(() => { const sectionId = `widgetAreaSection-${sidebarId}` const widgetsToMove = Object.keys(wp.customize.control._value).filter( (id) => { if (id.indexOf('widget_') !== 0) { return false } return ( wp.customize.control(id).section() === `sidebar-widgets-${sidebarId}` ) } ) const Section = wp.customize.Section.extend({ containerParent: jQuery(parentEl.current), // containerPaneParent: parentEl.current collapse: function () {}, embed: function () { var section = this section.containerParent = wp.customize.ensure( section.containerParent ) var parentContainer = wp.customize.ensure( section.containerPaneParent ) if ( !section.contentContainer.parent().is(section.headContainer) ) { section.containerParent.append(section.contentContainer) section.contentContainer[0].classList.add('open') section.contentContainer[0] .querySelector( '.customize-section-description-container' ) .remove() } section.deferred.embedded.resolve() setTimeout(() => { widgetsToMove.map((control) => { wp.customize.control(control).embedWidgetControl() return console.log('here', control) setTimeout(() => { wp.customize .control(control) .container.one( 'click.toggle-widget-expanded', function toggleWidgetExpanded() { const widgetControl = wp.customHtmlWidgets.widgetControls[ control .replace('widget_', '') .replace(']', '') .replace('[', '-') ] if (widgetControl) { widgetControl.updateFields() wp.customize .control(control) .container.find('textarea') .change() } } ) const widgetControl = wp.customHtmlWidgets.widgetControls[ control .replace('widget_', '') .replace(']', '') .replace('[', '-') ] if (widgetControl) { widgetControl.updateFields() wp.customize .control(control) .container.find('textarea') .change() } jQuery(document).trigger('widget-added', [ wp.customize .control(control) .container.find('.widget:first'), ]) if (wp.mediaWidgets.widgetControls[control]) { jQuery( wp.mediaWidgets.widgetControls[ control ].el.closest('.widget') ).one( 'click.toggle-widget-expanded', function toggleWidgetExpanded() { var widgetContainer = $(this) wp.mediaWidgets.handleWidgetAdded( new jQuery.Event('widget-added'), widgetContainer ) } ) } }, 50) }) }) }, }) const section = new Section(sectionId, {}) wp.customize.section.add(section) const control = new wp.customize.controlConstructor.sidebar_widgets( 'checkcheck', { params: { sidebar_id: sidebarId, priority: 999, section: section.id, setting: `sidebars_widgets[${sidebarId}]`, content: `