<?php

/**
 * Handle Freemius integration.
 *
 * @since      1.0.0
 *
 * @package    Woocommerce_Product_Tabs
 */

namespace Barn2\Plugin\WC_Product_Tabs_Pro;

use Freemius as FS;

/**
 * Handle Freemius integration.
 *
 * @package    Woocommerce_Product_Tabs
 */
class Freemius {

	/**
	 * The main plugin instance.
	 *
	 * @since    1.1.0
	 * @access   protected
	 * @var      Plugin $plugin    The main plugin instance.
	 */
	protected $plugin;

	/**
	 * The array of actions registered with WordPress.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      array    $actions    The actions registered with WordPress to fire when the plugin loads.
	 */
	protected $actions;

	/**
	 * The array of filters registered with WordPress.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      array    $filters    The filters registered with WordPress to fire when the plugin loads.
	 */
	protected $filters;

	protected $instance;
	protected $helper;

	/**
	 * Initialize the collections used to maintain the actions and filters.
	 *
	 * @since    1.0.0
	 */
	public function __construct( $plugin ) {
		$this->plugin   = $plugin;
		$this->instance = null;
		$this->helper   = null;

		$this->instance = FS::instance( WOOCOMMERCE_PRODUCT_TABS_FREEMIUS_ID, WOOCOMMERCE_PRODUCT_TABS_SLUG, true );
		$this->init();
	}

	public function init() {
		global $barn2_freemius_helper;

		if ( ! isset( $barn2_freemius_helper ) ) {
			if ( ! $this->instance->can_use_premium_code() ) {
				$this->disable_freemius_hooks();

				return;
			}

			$barn2_freemius_helper = fs_dynamic_init( array(
				'id'              => WOOCOMMERCE_PRODUCT_TABS_FREEMIUS_ID,
				'slug'            => WOOCOMMERCE_PRODUCT_TABS_SLUG,
				'premium_slug'    => 'woocommerce-product-tabs-pro',
				'type'            => 'plugin',
				'public_key'      => 'pk_afd4e1ad89d8569866f3deea36605',
				'is_premium'      => true,
				'is_premium_only' => true,
				'has_addons'      => false,
				'has_paid_plans'  => true,
				'menu'            => array(
					'slug'       => 'wpt-settings',
					'first-path' => 'admin.php?page=wpt-welcome',
					'support'    => false,
					'parent'     => array(
						'slug' => 'wpt-options',
					),
				),
			) );
		}

		$this->helper = $barn2_freemius_helper;
	}

	public function get_instance() {
		return $this->instance;
	}

	public function get_helper() {
		return $this->helper;
	}

	public function has_valid_freemius_license() {
		return $this->instance->can_use_premium_code();
	}

	public function disable_freemius_hooks() {
		$freemius = $this->instance;

		remove_action( 'admin_init', array( $freemius, '_hook_action_links_and_register_account_hooks' ) );
		remove_filter( 'site_transient_update_plugins', array( 'Freemius', '_remove_fs_updates_from_plugin_install_page' ), 10 );
		remove_action( 'admin_footer', array( 'Freemius', '_prepend_fs_allow_updater_and_dialog_flag_url_param' ) );
		remove_action( 'after_switch_theme', array( $freemius, '_activate_theme_event_hook' ) );
		remove_action( 'admin_footer', array( $freemius, '_style_premium_theme' ) );
		remove_action( 'plugins_loaded', array( $freemius, '_plugins_loaded' ) );
		remove_action( 'init', array( $freemius, '_add_default_submenu_items' ), 999999999 );
		remove_action( 'init', array( $freemius, '_maybe_show_gdpr_admin_notice' ) );
		remove_action( 'init', array( $freemius, '_maybe_add_gdpr_optin_ajax_handler') );
		remove_action( 'init', array( $freemius, '_maybe_add_pricing_ajax_handler' ) );
		remove_action( 'wpmu_new_blog', array( $freemius, '_after_new_blog_callback' ) );
		remove_action( 'wp_initialize_site', array( $freemius, '_after_wp_initialize_site_callback' ), 11 );
		remove_action( 'deactivate_blog', array( $freemius, '_after_site_deactivated_callback' ) );
		remove_action( 'archive_blog', array( $freemius, '_after_site_deactivated_callback' ) );
		remove_action( 'make_spam_blog', array( $freemius, '_after_site_deactivated_callback' ) );
		remove_action( 'deleted_blog', array( $freemius, '_after_site_deleted_callback' ) );
		remove_action( 'wp_delete_site', array( $freemius, '_after_wpsite_deleted_callback' ) );
		remove_action( 'activate_blog', array( $freemius, '_after_site_reactivated_callback' ) );
		remove_action( 'unarchive_blog', array( $freemius, '_after_site_reactivated_callback' ) );
		remove_action( 'make_ham_blog', array( $freemius, '_after_site_reactivated_callback' ) );
		remove_action( 'customize_register', array( $freemius, '_customizer_register' ) );
		remove_action( 'admin_init', array( $freemius, '_redirect_on_clicked_menu_link' ), 999999999 );
		remove_action( 'admin_init', array( $freemius, '_add_tracking_links' ) );
		remove_action( 'admin_init', array( $freemius, '_add_license_activation' ) );
		remove_action( 'admin_init', array( $freemius, '_add_premium_version_upgrade_selection' ) );
		remove_action( 'admin_init', array( $freemius, '_add_beta_mode_update_handler' ) );
		remove_action( 'admin_init', array( $freemius, '_add_user_change_option' ) );
		remove_action( 'admin_init', array( $freemius, '_add_email_address_update_option' ) );
		remove_action( 'wp_ajax_update_billing', array( $freemius, '_update_billing_ajax_action' ) );
		remove_action( 'wp_ajax_start_trial', array( $freemius, '_start_trial_ajax_action' ) );
		remove_action( 'wp_ajax_set_data_debug_mode', array( $freemius, '_set_data_debug_mode' ) );
		remove_action( 'wp_ajax_toggle_whitelabel_mode', array( $freemius, '_toggle_whitelabel_mode_ajax_handler' ) );
		remove_action( 'wp_ajax_network_activate', array( $freemius, '_network_activate_ajax_action' ) );
		remove_action( 'wp_ajax_install_premium_version', array( $freemius, '_install_premium_version_ajax_action' ) );
		remove_action( 'submit_affiliate_application', array( $freemius, '_submit_affiliate_application' ) );
		remove_action( 'fs_after_plans_sync', array( $freemius, '_check_for_trial_plans' ) );
		remove_action( 'fs_sdk_version_update', array( $freemius, '_sdk_version_update' ) );
		remove_action( 'fs_plugin_version_update', array( $freemius, '_after_version_update' ) );
		remove_filter( 'fs_after_code_type_change', array( $freemius, '_after_code_type_change' ) );
		remove_action( 'admin_init', array( $freemius, '_add_trial_notice' ) );
		remove_action( 'admin_init', array( $freemius, '_add_affiliate_program_notice' ) );
		remove_action( 'admin_enqueue_scripts', array( $freemius, '_enqueue_common_css' ) );
		remove_action( 'admin_init', array( $freemius, 'connect_again' ) );
	}
}
