<?php
/**
 * Plugin Name: Easy Digital Downloads - Wish Lists
 * Plugin URI: https://easydigitaldownloads.com/downloads/edd-wish-lists/
 * Description: Gives your customers the ability to save and share their favorite products on your site.
 * Version: 1.2.3
 * Requires PHP: 7.1
 * Requires at least: 5.4
 * Author: Easy Digital Downloads
 * Author URI: https://easydigitaldownloads.com
 * License: GPL-2.0+
 * License URI: http://www.opensource.org/licenses/gpl-license.php
 */

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

if ( ! class_exists( 'EDD_Wish_Lists' ) ) {

	/**
	 * EDD_Wish_Lists
	 *
	 * The main class for the Wish Lists extension.
	 */
	final class EDD_Wish_Lists {

		/**
		 * Holds the instance
		 *
		 * Ensures that only one instance of EDD Wish Lists exists in memory at any one
		 * time and it also prevents needing to define globals all over the place.
		 *
		 * TL;DR This is a static property property that holds the singleton instance.
		 *
		 * @var object
		 * @static
		 * @since 1.0
		 */
		private static $instance;

		/**
		 * The plugin file.
		 *
		 * @var string
		 */
		public $file;

		/**
		 * The plugin basename.
		 *
		 * @var string
		 */
		public $basename;

		/**
		 * The plugin directory.
		 *
		 * @var string
		 */
		public $plugin_dir;

		/**
		 * The plugin URL.
		 *
		 * @var string
		 */
		public $plugin_url;

		/**
		 * Main Instance
		 *
		 * Ensures that only one instance exists in memory at any one
		 * time. Also prevents needing to define globals all over the place.
		 *
		 * @since 1.0
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EDD_Wish_Lists ) ) {
				self::$instance = new EDD_Wish_Lists();
				self::$instance->setup_globals();
				self::$instance->includes();
				self::$instance->hooks();
				self::$instance->licensing();
			}

			return self::$instance;
		}

		/**
		 * Constructor Function
		 *
		 * @since 1.0
		 * @access private
		 * @see EDD_Wish_Lists::init()
		 * @see EDD_Wish_Lists::activation()
		 */
		private function __construct() {
			self::$instance = $this;

			add_action( 'init', array( $this, 'init' ) );
		}

		/**
		 * Reset the instance of the class
		 *
		 * @since 1.0
		 * @access public
		 * @static
		 */
		public static function reset() {
			self::$instance = null;
		}

		/**
		 * Globals
		 *
		 * @since 1.0
		 * @return void
		 */
		private function setup_globals() {
			// Constants.

			if ( ! defined( 'EDD_WL_VERSION' ) ) {
				define( 'EDD_WL_VERSION', '1.2.3' );
			}

			if ( ! defined( 'EDD_WL_TITLE' ) ) {
				define( 'EDD_WL_TITLE', 'Wish Lists' );
			}

			if ( ! defined( 'EDD_WL_PLUGIN_FILE' ) ) {
				define( 'EDD_WL_PLUGIN_FILE', __FILE__ );
			}

			if ( ! defined( 'EDD_WL_PLUGIN_URL' ) ) {
				define( 'EDD_WL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
			}

			if ( ! defined( 'EDD_WL_PLUGIN_DIR' ) ) {
				define( 'EDD_WL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
			}

			// Paths.
			$this->file       = EDD_WL_PLUGIN_FILE;
			$this->basename   = apply_filters( 'edd_wl_plugin_basenname', plugin_basename( $this->file ) );
			$this->plugin_dir = apply_filters( 'edd_wl_plugin_dir_path', plugin_dir_path( $this->file ) );
			$this->plugin_url = apply_filters( 'edd_wl_plugin_dir_url', plugin_dir_url( $this->file ) );
		}

		/**
		 * Function fired on init
		 *
		 * This function is called on WordPress 'init'. It's triggered from the
		 * constructor function.
		 *
		 * @since 1.0
		 * @access public
		 *
		 * @uses EDD_Wish_Lists::load_textdomain()
		 *
		 * @return void
		 */
		public function init() {
			do_action( 'edd_wl_before_init' );

			$this->load_textdomain();

			do_action( 'edd_wl_after_init' );
		}

		/**
		 * Includes
		 *
		 * @since 1.0
		 * @access private
		 * @return void
		 */
		private function includes() {
			require_once dirname( $this->file ) . '/includes/post-type.php';
			require_once dirname( $this->file ) . '/includes/shortcodes.php';
			require_once dirname( $this->file ) . '/includes/form-processing.php';
			require_once dirname( $this->file ) . '/includes/user-functions.php';
			require_once dirname( $this->file ) . '/includes/functions.php';
			require_once dirname( $this->file ) . '/includes/template-functions.php';
			require_once dirname( $this->file ) . '/includes/wish-list-functions.php';
			require_once dirname( $this->file ) . '/includes/ajax-functions.php';
			require_once dirname( $this->file ) . '/includes/scripts.php';
			require_once dirname( $this->file ) . '/includes/sharing.php';
			require_once dirname( $this->file ) . '/includes/modals.php';
			require_once dirname( $this->file ) . '/includes/rewrites.php';
			require_once dirname( $this->file ) . '/includes/actions.php';
			require_once dirname( $this->file ) . '/includes/messages.php';

			if ( is_admin() ) {
				require_once dirname( $this->file ) . '/includes/cron.php';
				require_once dirname( $this->file ) . '/includes/metabox.php';
				require_once dirname( $this->file ) . '/includes/admin-settings.php';
				require_once dirname( $this->file ) . '/includes/dashboard-columns.php';
				require_once dirname( $this->file ) . '/includes/admin/deprecated/functions.php';
			}
		}

		/**
		 * Setup the default hooks and actions
		 *
		 * @since 1.0
		 *
		 * @return void
		 */
		private function hooks() {

			add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );

			// insert actions.
			do_action( 'edd_wl_setup_actions' );
		}

		/**
		 * Licensing
		 *
		 * @since 1.0
		 * @since 1.1.8 Updated to use the new Extension Registry.
		 */
		private function licensing() {
			// We're dropping `EDD` from the title, so we need to move the license key over.
			if ( ! empty( edd_get_option( 'edd_edd_wish_lists_license_key', '' ) ) ) {
				$license_key = edd_get_option( 'edd_edd_wish_lists_license_key' );
				edd_update_option( 'edd_wish_lists_license_key', $license_key );
				edd_delete_option( 'edd_edd_wish_lists_license_key' );

				$license_data = get_option( 'edd_edd_wish_lists_license_active', '' );
				update_option( 'edd_wish_lists_license_active', $license_data, false );
				delete_option( 'edd_edd_wish_lists_license_active' );
			}

			add_action(
				'edd_extension_license_init',
				function ( \EDD\Extensions\ExtensionRegistry $registry ) {
					$registry->addExtension( __FILE__, EDD_WL_TITLE, 188268, EDD_WL_VERSION );
				}
			);
		}

		/**
		 * Loads the plugin language files
		 *
		 * @access public
		 * @since 1.0
		 * @return void
		 */
		public function load_textdomain() {
			// Set filter for plugin's languages directory.
			$lang_dir = dirname( plugin_basename( $this->file ) ) . '/languages/';
			$lang_dir = apply_filters( 'edd_wl_languages_directory', $lang_dir );

			// Traditional WordPress plugin locale filter.
			$locale = apply_filters( 'plugin_locale', get_locale(), 'edd-wish-lists' );
			$mofile = sprintf( '%1$s-%2$s.mo', 'edd-wish-lists', $locale );

			// Setup paths to current locale file.
			$mofile_local  = $lang_dir . $mofile;
			$mofile_global = WP_LANG_DIR . '/edd-wish-lists/' . $mofile;

			if ( file_exists( $mofile_global ) ) {
				load_textdomain( 'edd-wish-lists', $mofile_global );
			} elseif ( file_exists( $mofile_local ) ) {
				load_textdomain( 'edd-wish-lists', $mofile_local );
			} else {
				// Load the default language files.
				load_plugin_textdomain( 'edd-wish-lists', false, $lang_dir );
			}
		}

		/**
		 * Plugin settings link
		 *
		 * @since 1.0
		 *
		 * @param array $links The settings links for the plugins list.
		 *
		 * @return array List of plugin links.
		 */
		public function settings_link( $links ) {
			$plugin_links = array(
				'<a href="' . admin_url( 'edit.php?post_type=download&page=edd-settings&tab=extensions&section=wish-list-settings' ) . '">' . __( 'Settings', 'edd-wish-lists' ) . '</a>',
			);

			return array_merge( $plugin_links, $links );
		}

		/**
		 * Plugins row action links
		 *
		 * @access public
		 * @since 1.1.2
		 *
		 * @param array  $links already defined action links.
		 * @param string $file  plugin file path and name being processed.
		 *
		 * @return array $links
		 */
		public function plugin_action_links( $links, $file ) {
			$settings_link = '<a href="' . admin_url( 'edit.php?post_type=download&page=edd-settings&tab=extensions&section=wish-list-settings' ) . '">' . esc_html__( 'Settings', 'edd-wish-lists' ) . '</a>';

			if ( 'edd-wish-lists/edd-wish-lists.php' === $file ) {
				array_unshift( $links, $settings_link );
			}

			return $links;
		}
	}

	/**
	 * Loads a single instance of EDD Wish Lists
	 *
	 * This follows the PHP singleton design pattern.
	 *
	 * Use this function like you would a global variable, except without needing
	 * to declare the global.
	 *
	 * @example <?php $edd_wish_lists = edd_wish_lists(); ?>
	 *
	 * @since 1.0
	 *
	 * @see EDD_Wish_Lists::get_instance()
	 *
	 * @return object Returns an instance of the EDD_Wish_Lists class
	 */
	function edd_wish_lists() {
		return EDD_Wish_Lists::get_instance();
	}

	/**
	 * Installation
	 *
	 * Registering the hook inside the 'plugins_loaded' hook will not work.
	 * You can't call register_activation_hook() inside a function hooked to the 'plugins_loaded' or 'init' hooks (or any other hook).
	 * These hooks are called before the plugin is loaded or activated.
	 *
	 * @since 1.0
	 */
	function edd_wl_plugin_activate() {
		include_once __DIR__ . '/includes/install.php';
		include_once __DIR__ . '/includes/rewrites.php';
		edd_wl_install();
	}
	register_activation_hook( __FILE__, 'edd_wl_plugin_activate' );

	/**
	 * Load the Wish Lists Extension Class.
	 *
	 * @deprecated 1.2.0
	 */
	function edd_wl_load_plugin() {}

	require_once __DIR__ . '/vendor/autoload.php';
	\EDD\ExtensionUtils\v1\ExtensionLoader::loadOrQuit(
		__FILE__,
		'edd_wish_lists',
		array(
			'php'                    => '7.1',
			'wp'                     => '5.4',
			'easy-digital-downloads' => '3.1.1',
		)
	);

}
