Skip to content

Commit

Permalink
Rest API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Nov 12, 2024
1 parent 92630c7 commit 9e4ec73
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions lib/compat/wordpress-6.7/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,33 @@ 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',
array(
'methods' => 'POST',
'callback' => 'deactivate_all_plugins',
'permission_callback' => function () {
/**
* Registers the custom REST API route for deactivating all plugins.
*/
function register_deactivate_plugins_endpoint() {
register_rest_route(
'custom/v1',
'/deactivate-plugins',
array(
'methods' => 'POST',
'callback' => 'deactivate_all_plugins',
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
)
);
}
);
},
)
);
}

// Hook to register the custom REST API endpoint.
add_action( 'rest_api_init', 'register_deactivate_plugins_endpoint' );

if ( ! function_exists( 'deactivate_all_plugins' ) ) {

/**
* 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 function ensures that only users with the `manage_options` capability 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
Expand All @@ -190,20 +192,13 @@ function () {
* if the current user does not have the required permissions.
*/
function deactivate_all_plugins() {
// Check if the current user has the necessary permissions.
if ( ! current_user_can( 'manage_options' ) ) {
return new WP_Error(
'rest_forbidden',
__( 'You do not have permissions to perform this action', 'gutenberg' ),
array( 'status' => 403 )
);
}

// Load the necessary WordPress plugin functions.
require_once ABSPATH . 'wp-admin/includes/plugin.php';

// Retrieve the list of all plugins and deactivate each one.
// Retrieve the list of all plugins.
$all_plugins = get_plugins();

// Deactivate each plugin.
foreach ( array_keys( $all_plugins ) as $plugin ) {
deactivate_plugins( $plugin );
}
Expand Down

0 comments on commit 9e4ec73

Please sign in to comment.