This loader instantiates Endurance WordPress Modules inside our WordPress Plugins.
- What are modules?
- Creating/registering modules
- Installing from our Satis
- Local development notes
- Understanding the module lifecycle
Endurance WordPress Modules are PHP packages intended to be installed in WordPress Plugins via composer from our satis registry.
They were first used in the Mojo Marketplace WordPress Plugin and later the Bluehost WordPress Plugin.
Modules essentially function as WordPress Plugins we can reuse in Endurance products and control programatically.
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/inc. - Modules are loaded on the
inithook with a priority of10. - Module registration should tap the
after_setup_themehook. - In
mojoness/mojo-marketplace-wp-plugintwo constants exist for tapping the Plugin's path and base URL:MM_BASE_DIRandMM_BASE_URL. Modules are in/vendor. - In
bluehost/bluehost-wordpress-pluginthe equivalents areBLUEHOST_PLUGIN_DIRandBLUEHOST_PLUGIN_URL. Modules are in/vendor.
This global function should be run during after_theme_setup.
/**
* @param array $args
*
* $args = array(
* 'name' => (string) **required** slug used for internal reference, like a CPT.
* 'label => (string) **required** i18n display label
* 'callback' => (callable) **required** executed when module is active
* 'isActive' => (boolean) **optional, default: false** whether module is forced active by default (can be overriden if !isHidden)
* 'isHidden' => (boolean) **optional, default: false** whether module can be toggled in UI.
* )
*/
eig_register_module( $args );Example bootstrap.php:
<?php
if ( function_exists( 'add_action' ) ) {
add_action( 'after_setup_theme', 'eig_module_spam_prevention_register' );
}
/**
* Register the spam prevention module
*/
function eig_module_spam_prevention_register() {
eig_register_module( array(
'name' => 'spam-prevention',
'label' => __( 'Spam Prevention', 'endurance' ),
'callback' => 'eig_module_spam_prevention_load',
'isActive' => true,
) );
}
/**
* Load the spam prevention module
*/
function eig_module_spam_prevention_load() {
require dirname( __FILE__ ) . '/spam-prevention.php';
}Our modules are sourced from our 3rd-party package repository (Satis).
Via command line: composer config repositories.bluehost composer https://bluehost.github.io/satis
OR
{
"repositories": [
{
"type": "composer",
"url": "https://bluehost.github.io/satis/",
"only": [
"bluehost/*",
"mojoness/*",
"endurance/*"
]
}
]
}When working on modules locally:
- Clone the module repository somewhere on the filesystem, i.e.
/wp-content/modules/endurance-wp-module-awesome. - Modify the sourcing-plugin's
composer.jsonto reference that directory as apathin the repositories array (above the satis reference).
{
"repositories": [
{
"type": "path",
"url": "../../modules/endurance-wp-module-awesome",
"options": {
"symlink": true
}
},
{
"type": "composer",
"url": "https://bluehost.github.io/satis/",
"only": [
"bluehost/*",
"mojoness/*",
"endurance/*"
]
}
]
}- In the
requiredeclaration, change the version to@dev composer install
- During plugin release, a
composer installis run, creating autoloader files and pulling in composer dependencies -- which include EIG 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 EIG module defines a bootstrap.php that 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 during
plugins_loaded. - In the boostrap.php for each module, the module is registered with the module loader module using
eig_register_module(). Most modules should be registered indo_action('after_theme_setup')and before thedo_action('init')hook. - In
bluehost/endurance-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