HEX
Server: nginx
System: Linux pool195-106-36.bur.atomicsites.net 6.12.57+deb12-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1~bpo12+1 (2025-11-17) x86_64
User: (0)
PHP: 8.3.32
Disabled: pcntl_fork
Upload Files
File: //srv/htdocs/wp-content/mu-plugins/wp-cloud-client/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.',
		];
	}
}