diff --git a/src/Plugin/AbstractPlugin.php b/src/Plugin/AbstractPlugin.php index 9678d0cf08adc17bcf7337db7df362af6987a9fd..70e12810314cc43182605f191820a6446a1b5c7a 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, Hookable { /** @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. * @@ -37,17 +42,35 @@ abstract class AbstractPlugin implements \WPDesk_Translable { 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. + */ + public function add_hookable( $hookable_object ) { + $hookable_object->set_plugin( $this ); + $this->hookable_objects[] = $hookable_object; + } + + /** + * Set plugin - currently do nothing. + * + * @param AbstractPlugin $plugin Plugin. + * + * @return null + */ + public function set_plugin( $plugin ) { + return; + } + /** * @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 +80,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..1506c1dabac5bac628d2ec8930dfb3be833eb7fb --- /dev/null +++ b/src/Plugin/Hookable.php @@ -0,0 +1,38 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +interface Hookable { + + /** + * Add hookable object. + * + * @param Hookable $hookable_object Hookable object to add. + */ + public function add_hookable( $hookable_object ); + + /** + * Set Plugin. + * + * @param AbstractPlugin $plugin Plugin. + * + * @return null + */ + public function set_plugin( $plugin ); + + /** + * Get plugin. + * + * @return AbstractPlugin. + */ + public function get_plugin(); + + /** + * Init hooks (actions and filters). + * + * @return null + */ + public function hooks(); + +} +