<?php

/**
 * Magic Link
 *
 * Secure one click passwordless login
 *
 * @link            http://wordpress.org/plugins/magic-link
 * @author          KaizenCoders <hello@kaizencoders.com>
 * @license         GPL-2.0+
 * @package         MagicLink
 * @copyright       2026 KaizenCoders
 *
 * @wordpress-plugin
 * Plugin Name:       Magic Link
 * Plugin URI:        https://kaizencoders.com/magic-link
 * Description:       Secure one click passwordless login
 * Version:           1.2.3
 * Author:            KaizenCoders
 * Author URI:        https://kaizencoders.com
 * Tested up to:      6.9
 * Requires PHP:      5.6
 * Text Domain:       magic-link
 * License:           GPL-3.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Domain Path:       /languages
 *
 * @fs_premium_only /pro/
 * @fs_ignore       /vendor/, /lite/dist/styles/app.css, /lite/scripts/app.js
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! defined( 'KC_ML_PLUGIN_VERSION' ) ) {
	define( 'KC_ML_PLUGIN_VERSION', '1.2.3' );
}

if ( function_exists( 'magic_link_fs' ) ) {
	magic_link_fs()->set_basename( true, __FILE__ );
} else {
	// Create a helper function for easy SDK access.
	function magic_link_fs() {
		global $magic_link_fs;

		if ( ! isset( $magic_link_fs ) ) {
			// Include Freemius SDK.
			require_once dirname( __FILE__ ) . '/libs/fs/start.php';

			$magic_link_fs = fs_dynamic_init( [
				'id'                  => '16733',
				'slug'                => 'magic-link',
				'type'                => 'plugin',
				'public_key'          => 'pk_2df226c8b5135d3f92b1d1ffa6a7d',
				'is_premium'          => false,
				'has_premium_version' => true,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'menu'                => [
					'slug'        => 'magic-link',
					'first-path'  => 'admin.php?page=magic-link',
					'account'     => true,
					'contact'     => true,
					'support'     => true,
					'affiliation' => false,
				],
			] );
		}

		return $magic_link_fs;
	}

	// Init Freemius.
	magic_link_fs();

	// Use custom icon for onboarding.
	magic_link_fs()->add_filter( 'plugin_icon', function () {
		return dirname( __FILE__ ) . '/assets/images/plugin-icon.png';
	} );


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


	if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
		require_once __DIR__ . '/vendor/autoload.php';
	}

	if ( ! function_exists( 'kc_ml_fail_php_version_notice' ) ) {
		/**
		 * Admin notice for minimum PHP version.
		 *
		 * Warning when the site doesn't have the minimum required PHP version.
		 *
		 * @return void
		 * @since 1.0.0
		 *
		 */
		function kc_ml_fail_php_version_notice() {
			/* translators: %s: PHP version */
			$message      = sprintf( esc_html__(
				'Magic Link requires PHP version %s+, plugin is currently NOT RUNNING.',
				'magic-link'
			), '5.6' );
			$html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) );
			echo wp_kses_post( $html_message );
		}
	}

	if ( ! version_compare( PHP_VERSION, '5.6', '>=' ) ) {
		add_action( 'admin_notices', 'kc_ml_fail_php_version_notice' );

		return;
	}


	// Plugin Folder Path.
	if ( ! defined( 'KC_ML_PLUGIN_DIR' ) ) {
		define( 'KC_ML_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
	}

	if ( ! defined( 'KC_ML_PLUGIN_BASE_NAME' ) ) {
		define( 'KC_ML_PLUGIN_BASE_NAME', plugin_basename( __FILE__ ) );
	}

	if ( ! defined( 'KC_ML_PLUGIN_FILE' ) ) {
		define( 'KC_ML_PLUGIN_FILE', __FILE__ );
	}

	if ( ! defined( 'KC_ML_PLUGIN_URL' ) ) {
		define( 'KC_ML_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
	}

	if ( ! defined( 'KC_ML_PLUGIN_ASSETS_DIR_URL' ) ) {
		define( 'KC_ML_PLUGIN_ASSETS_DIR_URL', KC_ML_PLUGIN_URL . 'lite/dist/assets' );
	}

	$upload_dir = wp_upload_dir( null, false );

	if ( ! defined( 'KC_ML_LOG_DIR' ) ) {
		define( 'KC_ML_LOG_DIR', $upload_dir['basedir'] . '/kaizencoders_uploads/magic-link/logs/' );
	}

	if ( ! defined( 'KC_ML_UPLOADS_DIR' ) ) {
		define( 'KC_ML_UPLOADS_DIR', $upload_dir['basedir'] . '/kaizencoders_uploads/magic-link/uploads/' );
	}

	/**
	 * The code that runs during plugin activation.
	 * This action is documented in lib/Activator.php
	 */
	\register_activation_hook( __FILE__, '\KaizenCoders\MagicLink\Activator::activate' );

	/**
	 * The code that runs during plugin deactivation.
	 * This action is documented in lib/Deactivator.php
	 */
	\register_deactivation_hook( __FILE__, '\KaizenCoders\MagicLink\Deactivator::deactivate' );


	if ( ! function_exists( 'KC_ML' ) ) {
		/**
		 * Initialize.
		 *
		 * @since 1.0.0
		 */
		function KC_ML(): \KaizenCoders\MagicLink\Plugin {
			return \KaizenCoders\MagicLink\Plugin::instance();
		}
	}

	add_action( 'plugins_loaded', function () {
		KC_ML()->run();
	} );
}
