From 47491f4164f41b5dcf6c82aa47e2c17b11ce2f33 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Tue, 7 Jan 2020 11:58:12 -0500 Subject: [PATCH 1/3] Introduce PluginManager class --- packages/plugins/src/api/index.js | 37 ++++++++------ .../plugins/src/api/plugin-manager/index.js | 50 +++++++++++++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 packages/plugins/src/api/plugin-manager/index.js diff --git a/packages/plugins/src/api/index.js b/packages/plugins/src/api/index.js index 5e8fe1d19c851..d940dd7c5b0c5 100644 --- a/packages/plugins/src/api/index.js +++ b/packages/plugins/src/api/index.js @@ -1,14 +1,19 @@ /* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ +/** + * External dependencies + */ +import { isFunction } from 'lodash'; + /** * WordPress dependencies */ import { applyFilters, doAction } from '@wordpress/hooks'; /** - * External dependencies + * Internal dependencies */ -import { isFunction } from 'lodash'; +import PluginManager from '../api/plugin-manager'; /** * Defined behavior of a plugin type. @@ -27,11 +32,11 @@ import { isFunction } from 'lodash'; */ /** - * Plugin definitions keyed by plugin name. + * Plugin manager class * - * @type {Object.} + * @typedef {Object} PluginManager */ -const plugins = {}; +const pluginManager = applyFilters( 'plugins.pluginManager', new PluginManager() ); /** * Registers a plugin to the editor. @@ -125,7 +130,7 @@ export function registerPlugin( name, settings ) { ); return null; } - if ( plugins[ name ] ) { + if ( pluginManager.getPlugin( name ) ) { console.error( `Plugin "${ name }" is already registered.` ); @@ -140,11 +145,13 @@ export function registerPlugin( name, settings ) { return null; } - plugins[ name ] = { + pluginManager.addPlugin( name, - icon: 'admin-plugins', - ...settings, - }; + { + icon: 'admin-plugins', + ...settings, + } + ); doAction( 'plugins.pluginRegistered', settings, name ); @@ -176,14 +183,14 @@ export function registerPlugin( name, settings ) { * successfully unregistered; otherwise `undefined`. */ export function unregisterPlugin( name ) { - if ( ! plugins[ name ] ) { + if ( ! pluginManager.getPlugin( name ) ) { console.error( 'Plugin "' + name + '" is not registered.' ); return; } - const oldPlugin = plugins[ name ]; - delete plugins[ name ]; + const oldPlugin = pluginManager.getPlugin( name ); + pluginManager.removePlugin( name ); doAction( 'plugins.pluginUnregistered', oldPlugin, name ); @@ -198,7 +205,7 @@ export function unregisterPlugin( name ) { * @return {?WPPlugin} Plugin setting. */ export function getPlugin( name ) { - return plugins[ name ]; + return pluginManager.getPlugin( name ); } /** @@ -207,5 +214,5 @@ export function getPlugin( name ) { * @return {WPPlugin[]} Plugin settings. */ export function getPlugins() { - return Object.values( plugins ); + return Object.values( pluginManager.getPlugins() ); } diff --git a/packages/plugins/src/api/plugin-manager/index.js b/packages/plugins/src/api/plugin-manager/index.js new file mode 100644 index 0000000000000..812d5aaf07c24 --- /dev/null +++ b/packages/plugins/src/api/plugin-manager/index.js @@ -0,0 +1,50 @@ +export default class PluginManager { + /** + * Create a PluginManager + */ + constructor() { + this.plugins = {}; + } + + /** + * Adds a new plugin. + * + * @param {string} name Plugin name. + * @param {Object} settings The settings for this plugin. + */ + addPlugin( name, settings ) { + this.plugins[ name ] = { + name, + ...settings, + }; + } + + /** + * Removes a plugin + * + * @param {string} name Plugin name. + */ + removePlugin( name ) { + delete this.plugins[ name ]; + } + + /** + * Return settings for a single plugin. + * + * @param {string} name Plugin name. + * + * @return {Object} The plugin settings + */ + getPlugin( name ) { + return this.plugins[ name ]; + } + + /** + * Returns settings for all registered plugins + * + * @return {Array} All registered plugin settings. + */ + getPlugins() { + return this.plugins; + } +} From 7b29bc8df5fc9c8e8f4a346c27381c9eeeaa8156 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Tue, 7 Jan 2020 11:58:12 -0500 Subject: [PATCH 2/3] Introduce PluginManager class --- packages/plugins/src/api/index.js | 37 ++++++++------ .../plugins/src/api/plugin-manager/index.js | 50 +++++++++++++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 packages/plugins/src/api/plugin-manager/index.js diff --git a/packages/plugins/src/api/index.js b/packages/plugins/src/api/index.js index 5e8fe1d19c851..d940dd7c5b0c5 100644 --- a/packages/plugins/src/api/index.js +++ b/packages/plugins/src/api/index.js @@ -1,14 +1,19 @@ /* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ +/** + * External dependencies + */ +import { isFunction } from 'lodash'; + /** * WordPress dependencies */ import { applyFilters, doAction } from '@wordpress/hooks'; /** - * External dependencies + * Internal dependencies */ -import { isFunction } from 'lodash'; +import PluginManager from '../api/plugin-manager'; /** * Defined behavior of a plugin type. @@ -27,11 +32,11 @@ import { isFunction } from 'lodash'; */ /** - * Plugin definitions keyed by plugin name. + * Plugin manager class * - * @type {Object.} + * @typedef {Object} PluginManager */ -const plugins = {}; +const pluginManager = applyFilters( 'plugins.pluginManager', new PluginManager() ); /** * Registers a plugin to the editor. @@ -125,7 +130,7 @@ export function registerPlugin( name, settings ) { ); return null; } - if ( plugins[ name ] ) { + if ( pluginManager.getPlugin( name ) ) { console.error( `Plugin "${ name }" is already registered.` ); @@ -140,11 +145,13 @@ export function registerPlugin( name, settings ) { return null; } - plugins[ name ] = { + pluginManager.addPlugin( name, - icon: 'admin-plugins', - ...settings, - }; + { + icon: 'admin-plugins', + ...settings, + } + ); doAction( 'plugins.pluginRegistered', settings, name ); @@ -176,14 +183,14 @@ export function registerPlugin( name, settings ) { * successfully unregistered; otherwise `undefined`. */ export function unregisterPlugin( name ) { - if ( ! plugins[ name ] ) { + if ( ! pluginManager.getPlugin( name ) ) { console.error( 'Plugin "' + name + '" is not registered.' ); return; } - const oldPlugin = plugins[ name ]; - delete plugins[ name ]; + const oldPlugin = pluginManager.getPlugin( name ); + pluginManager.removePlugin( name ); doAction( 'plugins.pluginUnregistered', oldPlugin, name ); @@ -198,7 +205,7 @@ export function unregisterPlugin( name ) { * @return {?WPPlugin} Plugin setting. */ export function getPlugin( name ) { - return plugins[ name ]; + return pluginManager.getPlugin( name ); } /** @@ -207,5 +214,5 @@ export function getPlugin( name ) { * @return {WPPlugin[]} Plugin settings. */ export function getPlugins() { - return Object.values( plugins ); + return Object.values( pluginManager.getPlugins() ); } diff --git a/packages/plugins/src/api/plugin-manager/index.js b/packages/plugins/src/api/plugin-manager/index.js new file mode 100644 index 0000000000000..812d5aaf07c24 --- /dev/null +++ b/packages/plugins/src/api/plugin-manager/index.js @@ -0,0 +1,50 @@ +export default class PluginManager { + /** + * Create a PluginManager + */ + constructor() { + this.plugins = {}; + } + + /** + * Adds a new plugin. + * + * @param {string} name Plugin name. + * @param {Object} settings The settings for this plugin. + */ + addPlugin( name, settings ) { + this.plugins[ name ] = { + name, + ...settings, + }; + } + + /** + * Removes a plugin + * + * @param {string} name Plugin name. + */ + removePlugin( name ) { + delete this.plugins[ name ]; + } + + /** + * Return settings for a single plugin. + * + * @param {string} name Plugin name. + * + * @return {Object} The plugin settings + */ + getPlugin( name ) { + return this.plugins[ name ]; + } + + /** + * Returns settings for all registered plugins + * + * @return {Array} All registered plugin settings. + */ + getPlugins() { + return this.plugins; + } +} From fa7b9e72193634dffefa55da5fd60a0ec52b7b34 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Tue, 14 Jan 2020 10:04:26 -0500 Subject: [PATCH 3/3] Removes filter --- packages/plugins/src/api/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugins/src/api/index.js b/packages/plugins/src/api/index.js index d940dd7c5b0c5..52149cf46f9e7 100644 --- a/packages/plugins/src/api/index.js +++ b/packages/plugins/src/api/index.js @@ -36,7 +36,7 @@ import PluginManager from '../api/plugin-manager'; * * @typedef {Object} PluginManager */ -const pluginManager = applyFilters( 'plugins.pluginManager', new PluginManager() ); +const pluginManager = PluginManager(); /** * Registers a plugin to the editor.