| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
export function API_FETCH({ request }) { |
| 4 |
// create iframe with customizer url to prepare customizer data. |
| 5 |
async function maybeGetCustomizerData() { |
| 6 |
const promise = new Promise((resolve) => { |
| 7 |
const iframe = document.createElement('iframe'); |
| 8 |
iframe.style.display = 'none'; |
| 9 |
iframe.onload = () => { |
| 10 |
iframe.parentNode.removeChild(iframe); |
| 11 |
resolve(); |
| 12 |
}; |
| 13 |
iframe.src = `${window.GHOSTKIT.adminUrl}customize.php`; |
| 14 |
document.body.appendChild(iframe); |
| 15 |
}); |
| 16 |
|
| 17 |
return promise; |
| 18 |
} |
| 19 |
|
| 20 |
return apiFetch(request) |
| 21 |
.catch(async (fetchedData) => { |
| 22 |
// try to get customizer data. |
| 23 |
if ( |
| 24 |
fetchedData && |
| 25 |
fetchedData.error && |
| 26 |
fetchedData.error_code === 'no_options_found' |
| 27 |
) { |
| 28 |
await maybeGetCustomizerData(); |
| 29 |
return apiFetch(request); |
| 30 |
} |
| 31 |
|
| 32 |
return fetchedData; |
| 33 |
}) |
| 34 |
.catch((fetchedData) => { |
| 35 |
if ( |
| 36 |
fetchedData && |
| 37 |
fetchedData.error && |
| 38 |
fetchedData.error_code === 'no_options_found' |
| 39 |
) { |
| 40 |
return { |
| 41 |
response: {}, |
| 42 |
error: false, |
| 43 |
success: true, |
| 44 |
}; |
| 45 |
} |
| 46 |
|
| 47 |
return false; |
| 48 |
}) |
| 49 |
.then((fetchedData) => { |
| 50 |
if (fetchedData && fetchedData.success && fetchedData.response) { |
| 51 |
return fetchedData.response; |
| 52 |
} |
| 53 |
return {}; |
| 54 |
}); |
| 55 |
} |
| 56 |
|