<?php
/**
 * Freemius bootstrap for the free plugin.
 *
 * Its own file, in the global namespace, for two reasons that pull the same
 * way. PHP will not accept a `namespace` statement after other code, so the
 * main plugin file cannot open with global functions and then declare its
 * namespace — and `fs_dynamic_init()` and the helper Freemius' documentation
 * tells you to call are global functions anyway, so a namespaced copy would
 * quietly not be the thing their docs describe.
 *
 * Required as the first statement of the main plugin file. The SDK hooks its
 * own uninstall and update machinery as it initialises, and anything running
 * ahead of it can end up registered against a plugin Freemius has not finished
 * describing yet.
 *
 * Free and Pro are **one Freemius product**, not two. This file declares the
 * free half; `menuverse-pro` declares the premium half against the same id and
 * reuses this instance whenever it can. That is what `parallel_activation`
 * means: both plugins are active at once, one licence covers them, and Freemius
 * is told which folder holds the premium build so it stops trying to replace
 * this one with it.
 *
 * @package MenuVerse
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'menuverse_fs' ) ) {
	/**
	 * Returns the Freemius instance for MenuVerse.
	 *
	 * @return \Freemius|null Null when the SDK is not installed.
	 */
	function menuverse_fs() {
		global $menuverse_fs;

		if ( ! isset( $menuverse_fs ) ) {
			$sdk = __DIR__ . '/../vendor/freemius/wordpress-sdk/start.php';

			// A checkout without `composer install` is a broken plugin, not a
			// fatal one. Say so where somebody can act on it rather than white
			// screening a site.
			if ( ! file_exists( $sdk ) ) {
				add_action(
					'admin_notices',
					static function () {
						if ( ! current_user_can( 'activate_plugins' ) ) {
							return;
						}

						echo '<div class="notice notice-error"><p>';
						echo esc_html__( 'MenuVerse: the Freemius SDK is missing. Run composer install in the plugin folder — licensing and updates are switched off until it is there.', 'menuverse' );
						echo '</p></div>';
					}
				);

				return null;
			}

			require_once $sdk;

			try {
				$menuverse_fs = fs_dynamic_init(
					array(
						'id'                  => '38909',
						'slug'                => 'menuverse',
						'premium_slug'        => 'menuverse-pro',
						'type'                => 'plugin',
						'public_key'          => 'pk_9151b63c22ebad30e8a46ca205c65',
						'is_premium'          => false,
						'is_premium_only'     => false,
						'has_paid_plans'      => true,
						'is_live'             => true,
						'is_org_compliant'    => true,
						'parallel_activation' => array(
							'enabled'                  => true,
							'premium_version_basename' => 'menuverse-pro/menuverse-pro.php',
						),
						'menu'                => array(
							'slug'       => 'menuverse',
							'first-path' => 'admin.php?page=menuverse',
							'support'    => false,
							'contact'    => false,
							'pricing'    => true,
						),
					)
				);
			} catch ( Exception $e ) {
				// Licensing is not worth taking a site down for. The dashboard
				// and every menu keep working without it; only the upgrade and
				// account screens go.
				$menuverse_fs = null;
			}

			do_action( 'menuverse_fs_loaded' );
		}

		return $menuverse_fs;
	}

	menuverse_fs();
}
