PluginProbe
Extendify / 1.6.1
Extendify v1.6.1
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Library / buttons.js

buttons.js in Extendify 1.6.1, at src/Library/buttons.js

117 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { subscribe } from '@wordpress/data'
2 import { PluginSidebarMoreMenuItem } from '@wordpress/edit-post'
3 import { render } from '@wordpress/element'
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 unsubMainBtn = subscribe(() => {
22 requestAnimationFrame(() => {
23 if (document.getElementById('extendify-templates-inserter')) {
24 return
25 }
26 if (
27 !document.querySelector('.edit-post-header-toolbar') &&
28 !document.querySelector('.edit-site-header-edit-mode__start') // FSE
29 ) {
30 return
31 }
32 if (!isGlobalLibraryEnabled() && !isAdmin()) {
33 return unsubMainBtn()
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-edit-mode__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 })
55 })
56
57 // The CTA button inside patterns
58 const finish2 = subscribe(() => {
59 requestAnimationFrame(() => {
60 if (!isGlobalLibraryEnabled() && !isAdmin()) {
61 return
62 }
63 if (!document.querySelector('[id$=patterns-view]')) {
64 return
65 }
66 if (document.getElementById('extendify-cta-button')) {
67 return
68 }
69 const ctaButtonContainer = Object.assign(
70 document.createElement('div'),
71 { id: 'extendify-cta-button-container' },
72 )
73
74 document
75 .querySelector('[id$=patterns-view]')
76 .prepend(ctaButtonContainer)
77 render(<CtaButton />, ctaButtonContainer)
78
79 finish2()
80 })
81 })
82
83 // This will add a button to enable or disable the library button
84 const LibraryEnableDisable = () => {
85 const setOpenSiteSettingsModal = () => {
86 const util = document.getElementById('extendify-util')
87 render(<LibraryAccessModal />, util)
88 }
89 // If the url isn't post.php or post-new.php, don't show the button
90 const href = window.location.href
91 if (!href.includes('post.php') && !href.includes('post-new.php')) {
92 return null
93 }
94
95 return (
96 <PluginSidebarMoreMenuItem
97 onClick={setOpenSiteSettingsModal}
98 icon={<Icon icon={brandMark} size={24} />}>
99 {' '}
100 Extendify
101 </PluginSidebarMoreMenuItem>
102 )
103 }
104
105 // Load this button always, which is used to enable or disable
106 // FSE doesn't seem to recognize registerPlugin (yet)
107 try {
108 registerPlugin('extendify-settings-enable-disable', {
109 render: LibraryEnableDisable,
110 })
111 } catch (e) {
112 console.error(
113 'registerPlugin not supported? (error handled gracefully)',
114 e.message,
115 )
116 }
117