Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions openpub-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
* plugin overrides. The plugins_loaded action hook fires early, and precedes the setup_theme, after_setup_theme, init
* and wp_loaded action hooks.
*/
\add_action('plugins_loaded', function () {
$plugin = (new OWC\OpenPub\Base\Foundation\Plugin(__DIR__))->boot();
do_action('owc/openpub-base/plugin', $plugin);
add_action('plugins_loaded', function () {
$plugin = (new OWC\OpenPub\Base\Foundation\Plugin(__DIR__));

add_action('after_setup_theme', function() use ($plugin) {
$plugin->boot();
do_action('owc/openpub-base/plugin', $plugin);
});

}, 10);
89 changes: 29 additions & 60 deletions src/Base/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,54 @@
use OWC\OpenPub\Base\Settings\SettingsPageOptions;
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;

/**
* BasePlugin which sets all the serviceproviders.
*/
class Plugin
{
/**
* Name of the plugin.
*/
public const NAME = 'openpub-base';

/**
* Version of the plugin.
* Used for setting versions of enqueue scripts and styles.
*/
public const VERSION = '3.5.2';

/**
* Path to the root of the plugin.
*/
protected string $rootPath;

/**
* Instance of the configuration repository.
*/
public Config $config;

/**
* Instance of the Hook loader.
*/
public Loader $loader;

/**
* Instance of the Settings object.
*/
public SettingsPageOptions $settings;

/**
* Constructor of the BasePlugin
*/
public function __construct(string $rootPath)
{
$this->rootPath = $rootPath;
\load_plugin_textdomain($this->getName(), false, $this->getName() . '/languages/');

$this->loader = new Loader();

$this->config = new Config($this->rootPath . '/config');
$this->config->setProtectedNodes(['core']);
$this->config->boot();

$this->settings = SettingsPageOptions::make();
}

/**
* Boot the plugin.
*
* @hook plugins_loaded
*
* @return bool
*/
public function boot(): bool
{
$this->loadTextDomain();
$this->config->boot();

$dependencyChecker = new DependencyChecker($this->config->get('core.dependencies'));

if ($dependencyChecker->failed()) {
$dependencyChecker->notify();
\deactivate_plugins(\plugin_basename($this->rootPath . '/' . $this->getName() . '.php'));
deactivate_plugins(\plugin_basename($this->rootPath . '/' . $this->getName() . '.php'));

return false;
}

$this->checkForUpdate();
$this->registerProviders();

// Set up service providers
$this->callServiceProviders('register');

if (\is_admin()) {
$this->callServiceProviders('register', 'admin');
$this->callServiceProviders('boot', 'admin');
}

if ('cli' === php_sapi_name()) {
$this->callServiceProviders('register', 'cli');
$this->callServiceProviders('boot', 'cli');
}

$this->callServiceProviders('boot');

// Register the Hook loader.
$this->loader->addAction('init', $this, 'filterPlugin', 4);
$this->loader->register();

return true;
}

protected function checkForUpdate()
public function loadTextDomain(): void
{
load_plugin_textdomain($this->getName(), false, $this->getName() . '/languages/');
}

protected function checkForUpdate(): void
{
if (! class_exists(PucFactory::class) || $this->isExtendedClass()) {
return;
Expand All @@ -119,6 +71,23 @@ protected function checkForUpdate()
}
}

protected function registerProviders(): void
{
$this->callServiceProviders('register');

if (\is_admin()) {
$this->callServiceProviders('register', 'admin');
$this->callServiceProviders('boot', 'admin');
}

if ('cli' === php_sapi_name()) {
$this->callServiceProviders('register', 'cli');
$this->callServiceProviders('boot', 'cli');
}

$this->callServiceProviders('boot');
}

/**
* Check if current class extends parent class.
*
Expand All @@ -135,7 +104,7 @@ protected function isExtendedClass(): bool
*/
public function filterPlugin(): void
{
\do_action('owc/' . self::NAME . '/plugin', $this);
do_action('owc/' . self::NAME . '/plugin', $this);
}

/**
Expand Down
Loading