Skip to content

Commit

Permalink
Introduced AbstractTaskManager to centralize common behaviors of al…
Browse files Browse the repository at this point in the history
…l Task Manager classes, reducing redundancy and improving code clarity and maintainability.

Modified Task Managers to instantiate only when their associated WP Cron hooks are scheduled, optimizing resource usage and avoiding errors about unhandled hooks.
Ensured recurring WP Cron hooks are cleared after completion using `wp_clear_scheduled_hook`, preventing unnecessary executions.
Added `TaskManagerSchedules` class to register new custom schedules for WP Cron jobs via filters, enabling more flexible scheduling for various Task Managers.
  • Loading branch information
GiuseppeArcifa committed Dec 13, 2024
1 parent 86aa546 commit 53ac0db
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 205 deletions.
69 changes: 69 additions & 0 deletions includes/TaskManagers/AbstractTaskManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace NewfoldLabs\WP\Module\Installer\TaskManagers;

use NewfoldLabs\WP\Module\Installer\Data\Options;

/**
* Manages the execution of Tasks.
*/
abstract class AbstractTaskManager {

/**
* The number of times a PluginUninstallTask can be retried.
*
* @var int
*/
protected static $retry_limit = 1;

/**
* Name of the Queue.
*
* @var string
*/
protected static $queue_name = '';

/**
* Name of the Hook.
*
* @var string
*/
protected static $hook_name = '';

/**
* PluginUninstallTaskManager constructor.
*/
public function __construct() {
TaskManagerSchedules::init();
}

/**
* Retrieve the Queue Name for the TaskManager.
*
* @return string
*/
public static function get_queue_name() {
return static::$queue_name;
}


/**
* Retrieve the Hook Name for the TaskManager.
*
* @return string
*/
public static function get_hook_name() {
return static::$hook_name;
}


/**
* Returns the status of given plugin slug - uninstalling/completed.
*
* @param string $plugin Plugin Slug
* @return string|false
*/
public static function status( $plugin ) {
$plugins = \get_option( Options::get_option_name( static::$queue_name ), array() );
return array_search( $plugin, array_column( $plugins, 'slug' ), true );
}
}
61 changes: 24 additions & 37 deletions includes/TaskManagers/PluginActivationTaskManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\TaskManagers;

use NewfoldLabs\WP\Module\Installer\Data\Options;
Expand All @@ -8,46 +9,43 @@
/**
* Manages the execution of PluginActivationTasks.
*/
class PluginActivationTaskManager {

class PluginActivationTaskManager extends AbstractTaskManager {
/**
* The number of times a PluginActivationTask can be retried.
* The number of times Hook can be retried.
*
* @var int
*/
private static $retry_limit = 1;
protected static $retry_limit = 1;

/**
* The name of the queue, might be prefixed.
*
* @var string
*/
private static $queue_name = 'plugin_activation_queue';
protected static $queue_name = 'plugin_activation_queue';

/**
* The name of the hook, might be prefixed.
*
* @var string
*/
protected static $hook_name = 'nfd_module_installer_plugin_activation_event';

/**
* Schedules the crons.
*/
public function __construct() {
parent::__construct();

// Thirty second cron hook
add_action( 'nfd_module_installer_plugin_activation_event', array( $this, 'activate' ) );
// Thirty seconds cron hook
add_action( self::$hook_name, array( $this, 'activate' ) );

if ( ! wp_next_scheduled( 'nfd_module_installer_plugin_activation_event' ) ) {
wp_schedule_single_event( time() + 5, 'nfd_module_installer_plugin_activation_event' );
if ( ! wp_next_scheduled( self::$hook_name ) ) {
wp_schedule_single_event( time() + 5, self::$hook_name );
}
}


/**
* Returns the queue name, might be prefixed.
*
* @return string
*/
public static function get_queue_name() {
return self::$queue_name;
}


/**
* Queue out all the PluginActivationTask's in the plugin activation queue and execute them.
*
Expand All @@ -64,9 +62,9 @@ public function activate() {
$retries = array();
foreach ( $plugins as $plugin ) {
$plugin_activation_task = new PluginActivationTask(
$plugin['slug'],
$plugin['priority'],
$plugin['retries']
$plugin[ 'slug' ],
$plugin[ 'priority' ],
$plugin[ 'retries' ]
);
$status = $plugin_activation_task->execute();
if ( is_wp_error( $status ) ) {
Expand Down Expand Up @@ -103,10 +101,10 @@ public static function add_to_queue( PluginActivationTask $plugin_activation_tas
Check if there is an already existing PluginActivationTask in the queue
for a given slug.
*/
if ( $queued_plugin['slug'] === $plugin_activation_task->get_slug() ) {
if ( $queued_plugin[ 'slug' ] === $plugin_activation_task->get_slug() ) {
return false;
}
$queue->insert( $queued_plugin, $queued_plugin['priority'] );
$queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] );
}

// Insert a new PluginActivationTask at the appropriate position in the queue.
Expand Down Expand Up @@ -136,22 +134,11 @@ public static function remove_from_queue( $plugin ) {
/*
If the Plugin slug does not match add it back to the queue.
*/
if ( $queued_plugin['slug'] !== $plugin ) {
$queue->insert( $queued_plugin, $queued_plugin['priority'] );
if ( $queued_plugin[ 'slug' ] !== $plugin ) {
$queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] );
}
}

return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
}

/**
* Get the status of a given plugin slug from the queue.
*
* @param string $plugin The slug of the plugin.
* @return boolean
*/
public static function status( $plugin ) {
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
return array_search( $plugin, array_column( $plugins, 'slug' ), true );
}
}
55 changes: 19 additions & 36 deletions includes/TaskManagers/PluginDeactivationTaskManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\TaskManagers;

use NewfoldLabs\WP\Module\Installer\Data\Options;
Expand All @@ -8,44 +9,37 @@
/**
* Manages the execution of PluginDeactivationTasks.
*/
class PluginDeactivationTaskManager {
class PluginDeactivationTaskManager extends AbstractTaskManager {

/**
* The number of times a PluginDeactivationTask can be retried.
* The name of the queue, might be prefixed.
*
* @var int
* @var string
*/
private static $retry_limit = 1;
protected static $queue_name = 'plugin_deactivation_queue';

/**
* The name of the queue, might be prefixed.
* The name of the hook.
*
* @var string
*/
private static $queue_name = 'plugin_deactivation_queue';
protected static $hook_name = 'nfd_module_installer_plugin_deactivation_event';

/**
* Schedules the crons.
*/
public function __construct() {
parent::__construct();

// Thirty second cron hook
add_action( 'nfd_module_installer_plugin_deactivation_event', array( $this, 'deactivate' ) );
// Thirty seconds cron hook
add_action( self::$hook_name, array( $this, 'deactivate' ) );

// Register the cron task
if ( ! wp_next_scheduled( 'nfd_module_installer_plugin_deactivation_event' ) ) {
wp_schedule_single_event( time() + 5, 'nfd_module_installer_plugin_deactivation_event' );
if ( ! wp_next_scheduled( self::$hook_name ) ) {
wp_schedule_single_event( time() + 5, self::$hook_name );
}
}

/**
* Returns the queue name, might be prefixed.
*
* @return string
*/
public static function get_queue_name() {
return self::$queue_name;
}

/**
* Queue out all the PluginDeactivationTask's in the plugin deactivation queue and execute them.
Expand All @@ -62,9 +56,9 @@ public function deactivate() {
$retries = array();
foreach ( $plugins as $plugin ) {
$plugin_deactivation_task = new PluginDeactivationTask(
$plugin['slug'],
$plugin['priority'],
$plugin['retries']
$plugin[ 'slug' ],
$plugin[ 'priority' ],
$plugin[ 'retries' ]
);
$status = $plugin_deactivation_task->execute();
if ( ! $status ) {
Expand Down Expand Up @@ -101,10 +95,10 @@ public static function add_to_queue( PluginDeactivationTask $plugin_deactivation
Check if there is an already existing PluginDeactivationTask in the queue
for a given slug.
*/
if ( $queued_plugin['slug'] === $plugin_deactivation_task->get_slug() ) {
if ( $queued_plugin[ 'slug' ] === $plugin_deactivation_task->get_slug() ) {
return false;
}
$queue->insert( $queued_plugin, $queued_plugin['priority'] );
$queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] );
}

// Insert a new PluginDeactivationTask at the appropriate position in the queue.
Expand Down Expand Up @@ -134,22 +128,11 @@ public static function remove_from_queue( $plugin ) {
/*
If the Plugin slug does not match add it back to the queue.
*/
if ( $queued_plugin['slug'] !== $plugin ) {
$queue->insert( $queued_plugin, $queued_plugin['priority'] );
if ( $queued_plugin[ 'slug' ] !== $plugin ) {
$queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] );
}
}

return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
}

/**
* Get the status of a given plugin slug from the queue.
*
* @param string $plugin The slug of the plugin.
* @return boolean
*/
public static function status( $plugin ) {
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
return array_search( $plugin, array_column( $plugins, 'slug' ), true );
}
}
Loading

0 comments on commit 53ac0db

Please sign in to comment.