diff --git a/includes/Plugin.php b/includes/Plugin.php index 8d7531d..d9c8b8c 100644 --- a/includes/Plugin.php +++ b/includes/Plugin.php @@ -41,7 +41,20 @@ public function __construct( Container $container ) { } /** - * Initialize the plugin. + * Bootstraps the plugin, registers hooks and services. + * + * This method is intended to be called to kickstart the plugin setup. + * It registers the necessary hooks and initializes the services required for the plugin to work. + * + * @throws Exception If a service fails to register. + */ + public function boot(): void { + $this->register_hooks(); + $this->register_services(); + } + + /** + * Initialize the plugin hooks | actions and filters. * * @return void */ @@ -54,7 +67,7 @@ public function init(): void { * @return void * @throws Exception */ - public function register(): void { + public function register_services(): void { $services = $this->container->getKnownEntryNames(); foreach ( $services as $service ) { @@ -70,4 +83,40 @@ public function register(): void { } } } + + /** + * Register Infrastructure hooks. + * + * @return void + */ + public function register_hooks(): void { + register_activation_hook( WORDPRESS_RELATED_FILE, array( $this, 'on_activation' ) ); + register_deactivation_hook( WORDPRESS_RELATED_FILE, array( $this, 'on_deactivation' ) ); + } + + /** + * Activation hook callback. + * + * @return void + */ + public function on_activation(): void { + } + + /** + * Deactivation hook callback. + * + * @return void + */ + public function on_deactivation(): void { + wp_cache_flush(); + wp_rewrite_flush(); + } + + /** + * Uninstall hook callback. + * + * @return void + */ + public static function on_uninstall(): void { + } } diff --git a/wordpress-related.php b/wordpress-related.php index eb39bcf..ba66e81 100644 --- a/wordpress-related.php +++ b/wordpress-related.php @@ -31,6 +31,13 @@ } require_once __DIR__ . '/vendor/autoload.php'; +if ( ! defined( 'WORDPRESS_RELATED_FILE' ) ) { + define( 'WORDPRESS_RELATED_FILE', __FILE__ ); +} + +if ( class_exists( 'WordPress_Related\Plugin' ) ) { + register_uninstall_hook( __FILE__, [ 'WordPress_Related\Plugin', 'on_uninstall' ] ); +} /** * Activate Plugin @@ -45,7 +52,8 @@ function bootstrap_plugin(): void { $container = $container_builder->build(); $plugin = Plugin_Factory::create( $container ); - $plugin->register(); + $plugin->boot(); + $plugin->init(); } try {