Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defensive checks in cron rescheduling #53

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions includes/TaskManagers/AbstractTaskManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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 );
}
}
47 changes: 17 additions & 30 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 Down Expand Up @@ -143,15 +141,4 @@ public static function remove_from_queue( $plugin ) {

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 );
}
}
41 changes: 12 additions & 29 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 Down Expand Up @@ -141,15 +135,4 @@ public static function remove_from_queue( $plugin ) {

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 );
}
}
58 changes: 25 additions & 33 deletions includes/TaskManagers/PluginInstallTaskManager.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,36 @@
/**
* Manages the execution of PluginInstallTasks.
*/
class PluginInstallTaskManager {
class PluginInstallTaskManager extends AbstractTaskManager {

/**
* The number of times a PluginInstallTask 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_install_queue';

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

/**
* Schedules the crons.
*/
public function __construct() {
// Ensure there is a thirty second option in the cron schedules
add_filter( 'cron_schedules', array( $this, 'add_thirty_seconds_schedule' ) );
parent::__construct();

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

// Register the cron task
if ( ! wp_next_scheduled( 'nfd_module_installer_plugin_install_cron' ) ) {
wp_schedule_event( time(), 'thirty_seconds', 'nfd_module_installer_plugin_install_cron' );
if ( ! wp_next_scheduled( self::$hook_name ) ) {
wp_schedule_event( time(), 'thirty_seconds', self::$hook_name );
}
}

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

/**
* Adds a 30 second cron schedule.
Expand All @@ -69,7 +60,7 @@ public function add_thirty_seconds_schedule( $schedules ) {
/**
* Queue out a PluginInstallTask with the highest priority in the plugin install queue and execute it.
*
* @return array|false
* @void
*/
public function install() {
/*
Expand All @@ -83,8 +74,11 @@ public function install() {
priority at the beginning of the array
*/
$plugin_to_install = array_shift( $plugins );

if ( ! $plugin_to_install ) {
return true;
self::complete();

return;
}

// Update the plugin install queue.
Expand Down Expand Up @@ -123,12 +117,10 @@ public function install() {
}
}

// If there are no more plugins to be installed then change the status to completed.
// If there are no more plugins to be installed then change the status to complete.
if ( empty( $plugins ) ) {
return \update_option( Options::get_option_name( 'plugins_init_status' ), 'completed' );
self::complete();
}

return true;
}

/**
Expand Down Expand Up @@ -191,15 +183,15 @@ public static function remove_from_queue( $plugin ) {
return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
}


/**
* Get the status of a given plugin slug from the queue.
* Clear all the hook scheduling and update the status option
*
* @param string $plugin The slug of the plugin.
* @return boolean
* @return bool
*/
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 );
private static function complete() {
wp_clear_scheduled_hook( 'nfd_module_installer_plugin_install_cron' );
return \update_option( Options::get_option_name( 'plugins_init_status' ), 'completed' );
}

/**
Expand Down
Loading
Loading