'Deals', * 'description' => 'Track deals through your own pipeline stages.', * 'class' => 'My_Deals_Module', // extends Nodebook_Module_Base * 'version' => '1.0.0', * 'provider' => 'My add-on', * 'footprint' => array( 'tables' => 2, 'cron' => false, 'frontend' => false ), * ); * return $modules; * } ); * * @package Nodebook */ defined( 'ABSPATH' ) || exit; final class Nodebook_Modules { const OPTION = 'nodebook_modules'; const VERSIONS = 'nodebook_module_versions'; const TABLES = 'nodebook_module_tables'; /** @var Nodebook_Module[] Booted modules by ID. */ private static $booted = array(); /** @var array|null */ private static $all = null; public static function init() { // On init: add-ons have registered by then, and module names can be translated. add_action( 'init', array( __CLASS__, 'boot' ), 1 ); add_action( 'update_option_nodebook_settings', array( __CLASS__, 'niche_changed' ), 10, 2 ); add_action( 'add_option_nodebook_settings', array( __CLASS__, 'niche_added' ), 10, 2 ); } /** Picking "Retreats & gatherings" as the kind of business switches the Gatherings module on. */ public static function niche_changed( $old, $new ) { $was = is_array( $old ) ? (string) ( $old['niche'] ?? '' ) : ''; $now = is_array( $new ) ? (string) ( $new['niche'] ?? '' ) : ''; if ( 'retreats' === $now && 'retreats' !== $was && ! self::is_enabled( 'gatherings' ) ) { self::enable( 'gatherings' ); } } public static function niche_added( $name, $value ) { self::niche_changed( array(), $value ); } /** * Every known module, normalised. * * @return array */ public static function all() { if ( null !== self::$all ) { return self::$all; } $builtin = array( 'abilities' => array( 'name' => __( 'AI assistants', 'nodebook' ), 'description' => __( 'Lets AI assistants that connect to WordPress (through the Abilities API, for example with the MCP adapter) look up contacts, read a contact\'s timeline, see who to follow up with and add notes. They act as the signed-in user: only people who may use Nodebook can use these abilities.', 'nodebook' ), 'class' => 'Nodebook_Module_Abilities', 'version' => '1.0.0', 'provider' => 'Nodebook', 'tier' => 'free', 'footprint' => array( 'tables' => 0, 'cron' => false, 'frontend' => false, ), 'needs' => array( 'Nodebook_Module_Abilities', 'missing' ), ), 'gatherings' => array( 'name' => __( 'Gatherings', 'nodebook' ), 'description' => __( 'Retreats, workshops, trips and events: dates, capacity and price, and a roster of who is coming with deposits, balances, a waitlist and who came. People join from the roster, from a form on the gathering\'s page, a WooCommerce order or a booking plugin. Node reminds you of balances due, spots that open up and past guests to invite back.', 'nodebook' ), 'class' => 'Nodebook_Module_Gatherings', 'version' => '1.0.0', 'provider' => 'Nodebook', 'tier' => 'free', 'footprint' => array( 'tables' => 2, 'cron' => false, 'frontend' => false, ), 'settings' => admin_url( 'admin.php?page=nodebook-gatherings' ), ), ); /** * Register modules. See the class docblock for the format. * * @param array $modules Module ID => definition. */ $list = (array) apply_filters( 'nodebook_modules', $builtin ); $out = array(); foreach ( $list as $id => $m ) { $id = (string) $id; if ( ! is_array( $m ) || ! preg_match( '/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $id ) || empty( $m['class'] ) ) { continue; } $out[ $id ] = array( 'id' => $id, 'name' => isset( $m['name'] ) ? (string) $m['name'] : $id, 'description' => isset( $m['description'] ) ? (string) $m['description'] : '', 'class' => (string) $m['class'], 'version' => isset( $m['version'] ) ? (string) $m['version'] : '1.0.0', 'provider' => isset( $m['provider'] ) ? (string) $m['provider'] : '', 'tier' => 'free', 'requires' => array_values( array_filter( array_map( 'strval', (array) ( $m['requires'] ?? array() ) ) ) ), 'footprint' => wp_parse_args( (array) ( $m['footprint'] ?? array() ), array( 'tables' => 0, 'cron' => false, 'frontend' => false, ) ), 'needs' => $m['needs'] ?? null, 'settings' => isset( $m['settings'] ) ? (string) $m['settings'] : '', ); } self::$all = $out; return $out; } /** Forget the cached list (tests, or after an add-on registers late). */ public static function reset() { self::$all = null; self::$booted = array(); } public static function get( $id ) { $all = self::all(); return $all[ $id ] ?? null; } /** IDs that are switched on (and still exist). */ public static function enabled() { $on = get_option( self::OPTION, array() ); $on = is_array( $on ) ? array_map( 'strval', $on ) : array(); return array_values( array_intersect( $on, array_keys( self::all() ) ) ); } public static function is_enabled( $id ) { return in_array( (string) $id, self::enabled(), true ); } /** True when the module is switched on and loaded on this request. */ public static function is_active( $id ) { return isset( self::$booted[ $id ] ); } /** * Why a module cannot run here, or '' when it can. */ public static function problem( $id ) { $m = self::get( $id ); if ( ! $m ) { return __( 'This module is not installed.', 'nodebook' ); } if ( ! class_exists( $m['class'] ) || ! is_subclass_of( $m['class'], 'Nodebook_Module' ) ) { /* translators: %s: add-on name */ return $m['provider'] ? sprintf( __( 'Its code is missing. Is %s active?', 'nodebook' ), $m['provider'] ) : __( 'Its code is missing.', 'nodebook' ); } if ( is_callable( $m['needs'] ) ) { $why = (string) call_user_func( $m['needs'] ); if ( '' !== $why ) { return $why; } } foreach ( $m['requires'] as $req ) { $r = self::get( $req ); if ( ! $r ) { /* translators: %s: module ID */ return sprintf( __( 'Needs the "%s" module, which is not installed.', 'nodebook' ), $req ); } } return ''; } /** Module instance (not booted). */ private static function make( $id ) { $m = self::get( $id ); if ( ! $m || '' !== self::problem( $id ) ) { return null; } $class = $m['class']; return new $class( $id ); } /** Load every switched-on module: upgrade it when its version changed, then boot it. */ public static function boot() { $versions = (array) get_option( self::VERSIONS, array() ); foreach ( self::ordered( self::enabled() ) as $id ) { if ( isset( self::$booted[ $id ] ) ) { continue; } $m = self::get( $id ); foreach ( $m['requires'] as $req ) { if ( ! isset( self::$booted[ $req ] ) ) { continue 2; } } $mod = self::make( $id ); if ( ! $mod ) { continue; } if ( ( $versions[ $id ] ?? '' ) !== $m['version'] ) { $mod->install(); $versions[ $id ] = $m['version']; update_option( self::VERSIONS, $versions, true ); } $mod->boot(); self::$booted[ $id ] = $mod; } /** * Fires after the switched-on modules are loaded. * * @param Nodebook_Module[] $booted Module ID => instance. */ do_action( 'nodebook_modules_loaded', self::$booted ); } /** Dependencies first. */ private static function ordered( array $ids ) { $out = array(); $visit = static function ( $id, $depth ) use ( &$visit, &$out, $ids ) { if ( $depth > 10 || in_array( $id, $out, true ) || ! in_array( $id, $ids, true ) ) { return; } $m = self::get( $id ); foreach ( $m ? $m['requires'] : array() as $req ) { $visit( $req, $depth + 1 ); } $out[] = $id; }; foreach ( $ids as $id ) { $visit( $id, 0 ); } return $out; } /** * Switch a module on (and the modules it needs). Installs its tables right away. * * @return true|WP_Error */ public static function enable( $id ) { $id = (string) $id; $problem = self::problem( $id ); if ( '' !== $problem ) { return new WP_Error( 'nodebook_module', $problem ); } $m = self::get( $id ); $on = self::enabled(); foreach ( $m['requires'] as $req ) { if ( ! in_array( $req, $on, true ) ) { $r = self::enable( $req ); if ( is_wp_error( $r ) ) { return $r; } $on = self::enabled(); } } $mod = self::make( $id ); $mod->install(); $versions = (array) get_option( self::VERSIONS, array() ); $versions[ $id ] = $m['version']; update_option( self::VERSIONS, $versions, true ); if ( ! in_array( $id, $on, true ) ) { $on[] = $id; } update_option( self::OPTION, array_values( $on ) ); /** * Fires when a module is switched on. */ do_action( 'nodebook_module_enabled', $id ); return true; } /** Switched-on modules that need this one. */ public static function dependents( $id ) { $out = array(); foreach ( self::enabled() as $other ) { $m = self::get( $other ); if ( $m && in_array( $id, $m['requires'], true ) ) { $out[] = $other; } } return $out; } /** * Switch a module off. Its data stays, so switching it back on brings everything back. * * @return true|WP_Error */ public static function disable( $id ) { $id = (string) $id; $deps = self::dependents( $id ); if ( $deps ) { $names = array(); foreach ( $deps as $d ) { $names[] = self::get( $d )['name']; } /* translators: %s: module names */ return new WP_Error( 'nodebook_module', sprintf( __( 'Switch off %s first: it needs this module.', 'nodebook' ), implode( ', ', $names ) ) ); } $mod = self::$booted[ $id ] ?? self::make( $id ); if ( $mod ) { $mod->deactivate(); } unset( self::$booted[ $id ] ); update_option( self::OPTION, array_values( array_diff( self::enabled(), array( $id ) ) ) ); do_action( 'nodebook_module_disabled', $id ); return true; } /** Whether a switched-off module left data behind (it was installed once). */ public static function has_data( $id ) { $versions = (array) get_option( self::VERSIONS, array() ); return isset( $versions[ $id ] ) && ! self::is_enabled( $id ); } /** * Delete everything a switched-off module stored. * * @return true|WP_Error */ public static function delete_data( $id ) { $id = (string) $id; if ( self::is_enabled( $id ) ) { return new WP_Error( 'nodebook_module', __( 'Switch the module off first.', 'nodebook' ) ); } $mod = self::make( $id ); if ( $mod ) { $mod->uninstall(); } $versions = (array) get_option( self::VERSIONS, array() ); unset( $versions[ $id ] ); update_option( self::VERSIONS, $versions, true ); $tables = (array) get_option( self::TABLES, array() ); unset( $tables[ $id ] ); update_option( self::TABLES, $tables, false ); do_action( 'nodebook_module_data_deleted', $id ); return true; } /** Remember a table so it is dropped with the module's data (or on uninstall). */ public static function track_table( $id, $table ) { $tables = (array) get_option( self::TABLES, array() ); $list = isset( $tables[ $id ] ) ? (array) $tables[ $id ] : array(); if ( ! in_array( $table, $list, true ) ) { $list[] = $table; $tables[ $id ] = $list; update_option( self::TABLES, $tables, false ); } } /** Tables a module created. */ public static function tables( $id ) { $tables = (array) get_option( self::TABLES, array() ); return isset( $tables[ $id ] ) ? array_values( (array) $tables[ $id ] ) : array(); } /** Nodebook itself is deactivated: let switched-on modules clear their scheduled tasks. */ public static function deactivate_all() { foreach ( self::enabled() as $id ) { $mod = self::$booted[ $id ] ?? self::make( $id ); if ( $mod ) { $mod->deactivate(); } } } /** Short, plain description of what a module adds to the site. */ public static function footprint_labels( array $m ) { $f = $m['footprint']; $out = array(); $n = (int) $f['tables']; /* translators: %d: number of database tables */ $out[] = $n ? sprintf( _n( '%d database table', '%d database tables', $n, 'nodebook' ), $n ) : __( 'No database tables', 'nodebook' ); $out[] = $f['cron'] ? __( 'Background task', 'nodebook' ) : __( 'No background tasks', 'nodebook' ); $out[] = $f['frontend'] ? __( 'Loads on your site\'s pages', 'nodebook' ) : __( 'Nothing on your site\'s pages', 'nodebook' ); return $out; } }