<?php

/**
 * Plugin loader.
 *
 * @package Ordsync
 */
defined( 'ABSPATH' ) || exit;
/**
 * Wires everything together. One instance, loaded on plugins_loaded.
 */
final class Ordsync_Plugin {
    /**
     * Singleton instance.
     *
     * @var Ordsync_Plugin|null
     */
    private static $instance = null;

    /**
     * Get the instance.
     *
     * @return Ordsync_Plugin
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor.
     */
    private function __construct() {
        $this->includes();
        $this->hooks();
    }

    /**
     * Load files.
     *
     * @return void
     */
    private function includes() {
        require_once ORDSYNC_PATH . 'includes/class-ordsync-settings.php';
        require_once ORDSYNC_PATH . 'includes/class-ordsync-logger.php';
        require_once ORDSYNC_PATH . 'includes/class-ordsync-events.php';
        require_once ORDSYNC_PATH . 'includes/class-ordsync-payload.php';
        require_once ORDSYNC_PATH . 'includes/destinations/abstract-ordsync-destination.php';
        require_once ORDSYNC_PATH . 'includes/destinations/class-ordsync-dest-sheets.php';
        require_once ORDSYNC_PATH . 'includes/destinations/class-ordsync-dest-webhook.php';
        require_once ORDSYNC_PATH . 'includes/destinations/class-ordsync-dest-email.php';
        require_once ORDSYNC_PATH . 'includes/class-ordsync-destinations.php';
        require_once ORDSYNC_PATH . 'includes/class-ordsync-engine.php';
        if ( is_admin() ) {
            require_once ORDSYNC_PATH . 'admin/apps-script.php';
            require_once ORDSYNC_PATH . 'admin/class-ordsync-admin.php';
            require_once ORDSYNC_PATH . 'admin/class-ordsync-ajax.php';
        }
        /*
         * PREMIUM ONLY.
         *
         * Freemius's build step physically deletes everything inside an
         * is__premium_only() block when it generates the free ZIP, so the
         * free version on WordPress.org never ships locked code. That is
         * what guideline 5 (no trialware) requires.
         *
         * Uncomment each line as the Pro destination is built.
         */
        /*
         * Two separate gates, and they are not interchangeable.
         *
         * is__premium_only()  -> "this is the Pro build". Controls what
         *                        Freemius physically strips from the free zip.
         * ordsync_is_premium() -> "this site holds a valid licence". Controls
         *                        whether the feature actually runs.
         *
         * Loading on the build check but ACTIVATING on the licence check is
         * the point: anyone holding the Pro zip would otherwise get every
         * paid feature without ever buying one.
         */
        if ( ordsync_fs() ) {
        }
    }

    /**
     * Register hooks.
     *
     * @return void
     */
    private function hooks() {
        /*
         * Only the Pro build loads its own translations.
         *
         * WordPress has loaded them automatically for wordpress.org-hosted
         * plugins since 4.6, and this plugin requires 6.4, so making the call
         * by hand in the free build is dead weight that the plugin review
         * flags. Pro ships through Freemius, which gets no wordpress.org
         * language packs at all -- the bundled .mo is the only German a Pro
         * customer ever sees, so there it has to be loaded explicitly.
         *
         * Freemius deletes this whole block from the free zip, along with the
         * .po/.mo named in @fs_premium_only in the main plugin file. The
         * function name is deliberately not written in this comment: it would
         * survive the strip and still show up in a grep of the free source.
         */
        if ( ordsync_fs() ) {
        }
        new Ordsync_Engine();
        if ( is_admin() ) {
            new Ordsync_Admin();
            new Ordsync_Ajax();
        }
    }

}
