From e5089d80792a8feb68e7aacb29e92b74c143d36b Mon Sep 17 00:00:00 2001 From: karthick-murugan Date: Mon, 11 Nov 2024 20:51:57 +0530 Subject: [PATCH] Add deactivate plugins command --- lib/compat/wordpress-6.7/rest-api.php | 38 ++++++++++++++++++ .../editor/src/components/commands/index.js | 40 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index 741d0ad1805e07..346554322eb009 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -156,3 +156,41 @@ function gutenberg_register_post_type_args_for_wp_global_styles( $args, $post_ty } add_filter( 'register_post_type_args', 'gutenberg_register_post_type_args_for_wp_global_styles', 10, 2 ); + +// Add the custom REST API endpoint to deactivate all plugins +add_action('rest_api_init', function() { + register_rest_route('custom/v1', '/deactivate-plugins', [ + 'methods' => 'POST', + 'callback' => 'deactivate_all_plugins', + 'permission_callback' => function() { + return current_user_can('manage_options'); + }, + ]); +}); + +/** + * Deactivates all plugins on the WordPress site. + * + * This function ensures that only users with the `manage_options` capability (typically administrators) + * can deactivate all plugins. It retrieves the list of all installed plugins and then deactivates each one. + * This action is irreversible and should be used with caution. + * + * @since 1.0.0 + * + * @return WP_REST_Response|WP_Error Returns a success message if all plugins are deactivated or an error message + * if the current user does not have the required permissions. + */ + +function deactivate_all_plugins() { + if (!current_user_can('manage_options')) { + return new WP_Error('rest_forbidden', __('You do not have permissions to perform this action', 'gutenberg'), ['status' => 403]); + } + + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + $all_plugins = get_plugins(); + foreach (array_keys($all_plugins) as $plugin) { + deactivate_plugins($plugin); + } + + return new WP_REST_Response(['message' => __('All plugins have been deactivated', 'gutenberg')], 200); +} diff --git a/packages/editor/src/components/commands/index.js b/packages/editor/src/components/commands/index.js index 16260bed3978fd..c9c8698c2c48e0 100644 --- a/packages/editor/src/components/commands/index.js +++ b/packages/editor/src/components/commands/index.js @@ -32,6 +32,7 @@ import { modalName as patternDuplicateModalName } from '../pattern-duplicate-mod const getEditorCommandLoader = () => function useEditorCommandLoader() { + const wpApiSettings = window.wpApiSettings; const { editorMode, isListViewOpen, @@ -247,6 +248,45 @@ const getEditorCommandLoader = () => }, } ); + commands.push( { + name: 'custom/deactivate-all-plugins', + label: __( 'Deactivate All Plugins' ), + icon: blockDefault, + callback: async ( { close } ) => { + close(); + try { + const response = await fetch( + '/wp-json/custom/v1/deactivate-plugins', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-WP-Nonce': wpApiSettings.nonce, + }, + } + ); + const result = await response.json(); + if ( response.ok ) { + createInfoNotice( + __( 'All plugins have been deactivated.' ), + { type: 'snackbar' } + ); + } else { + createInfoNotice( + __( 'Failed to deactivate plugins:' ) + + result.message, + { type: 'error' } + ); + } + } catch ( error ) { + createInfoNotice( + __( 'Error: Unable to deactivate plugins.' ), + { type: 'error' } + ); + } + }, + } ); + if ( isViewable ) { commands.push( { name: 'core/preview-link',