<?php // phpcs:ignore WordPress.Files.FileName
/**
 * The class that manage all plugin
 *
 * @package YITH\PayPalPayouts
 */

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

if ( ! class_exists( 'YITH_PayPal_PayOuts' ) ) {
	/**
	 * Class YITH_PayPal_PayOuts
	 */
	class YITH_PayPal_PayOuts {

		/**
		 * The instance of the class
		 *
		 * @var YITH_PayPal_PayOuts
		 */
		protected static $instance;

		/**
		 * YITH_PayPal_PayOuts constructor.
		 */
		public function __construct() {

			add_action( 'plugins_loaded', array( $this, 'plugin_fw_loader' ), 15 );
			add_action( 'plugins_loaded', array( $this, 'load_privacy_class' ), 20 );
			if ( is_admin() ) {

				$this->payouts_admin = new YITH_PayPal_PayOuts_Admin();
			}

			add_action( 'init', array( $this, 'load_payouts_classes' ), 20 );
			add_action( 'wp_enqueue_scripts', array( $this, 'include_frontend_scripts' ), 20 );
			// Add action for register and update plugin.
			add_action( 'wp_loaded', array( $this, 'register_plugin_for_activation' ), 99 );
			add_action( 'admin_init', array( $this, 'register_plugin_for_updates' ) );

			add_filter( 'yith_paypal_payoyts_exclude_customer_ids', array( $this, 'exclude_user_id_from_search' ), 10, 1 );
		}

		/**
		 * Unique instance for the class
		 *
		 * @return YITH_PayPal_PayOuts
		 * @since 1.0.0
		 * @author YITH <plugins@yithemes.com>
		 */
		public static function get_instance() {

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

			return self::$instance;
		}


		/**
		 * Load the YITH plugin Framework
		 *
		 * @since 1.0.0
		 */
		public function plugin_fw_loader() {
			if ( ! defined( 'YIT_CORE_PLUGIN' ) ) {
				global $plugin_fw_data;
				if ( ! empty( $plugin_fw_data ) ) {
					$plugin_fw_file = array_shift( $plugin_fw_data );
					require_once $plugin_fw_file;
				}
			}
		}

		/**
		 * Load Payout Service Class
		 *
		 * @since  1.0.0
		 */
		public function load_payouts_classes() {
			require_once YITH_PAYOUTS_INC . 'abstract-yith-payouts-service.php';
			require_once YITH_PAYOUTS_INC . 'class.yith-payouts-service.php';
			YITH_PayOuts_Service();
		}

		/**
		 * Register style
		 *
		 * @since 1.0.0
		 */
		public function include_frontend_scripts() {
			wp_register_style( 'yith_payouts_status', YITH_PAYOUTS_ASSETS_URL . 'css/payouts-status.css', array(), YITH_PAYOUTS_VERSION );
		}

		/**
		 * Load privacy class
		 *
		 * @since 1.0.0
		 */
		public function load_privacy_class() {

			require_once YITH_PAYOUTS_INC . 'class.yith-payouts-privacy.php';

			new YITH_Payouts_Privacy();
		}

		/**
		 * Register plugins for activation tab.
		 *
		 * @since 1.0.0
		 */
		public function register_plugin_for_activation() {
			if ( ! class_exists( 'YIT_Plugin_Licence' ) ) {

				require_once YITH_PAYOUTS_DIR . 'plugin-fw/licence/lib/yit-licence.php';
				require_once YITH_PAYOUTS_DIR . 'plugin-fw/licence/lib/yit-plugin-licence.php';
			}
			YIT_Plugin_Licence()->register( YITH_PAYOUTS_INIT, YITH_PAYOUTS_SECRET_KEY, YITH_PAYOUTS_SLUG );
		}

		/**
		 * Register plugins for update tab
		 *
		 * @since    1.0.0
		 */
		public function register_plugin_for_updates() {
			if ( ! class_exists( 'YIT_Upgrade' ) ) {

				require_once YITH_PAYOUTS_DIR . 'plugin-fw/lib/yit-upgrade.php';
			}
			YIT_Upgrade()->register( YITH_PAYOUTS_SLUG, YITH_PAYOUTS_INIT );
		}

		/**
		 * Exclude from search , specific users
		 *
		 * @param array $excluded_ids The ids excluded.
		 *
		 * @return array
		 * @since 2.0.0
		 */
		public function exclude_user_id_from_search( $excluded_ids ) {

			if ( function_exists( 'yith_wcmv_get_user_meta_key' ) ) {
				$excluded     = get_users(
					array(
						'fields'       => 'ids',
						'meta_key'     => yith_wcmv_get_user_meta_key(), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
						'meta_compare' => 'EXISTS',
					)
				);
				$excluded_ids = array_merge( $excluded_ids, $excluded );
			}
			if ( class_exists( 'YITH_WCAF_Affiliate_Factory' ) ) {
				$excluded     = YITH_WCAF_Affiliate_Factory::get_affiliates(
					array(
						'fields' => 'user_id',
					)
				);
				$excluded_ids = array_merge( $excluded_ids, $excluded );
			}

			return $excluded_ids;
		}
	}
}

/**
 * Return the instance
 *
 * @return YITH_PayPal_PayOuts
 */
function YITH_PayPal_Payouts() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName
	return YITH_PayPal_PayOuts::get_instance();
}
