<?php
/**
 * Freemius SDK integration.
 *
 * @package FailedOrderRescueForWooCommerce
 */

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

class FORD_Freemius {
	/**
	 * Initialize Freemius if credentials are configured.
	 *
	 * @return void
	 */
	public static function init() {
		if ( function_exists( 'ford_fs' ) && ford_fs() ) {
			return;
		}

		if ( empty( FORD_FREEMIUS_ID ) || empty( FORD_FREEMIUS_PUBLIC_KEY ) ) {
			return;
		}

		$sdk_path = FORD_PLUGIN_DIR . 'freemius/start.php';

		if ( ! file_exists( $sdk_path ) ) {
			return;
		}

		require_once $sdk_path;

		if ( ! function_exists( 'fs_dynamic_init' ) ) {
			return;
		}

		$GLOBALS['ford_fs'] = fs_dynamic_init(
			array(
				'id'                  => FORD_FREEMIUS_ID,
				'slug'                => 'failed-order-rescue-for-woocommerce',
				'premium_slug'        => 'failed-order-rescue-for-woocommerce-pro',
				'type'                => 'plugin',
				'public_key'          => FORD_FREEMIUS_PUBLIC_KEY,
				'is_premium'          => true,
				'has_premium_version' => true,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'trial'               => array(
					'days'               => 7,
					'is_require_payment' => true,
				),
				'menu'                => array(
					'slug'    => 'ford-recovery',
					'account' => true,
					'contact' => true,
					'pricing' => true,
				),
			)
		);
	}
}

/**
 * Freemius accessor.
 *
 * @return mixed|null
 */
function ford_fs() {
	return isset( $GLOBALS['ford_fs'] ) ? $GLOBALS['ford_fs'] : null;
}

/**
 * Check whether Pro features may run.
 *
 * @return bool
 */
function ford_is_pro_active() {
	if ( defined( 'FORD_PRO_DEV_UNLOCK' ) && FORD_PRO_DEV_UNLOCK ) {
		return true;
	}

	return function_exists( 'ford_fs' ) && ford_fs() && ford_fs()->can_use_premium_code();
}

/**
 * Get Freemius pricing URL if available.
 *
 * @return string
 */
function ford_get_upgrade_url() {
	if ( function_exists( 'ford_fs' ) && ford_fs() ) {
		return ford_fs()->get_upgrade_url();
	}

	return admin_url( 'admin.php?page=ford-recovery' );
}
