/module.php, keeps the on/off switches * (option cpod_modules) and boots the modules that may run. * * A module is skipped when: * - it is switched off, * - a module it requires is not running (e.g. Estimated Delivery without POD Profile), * - the plugin it replaces (SalesGen Store Info) is still active, to avoid * duplicate shortcodes and settings, * - the `cpod_module_can_boot` filter says no (state 'locked'). * * @package CPODKit */ namespace CPODKit\Core; defined( 'ABSPATH' ) || exit; class Modules { const OPTION = 'cpod_modules'; /** Display and boot order. A missing folder (separate zip builds) is simply skipped. */ const ORDER = array( 'size-chart', 'pod-profile', 'estimated-delivery', 'product-sections', 'hook-blocks', 'store-variables', 'checkout-extras', 'best-selling', 'upsell', 'quick-view', 'icons', 'collections', 'fulfillment-integrations' ); /** @var array|null */ private static $modules = null; /** @var array id => running|off|paused|blocked|locked */ private static $states = array(); /** * All module manifests: id => manifest. * * @return array */ public static function all() { if ( null === self::$modules ) { self::$modules = array(); foreach ( self::ORDER as $id ) { $file = CPOD_DIR . 'modules/' . $id . '/module.php'; if ( ! is_readable( $file ) ) { continue; } $manifest = include $file; if ( is_array( $manifest ) ) { self::$modules[ $id ] = wp_parse_args( $manifest, array( 'id' => $id, 'name' => $id, 'description' => '', 'icon' => 'dashicons-admin-generic', 'requires' => array(), 'conflict' => null, 'detect' => null, 'boot' => null, 'deactivate' => null, 'data' => null, 'links' => null, 'uninstall' => array(), 'default' => true, 'split_from' => '', 'pro' => false, ) ); } } } return self::$modules; } public static function get( $id ) { $all = self::all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** Resolve a manifest value that may be a closure (used for translated texts). */ public static function value( $module, $key ) { $value = isset( $module[ $key ] ) ? $module[ $key ] : null; return $value instanceof \Closure ? $value() : $value; } /** * Switch state of every module: id => bool. * * @return array */ public static function switches() { $saved = get_option( self::OPTION, false ); if ( ! is_array( $saved ) ) { $saved = self::initial(); } $out = array(); foreach ( self::all() as $id => $module ) { // A module split from another one (Hook Blocks from Product Sections in 3.4.0) // follows that module's switch until the switches are saved again. if ( ! isset( $saved[ $id ] ) && $module['split_from'] && isset( $saved[ $module['split_from'] ] ) ) { $out[ $id ] = ! empty( $saved[ $module['split_from'] ] ); continue; } $out[ $id ] = ! empty( $saved[ $id ] ); } return $out; } public static function enabled( $id ) { $switches = self::switches(); return ! empty( $switches[ $id ] ); } /** * First run: switch on the modules whose old plugin data is found. * On a store without any old data every module is switched on. * * @return array */ public static function initial() { $detected = array(); foreach ( self::all() as $id => $module ) { $detected[ $id ] = is_callable( $module['detect'] ) && call_user_func( $module['detect'] ); } if ( ! in_array( true, $detected, true ) ) { // Nothing to take over: every module on, except the ones marked 'default' => false. $out = array(); foreach ( self::all() as $id => $module ) { $out[ $id ] = (bool) $module['default']; } return $out; } // A required module follows the modules that need it. foreach ( self::all() as $id => $module ) { if ( $detected[ $id ] ) { foreach ( (array) $module['requires'] as $required ) { $detected[ $required ] = true; } } } return $detected; } /** * Save the switches. A module whose requirement is off is switched off too. * * @param array $input id => truthy. * @return array Saved switches. */ public static function save( $input ) { $out = array(); foreach ( array_keys( self::all() ) as $id ) { $out[ $id ] = ! empty( $input[ $id ] ); } foreach ( self::all() as $id => $module ) { foreach ( (array) $module['requires'] as $required ) { if ( empty( $out[ $required ] ) ) { $out[ $id ] = false; } } } $before = self::switches(); update_option( self::OPTION, $out ); // A module switched off does not boot any more: let it clear what it scheduled. foreach ( self::all() as $id => $module ) { if ( ! empty( $before[ $id ] ) && empty( $out[ $id ] ) && is_callable( $module['deactivate'] ) ) { call_user_func( $module['deactivate'] ); } } return $out; } /** Name of the old plugin that pauses this module, or ''. */ public static function conflict( $id ) { $module = self::get( $id ); return $module && is_callable( $module['conflict'] ) ? (string) call_user_func( $module['conflict'] ) : ''; } /** Required modules that are not running. */ public static function missing_requirements( $id ) { $module = self::get( $id ); $missing = array(); foreach ( $module ? (array) $module['requires'] : array() as $required ) { if ( ! self::is_running( $required ) ) { $missing[] = $required; } } return $missing; } /** Boot every module that may run, in ORDER (requirements come first). */ public static function boot() { foreach ( self::all() as $id => $module ) { if ( ! self::enabled( $id ) ) { self::$states[ $id ] = 'off'; } elseif ( '' !== self::conflict( $id ) ) { self::$states[ $id ] = 'paused'; } elseif ( self::missing_requirements( $id ) ) { self::$states[ $id ] = 'blocked'; } elseif ( ! apply_filters( 'cpod_module_can_boot', true, $id, $module ) ) { /** * Whether a switched-on module may boot (the module and its data are kept either way). * * @param bool $can True by default. * @param string $id Module id. * @param array $module Manifest. */ self::$states[ $id ] = 'locked'; } else { self::$states[ $id ] = 'running'; if ( is_callable( $module['boot'] ) ) { call_user_func( $module['boot'] ); } } } } public static function state( $id ) { return isset( self::$states[ $id ] ) ? self::$states[ $id ] : 'off'; } public static function is_running( $id ) { return 'running' === self::state( $id ); } }