<?php
/**
 * Who may use the address book, and who may change it.
 *
 * @package Vanquish\MultipleAddresses\Com
 */

namespace Vanquish\MultipleAddresses\Com;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * The two questions every surface of the plugin has to ask before showing an
 * address book: may this customer see one, and may they change it.
 *
 * Both answers are yes by default — the free edition puts no restriction on
 * anyone. They live behind filters so a restriction can be applied in one place
 * and hold everywhere: the account tab, both checkout selectors, the templates
 * and the AJAX endpoints. A rule enforced only in the interface would be no rule
 * at all, since the endpoints are reachable directly.
 */
class Access {

	/**
	 * Whether a customer may use the address book at all.
	 *
	 * @param int|null $user_id User id, or null for the current one.
	 * @return bool
	 */
	public static function can_use( $user_id = null ) {
		$user_id = null === $user_id ? get_current_user_id() : (int) $user_id;

		if ( ! $user_id ) {
			return false;
		}

		/**
		 * Filter whether a customer may use the address book.
		 *
		 * @param bool $can     Whether they may.
		 * @param int  $user_id User id.
		 */
		return (bool) apply_filters( 'vanmuad_can_use_address_book', true, $user_id );
	}

	/**
	 * Whether a customer may add, edit or delete their addresses.
	 *
	 * A customer who may use the book but not manage it keeps the ability to pick
	 * a saved address — the merchant maintains the list on their behalf, which is
	 * the usual arrangement when addresses are dictated by a contract.
	 *
	 * @param int|null $user_id User id, or null for the current one.
	 * @return bool
	 */
	public static function can_manage( $user_id = null ) {
		$user_id = null === $user_id ? get_current_user_id() : (int) $user_id;

		if ( ! self::can_use( $user_id ) ) {
			return false;
		}

		/**
		 * Filter whether a customer may change their address book.
		 *
		 * @param bool $can     Whether they may.
		 * @param int  $user_id User id.
		 */
		return (bool) apply_filters( 'vanmuad_can_manage_addresses', true, $user_id );
	}

	/**
	 * Whether a customer may save another address.
	 *
	 * Separate from can_manage() because the answer differs: someone who has
	 * filled up whatever allowance the merchant set can still correct and delete
	 * what they have — they simply cannot add to it. Asked before an "add" or
	 * "duplicate" control is drawn, so the control is absent rather than present
	 * and doomed to fail.
	 *
	 * This is presentation only. The save endpoint refuses on its own account
	 * (see the `vanmuad_before_save_address` filter): a hidden button stops
	 * nobody from posting.
	 *
	 * @param int|null $user_id User id, or null for the current one.
	 * @return bool
	 */
	public static function can_add( $user_id = null ) {
		$user_id = null === $user_id ? get_current_user_id() : (int) $user_id;

		if ( ! self::can_manage( $user_id ) ) {
			return false;
		}

		/**
		 * Filter whether a customer may save another address.
		 *
		 * @param bool $can     Whether they may.
		 * @param int  $user_id User id.
		 */
		return (bool) apply_filters( 'vanmuad_can_add_address', true, $user_id );
	}
}
