diff --git a/src/Plugin/AbstractPlugin.php b/src/Plugin/AbstractPlugin.php index 9678d0cf08adc17bcf7337db7df362af6987a9fd..480e706c68bfe6a1705ea65e388f28e29036e296 100644 --- a/src/Plugin/AbstractPlugin.php +++ b/src/Plugin/AbstractPlugin.php @@ -8,7 +8,7 @@ namespace WPDesk\PluginBuilder\Plugin; * @author Grzegorz * */ -abstract class AbstractPlugin implements \WPDesk_Translable { +abstract class AbstractPlugin implements \WPDesk_Translable, HookableCollection { /** @var \WPDesk_Plugin_Info */ protected $plugin_info; @@ -25,6 +25,11 @@ abstract class AbstractPlugin implements \WPDesk_Translable { /** @var string */ protected $settings_url; + /** + * @var array + */ + private $hookable_objects = array(); + /** * AbstractPlugin constructor. * @@ -35,19 +40,33 @@ abstract class AbstractPlugin implements \WPDesk_Translable { $this->plugin_namespace = strtolower( $plugin_info->get_plugin_dir() ); } + /** + * Init. + */ public function init() { $this->init_base_variables(); - $this->hooks(); } public function init_base_variables() { $this->plugin_url = plugin_dir_url( $this->plugin_info->get_plugin_dir() ); } + /** + * Add hookable object. + * + * @param Hookable|HookablePluginDependant $hookable_object Hookable object. + */ + public function add_hookable( $hookable_object ) { + if ( $hookable_object instanceof HookablePluginDependant ) { + $hookable_object->set_plugin( $this ); + } + $this->hookable_objects[] = $hookable_object; + } + /** * @return void */ - protected function hooks() { + public function hooks() { add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] ); @@ -57,6 +76,10 @@ abstract class AbstractPlugin implements \WPDesk_Translable { $this, 'links_filter' ] ); + /** @var Hookable $hookable_object */ + foreach ( $this->hookable_objects as $hookable_object ) { + $hookable_object->hooks(); + } } diff --git a/src/Plugin/Hookable.php b/src/Plugin/Hookable.php new file mode 100644 index 0000000000000000000000000000000000000000..60286828a0d41844a3cf55bf19ba998db93b6f12 --- /dev/null +++ b/src/Plugin/Hookable.php @@ -0,0 +1,15 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +interface Hookable { + + /** + * Init hooks (actions and filters). + * + * @return null + */ + public function hooks(); + +} + diff --git a/src/Plugin/HookableCollection.php b/src/Plugin/HookableCollection.php new file mode 100644 index 0000000000000000000000000000000000000000..15dd4297ac843f581d3deae9f6f7db1fb9d5bd58 --- /dev/null +++ b/src/Plugin/HookableCollection.php @@ -0,0 +1,15 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +interface HookableCollection extends Hookable { + + /** + * Add hookable object. + * + * @param Hookable $hookable_object Hookable object to add. + */ + public function add_hookable( $hookable_object ); + +} + diff --git a/src/Plugin/HookablePuginDependant.php b/src/Plugin/HookablePuginDependant.php new file mode 100644 index 0000000000000000000000000000000000000000..9f2da43096dd7f01e47bdbdc05b5d59f91227d55 --- /dev/null +++ b/src/Plugin/HookablePuginDependant.php @@ -0,0 +1,24 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +interface HookablePluginDependant extends Hookable { + + /** + * Set Plugin. + * + * @param AbstractPlugin $plugin Plugin. + * + * @return null + */ + public function set_plugin( $plugin ); + + /** + * Get plugin. + * + * @return AbstractPlugin. + */ + public function get_plugin(); + +} +