-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
211 lines (196 loc) · 4.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */
/**
* WordPress dependencies
*/
import { applyFilters, doAction } from '@wordpress/hooks';
/**
* External dependencies
*/
import { isFunction } from 'lodash';
/**
* Defined behavior of a plugin type.
*
* @typedef {Object} WPPlugin
*
* @property {string} name A string identifying the plugin. Must be
* unique across all registered plugins.
* unique across all registered plugins.
* @property {string|WPElement|Function} icon An icon to be shown in the UI. It can
* be a slug of the Dashicon, or an element
* (or function returning an element) if you
* choose to render your own SVG.
* @property {Function} render A component containing the UI elements
* to be rendered.
*/
/**
* Plugin definitions keyed by plugin name.
*
* @type {Object.<string,WPPlugin>}
*/
const plugins = {};
/**
* Registers a plugin to the editor.
*
* @param {string} name A string identifying the plugin.Must be
* unique across all registered plugins.
* @param {WPPlugin} settings The settings for this plugin.
*
* @example <caption>ES5</caption>
* ```js
* // Using ES5 syntax
* var el = wp.element.createElement;
* var Fragment = wp.element.Fragment;
* var PluginSidebar = wp.editPost.PluginSidebar;
* var PluginSidebarMoreMenuItem = wp.editPost.PluginSidebarMoreMenuItem;
* var registerPlugin = wp.plugins.registerPlugin;
*
* function Component() {
* return el(
* Fragment,
* {},
* el(
* PluginSidebarMoreMenuItem,
* {
* target: 'sidebar-name',
* },
* 'My Sidebar'
* ),
* el(
* PluginSidebar,
* {
* name: 'sidebar-name',
* title: 'My Sidebar',
* },
* 'Content of the sidebar'
* )
* );
* }
* registerPlugin( 'plugin-name', {
* icon: 'smiley',
* render: Component,
* } );
* ```
*
* @example <caption>ESNext</caption>
* ```js
* // Using ESNext syntax
* const { PluginSidebar, PluginSidebarMoreMenuItem } = wp.editPost;
* const { registerPlugin } = wp.plugins;
*
* const Component = () => (
* <>
* <PluginSidebarMoreMenuItem
* target="sidebar-name"
* >
* My Sidebar
* </PluginSidebarMoreMenuItem>
* <PluginSidebar
* name="sidebar-name"
* title="My Sidebar"
* >
* Content of the sidebar
* </PluginSidebar>
* </>
* );
*
* registerPlugin( 'plugin-name', {
* icon: 'smiley',
* render: Component,
* } );
* ```
*
* @return {WPPlugin} The final plugin settings object.
*/
export function registerPlugin( name, settings ) {
if ( typeof settings !== 'object' ) {
console.error(
'No settings object provided!'
);
return null;
}
if ( typeof name !== 'string' ) {
console.error(
'Plugin names must be strings.'
);
return null;
}
if ( ! /^[a-z][a-z0-9-]*$/.test( name ) ) {
console.error(
'Plugin names must include only lowercase alphanumeric characters or dashes, and start with a letter. Example: "my-plugin".'
);
return null;
}
if ( plugins[ name ] ) {
console.error(
`Plugin "${ name }" is already registered.`
);
}
settings = applyFilters( 'plugins.registerPlugin', settings, name );
if ( ! isFunction( settings.render ) ) {
console.error(
'The "render" property must be specified and must be a valid function.'
);
return null;
}
plugins[ name ] = {
name,
icon: 'admin-plugins',
...settings,
};
doAction( 'plugins.pluginRegistered', settings, name );
return settings;
}
/**
* Unregisters a plugin by name.
*
* @param {string} name Plugin name.
*
* @example <caption>ES5</caption>
* ```js
* // Using ES5 syntax
* var unregisterPlugin = wp.plugins.unregisterPlugin;
*
* unregisterPlugin( 'plugin-name' );
* ```
*
* @example <caption>ESNext</caption>
* ```js
* // Using ESNext syntax
* const { unregisterPlugin } = wp.plugins;
*
* unregisterPlugin( 'plugin-name' );
* ```
*
* @return {?WPPlugin} The previous plugin settings object, if it has been
* successfully unregistered; otherwise `undefined`.
*/
export function unregisterPlugin( name ) {
if ( ! plugins[ name ] ) {
console.error(
'Plugin "' + name + '" is not registered.'
);
return;
}
const oldPlugin = plugins[ name ];
delete plugins[ name ];
doAction( 'plugins.pluginUnregistered', oldPlugin, name );
return oldPlugin;
}
/**
* Returns a registered plugin settings.
*
* @param {string} name Plugin name.
*
* @return {?WPPlugin} Plugin setting.
*/
export function getPlugin( name ) {
return plugins[ name ];
}
/**
* Returns all registered plugins.
*
* @return {WPPlugin[]} Plugin settings.
*/
export function getPlugins() {
return Object.values( plugins );
}