ownership = $ownership; } /** * Tells whether the current user holds a capability. * * @since 0.1.0 * * @param string $capability Capability name. * @return bool */ public function can( string $capability ): bool { return current_user_can( $capability ); } /** * Customer id of the current user, or 0 when they are not a customer. * * @since 0.1.0 * * @return int */ public function current_customer_id(): int { return $this->ownership->customer_id_for_user( get_current_user_id() ); } /** * Tells whether the current user is the customer that owns the object. * * @since 0.1.0 * * @param string $type Object type: order, invoice, service, domain or payment. * @param int $id Object id. * @return bool */ public function customer_owns( string $type, int $id ): bool { $customer_id = $this->current_customer_id(); return $customer_id > 0 && $customer_id === $this->ownership->owner_customer_id( $type, $id ); } /** * Tells whether the current user may view an object: staff with the given * capability, or the customer who owns it. * * @since 0.1.0 * * @param string $type Object type. * @param int $id Object id. * @param string $staff_capability Capability that grants staff access. * @return bool */ public function can_view( string $type, int $id, string $staff_capability ): bool { return $this->can( $staff_capability ) || $this->customer_owns( $type, $id ); } }