<?php
/**
 * "I bought Premium — now what?" guidance.
 *
 * @package Vanquish\MultipleAddresses\Admin
 */

namespace Vanquish\MultipleAddresses\Admin;

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

/**
 * Explains, inside the free plugin, how to move to Premium after buying.
 *
 * Freemius deliberately offers no licence field in the free build — and it is
 * right not to: the premium code is not disabled there, it is physically absent
 * from the package, so a licence key would unlock nothing. What Premium takes is
 * installing a different zip.
 *
 * That is perfectly logical once you know it, and completely invisible if you do
 * not. A customer who has just paid goes looking for somewhere to type their key,
 * finds nothing, and concludes the plugin is broken. This page is the missing
 * step between "I paid" and "I have it".
 *
 * It matters more here than on our other plugins: this one is the continuation of
 * a CodeCanyon product, so a large share of its first buyers arrive by converting
 * a purchase code — with the free version already installed, which is exactly the
 * situation that produces the dead end.
 *
 * Shown only in the free build (`! is_premium()`): in the premium package the SDK
 * already provides Activate License, both on the Plugins screen and in the opt-in
 * dialog.
 */
class PremiumActivation {

	/**
	 * Menu/page slug.
	 */
	const SLUG = 'vanmuad-activate-premium';

	/**
	 * Constructor.
	 */
	public function __construct() {
		if ( ! self::is_free_build() ) {
			return;
		}

		add_action( 'vanmuad_admin_menu', array( $this, 'register_page' ) );

		// Also on the Plugins screen: that is where "Activate License" lives in
		// the premium build, so it is where someone who has bought will look
		// first, out of habit.
		add_filter( 'plugin_action_links_' . VANMUAD_BASENAME, array( $this, 'action_link' ) );
	}

	/**
	 * Whether this is the free package.
	 *
	 * Deliberately not the licence gate the upsell panels use: a premium install
	 * whose licence lapsed already HAS the premium files, so sending it off to
	 * download them would be wrong. The question here is which package is
	 * running, not whether a licence is valid.
	 *
	 * @return bool
	 */
	public static function is_free_build() {
		return function_exists( 'vanmuad_fs' ) && ! vanmuad_fs()->is_premium();
	}

	/**
	 * The page's URL, for anything that wants to link to it.
	 *
	 * @return string
	 */
	public static function url() {
		return admin_url( 'admin.php?page=' . self::SLUG );
	}

	/**
	 * Register the page.
	 *
	 * @param string $parent_slug The plugin's top-level menu slug.
	 * @return void
	 */
	public function register_page( $parent_slug ) {
		add_submenu_page(
			$parent_slug,
			__( 'Activate Premium', 'vanquish-multiple-addresses-for-woocommerce' ),
			__( 'Activate Premium', 'vanquish-multiple-addresses-for-woocommerce' ),
			'manage_woocommerce',
			self::SLUG,
			array( $this, 'render' )
		);
	}

	/**
	 * Add the link to the Plugins screen row.
	 *
	 * @param array $links Existing action links.
	 * @return array
	 */
	public function action_link( $links ) {
		$link = sprintf(
			'<a href="%s">%s</a>',
			esc_url( self::url() ),
			esc_html__( 'Activate Premium', 'vanquish-multiple-addresses-for-woocommerce' )
		);

		array_unshift( $links, $link );

		return $links;
	}

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

		$upgrade_url = function_exists( 'vanmuad_fs' ) ? vanmuad_fs()->get_upgrade_url() : '';
		$card        = 'max-width:90%;padding:2px 20px 20px;';
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Activate Premium', 'vanquish-multiple-addresses-for-woocommerce' ); ?></h1>

			<div class="card" style="<?php echo esc_attr( $card ); ?>">
				<h2><?php esc_html_e( 'Already bought Premium? Here is how to get it', 'vanquish-multiple-addresses-for-woocommerce' ); ?></h2>

				<p class="description" style="max-width:700px;font-size:14px;">
					<?php esc_html_e( 'Premium is a separate download rather than a key you type in here. This copy is the free version from WordPress.org, and it does not contain the premium features at all — so activating a licence on it would have nothing to unlock. Installing the Premium package takes about two minutes and keeps everything you have set up.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
				</p>

				<ol style="max-width:700px;font-size:14px;line-height:1.8;">
					<li>
						<?php
						printf(
							/* translators: %s: link to the Freemius user dashboard. */
							esc_html__( 'Sign in to %s with the email address you used for the purchase.', 'vanquish-multiple-addresses-for-woocommerce' ),
							'<a href="https://users.freemius.com" target="_blank" rel="noopener"><strong>users.freemius.com</strong></a>'
						);
						?>
						<br />
						<span style="color:#646970;">
							<?php esc_html_e( 'If you converted a CodeCanyon purchase code, use the email address you gave during the conversion.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
						</span>
					</li>
					<li><?php esc_html_e( 'Download the Premium version of this plugin (a .zip file).', 'vanquish-multiple-addresses-for-woocommerce' ); ?></li>
					<li>
						<?php
						printf(
							/* translators: %s: the WordPress menu path for uploading a plugin. */
							esc_html__( 'In WordPress, go to %s and upload that .zip.', 'vanquish-multiple-addresses-for-woocommerce' ),
							'<strong>' . esc_html__( 'Plugins → Add New Plugin → Upload Plugin', 'vanquish-multiple-addresses-for-woocommerce' ) . '</strong>'
						);
						?>
					</li>
					<li><?php esc_html_e( 'WordPress will tell you a version of the plugin is already installed: choose to replace it.', 'vanquish-multiple-addresses-for-woocommerce' ); ?></li>
					<li><?php esc_html_e( 'Activate it, then enter your licence key when prompted.', 'vanquish-multiple-addresses-for-woocommerce' ); ?></li>
				</ol>

				<p style="max-width:700px;font-size:14px;">
					<strong><?php esc_html_e( 'Nothing is lost in the process.', 'vanquish-multiple-addresses-for-woocommerce' ); ?></strong>
					<?php esc_html_e( 'Your settings and every address your customers have saved stay exactly where they are: the Premium package is the same plugin with the advanced features included.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
				</p>

				<p style="margin-bottom:0;">
					<a href="https://users.freemius.com" class="button button-primary" target="_blank" rel="noopener">
						<?php esc_html_e( 'Open my Freemius account', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
					</a>
				</p>
			</div>

			<?php if ( '' !== $upgrade_url ) : ?>
				<div class="card" style="<?php echo esc_attr( $card ); ?>">
					<h2><?php esc_html_e( 'Not bought it yet?', 'vanquish-multiple-addresses-for-woocommerce' ); ?></h2>

					<p class="description" style="max-width:700px;font-size:14px;">
						<?php esc_html_e( 'Premium sends each product in an order to a different saved address, splits the shipping accordingly, adds delivery notes and a shipping email per address, per-role limits, CSV import and export, and lets you manage a customer\'s address book from their profile or while editing an order.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
					</p>

					<p style="margin-bottom:0;">
						<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button">
							<?php esc_html_e( 'See the plans', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
						</a>
					</p>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}
}
