<?php

namespace EasyCriticalCSS;

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

function easy_cc_fs() {
	global $easy_cc_fs;

	if ( ! isset( $easy_cc_fs ) ) {
		$easy_cc_fs = fs_dynamic_init(
			[
				'id'                  => '17865',
				'slug'                => 'easy-critical-css',
				'type'                => 'plugin',
				'public_key'          => 'pk_acaf4bf8f8a2893341167663ca4ce',
				'premium_suffix'      => false,
				'has_premium_version' => false,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'is_org_compliant'    => true,
				'menu'                => [
					'slug'    => 'easy-critical-css-settings',
					'support' => false,
					'parent'  => [
						'slug' => 'options-general.php',
					],
				],
			]
		);
	}

	return $easy_cc_fs;
}

// Init Freemius.
easy_cc_fs();

// Opt-out on initial activation. We do this by blocking the opt-in screen for 30 seconds.
// This essentially skips the opt-in, so we have an anonymous user that never "phones home", giving a better user experience.
// Someone can opt-in via the Plugins page if they really want to.
// This isn't done for registered users so their preference isn't overwritten.
if ( ! easy_cc_fs()->is_registered() && get_option( 'easy_cc_activation', false ) ) {
	$easy_cc_initial_time = strtotime( get_gmt_from_date( get_option( 'easy_cc_activation' ) ) );
	$easy_cc_elapsed_time = time() - $easy_cc_initial_time;

	if ( $easy_cc_elapsed_time < 15 ) {
		easy_cc_fs()->skip_connection();
	} else {
		update_option( 'easy_cc_activation', false );
	}
}

// Plugins shouldn't nag on deactivation.
easy_cc_fs()->add_filter( 'show_deactivation_feedback_form', '__return_false' );

// Clear auto_mode_status cache when license changes.
easy_cc_fs()->add_action( 'after_license_change', [ __NAMESPACE__ . '\Timed_Option', 'delete_all' ] );
easy_cc_fs()->add_action( 'after_license_activation', [ __NAMESPACE__ . '\Timed_Option', 'delete_all' ] );
easy_cc_fs()->add_action( 'after_license_deactivation', [ __NAMESPACE__ . '\Timed_Option', 'delete_all' ] );

// Signal that SDK was initiated.
do_action( 'easy_cc_fs_loaded' );
