<?php

require_once CHINA_PAYMENTS_BASE_PATH . '/autoload.php';
require_once CHINA_PAYMENTS_BASE_PATH . '/lib/definitions.php';
require_once CHINA_PAYMENTS_BASE_PATH . '/lib/polyfill.php';

require_once CHINA_PAYMENTS_BASE_PATH . '/app/_functions/administration.php';
require_once CHINA_PAYMENTS_BASE_PATH . '/app/_functions/general.php';
require_once CHINA_PAYMENTS_BASE_PATH . '/app/_functions/utilities.php';

ChinaPayments\Migration::instance()->setup();

ChinaPayments\ThirdPartyIntegration\Freemius::instance();

if ( ! function_exists( 'china_payments_fs' ) ) {
	function china_payments_fs() {
		return ChinaPayments\ThirdPartyIntegration\Freemius::instance();
	}
}

ChinaPayments\ThirdPartyIntegration\MemberPress::instance()->setup();
ChinaPayments\ThirdPartyIntegration\PaymentPage::instance()->setup();
ChinaPayments\ThirdPartyIntegration\WooCommerce::instance()->setup();
ChinaPayments\ThirdPartyIntegration\LifterLMS::instance()->setup();

// Checkout Optimizations — defer until plugins_loaded so the Freemius
// premium-license check sees a fully-initialized FS instance regardless of
// plugin load order. Pre-3.5.3 these ran synchronously here, which silently
// disabled the features on sites where FS license state was populated later
// in the boot.
if ( did_action( 'plugins_loaded' ) || doing_action( 'plugins_loaded' ) ) {
	ChinaPayments\Optimizations\MultiCurrency::instance()->setup();
	ChinaPayments\Optimizations\ChineseLocalization::instance()->setup();
} else {
	add_action(
		'plugins_loaded',
		function () {
			ChinaPayments\Optimizations\MultiCurrency::instance()->setup();
			ChinaPayments\Optimizations\ChineseLocalization::instance()->setup();
		},
		99
	);
}

if ( did_action( 'plugins_loaded' ) || doing_action( 'plugins_loaded' ) ) {
	if ( defined( 'SIMPLE_WP_MEMBERSHIP_VER' ) ) {
		ChinaPayments\ThirdPartyIntegration\SimpleMembership::instance()->setup();
	}
} else {
	add_action(
		'plugins_loaded',
		function () {
			if ( defined( 'SIMPLE_WP_MEMBERSHIP_VER' ) ) {
				ChinaPayments\ThirdPartyIntegration\SimpleMembership::instance()->setup();
			}
		},
		99
	);
}

ChinaPayments\ActionScheduler::instance()->setup();
// General Functionality
ChinaPayments\Controller::instance()->setup();

if ( is_admin() ) {
	ChinaPayments\AdminController::instance()->setup();
}

// RestAPI
add_action( 'rest_api_init', array( ChinaPayments\RestAPI::instance(), 'setup' ) );

// Site Health
add_filter( 'site_status_tests', array( ChinaPayments\SiteHealth::instance(), 'tests' ) );
