<?php
/**
 * Plugin Name: Easy Digital Downloads - Cross-sell & Upsell
 * Plugin URI: https://easydigitaldownloads.com/downloads/edd-cross-sell-and-upsell/
 * Description: Increase sales and customer retention by Cross-selling and Upselling to your customers.
 * Version: 1.1.11
 * Author: Easy Digital Downloads
 * Author URI: https://easydigitaldownloads.com/
 * Requires at least: 4.9
 * Requires PHP: 5.6
 * Text Domain: edd-csau
 */

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

require_once __DIR__ . '/vendor/autoload.php';

if ( ! class_exists( 'EDD_Cross_Sell_And_Upsell' ) ) {

	class EDD_Cross_Sell_And_Upsell {

		/**
		 * Holds the instance
		 *
		 * Ensures that only one instance 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;

		/**
		 * Plugin Version
		 */
		private $version = '1.1.11';

		/**
		 * Plugin Title
		 */
		public $title = 'EDD Cross-sell and Upsell';

		/**
		 * Plugin File
		 */
		public $file = __FILE__;

		/**
		 * Plugin Basename
		 */
		public $basename = '';

		/**
		 * Plugin Directory
		 */
		public $plugin_dir = '';

		/**
		 * Plugin URL
		 */
		public $plugin_url = '';

		/**
		 * Includes Directory
		 */
		public $includes_dir = '';

		/**
		 * Includes URL
		 */
		public $includes_url = '';

		/**
		 * Tools
		 */
		public $tools = '';

		/**
		 * 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_Cross_Sell_And_Upsell ) ) {
				self::$instance = new EDD_Cross_Sell_And_Upsell();
				self::$instance->setup_globals();
				self::$instance->includes();
				self::$instance->setup_actions();
				self::$instance->licensing();
				self::$instance->load_textdomain();

				if ( is_admin() ) {
					self::$instance->tools = new EDD_CSAU_Tools();
				}
			}

			return self::$instance;
		}

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

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

			// paths
			$this->basename   = apply_filters( 'edd_csau_plugin_basenname', plugin_basename( $this->file ) );
			$this->plugin_dir = apply_filters( 'edd_csau_plugin_dir_path', plugin_dir_path( $this->file ) );
			$this->plugin_url = apply_filters( 'edd_csau_plugin_dir_url', plugin_dir_url( $this->file ) );

			// includes
			$this->includes_dir = apply_filters( 'edd_csau_includes_dir', trailingslashit( $this->plugin_dir . 'includes' ) );
			$this->includes_url = apply_filters( 'edd_csau_includes_url', trailingslashit( $this->plugin_url . 'includes' ) );

			// constants
			if ( ! defined( 'EDD_CSAU_VERSION' ) ) {
				define( 'EDD_CSAU_VERSION', $this->version );
			}

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

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

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

			add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ), 10, 2 );

			do_action( 'edd_csau_setup_actions' );
		}

		/**
		 * Licensing
		 *
		 * @since 1.0
		*/
		private function licensing() {
			add_action(
				'edd_extension_license_init',
				function ( \EDD\Extensions\ExtensionRegistry $registry ) {
					$registry->addExtension( __FILE__, $this->title, 82097, $this->version );
				}
			);
		}

		/**
		 * Loads the plugin language files
		 *
		 * @access public
		 * @since 1.0.4
		 * @return void
		 */
		public function load_textdomain() {

			// Set filter for plugin's languages directory
			$lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
			$lang_dir = apply_filters( 'edd_csau_languages_directory', $lang_dir );

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

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

			if ( file_exists( $mofile_global ) ) {
				// Look in global /wp-content/languages/edd-csau/ folder
				load_textdomain( 'edd-csau', $mofile_global );
			} elseif ( file_exists( $mofile_local ) ) {
				// Look in local /wp-content/plugins/edd-csau/languages/ folder
				load_textdomain( 'edd-csau', $mofile_local );
			} else {
				// Load the default language files
				load_plugin_textdomain( 'edd-csau', false, $lang_dir );
			}
		}

		/**
		 * Include required files.
		 *
		 * @since 1.0
		 *
		 * @return void
		 */
		private function includes() {

			include_once EDD_CSAU_DIR . 'includes/template-functions.php';
			include_once EDD_CSAU_DIR . 'includes/cart-functions.php';
			include_once EDD_CSAU_DIR . 'includes/payment-actions.php';
			include_once EDD_CSAU_DIR . 'includes/payment-functions.php';
			include_once EDD_CSAU_DIR . 'includes/functions.php';
			include_once EDD_CSAU_DIR . 'includes/scripts.php';

			do_action( 'edd_csau_include_files' );

			if ( is_admin() ) {

				include_once EDD_CSAU_DIR . 'includes/reports.php';
				include_once EDD_CSAU_DIR . 'includes/logs.php';
				include_once EDD_CSAU_DIR . 'includes/metabox.php';
				include_once EDD_CSAU_DIR . 'includes/admin-settings.php';
				include_once EDD_CSAU_DIR . 'includes/contextual-help.php';
				include_once EDD_CSAU_DIR . 'includes/dashboard-columns.php';
				include_once EDD_CSAU_DIR . 'includes/view-order-details.php';
				include_once EDD_CSAU_DIR . 'includes/export-functions.php';
				include_once EDD_CSAU_DIR . 'includes/admin/class-tools.php';

				do_action( 'edd_csau_include_admin_files' );

			}
		}

		/**
		 * Plugin settings link
		 *
		 * @since 1.1
		*/
		public function settings_link( $links ) {
			$plugin_links = array(
				'<a href="' . admin_url( 'edit.php?post_type=download&page=edd-settings&tab=extensions&section=csau' ) . '">' . __( 'Settings', 'edd-csau' ) . '</a>',
			);

			return array_merge( $plugin_links, $links );
		}
	}

	/**
	 * Loads a single instance
	 *
	 * 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_cross_sell_and_upsell = edd_cross_sell_and_upsell_load(); ?>
	 *
	 * @since 1.0
	 *
	 * @see EDD_Cross_Sell_And_Upsell::get_instance()
	 *
	 * @return object Returns an instance of the EDD_Cross_Sell_And_Upsell class
	 */
	function edd_cross_sell_and_upsell_load() {
		return EDD_Cross_Sell_And_Upsell::get_instance();
	}

	\EDD\ExtensionUtils\v1\ExtensionLoader::loadOrQuit(
		__FILE__,
		'edd_cross_sell_and_upsell_load',
		array(
			'php'                    => '5.6',
			'easy-digital-downloads' => '2.11.4',
		)
	);

}
