diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..692f0ab7e24025fd0fbcfe1995fdbb9527252ebb --- /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 0000000000000000000000000000000000000000..7b646f31a378cfcdd28f263ad33d644b8d50e9b6 --- /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 7afa279fc239574d0f17720b1c1412becc3aa2d2..d4d44c8b83e7e27310775556ff10560eb89cc446 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 0000000000000000000000000000000000000000..f9cdac6938e424db972b4110875d352a7c567fb7 --- /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 e9a217a766ead233f62db8099b9ce8e915f38b18..d882da1e249b4f2b8e618a4b96304f2480b5517c 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 e0c1c1bb7d4107845019223393f84341ed96764d..96e0ada648f0d5edd01eedeb16abb5b34ec50558 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 c3a009884aa98c59111e2ee6270d9a7ed382fcfa..6e695c463035fd324b913a517855ae6cb0c42f91 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 bae150dd37e7a1c6784801c22f50da95539a27dd..17dead5a1b7e56096a0bdf2a3ce5e42d6f7ce323 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" ); } }