<?php

/**
 * Plugin Name:       Form Flight Recorder
 * Plugin URI:        https://buildsbyluke.com/plugins/form-flight-recorder
 * Description:       Reconciles every form submission against the notification it should have produced, and raises the alarm when a form goes silent.
 * Version:           1.0.1
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Author:            Luke W
 * Author URI:        https://buildsbyluke.com
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       form-flight-recorder
 *
 * @package Form_Flight_Recorder
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'ffrd_fs' ) ) {
    // The other build of this plugin (free/premium) is already running. Hand it
    // this file's basename so Freemius can deactivate the redundant one. The
    // `true` marks THIS as the premium copy; the Freemius free-version processor
    // flips it to `false` in the generated wp.org build.
    ffrd_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'ffrd_fs' ) ) {
        /**
         * Freemius SDK access.
         *
         * @return Freemius
         */
        function ffrd_fs() {
            global $ffrd_fs;
            if ( !isset( $ffrd_fs ) ) {
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $ffrd_fs = fs_dynamic_init( array(
                    'id'               => '35815',
                    'slug'             => 'form-flight-recorder',
                    'premium_slug'     => 'form-flight-recorder-premium',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_676c56dcafd68b30c5dfa8b2e6bb8',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'trial'            => array(
                        'days'               => 14,
                        'is_require_payment' => false,
                    ),
                    'menu'             => array(
                        'slug' => 'ffrd',
                    ),
                    'is_live'          => true,
                ) );
            }
            return $ffrd_fs;
        }

        ffrd_fs();
        // Turn off the SDK's sticky trial notice (guideline 11 — the upsell lives
        // only on the plugin's own cockpit page + the SDK "Upgrade" menu item).
        ffrd_fs()->add_filter( 'show_trial', '__return_false' );
        // Turn off the SDK's deactivation-feedback dialog (it posts to
        // api.freemius.com even for users who never opted in).
        ffrd_fs()->add_filter( 'show_deactivation_feedback_form', '__return_false' );
        do_action( 'ffrd_fs_loaded' );
    }
    // ------------------------------------------------------------------
    // Form Flight Recorder's own bootstrap.
    // ------------------------------------------------------------------
    define( 'FFRD_VERSION', '1.0.1' );
    define( 'FFRD_FILE', __FILE__ );
    define( 'FFRD_DIR', plugin_dir_path( __FILE__ ) );
    define( 'FFRD_URL', plugin_dir_url( __FILE__ ) );
    define( 'FFRD_BASENAME', plugin_basename( __FILE__ ) );
    spl_autoload_register( function ( $class ) {
        if ( 0 !== strpos( $class, 'Ffrd_' ) ) {
            return;
        }
        $file = FFRD_DIR . 'includes/class-' . str_replace( '_', '-', strtolower( $class ) ) . '.php';
        if ( is_readable( $file ) ) {
            require $file;
        }
    } );
    register_activation_hook( __FILE__, array('Ffrd_Install', 'activate') );
    register_deactivation_hook( __FILE__, array('Ffrd_Install', 'deactivate') );
    /**
     * UNINSTALL. There is deliberately no uninstall.php — WordPress runs either
     * uninstall.php OR the registered uninstall hook (never both), and a present
     * uninstall.php would deregister the Freemius SDK's uninstall handling (which
     * is why the Freemius deployment processor rejects a plugin that has one).
     */
    ffrd_fs()->add_action( 'after_uninstall', array('Ffrd_Uninstall', 'run') );
    add_action( 'uninstall_' . plugin_basename( __FILE__ ), array('Ffrd_Uninstall', 'safety_net'), 100 );
    /**
     * Container accessor.
     *
     * @return Ffrd_Plugin
     */
    function ffrd() {
        return Ffrd_Plugin::instance();
    }

    /**
     * Boot. Priority 20 so the form plugins we observe (default priority) are
     * fully loaded first. The Pro layer loads before the container (when this is
     * the premium build AND a licence/trial is active) so it can hook
     * `ffrd_loaded`.
     */
    function ffrd_boot() {
        ffrd();
    }

    add_action( 'plugins_loaded', 'ffrd_boot', 20 );
}