time() + 5, 'woocommerce_plugins_install_callback', array( $plugins, $job_id ) ); return $job_id; } /** * Activate the requested plugins. * * @param array $plugins Plugins. * @return WP_Error|array Plugin Status */ public static function activate_plugins( $plugins ) { if ( empty( $plugins ) || ! is_array( $plugins ) ) { return new \WP_Error( 'woocommerce_plugins_invalid_plugins', __( 'Plugins must be a non-empty array.', 'woocommerce' ), 404 ); } require_once ABSPATH . 'wp-admin/includes/plugin.php'; // the mollie-payments-for-woocommerce plugin calls `WP_Filesystem()` during it's activation hook, which crashes without this include. require_once ABSPATH . 'wp-admin/includes/file.php'; /** * Filter the list of plugins to activate. * * @param array $plugins A list of the plugins to activate. * @since 6.4.0 */ $plugins = apply_filters( 'woocommerce_admin_plugins_pre_activate', $plugins ); $plugin_paths = self::get_installed_plugins_paths(); $errors = new \WP_Error(); $activated_plugins = array(); foreach ( $plugins as $plugin ) { $slug = $plugin; $path = isset( $plugin_paths[ $slug ] ) ? $plugin_paths[ $slug ] : false; if ( ! $path ) { $errors->add( $plugin, /* translators: %s: plugin slug (example: woocommerce-services) */ sprintf( __( 'The requested plugin `%s`. is not yet installed.', 'woocommerce' ), $slug ) ); continue; } $result = activate_plugin( $path ); if ( ! is_plugin_active( $path ) ) { /** * Action triggered when a plugin activation fails. * * @param string $slug The plugin slug. * @param null|\WP_Error $result The result of the plugin activation. * @since 6.4.0 */ do_action( 'woocommerce_plugins_activate_error', $slug, $result ); $errors->add( $plugin, /* translators: %s: plugin slug (example: woocommerce-services) */ sprintf( __( 'The requested plugin `%s` could not be activated.', 'woocommerce' ), $slug ) ); continue; } $activated_plugins[] = $plugin; } $data = array( 'activated' => $activated_plugins, 'active' => self::get_active_plugin_slugs(), 'errors' => $errors, ); return $data; } /** * Schedule plugin activation. * * @param array $plugins Plugins to activate. * @return string Job ID. */ public static function schedule_activate_plugins( $plugins ) { if ( empty( $plugins ) || ! is_array( $plugins ) ) { return new \WP_Error( 'woocommerce_plugins_invalid_plugins', __( 'Plugins must be a non-empty array.', 'woocommerce' ), 404 ); } $job_id = uniqid(); WC()->queue()->schedule_single( time() + 5, 'woocommerce_plugins_activate_callback', array( $plugins, $job_id ) ); return $job_id; } /** * Installation status. * * @param int $job_id Job ID. * @return array Job data. */ public static function get_installation_status( $job_id = null ) { $actions = WC()->queue()->search( array( 'hook' => 'woocommerce_plugins_install_callback', 'search' => $job_id, 'orderby' => 'date', 'order' => 'DESC', ) ); return self::get_action_data( $actions ); } /** * Gets the plugin data for the first action. * * @param array $actions Array of AS actions. * @return array Array of action data. */ public static function get_action_data( $actions ) { $data = []; foreach ( $actions as $action_id => $action ) { $store = new \ActionScheduler_DBStore(); $status = $store->get_status( $action_id ); $args = $action->get_args(); $data[] = array( 'job_id' => $args[1], 'plugins' => $args[0], 'status' => $store->get_status( $action_id ), ); } return $data; } /** * Activation status. * * @param int $job_id Job ID. * @return array Array of action data. */ public static function get_activation_status( $job_id = null ) { $actions = WC()->queue()->search( array( 'hook' => 'woocommerce_plugins_activate_callback', 'search' => $job_id, 'orderby' => 'date', 'order' => 'DESC', ) ); return self::get_action_data( $actions ); } }