<?php
/**
 * Freemius SDK bootstrap for the WordPress.org free build.
 *
 * @package CF7MasterPro\License
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'cf7mp_fs' ) ) {

	/**
	 * Get Freemius instance.
	 *
	 * @return \Freemius|null
	 */
	function cf7mp_fs() {
		global $cf7mp_fs;

		if ( isset( $cf7mp_fs ) ) {
			return $cf7mp_fs;
		}

		if ( ! file_exists( CF7MP_PLUGIN_DIR . 'vendor/freemius/start.php' ) ) {
			$cf7mp_fs = null;
			return $cf7mp_fs;
		}

		require_once CF7MP_PLUGIN_DIR . 'vendor/freemius/start.php';

		$cf7mp_fs = fs_dynamic_init(
			array(
				'id'                  => '31125',
				'slug'                => 'frontierline-forms',
				'type'                => 'plugin',
				'public_key'          => 'pk_2f17aa0cbd2f9f7dff8e2e8e0d209',
				'is_premium'          => false,
				'premium_suffix'      => 'Pro',
				'has_premium_version' => true,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'is_org_compliant'    => true,
				'menu'                => array(
					'slug'    => 'frontierline-forms',
					'support' => false,
				),
			)
		);

		return $cf7mp_fs;
	}

	cf7mp_fs();
	do_action( 'cf7mp_fs_loaded' );

	if ( cf7mp_fs() ) {
		cf7mp_fs()->add_action( 'after_uninstall', 'cf7mp_fs_uninstall' );
	}
}

if ( ! function_exists( 'cf7mp_fs_uninstall' ) ) {
	/**
	 * Cleanup plugin data when delete-on-uninstall is enabled.
	 */
	function cf7mp_fs_uninstall() {
		$settings = get_option( 'cf7mp_settings' );
		if ( empty( $settings['delete_on_uninstall'] ) ) {
			return;
		}
		global $wpdb;
		$prefix = $wpdb->prefix . 'cf7mp_';
		foreach ( array( 'submissions', 'form_settings' ) as $t ) {
			$wpdb->query( "DROP TABLE IF EXISTS {$prefix}{$t}" ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Uninstall cleanup; table names from trusted prefix and fixed list.
		}
		delete_option( 'cf7mp_settings' );
		delete_option( 'cf7mp_db_version' );
	}
}
