1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 
<?php
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'VSP_Framework_Loader' ) ) {
    
    final class VSP_Framework_Loader {
        
        public static $_instance = null;
        
        public static $_loaded = array();
        
        public static $data = array();
        
        public static $callbacks = array();
        
        public function __construct() {
            add_action( 'wponion/loaded', [ &$this, 'load_framework' ], 0 );
            add_action( 'vsp/loaded', [ &$this, 'load_plugins' ], -1 );
            add_action( 'wponion/sysinfo/datas', [ &$this, 'add_extra_info' ] );
        }
        
        public function add_extra_info( $_meta = array() ) {
            $meta       = array();
            $vsp_loaded = $this->loaded();
            $meta['Framework Version']     = $vsp_loaded['Version'];
            $meta['Textdomain']            = $vsp_loaded['TextDomain'];
            $meta['DomainPath']            = $vsp_loaded['DomainPath'];
            $meta['Framework Plugin Path'] = $vsp_loaded['plugin_path'];
            $meta['Framework Path']        = $vsp_loaded['framework_path'];
            $meta['Framework Included']    = self::$data;
            $_meta['VSP Framework']        = $meta;
            return $_meta;
        }
        
        public function load_framework() {
            if ( empty( self::$_loaded ) ) {
                $lists  = self::get();
                $latest = max( array_keys( $lists ) );
                $info   = ( isset( $lists[ $latest ] ) ) ? $lists[ $latest ] : [];
                if ( empty( $info ) ) {
                    $msg = base64_encode( wp_json_encode( self::$data ) );
                    $ms  = 'Unable To Load VSP Framework. Please Contact The Author';
                    $ms  = $ms . '<p style="word-break: break-all;"> <strong>' . 'ERROR ID : ' . '</strong>' . $msg . '</p>';
                    wp_die( $ms );
                }
                self::$_loaded = $info;
                require_once $info['framework_path'] . 'vsp-bootstrap.php';
            }
        }
        
        public static function instance() {
            if ( null === self::$_instance ) {
                self::$_instance = new self();
            }
            return self::$_instance;
        }
        
        public function add( $version = '', $data = array() ) {
            self::$data[ $version ] = $data;
            return $this;
        }
        
        public function register_plugin( $plugin_path = '', $framework_path = '/vsp-framework/' ) {
            $plugin_path    = trailingslashit( $plugin_path );
            $framework_path = trailingslashit( $plugin_path . $framework_path );
            if ( file_exists( $framework_path . 'vsp-bootstrap.php' ) ) {
                $info                   = get_file_data( $framework_path . 'vsp-bootstrap.php', [
                    'Name'       => 'Framework Name',
                    'Version'    => 'Version',
                    'TextDomain' => 'Text Domain',
                    'DomainPath' => 'Domain Path',
                ] );
                $info['plugin_path']    = trailingslashit( $plugin_path );
                $info['framework_path'] = $framework_path;
                self::add( $info['Version'], $info );
            }
            return $this;
        }
        
        public function get() {
            return self::$data;
        }
        
        public function loaded() {
            return self::$_loaded;
        }
        
        public function register_callback( $callback = array() ) {
            self::$callbacks[] = $callback;
            return true;
        }
        
        public function load_plugins() {
            if ( ! empty( self::$callbacks ) ) {
                foreach ( self::$callbacks as $callback ) {
                    call_user_func_array( $callback, [] );
                }
            }
        }
    }
}
if ( ! function_exists( 'vsp_maybe_load' ) ) {
    
    function vsp_maybe_load( $callback = [], $plugin_path = '', $framework_path = 'vsp-framework' ) {
        $plugin_path = ( ! empty( $plugin_path ) ) ? $plugin_path : __DIR__ . '/';
        VSP_Framework_Loader::instance()
            ->register_plugin( $plugin_path, $framework_path )
            ->register_callback( $callback );
    }
}
if ( ! function_exists( 'vsp_register_plugin' ) ) {
    
    function vsp_register_plugin( $callback = [] ) {
        return VSP_Framework_Loader::instance()->register_callback( $callback );
    }
}
if ( ! function_exists( 'vsp_force_load' ) ) {
    
    function vsp_force_load() {
        VSP_Framework_Loader::instance()->load_framework();
    }
}
if ( ! function_exists( 'vsp_force_load_vendors' ) ) {
    
    function vsp_force_load_vendors() {
        if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
            include __DIR__ . '/vendor/autoload.php';
            return true;
        }
        return false;
    }
}