This loader instantiates Newfold WordPress Modules inside our WordPress Plugins.
- What are modules?
- Creating/registering modules
- Installing from our Satis
- Local development notes
- Understanding the module lifecycle
- Module Loader API
- Provides a dependency injection container for shared dependencies across the brand plugin modules.
- Provides a framework for registering, unregistering, activating, deactivating, and checking if a module is active.
- Provides a plugin class that makes getting basic plugin information simple.
- The dependency injection container should be created by the brand plugin and successfully shared across all other modules.
- Calling the
register()function should successfully register the module and run the code on theafter_setup_themeaction hook.
Newfold WordPress modules are PHP packages intended to be installed in WordPress plugins via Composer from our Satis registry.
Modules essentially function as WordPress plugins we can reuse in Newfold products and control programmatically.
Modules can be required/forced, optional, hidden and can be toggled by code and (sometimes) by users.
Modules will eventually be created from templates, but for now here are some key things to know.
- Modules should contain a
bootstrap.phpfile that should get autoloaded by Composer. Functionality should load from/includes. - Modules are loaded on the
after_setup_themehook with a priority of100. - Module registration should tap the
plugins_loadedhook. - If a plugin registers a dependency injection container, then that container will be accessible to the registered modules. If the plugin doesn't register a container, an empty container will be passed to the module.
Below is an example of how to register a module within the bootstrap.php file:
<?php
use NewfoldLabs\WP\ModuleLoader\Container;
use function NewfoldLabs\WP\ModuleLoader\register;
if ( function_exists( 'add_action' ) ) {
add_action(
'plugins_loaded',
function () {
register(
[
'name' => 'sso',
'label' => __( 'SSO', 'newfold-sso-module' ),
'callback' => function ( Container $container ) {
require __DIR__ . '/includes/sso.php';
},
'isActive' => true,
'isHidden' => true,
]
);
}
);
}Notice that a dependency injection container is passed to the callback function. You can leverage this container in
the callback function, or you can use the NewfoldLabs\WP\ModuleLoader\container function to fetch the container
from any location.
Our modules are sourced from our 3rd-party package repository (Satis).
Via command line: composer config repositories.newfold composer https://newfold-labs.github.io/satis/
OR
{
"repositories": [
{
"type": "composer",
"url": "https://newfold-labs.github.io/satis/",
"only": [
"newfold-labs/*"
]
}
]
}- Use a dev environment for a Newfold Brand Plugin [wp-plugin-web, wp-plugin-hostgator, etc].
- Open the plugin root directory.
- Modify the
composer.jsonin a text editor.- Add a new object to the "repositories" top-level property. All local repositories should go at the beginning of the array declaration to supercede priority from the satis declarations.
- The
urlpath is relative to the current folder -- go up two directories for the/wp-contentfolder. - The symlink option set to false will copy files instead and wont update without manual intervention.
-
- If this is a new module, add it to the
"require"property with a version of@dev. - if this is an existing module, modify the entry in
"require"to@dev.
- If this is a new module, add it to the
{
"repositories": [
{
"type": "path",
"url": "../../path-in-wp-content-directory",
"options": {
"symlink": true
}
},
{
"type": "composer",
"url": "https://newfold-labs.github.io/satis/",
"only": [
"newfold-labs/*"
]
}
],
"require": {
"newfold-labs/wp-module-magic": "@dev",
"newfold-labs/wp-module-loader": "x.y.z"
}
}
Finally, you may want to run a composer update routine or remove your composer.lock file and composer install.
- During plugin release, a
composer installis run, creating autoloader files and pulling in composer dependencies -- which include Newfold modules. - A request is made to WordPress, firing Core hooks.
- The plugin containing modules is loaded during
do_action('plugins_loaded'). WordPress loads plugins alphabetically. - In the plugin, the composer autoloader is required and executes. This isn't attached to an action hook, but is
effectively running during
plugins_loaded. - Each module defines a
bootstrap.phpthat is explicitly set to autoload, so when the main plugin's autoloader fires, each module's bootstrap.php is loaded -- again outside the hook cascade, but these files are effectively run duringplugins_loaded. - In the
boostrap.phpfor each module, the module is registered with the module loader module usingNewfoldLabs\WP\ModuleLoader\Module\make(). Most modules should be registered indo_action('plugins_loaded')and before thedo_action('init')hook. - In
newfold-labs/wp-module-loader, the loader runs ondo_action('after_theme_setup')with a priority of100. - Any code in a module that is instantiated via
bootstrap.phpcan now access the WordPress Action Hook system, starting withinit.
init(first available)wp_loadedadmin_menuadmin_init- etc
plugins_loadedset_current_usersetup_themeafter_theme_setup
The following functions are namespaced with NewfoldLabs\WP\ModuleLoader.
Register a new module.
Required attributes:
- name (string) - The internal module name; should be lowercase with dashes.
- label (string) - The user-facing module name
- callback (callable) - The callback that kicks off the module's functionality.
- isActive (bool) - Whether the module defaults to active.
- isHidden (bool) - Whether the module should be hidden from users in the UI.
Unregister a module. The
$nameis the internal module name.
Activate a module by name.
Deactivate a module by name.
Check if a module is active by name.
Register a container that should be shared with all modules.
Currently, the container must be an instance of
NewfoldLabs\WP\ModuleLoader\Container.A container should be registered within the WordPress plugin and should be done like this:
use NewfoldLabs\WP\ModuleLoader\Container; use NewfoldLabs\WP\ModuleLoader\Plugin; use function NewfoldLabs\WP\ModuleLoader\container as setContainer; setContainer( new Container( [ 'plugin' => new Plugin( [ 'id' => 'bluehost', // Used for data module integration 'file' => __FILE__, ] ), ] ); );Documentation on how to use the container exists here: https://github.com/wp-forge/container