Skip to content

Commit

Permalink
add uninstall and deactivation hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
freibergergarcia committed Sep 29, 2023
1 parent e2d0327 commit 1bd58b9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
53 changes: 51 additions & 2 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 ) {
Expand All @@ -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 {
}
}
10 changes: 9 additions & 1 deletion wordpress-related.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +52,8 @@ function bootstrap_plugin(): void {
$container = $container_builder->build();

$plugin = Plugin_Factory::create( $container );
$plugin->register();
$plugin->boot();
$plugin->init();
}

try {
Expand Down

0 comments on commit 1bd58b9

Please sign in to comment.