| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import { renderToString, render } from '@wordpress/element' |
| 3 |
import { registerPlugin } from '@wordpress/plugins' |
| 4 |
import { openModal } from './util/general' |
| 5 |
import { PluginSidebarMoreMenuItem } from '@wordpress/edit-post' |
| 6 |
import { Icon } from '@wordpress/icons' |
| 7 |
import { brandMark } from './components/icons/' |
| 8 |
import LibraryAccessModal from './components/LibraryAccessModal' |
| 9 |
|
| 10 |
const openLibrary = (event) => { |
| 11 |
openModal( |
| 12 |
event.target.closest('[data-extendify-identifier]')?.dataset |
| 13 |
?.extendifyIdentifier, |
| 14 |
) |
| 15 |
} |
| 16 |
|
| 17 |
// This returns true if the user object is null (Library never opened), or if it's enabled in the user settings |
| 18 |
const isAdmin = () => |
| 19 |
window.extendifyData.user === null || |
| 20 |
window.extendifyData?.user?.state?.isAdmin |
| 21 |
const isGlobalLibraryEnabled = () => |
| 22 |
window.extendifyData.sitesettings === null || |
| 23 |
window.extendifyData?.sitesettings?.state?.enabled |
| 24 |
const isLibraryEnabled = () => |
| 25 |
window.extendifyData.user === null |
| 26 |
? isGlobalLibraryEnabled() |
| 27 |
: window.extendifyData?.user?.state?.enabled |
| 28 |
|
| 29 |
const mainButton = ( |
| 30 |
<div id="extendify-templates-inserter" className="extendify"> |
| 31 |
<button |
| 32 |
style="padding:4px 12px; height: 34px;" |
| 33 |
type="button" |
| 34 |
data-extendify-identifier="main-button" |
| 35 |
id="extendify-templates-inserter-btn" |
| 36 |
className="components-button bg-wp-theme-500 hover:bg-wp-theme-600 border-color-wp-theme-500 text-white ml-1"> |
| 37 |
<Icon icon={brandMark} size={24} className="-ml-1 mr-1" /> |
| 38 |
{__('Library', 'extendify')} |
| 39 |
</button> |
| 40 |
</div> |
| 41 |
) |
| 42 |
|
| 43 |
// Add the MAIN button when Gutenberg is available and ready |
| 44 |
if (window._wpLoadBlockEditor) { |
| 45 |
const finish = window.wp.data.subscribe(() => { |
| 46 |
requestAnimationFrame(() => { |
| 47 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
// Redundant extra check added because of a bug where the above check wasn't working |
| 52 |
if (document.getElementById('extendify-templates-inserter-btn')) { |
| 53 |
return |
| 54 |
} |
| 55 |
if (!document.querySelector('.edit-post-header-toolbar')) { |
| 56 |
return |
| 57 |
} |
| 58 |
document |
| 59 |
.querySelector('.edit-post-header-toolbar') |
| 60 |
.insertAdjacentHTML('beforeend', renderToString(mainButton)) |
| 61 |
document |
| 62 |
.getElementById('extendify-templates-inserter-btn') |
| 63 |
.addEventListener('click', openLibrary) |
| 64 |
if (!isLibraryEnabled()) { |
| 65 |
document |
| 66 |
.getElementById('extendify-templates-inserter-btn') |
| 67 |
.classList.add('invisible') |
| 68 |
} |
| 69 |
finish() |
| 70 |
}) |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
// The CTA button inside patterns |
| 75 |
if (window._wpLoadBlockEditor) { |
| 76 |
const finish = window.wp.data.subscribe(() => { |
| 77 |
requestAnimationFrame(() => { |
| 78 |
// Redundant extra check added because of a bug where the above check wasn't working |
| 79 |
if (!isGlobalLibraryEnabled() && !isAdmin()) { |
| 80 |
return |
| 81 |
} |
| 82 |
if (!document.querySelector('[id$=patterns-view]')) { |
| 83 |
return |
| 84 |
} |
| 85 |
if (document.getElementById('extendify-cta-button')) { |
| 86 |
return |
| 87 |
} |
| 88 |
const ctaButton = ( |
| 89 |
<div> |
| 90 |
<button |
| 91 |
id="extendify-cta-button" |
| 92 |
style="margin:1rem 1rem 0;width: calc(100% - 2rem);justify-content: center;" |
| 93 |
data-extendify-identifier="patterns-cta" |
| 94 |
className="components-button is-secondary"> |
| 95 |
{__( |
| 96 |
'Discover patterns in Extendify Library', |
| 97 |
'extendify', |
| 98 |
)} |
| 99 |
</button> |
| 100 |
</div> |
| 101 |
) |
| 102 |
document |
| 103 |
.querySelector('[id$=patterns-view]') |
| 104 |
.insertAdjacentHTML('afterbegin', renderToString(ctaButton)) |
| 105 |
document |
| 106 |
.getElementById('extendify-cta-button') |
| 107 |
.addEventListener('click', openLibrary) |
| 108 |
finish() |
| 109 |
}) |
| 110 |
}) |
| 111 |
} |
| 112 |
|
| 113 |
// This will add a button to enable or disable the library button |
| 114 |
const LibraryEnableDisable = () => { |
| 115 |
function setOpenSiteSettingsModal() { |
| 116 |
const util = document.getElementById('extendify-util') |
| 117 |
render(<LibraryAccessModal />, util) |
| 118 |
} |
| 119 |
|
| 120 |
return ( |
| 121 |
<> |
| 122 |
<PluginSidebarMoreMenuItem |
| 123 |
onClick={setOpenSiteSettingsModal} |
| 124 |
icon={<Icon icon={brandMark} size={24} />}> |
| 125 |
{' '} |
| 126 |
{__('Extendify', 'extendify')} |
| 127 |
</PluginSidebarMoreMenuItem> |
| 128 |
</> |
| 129 |
) |
| 130 |
} |
| 131 |
|
| 132 |
// Load this button always, which is used to enable or disable |
| 133 |
window._wpLoadBlockEditor && |
| 134 |
registerPlugin('extendify-settings-enable-disable', { |
| 135 |
render: LibraryEnableDisable, |
| 136 |
}) |
| 137 |
|