| 1 |
import { PluginSidebarMoreMenuItem } from '@wordpress/edit-post' |
| 2 |
import { render } from '@wordpress/element' |
| 3 |
import { __ } from '@wordpress/i18n' |
| 4 |
import { Icon } from '@wordpress/icons' |
| 5 |
import { registerPlugin } from '@wordpress/plugins' |
| 6 |
import LibraryAccessModal from '@extendify/components/LibraryAccessModal' |
| 7 |
import { CtaButton, MainButton } from '@extendify/components/MainButtons' |
| 8 |
import { brandMark } from '@extendify/components/icons/' |
| 9 |
|
| 10 |
const userState = window.extendifyData?.user?.state |
| 11 |
const isAdmin = () => window.extendifyData.user === null || userState?.isAdmin |
| 12 |
const isGlobalLibraryEnabled = () => |
| 13 |
window.extendifyData.sitesettings === null || |
| 14 |
window.extendifyData?.sitesettings?.state?.enabled |
| 15 |
const isLibraryEnabled = () => |
| 16 |
window.extendifyData.user === null |
| 17 |
? isGlobalLibraryEnabled() |
| 18 |
: userState?.enabled |
| 19 |
|
| 20 |
// Add the MAIN button when Gutenberg is available and ready |
| 21 |
if (window._wpLoadBlockEditor) { |
| 22 |
const finish = window.wp.data.subscribe(() => { |
| 23 |
requestAnimationFrame(() => { |
| 24 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 25 |
return |
| 26 |
} |
| 27 |
if (document.getElementById('extendify-templates-inserter')) { |
| 28 |
return |
| 29 |
} |
| 30 |
if (!document.querySelector('.edit-post-header-toolbar')) { |
| 31 |
return |
| 32 |
} |
| 33 |
const buttonContainer = Object.assign( |
| 34 |
document.createElement('div'), |
| 35 |
{ id: 'extendify-templates-inserter' }, |
| 36 |
) |
| 37 |
document |
| 38 |
.querySelector('.edit-post-header-toolbar') |
| 39 |
.append(buttonContainer) |
| 40 |
render(<MainButton />, buttonContainer) |
| 41 |
|
| 42 |
if (!isLibraryEnabled()) { |
| 43 |
document |
| 44 |
.getElementById('extendify-templates-inserter-btn') |
| 45 |
.classList.add('invisible') |
| 46 |
} |
| 47 |
finish() |
| 48 |
}) |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
// The CTA button inside patterns |
| 53 |
if (window._wpLoadBlockEditor) { |
| 54 |
window.wp.data.subscribe(() => { |
| 55 |
requestAnimationFrame(() => { |
| 56 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 57 |
return |
| 58 |
} |
| 59 |
if (!document.querySelector('[id$=patterns-view]')) { |
| 60 |
return |
| 61 |
} |
| 62 |
if (document.getElementById('extendify-cta-button')) { |
| 63 |
return |
| 64 |
} |
| 65 |
const ctaButtonContainer = Object.assign( |
| 66 |
document.createElement('div'), |
| 67 |
{ id: 'extendify-cta-button-container' }, |
| 68 |
) |
| 69 |
|
| 70 |
document |
| 71 |
.querySelector('[id$=patterns-view]') |
| 72 |
.prepend(ctaButtonContainer) |
| 73 |
render(<CtaButton />, ctaButtonContainer) |
| 74 |
}) |
| 75 |
}) |
| 76 |
} |
| 77 |
|
| 78 |
// This will add a button to enable or disable the library button |
| 79 |
const LibraryEnableDisable = () => { |
| 80 |
function setOpenSiteSettingsModal() { |
| 81 |
const util = document.getElementById('extendify-util') |
| 82 |
render(<LibraryAccessModal />, util) |
| 83 |
} |
| 84 |
|
| 85 |
return ( |
| 86 |
<> |
| 87 |
<PluginSidebarMoreMenuItem |
| 88 |
onClick={setOpenSiteSettingsModal} |
| 89 |
icon={<Icon icon={brandMark} size={24} />}> |
| 90 |
{' '} |
| 91 |
{__('Extendify', 'extendify')} |
| 92 |
</PluginSidebarMoreMenuItem> |
| 93 |
</> |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
// Load this button always, which is used to enable or disable |
| 98 |
window._wpLoadBlockEditor && |
| 99 |
registerPlugin('extendify-settings-enable-disable', { |
| 100 |
render: LibraryEnableDisable, |
| 101 |
}) |
| 102 |
|