| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Activator; |
| 6 |
use FluentForm\App\Modules\Deactivator; |
| 7 |
|
| 8 |
class Bootstrap |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The main plugin file path |
| 12 |
* @var strring |
| 13 |
*/ |
| 14 |
protected static $file = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* The base dir path of the plugin |
| 18 |
* @var strring |
| 19 |
*/ |
| 20 |
protected static $basePath = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* The app config (/config/app.php) |
| 24 |
* @var strring |
| 25 |
*/ |
| 26 |
protected static $config = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Conveniently start the framework |
| 30 |
* @param string $file |
| 31 |
* @return $ |
| 32 |
*/ |
| 33 |
public static function run($file) |
| 34 |
{ |
| 35 |
if (!extension_loaded('pdo')) { |
| 36 |
add_action('admin_notices', function () { |
| 37 |
echo '<div class="notice notice-warning"><p>Fluent Forms Plugin requires PDO extension. Please install <code>pdo_mysql</code> extension or ask your hosting prover to install it. After PDO enabled, deactivate and activate Fluent Forms again</p></div>'; |
| 38 |
}); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
static::init($file); |
| 43 |
static::registerHooks(); |
| 44 |
static::registerAutoLoader(); |
| 45 |
static::registerApplication(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize the framework |
| 50 |
* @param string $file [the main plugin file path] |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public static function init($file) |
| 54 |
{ |
| 55 |
static::$file = $file; |
| 56 |
static::$basePath = plugin_dir_path($file); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Register activation/deactivation hooks |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function registerHooks() |
| 64 |
{ |
| 65 |
static::registerActivationHook(); |
| 66 |
static::registerDeactivationHook(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register activation hook |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public static function registerActivationHook() |
| 74 |
{ |
| 75 |
return register_activation_hook( |
| 76 |
static::$file, array(__CLASS__, 'activate') |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
public static function registerDeactivationHook() |
| 82 |
{ |
| 83 |
return register_deactivation_hook( |
| 84 |
static::$file, array(__CLASS__, 'deactivate') |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Validate and activate the plugin |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public static function activate($netowrkwide = false) |
| 93 |
{ |
| 94 |
static::validatePlugin(); |
| 95 |
if (file_exists($activator = static::$basePath . 'app/Modules/Activator.php')) { |
| 96 |
include_once $activator; |
| 97 |
} |
| 98 |
|
| 99 |
if (class_exists('FluentForm\App\Modules\Activator')) { |
| 100 |
(new Activator)->handleActivation($netowrkwide); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public static function deactivate() |
| 105 |
{ |
| 106 |
if (file_exists($deactivator = static::$basePath . 'app/Modules/Deactivator.php')) { |
| 107 |
include_once $deactivator; |
| 108 |
} |
| 109 |
if (class_exists('FluentForm\App\Modules\Deactivator')) { |
| 110 |
(new Deactivator())->handleDeactivation(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Validate the plugin by checking all rquired files/settings |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public static function validatePlugin() |
| 119 |
{ |
| 120 |
if (!file_exists($glueJson = static::$basePath . 'glue.json')) { |
| 121 |
die('The [glue.json] file is missing from "' . static::$basePath . '" directory.'); |
| 122 |
} |
| 123 |
|
| 124 |
static::$config = json_decode(file_get_contents($glueJson), true); |
| 125 |
|
| 126 |
$configPath = static::$basePath . 'config'; |
| 127 |
|
| 128 |
if (!file_exists($file = $configPath . '/app.php')) { |
| 129 |
die('The [config.php] file is missing from "' . $configPath . '" directory.'); |
| 130 |
} |
| 131 |
|
| 132 |
static::$config = array_merge(include $file, static::$config); |
| 133 |
|
| 134 |
if (!($autoload = @static::$config['autoload'])) { |
| 135 |
die('The [autoload] key is not specified or invalid in "' . $glueJson . '" file.'); |
| 136 |
} |
| 137 |
|
| 138 |
if (!($namespace = @$autoload['namespace'])) { |
| 139 |
die('The [namespace] key is not specified or invalid in "' . $glueJson . '" file.'); |
| 140 |
} |
| 141 |
|
| 142 |
$namespaceMapping = (array)@$autoload['mapping']; |
| 143 |
|
| 144 |
|
| 145 |
if (!$namespaceMapping) { |
| 146 |
die('The [mapping] key is not specified or invalid in "' . $glueJson . '" file.'); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register the autoloader |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public static function registerAutoLoader() |
| 155 |
{ |
| 156 |
if (!static::$config) { |
| 157 |
static::$config = json_decode(file_get_contents(static::$basePath . 'glue.json'), true); |
| 158 |
static::$config = array_merge(include static::$basePath . 'config/app.php', static::$config); |
| 159 |
} |
| 160 |
|
| 161 |
spl_autoload_register([__CLASS__, 'loader']); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Framework's custom autoloader |
| 166 |
* @param string $class |
| 167 |
* @return mixed |
| 168 |
*/ |
| 169 |
public static function loader($class) |
| 170 |
{ |
| 171 |
$namespace = static::$config['autoload']['namespace']; |
| 172 |
|
| 173 |
if (substr($class, 0, strlen($namespace)) !== $namespace) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
foreach (static::$config['autoload']['mapping'] as $key => $value) { |
| 178 |
$className = str_replace( |
| 179 |
array('\\', $key, $namespace), |
| 180 |
array('/', $value, $namespace), |
| 181 |
$class |
| 182 |
); |
| 183 |
|
| 184 |
$className = substr_replace($className, '', 0, strlen($namespace)); |
| 185 |
|
| 186 |
$file = static::$basePath . trim($className, '/') . '.php'; |
| 187 |
|
| 188 |
if (is_readable($file)) { |
| 189 |
return include $file; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Register "init" hook to run the plugin |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public static function registerApplication() |
| 199 |
{ |
| 200 |
add_action('plugins_loaded', function () { |
| 201 |
Application::run(static::$file, static::$config); |
| 202 |
do_action('fluentform_loaded'); |
| 203 |
}, 1); |
| 204 |
} |
| 205 |
} |
| 206 |
|