Automattic\WooCommerce\Api\Attributes

RequiredCapability{}finalWC 1.0

[Attribute( Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY )]

No Hooks.

Usage

$RequiredCapability = new RequiredCapability();
// use class methods

Methods

  1. public __construct(
  2. public authorize( Principal $principal )

RequiredCapability{} code WC 10.9.1

final class RequiredCapability {
	/**
	 * Constructor.
	 *
	 * @param string $capability A WordPress capability slug
	 *                           (e.g. 'manage_woocommerce').
	 */
	public function __construct(
		public readonly string $capability,
	) {
	}

	/**
	 * Decide whether the given principal holds the required capability.
	 *
	 * Reads the WordPress user from the principal wrapper and delegates to
	 * {@see \user_can()}. Anonymous principals (the WP user has `ID === 0`)
	 * never hold any capability, so the check returns false naturally.
	 *
	 * @param Principal $principal The resolved request principal.
	 */
	public function authorize( Principal $principal ): bool {
		return user_can( $principal->user, $this->capability );
	}
}