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.31
Disabled: pcntl_fork
Upload Files
File: /wordpress/plugins/wp-cloud-client/beta/src/Handler/SetDebugModeHandler.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Handler;

use VPlugins\WPCloudClient\Debug\WpConfigEditor;

final class SetDebugModeHandler extends AbstractHandler {

	/**
	 * Class constructor.
	 *
	 * @param WpConfigEditor $editor wp-config.php editor service.
	 */
	public function __construct(
		private readonly WpConfigEditor $editor,
	) {}

	/**
	 * Return the action name.
	 *
	 * @return string Action name.
	 */
	public function action(): string {
		return 'set_debug_mode';
	}

	/**
	 * Enable or disable WordPress debug mode in wp-config.php.
	 *
	 * On enable: WP_DEBUG=true, WP_DEBUG_LOG=true. On disable: both set to false.
	 * WP_DEBUG_DISPLAY is always forced to false on every call to avoid leaking
	 * error output to site visitors.
	 *
	 * @param array<string, mixed> $params Must contain 'enabled' (bool).
	 * @return array<string, mixed> Applied debug state.
	 *
	 * @throws \InvalidArgumentException If 'enabled' is missing or not boolean.
	 */
	public function execute( array $params ): array {
		$this->requireParams( $params, 'enabled' );

		if ( ! is_bool( $params['enabled'] ) ) {
			throw new \InvalidArgumentException( "'enabled' must be a boolean." );
		}

		$enabled = $params['enabled'];

		$this->editor->setConstant( 'WP_DEBUG', $enabled );
		$this->editor->setConstant( 'WP_DEBUG_LOG', $enabled );
		$this->editor->setConstant( 'WP_DEBUG_DISPLAY', false );

		return [
			'enabled'          => $enabled,
			'wp_debug'         => $enabled,
			'wp_debug_log'     => $enabled,
			'wp_debug_display' => false,
			'message'          => $enabled ? 'Debug mode enabled.' : 'Debug mode disabled.',
		];
	}
}