<?php

/**
 * Loads and wires up Pro-only classes, but only when the /pro directory actually contains its files.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 */

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

/**
 * NudgeSells_Pro_Loader
 *
 * Detects whether Pro is available and, if so, requires and registers every Pro-only class's hooks.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Pro_Loader
{

	private static $active = null;

	/**
	 * Is active.
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public static function is_active()
	{
		if (null === self::$active) {
			self::$active = nud_fs()->is_plan_or_trial('pro');
		}
		return self::$active;
	}

	/**
	 * Load.
	 *
	 * @param NudgeSells_Loader $loader
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function load(NudgeSells_Loader $loader)
	{
		if (! self::is_active()) {
			return;
		}

		if (! file_exists(NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-experiment-cpt.php')) {
			return;
		}

		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-experiment-cpt.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-experiment-repository.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-experiment.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-tools.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-analytics.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-suggestions-data.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-settings.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-conditions.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'pro/class-nudgesells-pro-admin-offer-tabs.php';

		new NudgeSells_Pro_Admin_Settings();
		new NudgeSells_Pro_Admin_Conditions();
		new NudgeSells_Pro_Admin_Offer_Tabs();

		$experiment_cpt = new NudgeSells_Pro_Experiment_CPT();
		add_action('init', array($experiment_cpt, 'register'), 5);
		$loader->add_filter('manage_' . NudgeSells_Pro_Experiment_CPT::POST_TYPE . '_posts_columns', $experiment_cpt, 'columns');
		$loader->add_action('manage_' . NudgeSells_Pro_Experiment_CPT::POST_TYPE . '_posts_custom_column', $experiment_cpt, 'render_column', 10, 2);
		$loader->add_filter('post_row_actions', $experiment_cpt, 'row_actions', 10, 2);

		$admin_experiment = new NudgeSells_Pro_Admin_Experiment();
		$loader->add_action('add_meta_boxes', $admin_experiment, 'add_metaboxes');
		$loader->add_action('save_post_' . NudgeSells_Pro_Experiment_CPT::POST_TYPE, $admin_experiment, 'save');
		$loader->add_action('admin_enqueue_scripts', $admin_experiment, 'assets');

		$loader->add_filter('nudgesells_resolve_offer_id', 'NudgeSells_Pro_Experiment_Repository', 'resolve_offer_id');
		$loader->add_action('nudgesells_impression_recorded', 'NudgeSells_Pro_Experiment_Repository', 'record_impression');
		$loader->add_action('nudgesells_conversion_recorded', 'NudgeSells_Pro_Experiment_Repository', 'record_conversion', 10, 2);

		$admin_tools = new NudgeSells_Pro_Admin_Tools();
		$loader->add_action('admin_post_nudgesells_export_offers', $admin_tools, 'handle_export');
		$loader->add_action('admin_post_nudgesells_import_offers', $admin_tools, 'handle_import');
		$loader->add_action('admin_enqueue_scripts', $admin_tools, 'assets');
	}
}
