<?php
/**
 * Plugin Name: Easy Digital Downloads - Braintree
 * Plugin URI: https://easydigitaldownloads.com/extensions/braintree-gateway/
 * Description: Accept credit card payments in EDD using your Braintree merchant account.
 * Author: Sandhills Development, LLC
 * Author URI: https://sandhillsdev.com
 * Version: 1.2.1
 * Text Domain: edd-braintree
 * Domain Path: languages

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace EDD_Braintree;

use EDD_Braintree\Admin\Settings;

class Main {

	/**
	 * Holds the singleton instance.
	 *
	 * @var $instance Main
	 */
	private static $instance;

	/**
	 * The gateway specific operations for the plugin.
	 *
	 * @var $gateway Gateway
	 */
	public $gateway;

	/**
	 * Handles settings related to the Braintree integration.
	 *
	 * @var $gateway Settings
	 */
	private $settings;

	/**
	 * Load the one true instance of the EDD Braintree integration.
	 *
	 * @return Main
	 */
	public static function instance() {
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Main ) ) {
			self::$instance = new Main();

			self::$instance->setup_constants();

			if ( class_exists( 'EDD_License' ) && is_admin() ) {
				$edd_bt_license = new \EDD_License( __FILE__, 'Braintree', EDD_BRAINTREE_VERSION, 'Sandhills Development', 'braintree_license_key', null, 28130 );
				unset( $edd_bt_license );
			}

			if ( self::$instance->requirements_met() ) {
				self::$instance->includes();
				self::$instance->loader();
				self::$instance->hooks();
			}
		}

		return self::$instance;
	}

	/**
	 * @since 1.2
	 */
	private function setup_constants() {
		if ( ! defined( 'EDD_BRAINTREE_VERSION' ) ) {
			define( 'EDD_BRAINTREE_VERSION', '1.2.1' );
		}

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

	/**
	 * Determines whether or not the plugin's requirements have been met.
	 *
	 * @since 1.2
	 * @return bool
	 */
	private function requirements_met() {
		if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
			add_action( 'admin_notices', array( self::$instance, 'php_version_notice' ) );

			return false;
		}

		if ( ! class_exists( 'Easy_Digital_Downloads' ) || version_compare( EDD_VERSION, '2.4', '<' ) ) {
			add_action( 'admin_notices', array( self::$instance, 'edd_version_notice' ) );

			return false;
		}

		global $wp_version;

		if ( version_compare( $wp_version, '4.2', '<' ) ) {
			add_action( 'admin_notices', array( self::$instance, 'wp_version_notice' ) );

			return false;
		}

		return true;
	}

	/**
	 * Displays an admin notice about PHP version requirements.
	 */
	public function php_version_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<?php printf( __( '<strong>Notice:</strong> Easy Digital Downloads Braintree requires PHP version 5.4 or higher in order to function properly. You are running version %s.', 'edd-braintree' ), PHP_VERSION ); ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Displays an admin notice about EDD version requirements.
	 */
	public function edd_version_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<?php _e( '<strong>Notice:</strong> Easy Digital Downloads Braintree requires Easy Digital Downloads 2.5 or higher in order to function properly.', 'edd-braintree' ); ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Displays an admin notice about WP Core version requirements.
	 */
	public function wp_version_notice() {
		global $wp_version;
		?>
		<div class="notice notice-error">
			<p>
				<?php printf( __( '<strong>Notice:</strong> Easy Digital Downloads Braintree requires WordPress version 4.2 or higher in order to function properly. You are running version %s.', 'edd-braintree' ), $wp_version ); ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Include any files needed.
	 */
	private function includes() {
		require_once plugin_dir_path( __FILE__ ) . 'braintree/lib/Braintree.php';
		require_once plugin_dir_path( __FILE__ ) . 'includes/class-edd-braintree-gateway.php';
		require_once plugin_dir_path( __FILE__ ) . 'includes/scripts.php';
		require_once plugin_dir_path( __FILE__ ) . 'includes/functions.php';

		if ( is_admin() ) {
			require_once plugin_dir_path( __FILE__ ) . 'includes/admin/class-settings.php';
			require_once plugin_dir_path( __FILE__ ) . 'includes/admin/orders.php';
			require_once plugin_dir_path( __FILE__ ) . 'includes/admin/upgrades.php';
		}
	}

	/**
	 * Load the classes needed to operate.
	 */
	private function loader() {
		self::$instance->gateway = new Gateway();

		if ( is_admin() ) {
			self::$instance->settings = new Admin\Settings();
		}
	}

	/**
	 * Register any hooks.
	 */
	private function hooks() {
		add_action( 'init', array( $this, 'load_textdomain' ) );
	}

	/**
	 * Load the text domain.
	 */
	public function load_textdomain() {

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

		// Load the translations.
		load_plugin_textdomain( 'edd-braintree', false, $edd_lang_dir );

	}


}

/**
 * Load and return the one true instance of the EDD_Braintree\Main class.
 *
 * @return Main
 */
function edd_braintree() {
	return Main::instance();
}

add_action( 'plugins_loaded', '\EDD_Braintree\edd_braintree' );
