| 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 '@library/components/LibraryAccessModal' |
| 7 |
import { CtaButton, MainButtonWrapper } from '@library/components/MainButtons' |
| 8 |
import { brandMark } from '@library/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 |
const finish = window?.wp?.data?.subscribe(() => { |
| 22 |
requestAnimationFrame(() => { |
| 23 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 24 |
return |
| 25 |
} |
| 26 |
if (document.getElementById('extendify-templates-inserter')) { |
| 27 |
return |
| 28 |
} |
| 29 |
if ( |
| 30 |
!document.querySelector('.edit-post-header-toolbar') && |
| 31 |
!document.querySelector('.edit-site-header_start') // FSE |
| 32 |
) { |
| 33 |
return |
| 34 |
} |
| 35 |
const buttonContainer = Object.assign(document.createElement('div'), { |
| 36 |
id: 'extendify-templates-inserter', |
| 37 |
}) |
| 38 |
// Standard block editor |
| 39 |
document |
| 40 |
.querySelector('.edit-post-header-toolbar') |
| 41 |
?.append(buttonContainer) |
| 42 |
// FSE block editor |
| 43 |
document |
| 44 |
.querySelector('.edit-site-header_start') |
| 45 |
?.append(buttonContainer) |
| 46 |
|
| 47 |
render(<MainButtonWrapper />, buttonContainer) |
| 48 |
|
| 49 |
if (!isLibraryEnabled()) { |
| 50 |
document |
| 51 |
.getElementById('extendify-templates-inserter-btn') |
| 52 |
.classList.add('hidden') |
| 53 |
} |
| 54 |
finish() |
| 55 |
}) |
| 56 |
}) |
| 57 |
|
| 58 |
// The CTA button inside patterns |
| 59 |
const finish2 = window?.wp?.data?.subscribe(() => { |
| 60 |
requestAnimationFrame(() => { |
| 61 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 62 |
return |
| 63 |
} |
| 64 |
if (!document.querySelector('[id$=patterns-view]')) { |
| 65 |
return |
| 66 |
} |
| 67 |
if (document.getElementById('extendify-cta-button')) { |
| 68 |
return |
| 69 |
} |
| 70 |
const ctaButtonContainer = Object.assign( |
| 71 |
document.createElement('div'), |
| 72 |
{ id: 'extendify-cta-button-container' }, |
| 73 |
) |
| 74 |
|
| 75 |
document |
| 76 |
.querySelector('[id$=patterns-view]') |
| 77 |
.prepend(ctaButtonContainer) |
| 78 |
render(<CtaButton />, ctaButtonContainer) |
| 79 |
|
| 80 |
finish2() |
| 81 |
}) |
| 82 |
}) |
| 83 |
|
| 84 |
// This will add a button to enable or disable the library button |
| 85 |
const LibraryEnableDisable = () => { |
| 86 |
function setOpenSiteSettingsModal() { |
| 87 |
const util = document.getElementById('extendify-util') |
| 88 |
render(<LibraryAccessModal />, util) |
| 89 |
} |
| 90 |
|
| 91 |
return ( |
| 92 |
<> |
| 93 |
<PluginSidebarMoreMenuItem |
| 94 |
onClick={setOpenSiteSettingsModal} |
| 95 |
icon={<Icon icon={brandMark} size={24} />}> |
| 96 |
{' '} |
| 97 |
{__('Extendify', 'extendify')} |
| 98 |
</PluginSidebarMoreMenuItem> |
| 99 |
</> |
| 100 |
) |
| 101 |
} |
| 102 |
|
| 103 |
// Load this button always, which is used to enable or disable |
| 104 |
// FSE doesn't seem to recognize registerPlugin (yet) |
| 105 |
try { |
| 106 |
registerPlugin('extendify-settings-enable-disable', { |
| 107 |
render: LibraryEnableDisable, |
| 108 |
}) |
| 109 |
} catch (e) { |
| 110 |
console.error( |
| 111 |
'registerPlugin not supported? (error handled gracefully)', |
| 112 |
e.message, |
| 113 |
) |
| 114 |
} |
| 115 |
|