<?php
/**
 * Capability registration helper: a plugin registers its custom capabilities
 * once and removes them cleanly on uninstall.
 *
 * @package Hakki\WooCore
 */

namespace ErenLabs\Stocktake\Vendor\Hakki\WooCore\Security;

use ErenLabs\Stocktake\Vendor\Hakki\WooCore\Core;

defined( 'ABSPATH' ) || exit;

final class Caps {

	/** @var Core */
	private $core;

	public function __construct( Core $core ) {
		$this->core = $core;
	}

	/**
	 * Grant a list of capabilities to a role (default: administrator). Idempotent.
	 *
	 * @param string[] $capabilities
	 */
	public function grant( array $capabilities, string $role = 'administrator' ): void {
		$wp_role = get_role( $role );
		if ( ! $wp_role ) {
			return;
		}
		foreach ( $capabilities as $cap ) {
			if ( ! $wp_role->has_cap( $cap ) ) {
				$wp_role->add_cap( $cap );
			}
		}
	}

	/**
	 * Remove capabilities from a role (used on uninstall).
	 *
	 * @param string[] $capabilities
	 */
	public function revoke( array $capabilities, string $role = 'administrator' ): void {
		$wp_role = get_role( $role );
		if ( ! $wp_role ) {
			return;
		}
		foreach ( $capabilities as $cap ) {
			$wp_role->remove_cap( $cap );
		}
	}
}
