From 86aa546f7904a3899f24adf1d1a2f7a813647fc5 Mon Sep 17 00:00:00 2001 From: Giuseppe Arcifa Date: Wed, 11 Dec 2024 17:17:15 +0100 Subject: [PATCH 1/3] Prevented rescheduling of plugin installation task after its completion --- .../TaskManagers/PluginInstallTaskManager.php | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/includes/TaskManagers/PluginInstallTaskManager.php b/includes/TaskManagers/PluginInstallTaskManager.php index f09c9c2..dc1ece8 100644 --- a/includes/TaskManagers/PluginInstallTaskManager.php +++ b/includes/TaskManagers/PluginInstallTaskManager.php @@ -28,13 +28,12 @@ class PluginInstallTaskManager { * Schedules the crons. */ public function __construct() { - // Ensure there is a thirty second option in the cron schedules + // Ensure there is a thirty seconds option in the cron schedules add_filter( 'cron_schedules', array( $this, 'add_thirty_seconds_schedule' ) ); - // Thirty second cron hook + // Thirty seconds cron hook add_action( 'nfd_module_installer_plugin_install_cron', 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' ); } @@ -69,7 +68,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() { /* @@ -83,8 +82,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. @@ -123,12 +125,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; } /** @@ -202,6 +202,16 @@ public static function status( $plugin ) { return array_search( $plugin, array_column( $plugins, 'slug' ), true ); } + /** + * Clear all the hook scheduling and update the status option + * + * @return bool + */ + 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' ); + } + /** * Reset the Plugin install status and the queue. * From 53ac0db900bcd142d7fa11cd100e2252e99e2fa5 Mon Sep 17 00:00:00 2001 From: Giuseppe Arcifa Date: Fri, 13 Dec 2024 18:07:41 +0100 Subject: [PATCH 2/3] Introduced `AbstractTaskManager` to centralize common behaviors of all 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. --- includes/TaskManagers/AbstractTaskManager.php | 69 ++++++++++++++ .../PluginActivationTaskManager.php | 61 +++++-------- .../PluginDeactivationTaskManager.php | 55 ++++-------- .../TaskManagers/PluginInstallTaskManager.php | 60 +++++-------- .../PluginUninstallTaskManager.php | 59 +++++------- includes/TaskManagers/TaskManager.php | 8 +- .../TaskManagers/TaskManagerSchedules.php | 57 ++++++++++++ .../TaskManagers/ThemeInstallTaskManager.php | 89 +++++++------------ 8 files changed, 253 insertions(+), 205 deletions(-) create mode 100644 includes/TaskManagers/AbstractTaskManager.php create mode 100644 includes/TaskManagers/TaskManagerSchedules.php diff --git a/includes/TaskManagers/AbstractTaskManager.php b/includes/TaskManagers/AbstractTaskManager.php new file mode 100644 index 0000000..1c1605c --- /dev/null +++ b/includes/TaskManagers/AbstractTaskManager.php @@ -0,0 +1,69 @@ +execute(); if ( is_wp_error( $status ) ) { @@ -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. @@ -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 ); - } } diff --git a/includes/TaskManagers/PluginDeactivationTaskManager.php b/includes/TaskManagers/PluginDeactivationTaskManager.php index ccbe835..326d4d3 100644 --- a/includes/TaskManagers/PluginDeactivationTaskManager.php +++ b/includes/TaskManagers/PluginDeactivationTaskManager.php @@ -1,4 +1,5 @@ execute(); if ( ! $status ) { @@ -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. @@ -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 ); - } } diff --git a/includes/TaskManagers/PluginInstallTaskManager.php b/includes/TaskManagers/PluginInstallTaskManager.php index dc1ece8..de3f0e0 100644 --- a/includes/TaskManagers/PluginInstallTaskManager.php +++ b/includes/TaskManagers/PluginInstallTaskManager.php @@ -1,4 +1,5 @@ 30, 'display' => __( 'Once Every Thirty Seconds' ), ); @@ -94,10 +86,10 @@ public function install() { // Recreate the PluginInstall task from the associative array. $plugin_install_task = new PluginInstallTask( - $plugin_to_install['slug'], - $plugin_to_install['activate'], - $plugin_to_install['priority'], - $plugin_to_install['retries'] + $plugin_to_install[ 'slug' ], + $plugin_to_install[ 'activate' ], + $plugin_to_install[ 'priority' ], + $plugin_to_install[ 'retries' ] ); // Update status to the current slug being installed. @@ -151,8 +143,8 @@ public static function add_to_queue( PluginInstallTask $plugin_install_task ) { Check if there is an already existing PluginInstallTask in the queue for a given slug. */ - if ( $queued_plugin['slug'] !== $plugin_install_task->get_slug() ) { - $queue->insert( $queued_plugin, $queued_plugin['priority'] ); + if ( $queued_plugin[ 'slug' ] !== $plugin_install_task->get_slug() ) { + $queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] ); } } @@ -183,24 +175,14 @@ 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 ); - } /** * Clear all the hook scheduling and update the status option diff --git a/includes/TaskManagers/PluginUninstallTaskManager.php b/includes/TaskManagers/PluginUninstallTaskManager.php index 61bd8c9..faf154e 100644 --- a/includes/TaskManagers/PluginUninstallTaskManager.php +++ b/includes/TaskManagers/PluginUninstallTaskManager.php @@ -1,4 +1,5 @@ 10, 'display' => __( 'Once Every Ten Seconds' ), ); @@ -86,6 +85,7 @@ public function uninstall() { */ $plugin_to_uninstall = array_shift( $plugins ); if ( ! $plugin_to_uninstall ) { + wp_unschedule_event( self::get_hook_name() ); return true; } @@ -94,9 +94,9 @@ public function uninstall() { // Recreate the PluginInstall task from the associative array. $plugin_uninstall_task = new PluginUninstallTask( - $plugin_to_uninstall['slug'], - $plugin_to_uninstall['priority'], - $plugin_to_uninstall['retries'] + $plugin_to_uninstall[ 'slug' ], + $plugin_to_uninstall[ 'priority' ], + $plugin_to_uninstall[ 'retries' ] ); // Execute the PluginUninstall Task. @@ -146,7 +146,7 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task $plugin_list = Plugins::get_squashed(); // Gets the specified path for the Plugin from the predefined list - $plugin_path = $plugin_list[ $plugin_uninstall_task->get_slug() ]['path']; + $plugin_path = $plugin_list[ $plugin_uninstall_task->get_slug() ][ 'path' ]; if ( ! PluginUninstaller::is_plugin_installed( $plugin_path ) ) { return true; @@ -158,10 +158,10 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task Check if there is an already existing PluginUninstallTask in the queue for a given slug. */ - if ( $queued_plugin['slug'] === $plugin_uninstall_task->get_slug() ) { + if ( $queued_plugin[ 'slug' ] === $plugin_uninstall_task->get_slug() ) { return false; } - $queue->insert( $queued_plugin, $queued_plugin['priority'] ); + $queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] ); } // Insert a new PluginUninstallTask at the appropriate position in the queue. @@ -172,15 +172,4 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() ); } - - /** - * 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( self::$queue_name ), array() ); - return array_search( $plugin, array_column( $plugins, 'slug' ), true ); - } } diff --git a/includes/TaskManagers/TaskManager.php b/includes/TaskManagers/TaskManager.php index 0576e05..156e3b1 100644 --- a/includes/TaskManagers/TaskManager.php +++ b/includes/TaskManagers/TaskManager.php @@ -1,4 +1,5 @@ task_managers as $task_manager ) { - if ( ! empty( get_option( Options::get_option_name( $task_manager::get_queue_name() ), array() ) ) ) { + if ( wp_next_scheduled( $task_manager::get_hook_name() ) || ! empty( get_option( Options::get_option_name( $task_manager::get_queue_name() ), array() ) ) ) { new $task_manager(); } } diff --git a/includes/TaskManagers/TaskManagerSchedules.php b/includes/TaskManagers/TaskManagerSchedules.php new file mode 100644 index 0000000..a7242d6 --- /dev/null +++ b/includes/TaskManagers/TaskManagerSchedules.php @@ -0,0 +1,57 @@ + array( + 'interval' => 30, + 'display' => __( 'Once Every Thirty Seconds' ), + ), + 'ten_seconds' => array( + 'interval' => 10, + 'display' => __( 'Once Every Ten Seconds' ), + ), + ); + } + + /** + * Adds a task manager cron schedule. + * + * @param array $schedules The existing cron schedule. + * @return array + */ + public static function add_schedules( $schedules ) { + foreach ( self::$schedules as $schedule_slug => $schedule_data ) { + if ( ! array_key_exists( $schedule_slug, $schedules ) || $schedule_data[ 'interval' ] !== $schedules[ $schedule_slug ][ 'interval' ] ) { + $schedules[ $schedule_slug ] = $schedule_data; + } + } + + return $schedules; + } +} diff --git a/includes/TaskManagers/ThemeInstallTaskManager.php b/includes/TaskManagers/ThemeInstallTaskManager.php index 4dee92c..7d35280 100644 --- a/includes/TaskManagers/ThemeInstallTaskManager.php +++ b/includes/TaskManagers/ThemeInstallTaskManager.php @@ -1,4 +1,5 @@ 10, - 'display' => __( 'Once Every Ten Seconds' ), - ); - } - - return $schedules; - } - /** * Expedites an existing ThemeInstallTask with a given slug. * @@ -85,10 +59,10 @@ public static function expedite( $theme_slug ) { \update_option( Options::get_option_name( self::$queue_name ), $themes ); $theme_install_task = new ThemeInstallTask( - $theme_to_install['slug'], - $theme_to_install['activate'], - $theme_to_install['priority'], - $theme_to_install['retries'] + $theme_to_install[ 'slug' ], + $theme_to_install[ 'activate' ], + $theme_to_install[ 'priority' ], + $theme_to_install[ 'retries' ] ); // Update status to the current slug being installed. @@ -106,7 +80,7 @@ public static function expedite( $theme_slug ) { then re-queue the task at the end of the queue to be retried. */ if ( $theme_install_task->get_retries() <= self::$retry_limit ) { - array_push( $themes, $theme_install_task->to_array() ); + array_push( $themes, $theme_install_task->to_array() ); } } @@ -138,15 +112,16 @@ public function install() { */ $theme_to_install = array_shift( $themes ); if ( ! $theme_to_install ) { + self::complete(); return true; } // Recreate the ThemeInstallTask from the associative array. $theme_install_task = new ThemeInstallTask( - $theme_to_install['slug'], - $theme_to_install['activate'], - $theme_to_install['priority'], - $theme_to_install['retries'] + $theme_to_install[ 'slug' ], + $theme_to_install[ 'activate' ], + $theme_to_install[ 'priority' ], + $theme_to_install[ 'retries' ] ); // Update status to the current slug being installed. @@ -164,13 +139,13 @@ public function install() { then re-queue the task at the end of the queue to be retried. */ if ( $theme_install_task->get_retries() <= self::$retry_limit ) { - array_push( $themes, $theme_install_task->to_array() ); + array_push( $themes, $theme_install_task->to_array() ); } } // If there are no more themes to be installed then change the status to completed. if ( empty( $themes ) ) { - \update_option( Options::get_option_name( 'theme_init_status' ), 'completed' ); + self::complete(); } // Update the theme install queue. @@ -197,11 +172,11 @@ public static function add_to_queue( ThemeInstallTask $theme_install_task ) { Check if there is an already existing ThemeInstallTask in the queue for a given slug and activation criteria. */ - if ( $queued_theme['slug'] === $theme_install_task->get_slug() - && $queued_theme['activate'] === $theme_install_task->get_activate() ) { + if ( $queued_theme[ 'slug' ] === $theme_install_task->get_slug() + && $queued_theme[ 'activate' ] === $theme_install_task->get_activate() ) { return false; } - $queue->insert( $queued_theme, $queued_theme['priority'] ); + $queue->insert( $queued_theme, $queued_theme[ 'priority' ] ); } // Insert a new ThemeInstallTask at the appropriate position in the queue. @@ -213,14 +188,14 @@ public static function add_to_queue( ThemeInstallTask $theme_install_task ) { return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() ); } + /** - * Returns the status of given plugin slug - installing/completed. + * Clear all the hook scheduling and update the status option * - * @param string $theme Theme Slug - * @return string|false + * @return bool */ - public static function status( $theme ) { - $themes = \get_option( Options::get_option_name( self::$queue_name ), array() ); - return array_search( $theme, array_column( $themes, 'slug' ), true ); + private static function complete() { + wp_clear_scheduled_hook( self::get_hook_name() ); + return \update_option( Options::get_option_name( 'theme_init_status' ), 'completed' ); } } From f33a39155ac71d85fd62539b8cd29d37062b53ef Mon Sep 17 00:00:00 2001 From: Giuseppe Arcifa Date: Fri, 13 Dec 2024 18:36:58 +0100 Subject: [PATCH 3/3] PHPCS and PHPCBF applied --- includes/TaskManagers/AbstractTaskManager.php | 3 +- .../PluginActivationTaskManager.php | 14 ++++----- .../PluginDeactivationTaskManager.php | 14 ++++----- .../TaskManagers/PluginInstallTaskManager.php | 20 ++++++------- .../PluginUninstallTaskManager.php | 16 +++++----- includes/TaskManagers/TaskManager.php | 2 ++ .../TaskManagers/TaskManagerSchedules.php | 30 +++++++------------ .../TaskManagers/ThemeInstallTaskManager.php | 24 +++++++-------- 8 files changed, 58 insertions(+), 65 deletions(-) diff --git a/includes/TaskManagers/AbstractTaskManager.php b/includes/TaskManagers/AbstractTaskManager.php index 1c1605c..c017000 100644 --- a/includes/TaskManagers/AbstractTaskManager.php +++ b/includes/TaskManagers/AbstractTaskManager.php @@ -1,4 +1,5 @@ execute(); if ( is_wp_error( $status ) ) { @@ -101,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. @@ -134,8 +134,8 @@ 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'] ); } } diff --git a/includes/TaskManagers/PluginDeactivationTaskManager.php b/includes/TaskManagers/PluginDeactivationTaskManager.php index 326d4d3..3f78d06 100644 --- a/includes/TaskManagers/PluginDeactivationTaskManager.php +++ b/includes/TaskManagers/PluginDeactivationTaskManager.php @@ -56,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 ) { @@ -95,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. @@ -128,8 +128,8 @@ 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'] ); } } diff --git a/includes/TaskManagers/PluginInstallTaskManager.php b/includes/TaskManagers/PluginInstallTaskManager.php index de3f0e0..2605ceb 100644 --- a/includes/TaskManagers/PluginInstallTaskManager.php +++ b/includes/TaskManagers/PluginInstallTaskManager.php @@ -47,8 +47,8 @@ public function __construct() { * @return array */ public function add_thirty_seconds_schedule( $schedules ) { - if ( ! array_key_exists( 'thirty_seconds', $schedules ) || 30 !== $schedules[ 'thirty_seconds' ][ 'interval' ] ) { - $schedules[ 'thirty_seconds' ] = array( + if ( ! array_key_exists( 'thirty_seconds', $schedules ) || 30 !== $schedules['thirty_seconds']['interval'] ) { + $schedules['thirty_seconds'] = array( 'interval' => 30, 'display' => __( 'Once Every Thirty Seconds' ), ); @@ -86,10 +86,10 @@ public function install() { // Recreate the PluginInstall task from the associative array. $plugin_install_task = new PluginInstallTask( - $plugin_to_install[ 'slug' ], - $plugin_to_install[ 'activate' ], - $plugin_to_install[ 'priority' ], - $plugin_to_install[ 'retries' ] + $plugin_to_install['slug'], + $plugin_to_install['activate'], + $plugin_to_install['priority'], + $plugin_to_install['retries'] ); // Update status to the current slug being installed. @@ -143,8 +143,8 @@ public static function add_to_queue( PluginInstallTask $plugin_install_task ) { Check if there is an already existing PluginInstallTask in the queue for a given slug. */ - if ( $queued_plugin[ 'slug' ] !== $plugin_install_task->get_slug() ) { - $queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] ); + if ( $queued_plugin['slug'] !== $plugin_install_task->get_slug() ) { + $queue->insert( $queued_plugin, $queued_plugin['priority'] ); } } @@ -175,8 +175,8 @@ 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'] ); } } diff --git a/includes/TaskManagers/PluginUninstallTaskManager.php b/includes/TaskManagers/PluginUninstallTaskManager.php index faf154e..f584aa4 100644 --- a/includes/TaskManagers/PluginUninstallTaskManager.php +++ b/includes/TaskManagers/PluginUninstallTaskManager.php @@ -57,8 +57,8 @@ public function __construct() { * @return array */ public function add_ten_seconds_schedule( $schedules ) { - if ( ! array_key_exists( 'ten_seconds', $schedules ) || 10 !== $schedules[ 'ten_seconds' ][ 'interval' ] ) { - $schedules[ 'ten_seconds' ] = array( + if ( ! array_key_exists( 'ten_seconds', $schedules ) || 10 !== $schedules['ten_seconds']['interval'] ) { + $schedules['ten_seconds'] = array( 'interval' => 10, 'display' => __( 'Once Every Ten Seconds' ), ); @@ -94,9 +94,9 @@ public function uninstall() { // Recreate the PluginInstall task from the associative array. $plugin_uninstall_task = new PluginUninstallTask( - $plugin_to_uninstall[ 'slug' ], - $plugin_to_uninstall[ 'priority' ], - $plugin_to_uninstall[ 'retries' ] + $plugin_to_uninstall['slug'], + $plugin_to_uninstall['priority'], + $plugin_to_uninstall['retries'] ); // Execute the PluginUninstall Task. @@ -146,7 +146,7 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task $plugin_list = Plugins::get_squashed(); // Gets the specified path for the Plugin from the predefined list - $plugin_path = $plugin_list[ $plugin_uninstall_task->get_slug() ][ 'path' ]; + $plugin_path = $plugin_list[ $plugin_uninstall_task->get_slug() ]['path']; if ( ! PluginUninstaller::is_plugin_installed( $plugin_path ) ) { return true; @@ -158,10 +158,10 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task Check if there is an already existing PluginUninstallTask in the queue for a given slug. */ - if ( $queued_plugin[ 'slug' ] === $plugin_uninstall_task->get_slug() ) { + if ( $queued_plugin['slug'] === $plugin_uninstall_task->get_slug() ) { return false; } - $queue->insert( $queued_plugin, $queued_plugin[ 'priority' ] ); + $queue->insert( $queued_plugin, $queued_plugin['priority'] ); } // Insert a new PluginUninstallTask at the appropriate position in the queue. diff --git a/includes/TaskManagers/TaskManager.php b/includes/TaskManagers/TaskManager.php index 156e3b1..185813a 100644 --- a/includes/TaskManagers/TaskManager.php +++ b/includes/TaskManagers/TaskManager.php @@ -29,6 +29,8 @@ public function __construct() { TaskManagerSchedules::init(); /** + * Task Manager Class + * * @var $task_manager PluginUninstallTaskManager|PluginDeactivationTaskManager|ThemeInstallTaskManager|PluginInstallTaskManager|PluginActivationTaskManager */ foreach ( $this->task_managers as $task_manager ) { diff --git a/includes/TaskManagers/TaskManagerSchedules.php b/includes/TaskManagers/TaskManagerSchedules.php index a7242d6..912b8c4 100644 --- a/includes/TaskManagers/TaskManagerSchedules.php +++ b/includes/TaskManagers/TaskManagerSchedules.php @@ -7,13 +7,6 @@ */ abstract class TaskManagerSchedules { - /** - * List of Task Managers. - * - * @var array - */ - protected static $schedules = array(); - /** * Init the Task Manager Schedules class */ @@ -21,13 +14,18 @@ public static function init() { static $initialized = false; if ( ! $initialized ) { - self::init_schedules(); add_filter( 'cron_schedules', array( __CLASS__, 'add_schedules' ) ); } } - protected static function init_schedules() { - self::$schedules = array( + /** + * Adds a task manager cron schedule. + * + * @param array $schedules The existing cron schedule. + * @return array + */ + public static function add_schedules( $schedules ) { + $schedules_to_add = array( 'thirty_seconds' => array( 'interval' => 30, 'display' => __( 'Once Every Thirty Seconds' ), @@ -37,17 +35,9 @@ protected static function init_schedules() { 'display' => __( 'Once Every Ten Seconds' ), ), ); - } - /** - * Adds a task manager cron schedule. - * - * @param array $schedules The existing cron schedule. - * @return array - */ - public static function add_schedules( $schedules ) { - foreach ( self::$schedules as $schedule_slug => $schedule_data ) { - if ( ! array_key_exists( $schedule_slug, $schedules ) || $schedule_data[ 'interval' ] !== $schedules[ $schedule_slug ][ 'interval' ] ) { + foreach ( $schedules_to_add as $schedule_slug => $schedule_data ) { + if ( ! array_key_exists( $schedule_slug, $schedules ) || $schedule_data['interval'] !== $schedules[ $schedule_slug ]['interval'] ) { $schedules[ $schedule_slug ] = $schedule_data; } } diff --git a/includes/TaskManagers/ThemeInstallTaskManager.php b/includes/TaskManagers/ThemeInstallTaskManager.php index 7d35280..5582b04 100644 --- a/includes/TaskManagers/ThemeInstallTaskManager.php +++ b/includes/TaskManagers/ThemeInstallTaskManager.php @@ -23,7 +23,7 @@ class ThemeInstallTaskManager extends AbstractTaskManager { * * @var string */ - protected static $hook_name = 'nfd_module_installer_theme_install_cron'; + protected static $hook_name = 'nfd_module_installer_theme_install_cron'; /** * ThemeInstallTaskManager constructor. @@ -59,10 +59,10 @@ public static function expedite( $theme_slug ) { \update_option( Options::get_option_name( self::$queue_name ), $themes ); $theme_install_task = new ThemeInstallTask( - $theme_to_install[ 'slug' ], - $theme_to_install[ 'activate' ], - $theme_to_install[ 'priority' ], - $theme_to_install[ 'retries' ] + $theme_to_install['slug'], + $theme_to_install['activate'], + $theme_to_install['priority'], + $theme_to_install['retries'] ); // Update status to the current slug being installed. @@ -118,10 +118,10 @@ public function install() { // Recreate the ThemeInstallTask from the associative array. $theme_install_task = new ThemeInstallTask( - $theme_to_install[ 'slug' ], - $theme_to_install[ 'activate' ], - $theme_to_install[ 'priority' ], - $theme_to_install[ 'retries' ] + $theme_to_install['slug'], + $theme_to_install['activate'], + $theme_to_install['priority'], + $theme_to_install['retries'] ); // Update status to the current slug being installed. @@ -172,11 +172,11 @@ public static function add_to_queue( ThemeInstallTask $theme_install_task ) { Check if there is an already existing ThemeInstallTask in the queue for a given slug and activation criteria. */ - if ( $queued_theme[ 'slug' ] === $theme_install_task->get_slug() - && $queued_theme[ 'activate' ] === $theme_install_task->get_activate() ) { + if ( $queued_theme['slug'] === $theme_install_task->get_slug() + && $queued_theme['activate'] === $theme_install_task->get_activate() ) { return false; } - $queue->insert( $queued_theme, $queued_theme[ 'priority' ] ); + $queue->insert( $queued_theme, $queued_theme['priority'] ); } // Insert a new ThemeInstallTask at the appropriate position in the queue.