File: /wordpress/plugins/wp-cloud-client/1.1.0/src/Handler/ActivatePluginHandler.php
<?php
declare(strict_types=1);
namespace VPlugins\WPCloudClient\Handler;
/**
* Activates an installed WordPress plugin.
*
* Action name: activate_plugin
*
* Required params:
* - slug (string) - Plugin file path (e.g., "woocommerce/woocommerce.php").
*
* Response example:
* {
* "activated": true,
* "slug": "woocommerce/woocommerce.php",
* "message": "Plugin activated."
* }
*/
final class ActivatePluginHandler extends AbstractHandler {
/**
* Return the action name.
*
* @return string
*/
public function action(): string {
return 'activate_plugin';
}
/**
* Activate the specified plugin.
*
* @param array<string, mixed> $params Action parameters.
* @return array<string, mixed> Activation result.
*
* @throws \InvalidArgumentException When the slug param is missing or the plugin does not exist.
* @throws \RuntimeException On WordPress activation error.
*/
public function execute( array $params ): array {
$this->requireParams( $params, 'slug' );
$slug = (string) $params['slug'];
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
if ( ! isset( $plugins[ $slug ] ) ) {
throw new \InvalidArgumentException( sprintf( 'Plugin "%s" not found.', $slug ) );
}
$result = activate_plugin( $slug );
if ( is_wp_error( $result ) ) {
throw new \RuntimeException(
sprintf( 'Failed to activate plugin "%s": %s', $slug, $result->get_error_message() )
);
}
return [
'activated' => true,
'slug' => $slug,
'message' => 'Plugin activated.',
];
}
}