Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PluginDeactivationTaskManager | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| deactivate | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| add_to_queue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| remove_from_queue | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Installer\TaskManagers; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Installer\Data\Options; |
| 6 | use NewfoldLabs\WP\Module\Installer\Models\PriorityQueue; |
| 7 | use NewfoldLabs\WP\Module\Installer\Tasks\PluginDeactivationTask; |
| 8 | |
| 9 | /** |
| 10 | * Manages the execution of PluginDeactivationTasks. |
| 11 | */ |
| 12 | class PluginDeactivationTaskManager extends AbstractTaskManager { |
| 13 | |
| 14 | /** |
| 15 | * The name of the queue, might be prefixed. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected static $queue_name = 'plugin_deactivation_queue'; |
| 20 | |
| 21 | /** |
| 22 | * The name of the hook. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected static $hook_name = 'nfd_module_installer_plugin_deactivation_event'; |
| 27 | |
| 28 | /** |
| 29 | * Schedules the crons. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | parent::__construct(); |
| 33 | |
| 34 | // Thirty seconds cron hook |
| 35 | add_action( self::$hook_name, array( $this, 'deactivate' ) ); |
| 36 | |
| 37 | // Register the cron task |
| 38 | if ( ! wp_next_scheduled( self::$hook_name ) ) { |
| 39 | wp_schedule_single_event( time() + 5, self::$hook_name ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Queue out all the PluginDeactivationTask's in the plugin deactivation queue and execute them. |
| 46 | * |
| 47 | * @return array|false |
| 48 | */ |
| 49 | public function deactivate() { |
| 50 | /* |
| 51 | Get the plugins queued up to be deactivated, the PluginDeactivationTask gets |
| 52 | converted to an associative array before storing it in the option. |
| 53 | */ |
| 54 | $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() ); |
| 55 | \update_option( Options::get_option_name( self::$queue_name ), array() ); |
| 56 | $retries = array(); |
| 57 | foreach ( $plugins as $plugin ) { |
| 58 | $plugin_deactivation_task = new PluginDeactivationTask( |
| 59 | $plugin['slug'], |
| 60 | $plugin['priority'], |
| 61 | $plugin['retries'] |
| 62 | ); |
| 63 | $status = $plugin_deactivation_task->execute(); |
| 64 | if ( ! $status ) { |
| 65 | if ( $plugin_deactivation_task->get_retries() <= self::$retry_limit ) { |
| 66 | array_push( $retries, $plugin_deactivation_task->to_array() ); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | // Update the plugin deactivation queue. |
| 71 | $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() ); |
| 72 | array_merge( $plugins, $retries ); |
| 73 | \update_option( Options::get_option_name( self::$queue_name ), $plugins ); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds a new PluginDeactivationTask to the Plugin deactivation queue. |
| 80 | * The Task will be inserted at an appropriate position in the queue based on it's priority. |
| 81 | * |
| 82 | * @param PluginDeactivationTask $plugin_deactivation_task The task to be inserted. |
| 83 | * @return array|false |
| 84 | */ |
| 85 | public static function add_to_queue( PluginDeactivationTask $plugin_deactivation_task ) { |
| 86 | /* |
| 87 | Get the plugins queued up to be deactivated, the PluginDeactivationTask gets |
| 88 | converted to an associative array before storing it in the option. |
| 89 | */ |
| 90 | $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() ); |
| 91 | |
| 92 | $queue = new PriorityQueue(); |
| 93 | foreach ( $plugins as $queued_plugin ) { |
| 94 | /* |
| 95 | Check if there is an already existing PluginDeactivationTask in the queue |
| 96 | for a given slug. |
| 97 | */ |
| 98 | if ( $queued_plugin['slug'] === $plugin_deactivation_task->get_slug() ) { |
| 99 | return false; |
| 100 | } |
| 101 | $queue->insert( $queued_plugin, $queued_plugin['priority'] ); |
| 102 | } |
| 103 | |
| 104 | // Insert a new PluginDeactivationTask at the appropriate position in the queue. |
| 105 | $queue->insert( |
| 106 | $plugin_deactivation_task->to_array(), |
| 107 | $plugin_deactivation_task->get_priority() |
| 108 | ); |
| 109 | |
| 110 | return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Removes a PluginDeactivationTask from the queue. |
| 115 | * |
| 116 | * @param string $plugin The slug of the task to remove. |
| 117 | * @return array |
| 118 | */ |
| 119 | public static function remove_from_queue( $plugin ) { |
| 120 | /* |
| 121 | Get the plugins queued up to be deactivated, the PluginDeactivationTask task gets |
| 122 | converted to an associative array before storing it in the option. |
| 123 | */ |
| 124 | $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() ); |
| 125 | |
| 126 | $queue = new PriorityQueue(); |
| 127 | foreach ( $plugins as $queued_plugin ) { |
| 128 | /* |
| 129 | If the Plugin slug does not match add it back to the queue. |
| 130 | */ |
| 131 | if ( $queued_plugin['slug'] !== $plugin ) { |
| 132 | $queue->insert( $queued_plugin, $queued_plugin['priority'] ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() ); |
| 137 | } |
| 138 | } |