<?php
/**
 * SkyVerge Dashboard
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Teams for WooCommerce Memberships to newer
 * versions in the future. If you wish to customize Teams for WooCommerce Memberships for your
 * needs please refer to https://docs.woocommerce.com/document/teams-woocommerce-memberships/ for more information.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2020, SkyVerge, Inc.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

namespace SkyVerge\Dashboard;

use SkyVerge\WooCommerce\PluginUpdater as Updater;

defined( 'ABSPATH' ) or exit;

/**
 * The SkyVerge Dashboard plugin main file.
 *
 * @since 1.0.0
 */
class Plugin {


	/** plugin version number */
	const VERSION = '1.0.0';

	/** @var string plugin download ID */
	const DOWNLOAD_ID = '376299';

	/** plugin id */
	const PLUGIN_ID = 'skyverge-dashboard';


	/** @var string plugin path, without trailing slash */
	private $plugin_path;

	/** @var string plugin URL */
	private $plugin_url;

	/** @var Updater\License $license license instance */
	private $license;

	/** @var \SkyVerge\Dashboard\Plugin instance of this class */
	private static $instance;


	/**
	 * Plugin constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		add_filter( 'plugin_action_links_' . plugin_basename( $this->get_plugin_file() ), [ $this, 'add_plugin_action_links' ] );

		$this->init_license();
	}


	/**
	 * Initializes the plugin license.
	 *
	 * @since 1.0.0
	 */
	private function init_license() {

		if ( ! class_exists( 'Updater\\License' ) ) {
			require_once( $this->get_plugin_path() . '/vendor/skyverge/wc-plugin-updater/class-skyverge-plugin-license.php' );
		}

		$this->license = new Updater\License( $this->get_plugin_file(), $this->get_plugin_path(), $this->get_plugin_url(), $this->get_plugin_name(), self::VERSION, self::DOWNLOAD_ID );
	}


	/**
	 * Gets the plugin's path without a trailing slash.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	private function get_plugin_path() {

		if ( null === $this->plugin_path ) {
			$this->plugin_path = untrailingslashit( plugin_dir_path( $this->get_file() ) );
		}

		return $this->plugin_path;
	}


	/**
	 * Gets the plugin's main file.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	private function get_plugin_file() {

		$slug = dirname( plugin_basename( $this->get_file() ) );

		return trailingslashit( $slug ) . $slug . '.php';
	}


	/**
	 * Gets the plugin's URL without a trailing slash.
	 *
	 * E.g. http://skyverge.com/wp-content/plugins/plugin-directory
	 *
	 * @since 2.0.0
	 *
	 * @return string
	 */
	private function get_plugin_url() {

		if ( null === $this->plugin_url ) {
			$this->plugin_url = untrailingslashit( plugins_url( '/', $this->get_file() ) );
		}

		return $this->plugin_url;
	}


	/**
	 * Returns the full path to the plugin entry script.
	 *
	 * @since 1.0.0
	 *
	 * @return string the full path and filename of the plugin file
	 */
	private function get_file() {

		return dirname( __DIR__ ) . "/skyverge-dashboard.php.php";
	}


	/**
	 * Gets the plugin name, localized.
	 *
	 * @since 1.0.0
	 *
	 * @return string the plugin name
	 */
	private function get_plugin_name() {

		return __( 'SkyVerge Dashboard', 'skyverge-dashboard' );
	}


	/**
	 * Adds plugin page links
	 *
	 * @internal
	 *
	 * @since 1.0.0
	 *
	 * @param array $links all plugin links
	 * @return array $links
	 */
	public function add_plugin_action_links( $links ) {

		$plugin_links = [];

		if ( $license = $this->license ) {
			$action_text    = $this->license->is_license_valid() ? __( 'License', 'skyverge-dashboard' ) : __( 'Get updates', 'skyverge-dashboard' );
			$plugin_links[] = '<a href="' . esc_url( $this->license->get_license_settings_url() ) . '">' . $action_text . '</a>';
		}

		return array_merge( $plugin_links, $links );
	}


	/**
	 * Initializes and returns the main instance of the dashboard plugin.
	 *
	 * @since 1.0.0
	 *
	 * @return Plugin instance of this class
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


}
