<?php
namespace WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\SiteHealth\Collectors;

/**
 * WordPress Constants Collector
 *
 * Collects WordPress defined constants
 */
class ConstantsCollector extends AbstractCollector
{
	/**
	 * Collect WordPress constants
	 *
	 * @return array
	 */
	public function collect(): array
	{
		$constants = [
			'WP_DEBUG',
			'WP_DEBUG_LOG',
			'WP_DEBUG_DISPLAY',
			'SCRIPT_DEBUG',
			'WP_CACHE',
			'CONCATENATE_SCRIPTS',
			'COMPRESS_SCRIPTS',
			'COMPRESS_CSS',
			'WP_LOCAL_DEV',
			'SAVEQUERIES',
			'DB_CHARSET',
			'DB_COLLATE',
			'WP_SITEURL',
			'WP_HOME',
			'WP_CONTENT_DIR',
			'WP_CONTENT_URL',
			'WP_PLUGIN_DIR',
			'WP_PLUGIN_URL',
			'WPMU_PLUGIN_DIR',
			'WPMU_PLUGIN_URL',
			'WP_MEMORY_LIMIT',
			'WP_MAX_MEMORY_LIMIT',
			'WP_POST_REVISIONS',
			'AUTOSAVE_INTERVAL',
			'WP_CRON_LOCK_TIMEOUT',
			'EMPTY_TRASH_DAYS',
			'FORCE_SSL_ADMIN',
			'FORCE_SSL_LOGIN',
			'AUTOMATIC_UPDATER_DISABLED',
			'WP_AUTO_UPDATE_CORE',
			'DISALLOW_FILE_EDIT',
			'DISALLOW_FILE_MODS',
		];

		$data = [];
		foreach ($constants as $constant) {
			$data[] = $this->item(
				$constant,
				defined($constant) ? $this->getConstantValue($constant) : __('Not defined', 'ultimate-security')
			);
		}

		return $data;
	}

	/**
	 * Get constant value
	 *
	 * @param string $constant Constant name
	 * @return mixed
	 */
	private function getConstantValue(string $constant)
	{
		if (!defined($constant)) {
			return __('Not defined', 'ultimate-security');
		}

		$value = constant($constant);

		// Return boolean representation
		if (is_bool($value)) {
			return $value;
		}

		// Return string/numeric value
		return $value;
	}

	/**
	 * Get collector name
	 *
	 * @return string
	 */
	public function getName(): string
	{
		return 'constants';
	}

	/**
	 * Get collector label
	 *
	 * @return string
	 */
	public function getLabel(): string
	{
		return __('WordPress Constants', 'ultimate-security');
	}
}
