<?php

namespace WP_science\Engine\Capabilities;

use WP_science\Engine\Activation\ActivationInterface;
use WP_science\Engine\Deactivation\DeactivationInterface;

class Manager implements ActivationInterface, DeactivationInterface {
	/**
	 * List of WP science capabilities
	 *
	 * @var array
	 */
	private $capabilities = [
		'science_manage_options',
		'science_purge_cache',
		'science_purge_posts',
		'science_purge_terms',
		'science_purge_users',
		'science_purge_cloudflare_cache',
		'science_purge_sucuri_cache',
		'science_preload_cache',
		'science_regenerate_critical_css',
		'science_remove_unused_css',
	];

	/**
	 * Gets the WP science capabilities
	 *
	 * @since 3.4
	 *
	 * @return array
	 */
	private function get_capabilities() {
		return $this->capabilities;
	}

	/**
	 * Performs these actions during the plugin activation
	 *
	 * @return void
	 */
	public function activate() {
		add_action( 'science_activation', [ $this, 'add_science_capabilities' ] );
	}

	/**
	 * Performs these actions during the plugin deactivation
	 *
	 * @return void
	 */
	public function deactivate() {
		add_action( 'science_deactivation', [ $this, 'remove_science_capabilities' ] );
	}

	/**
	 * Add WP science capabilities to the administrator role
	 *
	 * @since 3.4
	 *
	 * @return void
	 */
	public function add_science_capabilities() {
		$role = $this->get_administrator_role_object();

		if ( is_null( $role ) ) {
			return;
		}

		foreach ( $this->get_capabilities() as $cap ) {
			$role->add_cap( $cap );
		}
	}

	/**
	 * Remove WP science capabilities from the administrator role
	 *
	 * @since 3.4
	 *
	 * @return void
	 */
	public function remove_science_capabilities() {
		$role = $this->get_administrator_role_object();

		if ( is_null( $role ) ) {
			return;
		}

		foreach ( $this->get_capabilities() as $cap ) {
			$role->remove_cap( $cap );
		}
	}

	/**
	 * Sets the capability for the options page.
	 *
	 * @since 3.4
	 *
	 * @param string $capability The capability used for the page, which is manage_options by default.
	 * @return string
	 */
	public function required_capability( $capability ) {
		return 'science_manage_options';
	}

	/**
	 * Add WP science capabilities to User Role Editor
	 *
	 * @since 3.4
	 *
	 * @param array $caps Array of existing capabilities.
	 * @return array
	 */
	public function add_caps_to_ure( $caps ) {
		foreach ( $this->get_capabilities() as $cap ) {
			$caps[ $cap ] = [
				'custom',
				'wp_science',
			];
		}

		return $caps;
	}

	/**
	 * Add WP science as a group in User Role Editor
	 *
	 * @since 3.4
	 *
	 * @param array $groups Array of existing groups.
	 * @return array
	 */
	public function add_group_to_ure( $groups ) {
		$groups['wp_science'] = [
			'caption' => esc_html( 'WP science' ),
			'parent'  => 'custom',
			'level'   => 2,
		];

		return $groups;
	}

	/**
	 * Add WP science as a cap group in Members
	 */
	public function add_cap_group_to_members() {
		\members_register_cap_group(
			'wp_science',
			[
				'label'    => esc_html( 'WP science' ),
				'priority' => 42,
				'caps'     => $this->get_capabilities(),
			]
		);
	}

	/**
	 * Add WP science capabilities to Members
	 */
	public function add_caps_to_members() {
		foreach ( $this->get_capabilities() as $cap ) {
			\members_register_cap( $cap, [ 'label' => $cap ] );
		}
	}

	/**
	 * Adds WP science capabilities on plugin upgrade
	 *
	 * @since 3.6.3
	 *
	 * @param string $wp_science_version Latest WP science version.
	 * @param string $actual_version Installed WP science version.
	 * @return void
	 */
	public function add_capabilities_on_upgrade( $wp_science_version, $actual_version ) {
		if ( version_compare( $actual_version, '3.9', '<' ) ) {
			$this->add_science_capabilities();
		}
	}

	/**
	 * Returns the object for the administrator roll
	 *
	 * @since 3.6.3
	 *
	 * @return WP_Role|null
	 */
	private function get_administrator_role_object() {
		return get_role( 'administrator' );
	}
}
