import {
useRef,
useState,
useEffect,
createElement,
Fragment,
} from '@wordpress/element'
import { __ } from 'ct-i18n'
import saveAs from './file-saver'
import { Overlay } from 'blocksy-options'
import classnames from 'classnames'
import phpUnserialize from 'phpunserialize'
const CustomizerOptionsManager = () => {
const [futureConfig, setFutureConfig] = useState(null)
const [isCopyingOptions, setIsCopyingOptions] = useState(null)
const [isDraggedOver, setIsDraggerOver] = useState(false)
const [isExporting, setIsExporting] = useState(false)
const [isImporting, setIsImporting] = useState(false)
const [dataToExport, setDataToExport] = useState(['options'])
const inputRef = useRef()
const dropZoneRef = useRef()
useEffect(() => {
const onDragOver = (e) => {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
setIsDraggerOver(true)
}
const onDragLeave = (e) => {
e.stopPropagation()
e.preventDefault()
setIsDraggerOver(false)
}
const onDrop = (e) => {
e.stopPropagation()
e.preventDefault()
setIsDraggerOver(false)
const files = Array.from(e.dataTransfer.files || [])
const items = Array.from(e.dataTransfer.items || [])
if (items.length > 0) {
const futureConfig = e.dataTransfer.items[0].getAsFile()
setFutureConfig(futureConfig)
} else {
if (files.length > 0) {
setFutureConfig(files[0])
}
}
}
dropZoneRef.current.addEventListener('dragover', onDragOver, false)
dropZoneRef.current.addEventListener('dragleave', onDragLeave, false)
dropZoneRef.current.addEventListener('drop', onDrop, false)
return () => {
if (dropZoneRef.current) {
dropZoneRef.current.removeEventListener(
'dragover',
onDragOver,
false
)
dropZoneRef.current.removeEventListener(
'dragleave',
onDragLeave,
false
)
dropZoneRef.current.removeEventListener('drop', onDrop, false)
}
}
}, [])
return (
{__('Export Options', 'blocksy-companion')}
{__(
'Easily export the theme customizer settings.',
'blocksy-companion'
)}
{
e.preventDefault()
setIsExporting(true)
}}>
{__('Export Customizations', 'blocksy-companion')}
{__('Import Options', 'blocksy-companion')}
{__(
'Easily import the theme customizer settings.',
'blocksy-companion'
)}
{
inputRef.current.click()
}}>
{futureConfig
? futureConfig.name
: __(
'Click or drop to upload a file...',
'blocksy-companion'
)}
{
setFutureConfig(config)
}}
/>
{
e.preventDefault()
if (!futureConfig) {
return
}
setIsImporting(true)
var reader = new FileReader()
reader.readAsText(futureConfig, 'UTF-8')
reader.onload = function (evt) {
try {
fetch(
`${window.ajaxurl}?action=blocksy_customizer_import&wp_customize=on&nonce=${ct_customizer_localizations.customizer_reset_none}`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type':
'application/json',
},
body: JSON.stringify(
phpUnserialize(
evt.target.result
)
),
}
).then((response) => {
if (response.status === 200) {
response
.json()
.then(
({ success, data }) => {
location.reload()
}
)
}
})
} catch (e) {}
}
}}>
{isImporting ? (
) : (
__('Import Customizations', 'blocksy-companion')
)}
{ct_customizer_localizations.has_child_theme && (
{__('Copy Options', 'blocksy-companion')}
{__(
'Copy and import your customizations from parent or child theme.',
'blocksy-companion'
)}
{ct_customizer_localizations.is_parent_theme && (
{
e.preventDefault()
setIsCopyingOptions('child')
}}>
{__(
'Copy From Child Theme',
'blocksy-companion'
)}
)}
{!ct_customizer_localizations.is_parent_theme && (
{
e.preventDefault()
setIsCopyingOptions('parent')
}}>
{__(
'Copy From Parent Theme',
'blocksy-companion'
)}
)}
)}
setIsCopyingOptions(false)}
render={() => (
{!ct_customizer_localizations.is_parent_theme &&
__(
'Copy From Parent Theme',
'blocksy-companion'
)}
{ct_customizer_localizations.is_parent_theme &&
__(
'Copy From Child Theme',
'blocksy-companion'
)}
{!ct_customizer_localizations.is_parent_theme &&
__(
'You are about to copy all the settings from your parent theme into the child theme. Are you sure you want to continue?',
'blocksy-companion'
)}
{ct_customizer_localizations.is_parent_theme &&
__(
'You are about to copy all the settings from your child theme into the parent theme. Are you sure you want to continue?',
'blocksy-companion'
)}
{
e.preventDefault()
e.stopPropagation()
setIsCopyingOptions(false)
}}
className="button">
{__('Cancel', 'blocksy-companion')}
{
e.preventDefault()
const body = new FormData()
body.append(
'action',
'blocksy_customizer_copy_options'
)
body.append('wp_customize', 'on')
body.append('strategy', isCopyingOptions)
try {
fetch(window.ajaxurl, {
method: 'POST',
body,
}).then((response) => {
if (response.status === 200) {
response
.json()
.then(
({ success, data }) => {
location.reload()
}
)
}
})
} catch (e) {}
}}>
{__('Yes, I am sure', 'blocksy-companion')}
)}
/>
setIsExporting(false)}
render={() => (
{__('Export Settings', 'blocksy-companion')}
{__(
'Choose what set of settings you want to export.',
'blocksy-companion'
)}
{['options', 'widgets'].map((component) => (
{
if (
dataToExport.length === 1 &&
dataToExport[0] === component
) {
return
}
setDataToExport((dataToExport) =>
dataToExport.includes(component)
? dataToExport.filter(
(c) => c !== component
)
: [...dataToExport, component]
)
}}>
{
{
options: __(
'Customizer settings',
'blocksy-companion'
),
widgets: __(
'Widgets settings',
'blocksy-companion'
),
}[component]
}
))}
{
e.preventDefault()
e.stopPropagation()
setIsExporting(false)
}}
className="button">
{__('Cancel', 'blocksy-companion')}
{
e.preventDefault()
const body = new FormData()
body.append(
'action',
'blocksy_customizer_export'
)
body.append(
'strategy',
dataToExport.join(':')
)
body.append('wp_customize', 'on')
try {
fetch(window.ajaxurl, {
method: 'POST',
body,
}).then((response) => {
if (response.status === 200) {
response
.json()
.then(
({ success, data }) => {
if (!success) {
return
}
var blob = new Blob(
[data.data],
{
type: 'application/octet-stream;charset=utf-8',
}
)
saveAs(
blob,
`${data.site_url
.replace(
'http://',
''
)
.replace(
'https://',
''
)
.replace(
'.',
'-'
)
.replace(
'/',
'-'
)}-export.dat`
)
setIsExporting(
false
)
}
)
}
})
} catch (e) {}
}}>
{__('Export', 'blocksy-companion')}
)}
/>
)
}
export default CustomizerOptionsManager