<?php

/**
 * Plugin Name: Fliegerlogin – SSO for Vereinsflieger
 * Tags: sso, vereinsflieger, luftsport
 * Description: Allows users to login into wordpress using their Vereinsflieger login.
 * Version: 3.0.2
 * Requires at least: 6.0
 * Requires PHP: 8.2
 * Author: Simon Kuhn
 * Text Domain: sso-for-vereinsflieger
 * Domain Path: /languages
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 */

/**
 * Copyright (C) 2020-2022 Diginize (https://www.diginize.de)
 * Copyright (C) 2025-2026 Simon Kuhn
 *
 * This is a modified version of the WP Vereinsflieger plugin originally
 * published by Diginize at https://github.com/diginize/wp-vereinsflieger.
 * It has been modified by Simon Kuhn since 2025; see the changelog in
 * readme.txt for the list of changes and their release dates.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


// Direct access protection: nothing here may run outside WordPress.
if (!defined('ABSPATH')) {
	exit;
}

require_once(__DIR__ . '/vendor/autoload.php');

use Fliegerlogin\WpVereinsflieger;

if (!defined('FLIEGERLOGIN_VERSION')) {
	define('FLIEGERLOGIN_VERSION', '3.0.2');
}
if (!defined('FLIEGERLOGIN_BASE_FILE')) {
	define('FLIEGERLOGIN_BASE_FILE', __FILE__);
}

if (!function_exists('fliegerlogin_fs')) {
	/**
	 * Shared Freemius instance.
	 *
	 * The SDK ships in both editions: the free one needs it for the opt-in, the
	 * upgrade prompts and the licence field that pulls in the paid package. The
	 * secret key is deliberately absent - it belongs in the wp-config.php of a
	 * development site and must never enter this repository.
	 *
	 * There is no require of start.php here: freemius/wordpress-sdk declares it
	 * as an autoload "files" entry, so the Composer autoloader above has already
	 * pulled it in - which is exactly how Freemius documents the Composer
	 * installation. start.php returns early when ABSPATH is undefined, so this
	 * costs nothing outside WordPress.
	 */
	function fliegerlogin_fs() {
		global $fliegerlogin_fs;

		if (!isset($fliegerlogin_fs)) {
			$config = [
				'id'               => '36183',
				'public_key'       => 'pk_0077188f0f08ec6b2fe6f6109b569',
				'slug'             => 'sso-for-vereinsflieger',
				'premium_slug'     => 'sso-for-vereinsflieger-premium',
				'type'             => 'plugin',
				// Derived from the tree instead of patched in at build time: the
				// paid safeguards live in src/Premium, and that directory is
				// exactly what the free package leaves out.
				'is_premium'       => is_dir(__DIR__ . '/src/Premium'),
				// Freemius' generated snippet sets this in the paid codebase and
				// their own generator flips it for the free variant. We build both
				// ourselves, so it follows the same signal as is_premium.
				'is_premium_only'  => is_dir(__DIR__ . '/src/Premium'),
				'premium_suffix'   => 'Premium',
				'has_addons'       => false,
				'has_paid_plans'   => true,
				'is_org_compliant' => true,
				'menu'             => [
					// The top level menu slug from Pages\WpVereinsflieger::getSlug(),
					// not the plugin slug - Freemius hangs Account and Contact Us
					// underneath it.
					'slug'    => 'fliegerlogin',
					'account' => true,
					'contact' => true,
					'support' => true,
				],
			];

			// Freemius strips this token from the free edition it generates
			// itself. We build our own, so the token lives with the paid code and
			// is therefore absent from the WordPress.org package by construction
			// rather than by remembering to delete a line.
			$gatekeeper = __DIR__ . '/src/Premium/freemius-gatekeeper.php';
			if (is_file($gatekeeper)) {
				$config['wp_org_gatekeeper'] = require $gatekeeper;
			}

			$fliegerlogin_fs = fs_dynamic_init($config);
		}

		return $fliegerlogin_fs;
	}

	fliegerlogin_fs();
	do_action('fliegerlogin_fs_loaded');
}

register_activation_hook( __FILE__, [WpVereinsflieger::class, 'activate']);
register_deactivation_hook( __FILE__, [WpVereinsflieger::class, 'deactivate']);
register_uninstall_hook(__FILE__, [WpVereinsflieger::class, 'uninstall']);

// Deferred until after Freemius has loaded: the paid safeguards ask the SDK
// whether the licence is valid, and at plugin-file load time it cannot answer yet.
add_action('plugins_loaded', [WpVereinsflieger::class, 'init']);
