<?php

/**
 * Plugin Name: Tinyplugs Gift Wrapping – Cart & Checkout
 * Plugin URI: https://www.tinyplugs.com/plugins/gift-wrapping-for-woocommerce-cart-checkout/
 * Description: Offer a gift wrapping button under each product in the WooCommerce cart, on the single product page, or one option per order. Flat fee, optional gift message, category targeting, and a fully customizable button — all without reloading the cart.
 * Version: 1.15.0
 * Author: Tinyplugs.com
 * Author URI: https://www.tinyplugs.com
 * Text Domain: tinyplugs-gift-wrapping
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * WC requires at least: 6.0
 * WC tested up to: 10.8
 * Requires Plugins: woocommerce
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 *
 * @package CartGiftWrapping
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'cgwfw_' ) ) {
    // The free version is already loaded. Tell Freemius this is the premium
    // basename so its SDK auto-deactivates the free version, then bail out so
    // the plugin logic is not loaded (and redeclared) a second time.
    cgwfw_()->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( 'cgwfw_' ) ) {
        // Create a helper function for easy SDK access.
        function cgwfw_() {
            global $cgwfw_;
            if ( !isset( $cgwfw_ ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $cgwfw_ = fs_dynamic_init( array(
                    'id'               => '32898',
                    'slug'             => 'tinyplugs-gift-wrapping',
                    'premium_slug'     => 'tinyplugs-gift-wrapping-premium',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_8cf5b402f60f790045e03275ce24e',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'           => 'wc-settings',
                        'first-path'     => 'admin.php?page=wc-settings&tab=cgwfw',
                        'override_exact' => true,
                        'support'        => false,
                        'contact'        => false,
                        'parent'         => array(
                            'slug' => 'woocommerce',
                        ),
                    ),
                    'is_live'          => true,
                ) );
            }
            return $cgwfw_;
        }

        // Init Freemius.
        cgwfw_();
        // Signal that SDK was initiated.
        do_action( 'cgwfw__loaded' );
    }
    // Uninstall cleanup runs via Freemius (instead of WordPress' uninstall.php)
    // so the uninstall event is reported to Freemius before the data is removed.
    require_once dirname( __FILE__ ) . '/includes/cgwfw-uninstall.php';
    cgwfw_()->add_action( 'after_uninstall', 'cgwfw_uninstall_cleanup' );
    // Scope the Freemius opt-in/activation screen to our own WooCommerce
    // settings tab. Adding tab=cgwfw to the activation URL makes the SDK's
    // override_exact url match only on our tab, so it no longer takes over the
    // other wc-settings tabs (General, Products, …) before activation.
    cgwfw_()->add_filter( 'connect_url', function ( $url ) {
        return add_query_arg( 'tab', 'cgwfw', $url );
    } );
    // Use the bundled icon on the Freemius screens (pricing, opt-in, checkout).
    // Without this the SDK only fetches the icon from Freemius on localhost
    // installs, so customers' sites would show the generic plug icon.
    cgwfw_()->add_filter( 'plugin_icon', function () {
        return dirname( __FILE__ ) . '/assets/img/icon-256.png';
    } );
    define( 'CGWFW_VERSION', '1.15.0' );
    define( 'CGWFW_FILE', __FILE__ );
    define( 'CGWFW_PATH', plugin_dir_path( __FILE__ ) );
    define( 'CGWFW_URL', plugin_dir_url( __FILE__ ) );
    define( 'CGWFW_BASENAME', plugin_basename( __FILE__ ) );
    /**
     * Declare compatibility with WooCommerce HPOS (High-Performance Order Storage).
     */
    add_action( 'before_woocommerce_init', function () {
        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', CGWFW_FILE, true );
        }
    } );
    /**
     * Bootstrap the plugin once all plugins are loaded, so we can verify WooCommerce.
     */
    add_action( 'plugins_loaded', function () {
        if ( !class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p>';
                echo esc_html__( 'Tinyplugs Gift Wrapping requires WooCommerce to be installed and active.', 'tinyplugs-gift-wrapping' );
                echo '</p></div>';
            } );
            return;
        }
        require_once CGWFW_PATH . 'includes/class-cgwfw-settings.php';
        require_once CGWFW_PATH . 'includes/class-cgwfw-cart.php';
        require_once CGWFW_PATH . 'includes/class-cgwfw-compat.php';
        require_once CGWFW_PATH . 'includes/class-cgwfw-plugin.php';
        CGWFW_Plugin::instance();
    } );
}