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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
<?php
namespace Tev\Plugin;
use Closure;
use wpdb;
use Tev\Application\Application,
Tev\View\Renderer;
/**
* Simple plugin loading utility.
*
* Provides framework for loading plugin content by convention, as follows:
*
* - Custom post types are loaded from an array, defined in `config/post_types.php`
* in the plugin's directory. The array is a set of key value pairs, where
* the key is the post type identifier and the value is the config to pass
* to `register_post_type`
*
* - Custom field groups are loaded from an array, in `config/field_groups.php`
* in the plugin's directory. The array is a set of arrays, defining each
* field group config to pass to `register_field_group`
*
* - ACF JSON config is loaded from a directory, at `config/acf-json/`
* in the plugin's directory. See http://www.advancedcustomfields.com/resources/local-json/
* for more information on this
*
* - Action callbacks are loaded from an array, in `config/actions.php` in the
* plugin's directory. The array is a set of key-value pairs, of action names
* to closure or action provider classes
*
* - Shortcodes are loaded from an array, in `config/shortcodes.php` in the
* plugin's directory. The array is a set of key value pairs, where
* the key is the shortcode name and the value is the config to pass
* to `add_shortcode`
*
* - Custom WP CLI commands are loaded from an array, defined in `config/commands.php`
* in the plugin's directory. The array is a set of key value pairs, where the
* key is the command indentifier and the value is the fully-qualified class
* name
*
* - Custom database table installers are loaded from an array, defined in
* `config/tables.php` in the plugin's directory. The array is a set of
* fully-qualified installer class names
*
* Usage:
*
* ```php
* // Add the following to your plugin's config file
*
* tev_fetch('plugin_loader')->load(__DIR__);
* ```
*
*/
class Loader
{
/**
* Application.
*
* @var \Tev\Application\Application
*/
protected $app;
/**
* Plugin base path.
*
* @var string
*/
protected $basePath;
/**
* View renderer.
*
* @var \Tev\View\Renderer
*/
protected $renderer;
/**
* Constructor.
*
* Inject dependencies.
*
* @param \Tev\Application\Application $app Application
* @return void
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Load all plugin configuration.
*
* @param string $basePath Plugin path
* @return \Tev\Plugin\Loader This, for chaining
*/
public function load($basePath)
{
$this->basePath = $basePath;
$this->renderer = new Renderer($this->getViewsPath());
return
$this
->loadCustomTables()
->loadPostTypes()
->loadFieldGroups()
->loadAcfJson()
->loadActions()
->loadShortCodes()
->loadOptionScreens()
->loadCliCommands();
}
/**
* Load custom database table installers from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadCustomTables()
{
if ($config = $this->loadConfigFile('tables.php')) {
$app = $this->app;
foreach ($config as $installerClass) {
if (is_string($installerClass) && is_subclass_of($installerClass, 'Tev\Database\CustomTables\AbstractInstaller')) {
register_activation_hook($this->getPluginFile(), function () use ($installerClass, $app) {
global $wpdb;
$installer = new $installerClass($wpdb, $app);
$installer->install();
});
add_action('plugins_loaded', function () use ($installerClass, $app) {
global $wpdb;
$installer = new $installerClass($wpdb, $app);
$installer->update();
});
}
}
}
return $this;
}
/**
* Load custom post types from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadPostTypes()
{
if ($config = $this->loadConfigFile('post_types.php')) {
$callbacks = array();
// Create one callback for each post type, and register in
// init action
foreach ($config as $postTypeName => $args) {
$callbacks[] = $cb = function () use ($postTypeName, $args) {
register_post_type($postTypeName, $args);
};
add_action('init', $cb, 0);
}
// Flush URL caches for (you need to register custom post types
// first)
register_activation_hook($this->getPluginFile(), function () use ($callbacks) {
foreach ($callbacks as $cb) {
$cb();
}
flush_rewrite_rules();
});
}
return $this;
}
/**
* Load custom field groups from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadFieldGroups()
{
if (function_exists('register_field_group') && ($config = $this->loadConfigFile('field_groups.php'))) {
foreach ($config as $fieldGroupConfig) {
register_field_group($fieldGroupConfig);
}
}
return $this;
}
/**
* Load actions from configuration providers.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadActions()
{
if ($config = $this->loadConfigFile('actions.php')) {
$app = $this->app;
$renderer = $this->renderer;
foreach ($config as $actionName => $provider) {
if (is_string($provider) && is_subclass_of($provider, 'Tev\Plugin\Action\AbstractProvider')) {
$ap = new $provider($this->app, $this->renderer);
add_action($actionName, function () use ($ap) {
return call_user_func_array(array($ap, 'action'), func_get_args());
}, $ap->priority(), $ap->numArgs());
} elseif ($provider instanceof Closure) {
add_action($actionName, function () use ($provider)
{
return call_user_func($provider, func_get_args());
});
}
}
}
return $this;
}
/**
* Load ACF JSON config if supplied.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadAcfJson()
{
$config = $this->getConfigPath() . '/acf-json';
if (file_exists($config)) {
add_filter('acf/settings/load_json', function ($paths) use ($config) {
$paths[] = $config;
return $paths;
});
}
return $this;
}
/**
* Load shortcodes from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadShortCodes()
{
if ($config = $this->loadConfigFile('shortcodes.php')) {
$renderer = $this->renderer;
$app = $this->app;
foreach ($config as $shortcode => $provider) {
add_shortcode($shortcode, function ($attrs, $content) use ($app, $provider, $renderer)
{
if (is_string($provider) && is_subclass_of($provider, 'Tev\Plugin\Shortcode\AbstractProvider')) {
$sp = new $provider($app, $renderer);
return $sp->shortcode($attrs, $content);
} elseif ($provider instanceof Closure) {
return $provider($attrs, $content, $renderer);
}
});
}
}
return $this;
}
/**
* Load custom option screens from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadOptionScreens()
{
if (function_exists('acf_add_options_page') && ($config = $this->loadConfigFile('option_screens.php'))) {
foreach ($config as $optionScreenConfig) {
acf_add_options_page($optionScreenConfig);
}
}
return $this;
}
/**
* Load custom WP CLI commands from configuration files.
*
* @return \Tev\Plugin\Loader This, for chaining
*/
protected function loadCliCommands()
{
if (defined('WP_CLI') && WP_CLI && ($config = $this->loadConfigFile('commands.php'))) {
foreach ($config as $command => $className) {
\WP_CLI::add_command($command, $className);
}
}
return $this;
}
/**
* Get the path to the config directory.
*
* @return string
*/
protected function getConfigPath()
{
return $this->basePath . '/config';
}
/**
* Get the path to the config directory.
*
* @return string
*/
protected function getSrcPath()
{
return $this->basePath . '/src';
}
/**
* Get the path to the config directory.
*
* @return string
*/
protected function getViewsPath()
{
return $this->basePath . '/src/views';
}
/**
* Get the full path and filename of the plugin's registration file.
*
* @return string
*/
protected function getPluginFile()
{
return $this->basePath . '/' . strtolower(basename($this->basePath)) . '.php';
}
/**
* Load a config file from the config directory.
*
* @param string $file Filename
* @return array|null Array config or null if not found
*/
protected function loadConfigFile($file)
{
$path = $this->getConfigPath() . '/' . $file;
if (file_exists($path)) {
$config = include $path;
return is_array($config) ? $config : null;
}
return null;
}
}