<?php
/**
 * Describing the premium features in the free build.
 *
 * @package Vanquish\MultipleAddresses\Admin
 */

namespace Vanquish\MultipleAddresses\Admin;

use Vanquish\MultipleAddresses\Plugin;

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

/**
 * Puts a description of each premium feature where that feature's settings would
 * have been, and gives the one premium admin page a landing page in its place —
 * so the free edition explains what exists rather than looking like a plugin
 * with pieces missing.
 *
 * This class is FREE code, on purpose. `includes/Premium/` is stripped out of the
 * free zip, so anything that has to render in the free build cannot live there.
 *
 * It asks Plugin::premium_active(), which answers the only question that matters
 * here — are the premium features available to this install? — and answers it
 * `false` in the free build, where they are not present at all. Nothing in the
 * free edition is behind that check: it decides what to ADVERTISE, never what to
 * allow. Every free feature runs regardless of it, which is what keeps the free
 * build clear of the "features included but locked" pattern the WordPress.org
 * guidelines object to.
 *
 * On the WordPress.org guidelines: guideline 10 allows upgrade prompts that are
 * "limited in scope and used sparingly, be that contextually or only on the
 * plugin's setting page". Everything here is inside the plugin's own pages —
 * no dashboard notices, no widgets, nothing on activation, and no new top-level
 * menu. The panels describe features that are absent, rather than showing locked
 * controls for features that appear to be included, which guideline 9 objects to.
 */
class Upsell {

	/**
	 * Slug of the page shown where the premium Import / Export page would be.
	 */
	const LANDING_SLUG = 'vanmuad-premium-import';

	/**
	 * Constructor.
	 */
	public function __construct() {
		/*
		 * Both callbacks re-check whether premium is running, and neither the
		 * constructor nor anything else does it once and caches the answer. It
		 * would be wrong: Plugin::load_free() runs BEFORE load_premium(), so at
		 * construction time the flag is still false even for a licensed customer.
		 * Deciding here would show every paying user an advert for what they
		 * already bought.
		 */
		add_action( 'vanmuad_settings_extra', array( $this, 'render_sections' ), 100 );
		add_action( 'vanmuad_admin_menu', array( $this, 'register_page' ), 20 );
	}

	// -----------------------------------------------------------------------
	// Settings page
	// -----------------------------------------------------------------------

	/**
	 * Render a panel for each premium settings section.
	 *
	 * @return void
	 */
	public function render_sections() {
		if ( Plugin::premium_active() ) {
			return;
		}

		foreach ( self::features() as $feature ) {
			self::panel( $feature );
		}
	}

	// -----------------------------------------------------------------------
	// The premium admin page
	// -----------------------------------------------------------------------

	/**
	 * Register the stand-in for the premium Import / Export page.
	 *
	 * The menu entry keeps its real name rather than being labelled "Pro": a
	 * customer looking for the import screen should find it and be told what it
	 * does, not have to guess that it lives behind a differently-named item.
	 *
	 * @param string $parent_slug The plugin's top-level menu slug.
	 * @return void
	 */
	public function register_page( $parent_slug ) {
		if ( Plugin::premium_active() ) {
			return;
		}

		add_submenu_page(
			$parent_slug,
			__( 'Import / Export', 'vanquish-multiple-addresses-for-woocommerce' ),
			__( 'Import / Export', 'vanquish-multiple-addresses-for-woocommerce' ),
			'manage_woocommerce',
			self::LANDING_SLUG,
			array( $this, 'render_page' )
		);
	}

	/**
	 * Render the stand-in page.
	 *
	 * @return void
	 */
	public function render_page() {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return;
		}

		echo '<div class="wrap">';
		echo '<h1>' . esc_html__( 'Import / Export', 'vanquish-multiple-addresses-for-woocommerce' ) . '</h1>';

		self::panel(
			array(
				'title' => __( 'Import and export address books as CSV', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Fill in your customers\' address books from a spreadsheet instead of asking them to type anything, and take the whole set back out whenever you need it.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'Import addresses for any number of customers from one file, matched by email address.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Export every address book, edit it in a spreadsheet and import it back — each row carries its address id, so a re-import updates rather than duplicates.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Replace a customer\'s whole book, or add to it, one column at a time.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Large files are processed in batches with a progress bar, so an import does not time out halfway.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			)
		);

		echo '</div>';
	}

	// -----------------------------------------------------------------------
	// Rendering
	// -----------------------------------------------------------------------

	/**
	 * Render one description panel.
	 *
	 * @param array $feature Title, intro and bullet points.
	 * @return void
	 */
	public static function panel( array $feature ) {
		$items = isset( $feature['items'] ) ? (array) $feature['items'] : array();
		?>
		<div class="card" style="max-width:90%;padding:2px 20px 16px;">
			<h2><?php echo esc_html( $feature['title'] ); ?></h2>

			<p><?php echo esc_html( $feature['intro'] ); ?></p>

			<?php if ( $items ) : ?>
				<ul style="list-style:disc;margin:0 0 1em 1.4em;">
					<?php foreach ( $items as $item ) : ?>
						<li><?php echo esc_html( $item ); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>

			<p>
				<a href="<?php echo esc_url( self::upgrade_url() ); ?>" class="button button-primary">
					<?php esc_html_e( 'Upgrade to Pro', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
				</a>

				<?php
				/*
				 * The second half of the audience. Someone who has already paid
				 * sees these panels too — the free build looks the same whether
				 * you bought or not — and sending them to the pricing page is
				 * the wrong answer to the question they are actually asking.
				 */
				if ( PremiumActivation::is_free_build() ) :
					?>
					<a href="<?php echo esc_url( PremiumActivation::url() ); ?>" class="button">
						<?php esc_html_e( 'Already bought it?', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
					</a>
				<?php endif; ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Where the upgrade button goes.
	 *
	 * Freemius builds the pricing URL for the current install, so the customer
	 * lands on their own upgrade screen rather than a generic sales page. The
	 * fallback is the same screen's default slug, for the case where the SDK has
	 * not been initialised — a fresh install that has not opted in yet.
	 *
	 * @return string
	 */
	public static function upgrade_url() {
		if ( function_exists( 'vanmuad_fs' ) ) {
			return vanmuad_fs()->get_upgrade_url();
		}

		return admin_url( 'admin.php?page=' . AdminMenu::MENU_SLUG . '-pricing' );
	}

	// -----------------------------------------------------------------------
	// The features themselves
	// -----------------------------------------------------------------------

	/**
	 * The premium features, in the order their settings sections would appear.
	 *
	 * One panel per section premium would have added, so the free page has the
	 * same shape as the paid one and nothing appears to be missing. The features
	 * with no settings of their own are gathered in the last panel, next to the
	 * address book they extend.
	 *
	 * @return array[]
	 */
	private static function features() {
		return array(
			array(
				'title' => __( 'Shipping per product', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Let a customer send each product in one order to a different saved address — the reason most shops need an address book in the first place.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'A destination picker on every line of the cart and the checkout.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Shipping packages split by destination, so each address is quoted and shipped on its own.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'An optional handling fee per extra destination, flat or by number of destinations.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Categories and products you can exclude, for things that must ship together.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			),
			array(
				'title' => __( 'Address fields', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Add to a saved address the things a courier actually needs, and carry them through to the order.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'Delivery notes stored with the address — the entry code, the porter, which door — and copied into the order notes at checkout.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'A shipping email per address, so the person receiving a parcel is told about it and not only the person paying.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			),
			array(
				'title' => __( 'Limits and roles', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Decide who gets an address book and how far it goes — the B2B side, where addresses are usually dictated rather than chosen.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'A cap on how many addresses a customer may keep.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Roles with no address book at all, and roles that may choose from a list they cannot change.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Roles that keep the address book but cannot ship products to separate destinations.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			),
			array(
				'title' => __( 'Custom CSS', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Restyle the address book and the checkout selector from here, with no theme file involved.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'Separate rules for the account page and for the checkout.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Printed after the plugin stylesheet, so ordinary selectors are enough and nothing needs !important.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			),
			array(
				'title' => __( 'Managing addresses for your customers', 'vanquish-multiple-addresses-for-woocommerce' ),
				'intro' => __( 'Work on a customer\'s addresses yourself, instead of talking them through it — for phone orders, contract delivery points, and the typo they cannot fix.', 'vanquish-multiple-addresses-for-woocommerce' ),
				'items' => array(
					__( 'Their address book on their user profile, where you can add, edit and delete on their behalf.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Pick a saved address while editing an order, instead of retyping it.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'Search, multi-select and bulk delete in the customer\'s own address book, plus duplicating an address as a starting point.', 'vanquish-multiple-addresses-for-woocommerce' ),
					__( 'CSV import and export of every customer\'s addresses.', 'vanquish-multiple-addresses-for-woocommerce' ),
				),
			),
		);
	}
}
