<?php

/**
 * Plugin Name:       Upgrade Pilot
 * Plugin URI:        https://buildsbyluke.com/plugins/upgrade-pilot
 * Description:       Know when a plugin changes hands, is abandoned, or is closed, and freeze its updates. Plus a readiness report and PHP compatibility scan for your next WordPress or PHP upgrade. It never updates, installs, or deactivates anything.
 * Version:           1.0.10
 * 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:       upgrade-pilot
 * Domain Path:       /languages
 *
 * @package Upgrade_Pilot
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'upilot_fs' ) ) {
    // The other build of this plugin is already running. Hand it this file's
    // basename so Freemius can deactivate the redundant one, and stop here:
    // nothing below must run twice.
    upilot_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( !function_exists( 'upilot_fs' ) ) {
        // Create a helper function for easy SDK access.
        function upilot_fs() {
            global $upilot_fs;
            if ( !isset( $upilot_fs ) ) {
                // Activate multisite network integration.
                if ( !defined( 'WP_FS__PRODUCT_35807_MULTISITE' ) ) {
                    define( 'WP_FS__PRODUCT_35807_MULTISITE', true );
                }
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $upilot_fs = fs_dynamic_init( array(
                    'id'               => '35807',
                    'slug'             => 'upgrade-pilot',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_98b735e6de0167cfe779bf7c8188a',
                    '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' => 'upgrade-pilot',
                    ),
                    'is_live'          => true,
                ) );
            }
            return $upilot_fs;
        }

        // Init Freemius.
        upilot_fs();
        /**
         * Turn the SDK's trial-promotion notice off.
         *
         * Left on, the SDK raises a sticky notice on EVERY admin screen 24 hours
         * after activation ("Test all our awesome premium features with a 14-day
         * free trial"), and raises it again every 30 days thereafter
         * (class-freemius.php, _add_trial_notice()). It is dismissible, so it is
         * arguably within guideline 11 — but it is still a site-wide upsell on
         * screens that have nothing to do with this plugin, and guideline 11 asks
         * for upgrade prompts to be "limited in scope and used sparingly, be that
         * contextually or only on the plugin's setting page".
         *
         * This plugin's upsell therefore lives in exactly one place: the
         * "Reports & Automation" screen, which a person only sees if they click it.
         *
         * `show_trial` is the SDK's own documented opt-out (start.php lists the
         * filter as fs_show_trial_{slug}; it is read at class-freemius.php,
         * `if ( ! $this->apply_filters( 'show_trial', true ) )`).
         */
        upilot_fs()->add_filter( 'show_trial', '__return_false' );
        /**
         * Turn the SDK's deactivation-feedback dialog off.
         *
         * Left on, deactivating the plugin opens a "quick feedback" modal asking
         * why, and a submitted reason is posted to api.freemius.com — including
         * by users who never opted in. The readme promises Freemius is contacted
         * only in the four click-through situations it lists, and this dialog
         * would be a fifth. Deactivation should be one click, not a survey.
         *
         * `show_deactivation_feedback_form` is the SDK's own documented opt-out
         * (class-freemius.php: "Developers can optionally hide the deactivation
         * feedback form using the 'show_deactivation_feedback_form' filter").
         */
        upilot_fs()->add_filter( 'show_deactivation_feedback_form', '__return_false' );
        // Signal that SDK was initiated.
        do_action( 'upilot_fs_loaded' );
    }
    // ------------------------------------------------------------------
    // Upgrade Pilot's own bootstrap.
    // ------------------------------------------------------------------
    define( 'UPILOT_VERSION', '1.0.10' );
    // Bumped to 2 in this release: adds the upilot_site_status table, which the
    // network rollup reads. maybe_upgrade() runs dbDelta when this changes, so an
    // existing install gains the table without a reactivation.
    define( 'UPILOT_DB_VERSION', '2' );
    define( 'UPILOT_FILE', __FILE__ );
    define( 'UPILOT_DIR', plugin_dir_path( __FILE__ ) );
    define( 'UPILOT_URL', plugin_dir_url( __FILE__ ) );
    define( 'UPILOT_BASENAME', plugin_basename( __FILE__ ) );
    /**
     * Autoloader for UPilot_* classes.
     *
     * Class UPilot_Foo_Bar => includes/class-upilot-foo-bar.php
     */
    spl_autoload_register( function ( $class ) {
        if ( 0 !== strpos( $class, 'UPilot_' ) ) {
            return;
        }
        $file = UPILOT_DIR . 'includes/class-' . str_replace( '_', '-', strtolower( $class ) ) . '.php';
        if ( is_readable( $file ) ) {
            require $file;
        }
    } );
    /**
     * Activation.
     *
     * $network_wide is passed by WordPress and is true when a super admin used
     * "Network Activate". It matters: on a network, every subsite must end up with
     * a status row, not just the one that happened to be current.
     *
     * @param bool $network_wide Whether this was a network activation.
     */
    function upilot_activate(  $network_wide = false  ) {
        UPilot_Install::activate( (bool) $network_wide );
    }

    /**
     * Deactivation.
     *
     * @param bool $network_wide Whether this was a network deactivation.
     */
    function upilot_deactivate(  $network_wide = false  ) {
        UPilot_Install::deactivate( (bool) $network_wide );
    }

    register_activation_hook( __FILE__, 'upilot_activate' );
    register_deactivation_hook( __FILE__, 'upilot_deactivate' );
    /**
     * UNINSTALL.
     *
     * There is deliberately no uninstall.php. WordPress runs EITHER uninstall.php OR
     * the registered uninstall hook, never both — and when uninstall.php exists, core
     * actively DELETES the registered hook from the `uninstall_plugins` option before
     * running it (wp-admin/includes/plugin.php, uninstall_plugin()). Shipping one
     * would therefore silently disable the SDK's uninstall handling, which is exactly
     * why Freemius refuses a plugin that has one.
     *
     * So cleanup hangs off Freemius's `after_uninstall`. That action fires for
     * ANONYMOUS users too — the ones who skipped the opt-in, i.e. most WordPress.org
     * users — because in class-freemius.php the registration check guards only the
     * server call (:9894), while do_action( 'after_uninstall' ) sits outside it
     * (:9897). It does NOT fire if the SDK instance cannot be resolved (:9873).
     *
     * The second registration below closes that hole. WordPress include_once's this
     * very file before firing the uninstall hook, so we can put our own callback on
     * the same core action, at a late priority, with no dependency on Freemius being
     * healthy. It no-ops if the SDK path already cleaned up.
     */
    upilot_fs()->add_action( 'after_uninstall', array('UPilot_Uninstall', 'run') );
    add_action( 'uninstall_' . plugin_basename( __FILE__ ), array('UPilot_Uninstall', 'safety_net'), 100 );
    // Subsite lifecycle: a network that gains or loses a site must stay consistent
    // without anyone reactivating anything.
    UPilot_Install::hooks();
    /**
     * Boot the plugin.
     */
    function upilot() {
        return UPilot_Plugin::instance();
    }

    add_action( 'plugins_loaded', 'upilot' );
    add_action( 'upilot_loaded', function ( $plugin ) {
    } );
}