From 7823c89d1713af06e37d6e4bfb89c9600986230d Mon Sep 17 00:00:00 2001 From: dyszczo <krzysiek@inspirelabs.pl> Date: Wed, 26 Jun 2019 17:56:16 +0200 Subject: [PATCH] activation aware builder --- CHANGELOG.md | 3 + src/Builder/InfoActivationBuilder.php | 82 +++++++++++++++++++++++++++ src/Builder/InfoBuilder.php | 5 ++ src/Plugin/ActivationAware.php | 27 +++++++++ src/Plugin/ActivationTracker.php | 6 +- src/Plugin/PluginAccess.php | 3 + src/Plugin/TemplateLoad.php | 5 ++ src/Storage/StaticStorage.php | 8 ++- 8 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 src/Builder/InfoActivationBuilder.php create mode 100644 src/Plugin/ActivationAware.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..692f0ab --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## [1.2.0] - 2019-06-26 +### Added +- InfoActivationBuilder with capability to set info if plugin subscription is active diff --git a/src/Builder/InfoActivationBuilder.php b/src/Builder/InfoActivationBuilder.php new file mode 100644 index 0000000..7b646f3 --- /dev/null +++ b/src/Builder/InfoActivationBuilder.php @@ -0,0 +1,82 @@ +<?php + +namespace WPDesk\PluginBuilder\Builder; + +use WPDesk\PluginBuilder\Plugin\AbstractPlugin; +use WPDesk\PluginBuilder\Plugin\ActivationAware; +use WPDesk\PluginBuilder\Storage\PluginStorage; + +/** + * Builder that have info about activations + * + * Warning: We can't extend InfoBuilder.php as some old plugins(without wp-flow) will load the old version od InfoBuilder class that have private plugin property. + * + * @package WPDesk\PluginBuilder\Builder + */ +class InfoActivationBuilder extends AbstractBuilder +{ + const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class'; + const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init'; + const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init'; + + /** @var AbstractPlugin */ + private $plugin; + + /** @var \WPDesk_Buildable */ + private $info; + + /** @var string */ + protected $storage_id; + + /** + * @var bool + */ + private $is_active; + + /** + * @param \WPDesk_Buildable $info + * @param bool $is_active + */ + public function __construct(\WPDesk_Buildable $info, $is_active) + { + $this->info = $info; + $this->storage_id = $info->get_class_name(); + $this->is_active = $is_active; + } + + /** + * Builds instance of plugin + */ + public function build_plugin() + { + $class_name = apply_filters(self::FILTER_PLUGIN_CLASS, $this->info->get_class_name()); + + /** @var AbstractPlugin $plugin */ + $this->plugin = new $class_name($this->info); + if ($this->plugin instanceof ActivationAware && $this->is_active) { + $this->plugin->set_active(); + } + + return $this->plugin; + } + + public function store_plugin(PluginStorage $storage) + { + $storage->add_to_storage($this->storage_id, $this->plugin); + } + + public function init_plugin() + { + do_action(self::HOOK_BEFORE_PLUGIN_INIT, $this->plugin); + $this->plugin->init(); + do_action(self::HOOK_AFTER_PLUGIN_INIT, $this->plugin); + } + + /** + * @return AbstractPlugin + */ + public function get_plugin() + { + return $this->plugin; + } +} \ No newline at end of file diff --git a/src/Builder/InfoBuilder.php b/src/Builder/InfoBuilder.php index 7afa279..d4d44c8 100644 --- a/src/Builder/InfoBuilder.php +++ b/src/Builder/InfoBuilder.php @@ -5,6 +5,11 @@ namespace WPDesk\PluginBuilder\Builder; use WPDesk\PluginBuilder\Plugin\AbstractPlugin; use WPDesk\PluginBuilder\Storage\PluginStorage; +/** + * @deprecated Should not be used as some old plugins are using it and we can't touch this. + * + * @package WPDesk\PluginBuilder\Builder + */ class InfoBuilder extends AbstractBuilder { const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class'; const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init'; diff --git a/src/Plugin/ActivationAware.php b/src/Plugin/ActivationAware.php new file mode 100644 index 0000000..f9cdac6 --- /dev/null +++ b/src/Plugin/ActivationAware.php @@ -0,0 +1,27 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +/** + * It means that this class is should know about subscription activation + * + * @package WPDesk\PluginBuilder\Plugin + */ +interface ActivationAware { + + /** + * Set the activation flag to true + * + * @return void + */ + public function set_active(); + + /** + * Is subscription active? + * + * @return bool + */ + public function is_active(); + +} + diff --git a/src/Plugin/ActivationTracker.php b/src/Plugin/ActivationTracker.php index e9a217a..d882da1 100644 --- a/src/Plugin/ActivationTracker.php +++ b/src/Plugin/ActivationTracker.php @@ -2,7 +2,11 @@ namespace WPDesk\PluginBuilder\Plugin; - +/** + * @deprecated nobody uses it :) And also this library is not a place for this class + * + * @package WPDesk\PluginBuilder\Plugin + */ class ActivationTracker { /** diff --git a/src/Plugin/PluginAccess.php b/src/Plugin/PluginAccess.php index e0c1c1b..96e0ada 100644 --- a/src/Plugin/PluginAccess.php +++ b/src/Plugin/PluginAccess.php @@ -2,6 +2,9 @@ namespace WPDesk\PluginBuilder\Plugin; +/** + * @package WPDesk\PluginBuilder\Plugin + */ trait PluginAccess { /** * Plugin. diff --git a/src/Plugin/TemplateLoad.php b/src/Plugin/TemplateLoad.php index c3a0098..6e695c4 100644 --- a/src/Plugin/TemplateLoad.php +++ b/src/Plugin/TemplateLoad.php @@ -2,6 +2,11 @@ namespace WPDesk\PluginBuilder\Plugin; +/** + * @deprecated Use wpdesk/wp-view + * + * @package WPDesk\PluginBuilder\Plugin + */ trait TemplateLoad { /** diff --git a/src/Storage/StaticStorage.php b/src/Storage/StaticStorage.php index bae150d..17dead5 100644 --- a/src/Storage/StaticStorage.php +++ b/src/Storage/StaticStorage.php @@ -4,6 +4,11 @@ namespace WPDesk\PluginBuilder\Storage; use WPDesk\PluginBuilder\Plugin\AbstractPlugin; +/** + * Can store plugin instances in static variable + * + * @package WPDesk\PluginBuilder\Storage + */ class StaticStorage implements PluginStorage { protected static $instances = []; @@ -26,9 +31,8 @@ class StaticStorage implements PluginStorage { public function get_from_storage( $class ) { if ( isset( self::$instances[ $class ] ) ) { return self::$instances[ $class ]; - } else { - throw new Exception\ClassNotExists( "Class {$class} not exists in storage" ); } + throw new Exception\ClassNotExists( "Class {$class} not exists in storage" ); } } -- GitLab