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