<?php

/**
 * Freemius bootstrap — licensing, payments and the free/Pro build split.
 *
 * Loaded before anything else so ordsync_fs() exists for the whole request.
 *
 * @package Ordsync
 */
defined( 'ABSPATH' ) || exit;
if ( !function_exists( 'ordsync_fs' ) ) {
    /**
     * The Freemius instance, or null when the SDK is not bundled.
     *
     * Never let a missing SDK take the site down: a shop that cannot reach
     * its licensing provider should still be syncing its orders.
     *
     * @return Freemius|null
     */
    function ordsync_fs() {
        global $ordsync_fs;
        if ( isset( $ordsync_fs ) ) {
            return $ordsync_fs;
        }
        $sdk = ORDSYNC_PATH . 'freemius/start.php';
        if ( !file_exists( $sdk ) ) {
            $ordsync_fs = null;
            return null;
        }
        require_once $sdk;
        $ordsync_fs = fs_dynamic_init( array(
            'id'               => '36736',
            'slug'             => 'ordercarry-order-automation-for-woocommerce',
            'premium_slug'     => 'ordercarry-order-automation-for-woocommerce-premium',
            'type'             => 'plugin',
            'public_key'       => 'pk_85f1ca232e82e8cbf041201843b66',
            'is_premium'       => false,
            'premium_suffix'   => 'Pro',
            'has_addons'       => false,
            'has_paid_plans'   => true,
            'is_org_compliant' => true,
            'anonymous_mode'   => true,
            'has_affiliation'  => false,
            'trial'            => array(
                'days'               => 14,
                'is_require_payment' => false,
            ),
            'menu'             => array(
                'slug'       => 'ordsync',
                'parent'     => array(
                    'slug' => 'woocommerce',
                ),
                'first-path' => 'admin.php?page=ordsync',
            ),
            'is_live'          => true,
        ) );
        return $ordsync_fs;
    }

}
if ( !function_exists( 'ordsync_is_premium' ) ) {
    /**
     * Is a paid licence active right now?
     *
     * Note this asks about the LICENCE, not the build. is__premium_only()
     * only says "this is the Pro zip", which is true for anyone who obtained
     * that zip by any means. Gate features on this instead, or the Pro build
     * hands its features out for free.
     *
     * Always answers false when the SDK is absent, so the gate fails closed.
     *
     * can_use_premium_code() is used deliberately without the __premium_only
     * suffix: this helper has to survive in the free build too, and Freemius
     * strips suffixed calls when it generates that build.
     *
     * @return bool
     */
    function ordsync_is_premium() {
        $fs = ordsync_fs();
        if ( !$fs ) {
            return false;
        }
        return (bool) $fs->can_use_premium_code();
    }

}
if ( !function_exists( 'ordsync_uninstall' ) ) {
    /**
     * Delete our options when the plugin is deleted.
     *
     * This used to be a plain uninstall.php in the plugin root, which
     * Freemius rejects at upload — and it was right to. WordPress honours
     * exactly one uninstall handler per plugin and uninstall.php outranks
     * any registered hook, so that file was quietly suppressing Freemius's
     * own cleanup: the licence would never have been released on delete.
     *
     * Running from Freemius's hook also fixes a second bug the standalone
     * file had. Freemius stops before firing this when the other build of
     * the plugin is still active, so deleting the free copy after upgrading
     * to Pro no longer wipes the settings the Pro copy is using.
     *
     * Order meta (_ordsync_done_*, _ordsync_error_*) is deliberately left
     * alone: it is part of the order's history, and deleting it would make
     * every past order look unsent to a later install.
     *
     * @return void
     */
    function ordsync_uninstall() {
        delete_option( 'ordsync_settings' );
        delete_option( 'ordsync_secret' );
        delete_option( 'ordsync_webhook_secret' );
        delete_option( 'ordsync_status' );
        delete_option( 'ordsync_events' );
    }

}
/*
 * Registered at load time, not on activation: WordPress includes this file
 * again during uninstall, and the hook has to exist by the time Freemius
 * fires it moments later.
 *
 * Without the SDK there is nothing to hook, so a build with freemius/ missing
 * leaves the four options behind. That is the whole cost of failing safe here,
 * and it is not one a user can see.
 */
if ( ordsync_fs() ) {
    ordsync_fs()->add_action( 'after_uninstall', 'ordsync_uninstall' );
}