flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-builder
/
src
/
Builder
/
InfoActivationBuilder.php
InfoActivationBuilder.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\PluginBuilder\Builder; |
| 4 | |
| 5 | use FSVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin; |
| 6 | use FSVendor\WPDesk\PluginBuilder\Plugin\ActivationAware; |
| 7 | use FSVendor\WPDesk\PluginBuilder\Storage\PluginStorage; |
| 8 | /** |
| 9 | * Builder that have info about activations |
| 10 | * |
| 11 | * 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. |
| 12 | * |
| 13 | * @package WPDesk\PluginBuilder\Builder |
| 14 | */ |
| 15 | class InfoActivationBuilder extends AbstractBuilder |
| 16 | { |
| 17 | const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class'; |
| 18 | const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init'; |
| 19 | const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init'; |
| 20 | /** @var AbstractPlugin */ |
| 21 | private $plugin; |
| 22 | /** @var \WPDesk_Buildable */ |
| 23 | private $info; |
| 24 | /** @var string */ |
| 25 | protected $storage_id; |
| 26 | /** |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $is_active; |
| 30 | /** |
| 31 | * @param \WPDesk_Buildable $info |
| 32 | * @param bool $is_active |
| 33 | */ |
| 34 | public function __construct(\FSVendor\WPDesk_Buildable $info, $is_active) |
| 35 | { |
| 36 | $this->info = $info; |
| 37 | $this->storage_id = $info->get_class_name(); |
| 38 | $this->is_active = $is_active; |
| 39 | } |
| 40 | /** |
| 41 | * Builds instance of plugin |
| 42 | */ |
| 43 | public function build_plugin() |
| 44 | { |
| 45 | $class_name = apply_filters(self::FILTER_PLUGIN_CLASS, $this->info->get_class_name()); |
| 46 | /** @var AbstractPlugin $plugin */ |
| 47 | $this->plugin = new $class_name($this->info); |
| 48 | if ($this->plugin instanceof ActivationAware && $this->is_active) { |
| 49 | $this->plugin->set_active(); |
| 50 | } |
| 51 | return $this->plugin; |
| 52 | } |
| 53 | public function store_plugin(PluginStorage $storage) |
| 54 | { |
| 55 | $storage->add_to_storage($this->storage_id, $this->plugin); |
| 56 | } |
| 57 | public function init_plugin() |
| 58 | { |
| 59 | do_action(self::HOOK_BEFORE_PLUGIN_INIT, $this->plugin); |
| 60 | $this->plugin->init(); |
| 61 | do_action(self::HOOK_AFTER_PLUGIN_INIT, $this->plugin); |
| 62 | } |
| 63 | /** |
| 64 | * @return AbstractPlugin |
| 65 | */ |
| 66 | public function get_plugin() |
| 67 | { |
| 68 | return $this->plugin; |
| 69 | } |
| 70 | } |
| 71 |