<?php
// Since version 8.9
/*
How to use:
\Indeed\Uap\Integrations::isWooActive()
\Indeed\Uap\Integrations::isUmpActive()
\Indeed\Uap\Integrations::isEddActive()
\Indeed\Uap\Integrations::isUlpActive()
\Indeed\Uap\Integrations::isWpFormsActive()
\Indeed\Uap\Integrations::isNinjaActive()
\Indeed\Uap\Integrations::isCf7Active()
*/
namespace Indeed\Uap;

defined( 'ABSPATH' ) || exit;

class Integrations
{
    /**
     * @param none
     * @return none
     */
    public function __construct(){}

    /**
      * @param none
      * @return array
      */
    public static function getSystems()
    {
        $systems = array(
             			 	'woo' => array(
             			 			'label' => 'WooCommerce',
                        'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('This integration ensures accurate tracking and seamless commission management for all your %s transactions', 'uap'), 'WooCommerce' ),
             			 			'status' => self::isWooActive() && uap_is_woo_active() ? 1 : 0,
             			 			'extra-details-link' => 'https://ultimateaffiliate.pro/integrations/affiliate-program-for-woocommerce/',
             			 			'extra-settings-link' => '',
             						'installed' => uap_is_woo_active() ? 1 : 0,
             			 	),
             			 	'ump' => array(
             			 			'label' => 'Ultimate Membership Pro',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => esc_html__('Reward affiliates for referring new members and selling memberships via your membership site', 'uap'),
             			 			'status' => self::isUmpActive() && uap_is_ump_active() ? 1 : 0,
             			 			'extra-details-link' => '',
             			 			'extra-settings-link' => '',
             						'installed' => uap_is_ump_active() ? 1 : 0,
             			 	),
             			 	'edd' => array(
             			 			'label' => 'Easy Digital Downloads',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('Enhance your affiliate program with precise tracking and efficient commission management for your %s store', 'uap'), 'EDD' ),
             			 			'status' => self::isEddActive() && uap_is_edd_active() ? 1 : 0,
             			 			'extra-details-link' => 'https://ultimateaffiliate.pro/integrations/easy-digital-downloads/',
             			 			'extra-settings-link' => '',
             						'installed' => uap_is_edd_active() ? 1 : 0,
             			 	),
             			 	'ulp' => array(
             			 			'label' => 'Ultimate Learning Pro',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('Easily integrate %s to seamlessly track and reward your affiliates for promoting and selling your online courses', 'uap'), 'Ultimate Learning Pro' ),
             			 			'status' => self::isUlpActive() && uap_is_ulp_active() ? 1 : 0,
             			 			'extra-details-link' => '',
             			 			'extra-settings-link' => '',
             						'installed' => uap_is_ulp_active() ? 1 : 0,
             			 	),
                    'woocommerce_subscriptions' =>  array(
                         'label' 								=> esc_html__('WooCommerce Subscriptions', 'uap'),
                         'author' 							=> UAP_NAME,
                         'author-link' 					=> 'https://ultimateaffiliate.pro',
                         'description' 					=> esc_html__('Pay off affiliates for every WooCommerce Subscription', 'uap'),
                         'status' 							=> get_option( 'uap-integrations-woocommerce_subscriptions', false ) && self::woocsActive() ? 1 : 0,
                         'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/woocommerce-subscriptions-tracking/',
                         'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_woos'),
                         'installed' 						=> self::woocsActive() ? 1 : 0,
                    ),
             				'cf7' => array(
             			 			'label' => 'Contact Form 7',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('Effectively reward your affiliates for every %s submitted by referred users', 'uap'), 'Contact Form 7' ),
             			 			'status' => self::isCf7Active() && (uap_is_cf7_active() && self::cf7AddonActive() ) ? 1 : 0,
             			 			'extra-details-link' => '',
             			 			'extra-settings-link' => admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_cf7t'),
             						'installed' => (uap_is_cf7_active() && self::cf7AddonActive() ) ? 1 : 0,
             			 	),
             				'wpforms' => array(
             			 			'label' => 'WPForms',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('Allocate commissions to your affiliates for each %s submission', 'uap'), 'WPForms' ),
             			 			'status' => self::isWpFormsActive() && (uap_is_wpforms_active() && self::wpformsAddonActive()) ? 1 : 0,
             			 			'extra-details-link' => 'https://ultimateaffiliate.pro/integrations/wpforms/',
             			 			'extra-settings-link' => admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_wp_forms'),
             						'installed' => (uap_is_wpforms_active() && self::wpformsAddonActive()) ? 1 : 0,
             			 	),
             				'ninjaforms' => array(
             			 			'label' => 'Ninja Forms',
             			 			'author' => UAP_NAME,
             			 			'author-link' => 'https://ultimateaffiliate.pro',
             			 			'description' => sprintf( esc_html__('Pay off affiliates for every %s submission', 'uap'), 'Ninja Form' ),
             			 			'status' => self::isNinjaActive() && ( ( uap_is_ninjaforms_active() || uap_is_ninjaformsdemo_active() ) && self::ninjaAddonActive()) ? 1 : 0,
             			 			'extra-details-link' => 'https://ultimateaffiliate.pro/integrations/ninja-forms/',
             			 			'extra-settings-link' => admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_nft'),
             						'installed' => ( ( uap_is_ninjaforms_active() || uap_is_ninjaformsdemo_active() ) && self::ninjaAddonActive()) ? 1 : 0,
             			 	),
                    'fluent_forms'           =>  array(
              					 'label' 								=> 'Fluent Forms',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Every time a form is submitted through %s, the affiliate will receive a referral.', 'uap'), 'Fluent Forms' ),
              					 'status' 							=> get_option( 'uap-integrations-fluent_forms', false ) && self::isFluentFormsActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/fluent-forms-tracking/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_fluent_forms'),
              					 'installed' 						=> self::isFluentFormsActive() ? 1 : 0,
              			),
                    'formidable_forms' =>  array(
              					 'label' 								=> 'Formidable Forms',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive referrals every time when a %s is submitted', 'uap'), 'Formidable Form' ),
              					 'status' 							=> get_option( 'uap-integrations-formidable_forms', false ) && self::isFfActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/formidable-forms/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_formidable_forms'),
              					 'installed' 						=> self::isFfActive() ? 1 : 0,
              			),
                    'gravity_forms'     =>  array(
              					 'label' 								=> 'Gravity Forms',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive referrals every time when a %s is submitted', 'uap'), 'Gravity Form' ),
              					 'status' 							=> get_option( 'uap-integrations-gravity_forms', false ) && self::isGfActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/gravity-forms/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_gft'),
              					 'installed' 						=> self::isGfActive() ? 1 : 0,
              			),
                    'member_mouse'      =>  array(
              					 'label' 								=> 'MemberMouse',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every purchase through %s.', 'uap'), 'MemberMouse' ),
              					 'status' 							=> get_option( 'uap-integrations-member_mouse', false ) && self::isMemberMouseActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/membermouse/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_member_mouse'),
              					 'installed' 						=> self::isMemberMouseActive() ? 1 : 0,
              			),
                    'memberpress'       =>  array(
              					 'label' 								=> 'MemberPress',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every purchase through %s.', 'uap'), 'MemberPress' ),
              					 'status' 							=> get_option( 'uap-integrations-memberpress', false ) && self::isMemberpressActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/memberpress/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_memberpress'),
              					 'installed' 						=> self::isMemberpressActive() ? 1 : 0,
              			),
                    'pmpro'              =>  array(
              					 'label' 								=> 'Paid Memberships Pro',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive referrals every time a membership is purchased through $s.', 'uap'), 'Paid Memberships Pro' ),
              					 'status' 							=> get_option( 'uap-integrations-pmpro', false ) && self::isPmProActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/paid-memberships-pro/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_pmpro'),
              					 'installed' 						=> self::isPmProActive() ? 1 : 0,
              			),
                    'pms'                 =>  array(
              					 'label' 								=> 'Paid Member Subscriptions',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every purchase through %s.', 'uap'), 'Paid Member Subscriptions' ),
              					 'status' 							=> get_option( 'uap-integrations-uap_pms', false ) && self::isPmsActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/paid-membership-subscriptions/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_pms'),
              					 'installed' 						=> self::isPmsActive() ? 1 : 0,
              			),
                    'rcp'                 =>  array(
              					 'label' 								=> 'Restrict Content Pro',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every purchase through %s plugin.', 'uap'), 'Restrict Content Pro' ),
              					 'status' 							=> get_option( 'uap-integrations-rcp', false ) && self::isRcpActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/restrict-content-pro/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_rcp'),
              					 'installed' 						=> self::isRcpActive() ? 1 : 0,
              			),
                    'tutor_lms'           =>  array(
              					 'label' 								=> 'Tutor LMS',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every course purchased through %s.', 'uap'), 'Tutor LMS' ),
              					 'status' 							=> get_option( 'uap-integrations-tutor_lms', false ) && self::isTutorLmsActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/tutor-lms-tracking/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_tutor_lms'),
              					 'installed' 						=> self::isTutorLmsActive() ? 1 : 0,
              			),
                    'lifter_lms'           =>  array(
              					 'label' 								=> 'Lifter LMS',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every course purchased through %s.', 'uap'), 'Lifter LMS' ),
              					 'status' 							=> get_option( 'uap-integrations-lifter_lms', false ) && self::isLifterLmsActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/lifter-lms-tracking/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_lifter_lms'),
              					 'installed' 						=> self::isLifterLmsActive() ? 1 : 0,
              			),
                    'learndash'           =>  array(
              					 'label' 								=> 'LearnDash',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every course purchased through %s.', 'uap'), 'LearnDash' ),
              					 'status' 							=> get_option( 'uap-integrations-learndash', false ) && self::isLearnDashActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/learndash/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_learndash'),
              					 'installed' 						=> self::isLearnDashActive() ? 1 : 0,
              			),
                    'learnpress'           =>  array(
              					 'label' 								=> 'LearnPress',
              					 'author' 							=> UAP_NAME,
              					 'author-link' 					=> 'https://ultimateaffiliate.pro',
              					 'description' 					=> sprintf( esc_html__('Affiliates will receive commissions for every course purchased through %s.', 'uap'), 'LearnPress' ),
              					 'status' 							=> get_option( 'uap-integrations-learnpress', false ) && self::isLearnPressActive() ? 1 : 0,
              					 'extra-details-link' 	=> 'https://ultimateaffiliate.pro/integrations/learnpress/',
              					 'extra-settings-link' 	=> admin_url('admin.php?page=ultimate_affiliates_pro&tab=uap_learnpress'),
              					 'installed' 						=> self::isLearnPressActive() ? 1 : 0,
              			),

 			 );
       $systems = apply_filters( 'uap_filter_integrations_systems', $systems );
       return $systems;
    }

    /**
      * @param none
      * @return bool
      */
    public static function woocsActive()
    {
        if ( ! class_exists( '\WC_Subscriptions' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return int
      */
    public static function isWooActive()
    {
        $option = get_option( 'uap-integrations-woo', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isUmpActive()
    {
        $option = get_option( 'uap-integrations-ump', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isUlpActive()
    {
        $option = get_option( 'uap-integrations-ulp', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isEddActive()
    {
        $option = get_option( 'uap-integrations-edd', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isNinjaActive()
    {
        $option = get_option( 'uap-integrations-ninjaforms', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isCf7Active()
    {
        $option = get_option( 'uap-integrations-cf7', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function isWpFormsActive()
    {
        $option = get_option( 'uap-integrations-wpforms', 1 );
        if ( $option === null || $option === '' || $option === false ){
            return 1;
        }
        return (int)$option;
    }

    /**
      * @param none
      * @return int
      */
    public static function ninjaAddonActive()
    {
        if ( !function_exists( 'is_plugin_active' ) ){
      			include_once ABSPATH . 'wp-admin/includes/plugin.php';
      	}
      	if ( is_plugin_active( 'uap-ninja-forms-tracking/uap-ninja-forms-tracking.php' ) ){
      			return true;
      	}
      	return false;
    }

    /**
      * @param none
      * @return int
      */
    public static function cf7AddonActive()
    {
        if ( !function_exists( 'is_plugin_active' ) ){
            include_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        if ( is_plugin_active( 'uap-cf7-tracking/uap-cf7-tracking.php' ) ){
            return true;
        }
        return false;
    }

    /**
      * @param none
      * @return int
      */
    public static function wpformsAddonActive()
    {
        if ( !function_exists( 'is_plugin_active' ) ){
            include_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        if ( is_plugin_active( 'uap-wp-forms/uap-wp-forms.php' ) ){
            return true;
        }
        return false;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isFfActive()
    {
        if ( ! function_exists( 'frm_forms_autoloader' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isGfActive()
    {
        if ( ! class_exists( '\GFAPI' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isMemberMouseActive()
    {
        if ( ! class_exists( '\MemberMouse' ) ) {
            return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isMemberpressActive()
    {
        if ( ! defined( 'MEPR_VERSION' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isPmProActive()
    {
        if ( ! defined( 'PMPRO_VERSION' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isPmsActive()
    {
        if ( ! class_exists( '\Paid_Member_Subscriptions' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isRcpActive()
    {
        if ( ! defined( 'RCP_PLUGIN_FILE' ) ) {
          return false;
        }
        return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isTutorLmsActive()
    {
        if ( ! defined( 'TUTOR_VERSION' ) ) {
          return false;
        }
        return true;
    }

    public static function isFluentFormsActive() {
      if ( ! defined( 'FLUENTFORM' ) ) {
        return false;
      }
      return true;
    }

    /**
      * @param none
      * @return bool
      */
    public static function isLifterLmsActive()
    {
      if ( ! class_exists( '\LifterLMS' ) ) {
        return false;
      }
      return true;
    }

    public static function isLearnDashActive() {
      if ( ! class_exists( '\SFWD_LMS' ) ) {
          return false;
      }
      return true;
    }

    /**
			* @param none
			* @return bool
			*/
		public static function isLearnPressActive()
		{
				if ( ! class_exists( '\LearnPress' ) ) {
				    return false;
				}
				return true;
		}

    /**
      * @param string
      * @param string
      * @return void
      */
    public static function update( $type='', $value='' )
    {
        update_option( 'uap-integrations-' . $type, $value );
    }

}
