<?php
/*
Plugin Name: Share, Print and PDF Products for WooCommerce
Plugin URI: https://xforwoocommerce.com
Description: XforWooCommerce Themes and Plugins! Visit https://xforwoocommerce.com
Author: XforWooCommerce
License: Codecanyon Split Licence
Version: 3.1.1
Requires at least: 4.5
Tested up to: 7.9.9
WC requires at least: 3.5.0
WC tested up to: 9.9.9
Author URI: https://xforwoocommerce.com
Text Domain: share-print-pdf-woocommerce
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'XforWC' ) ) {
	require_once 'includes/svx-settings/load.php';

	if ( xforwccb() ) {
		return false; }
}

$GLOBALS['svx'] = isset( $GLOBALS['svx'] ) && 1 === version_compare( $GLOBALS['svx'], '1.8.1' ) ? $GLOBALS['svx'] : '1.8.1';

if ( ! class_exists( 'XforWC_PDF_Print_Share' ) ) :

	final class XforWC_PDF_Print_Share {

		public static $version = '3.1.1';

		protected static $_instance = null;

		public static function instance() {

			if ( is_null( self::$_instance ) ) {
				self::$_instance = new self();
			}
			return self::$_instance;
		}

		public function __construct() {
			do_action( 'wcmnspp_loading' );

			$this->includes();

			if ( ! function_exists( 'XforWC' ) ) {
				$this->single_plugin();
			}

			do_action( 'wcmnspp_loaded' );
		}

		private function single_plugin() {
			if ( is_admin() ) {
				register_activation_hook( __FILE__, array( $this, 'activate' ) );
			}

			include_once 'includes/svx-settings/svx-get.php';
			add_action( 'init', array( $this, 'load_svx' ), 100 );

			// Texdomain only used if out of XforWC
			add_action( 'init', array( $this, 'textdomain' ), 0 );

			add_action(
				'before_woocommerce_init',
				function() {
					if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
						\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
					}
				}
			);
		}

		public function activate() {
			if ( ! class_exists( 'WooCommerce' ) ) {
				deactivate_plugins( plugin_basename( __FILE__ ) );

				wp_die( esc_html__( 'This plugin requires WooCommerce. Download it from WooCommerce official website', 'share-print-pdf-woocommerce' ) . ' &rarr; https://woo.com' );
				exit;
			}
		}

		public function load_svx() {
			if ( is_admin() ) {
				include_once 'includes/svx-settings/svx-settings.php';
			}
		}

		private function is_request( $type ) {
			switch ( $type ) {
				case 'admin':
					return is_admin();
				case 'ajax':
					return defined( 'DOING_AJAX' );
				case 'cron':
					return defined( 'DOING_CRON' );
				case 'frontend':
					return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
			}
		}

		public function includes() {

			if ( is_admin() ) {
				include_once 'includes/spp-settings.php';
			}

			if ( $this->is_request( 'frontend' ) ) {
				$this->frontend_includes();
			}

		}

		public function frontend_includes() {
			include_once 'includes/spp-frontend.php';
		}

		public function textdomain() {

			$this->load_plugin_textdomain();

		}

		public function load_plugin_textdomain() {

			$domain = 'share-print-pdf-woocommerce';
			$dir    = untrailingslashit( WP_LANG_DIR );
			$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
			$loaded = load_textdomain( $domain, $dir . '/plugins/' . $domain . '-' . $locale . '.mo' );

			if ( $loaded ) {
				return $loaded;
			} else {
				load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/lang/' );
			}

		}

		public function plugin_url() {
			return untrailingslashit( plugins_url( '/', __FILE__ ) );
		}

		public function plugin_path() {
			return untrailingslashit( plugin_dir_path( __FILE__ ) );
		}

		public function plugin_basename() {
			return untrailingslashit( plugin_basename( __FILE__ ) );
		}

		public function ajax_url() {
			return admin_url( 'admin-ajax.php', 'relative' );
		}

		public function version() {
			return self::$version;
		}

	}

	function Wcmnspp() {
		return XforWC_PDF_Print_Share::instance();
	}

	XforWC_PDF_Print_Share::instance();


endif;
