import { __ } from '@wordpress/i18n' import { renderToString, render } from '@wordpress/element' import { registerPlugin } from '@wordpress/plugins' import { openModal } from './util/general' import { PluginSidebarMoreMenuItem } from '@wordpress/edit-post' import { Icon } from '@wordpress/icons' import { brandMark } from './components/icons/' import LibraryAccessModal from './components/LibraryAccessModal' const openLibrary = (event) => { openModal( event.target.closest('[data-extendify-identifier]')?.dataset ?.extendifyIdentifier, ) } // This returns true if the user object is null (Library never opened), or if it's enabled in the user settings const isAdmin = () => window.extendifyData.user === null || window.extendifyData?.user?.state?.isAdmin const isGlobalLibraryEnabled = () => window.extendifyData.sitesettings === null || window.extendifyData?.sitesettings?.state?.enabled const isLibraryEnabled = () => window.extendifyData.user === null ? isGlobalLibraryEnabled() : window.extendifyData?.user?.state?.enabled const mainButton = (
) // Add the MAIN button when Gutenberg is available and ready if (window._wpLoadBlockEditor) { const finish = window.wp.data.subscribe(() => { requestAnimationFrame(() => { if (!isGlobalLibraryEnabled() && !isAdmin()) { return } // Redundant extra check added because of a bug where the above check wasn't working if (document.getElementById('extendify-templates-inserter-btn')) { return } if (!document.querySelector('.edit-post-header-toolbar')) { return } document .querySelector('.edit-post-header-toolbar') .insertAdjacentHTML('beforeend', renderToString(mainButton)) document .getElementById('extendify-templates-inserter-btn') .addEventListener('click', openLibrary) if (!isLibraryEnabled()) { document .getElementById('extendify-templates-inserter-btn') .classList.add('invisible') } finish() }) }) } // The CTA button inside patterns if (window._wpLoadBlockEditor) { const finish = window.wp.data.subscribe(() => { requestAnimationFrame(() => { // Redundant extra check added because of a bug where the above check wasn't working if (!isGlobalLibraryEnabled() && !isAdmin()) { return } if (!document.querySelector('[id$=patterns-view]')) { return } if (document.getElementById('extendify-cta-button')) { return } const ctaButton = (
) document .querySelector('[id$=patterns-view]') .insertAdjacentHTML('afterbegin', renderToString(ctaButton)) document .getElementById('extendify-cta-button') .addEventListener('click', openLibrary) finish() }) }) } // This will add a button to enable or disable the library button const LibraryEnableDisable = () => { function setOpenSiteSettingsModal() { const util = document.getElementById('extendify-util') render(, util) } return ( <> }> {' '} {__('Extendify', 'extendify')} ) } // Load this button always, which is used to enable or disable window._wpLoadBlockEditor && registerPlugin('extendify-settings-enable-disable', { render: LibraryEnableDisable, })