<?php
/**
 * Terms & Conditions Per Product
 *
 * Set custom Terms and Conditions per WooCommerce product.
 *
 * @link              https://tacpp-pro.com
 * @since             1.0.1
 * @package           terms-per-product
 *
 * @wordpress-plugin
 * Plugin Name:       Terms and Conditions Per Product
 * Plugin URI:        https://tacpp-pro.com
 * Description:       Set custom Terms and Conditions per WooCommerce product, category or tag and display them on the checkout page.
 * Version:           1.3.1
 * Author:            Terms Per Product
 * Author URI:        https://tacpp-pro.com
 * Requires Plugins:  woocommerce
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       terms-and-conditions-per-product
 * Domain Path:       /languages
 * WC requires at least: 6.0
 * WC tested up to:     10.7
 */

/**
 * Main file, contains the plugin metadata and activation processes
 *
 * @package    terms-per-product
 */
if ( ! defined( 'TACPP4_PLUGIN_VERSION' ) ) {
    /**
     * The version of the plugin.
     */
    define( 'TACPP4_PLUGIN_VERSION', '1.3.1' );
}

if ( ! defined( 'TACPP4_PLUGIN_PATH' ) ) {
    /**
     *  The server file system path to the plugin directory.
     */
    define( 'TACPP4_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}

if ( ! defined( 'TACPP4_PLUGIN_URL' ) ) {
    /**
     * The url to the plugin directory.
     */
    define( 'TACPP4_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

if ( ! defined( 'TACPP4_PLUGIN_BASE_NAME' ) ) {
    /**
     * The url to the plugin directory.
     */
    define( 'TACPP4_PLUGIN_BASE_NAME', plugin_basename( __FILE__ ) );
}

if ( ! defined( 'TACPP4_PLUGIN_FILE' ) ) {
    /**
     * The url to the plugin directory.
     */
    define( 'TACPP4_PLUGIN_FILE', __FILE__ );
}


if ( ! defined( 'TACPP4_PLUGIN_PRO_BUY_URL' ) ) {
    /**
     * The url to the plugin directory.
     */
    define( 'TACPP4_PLUGIN_PRO_BUY_URL',
        get_admin_url( '', 'admin.php?page=tacpp-pricing' ) );
}

if ( ! defined( 'TACPP4_ACCEPT_LOG_TABLE_NAME' ) ) {
    /**
     * The url to the plugin directory.
     */
    define( 'TACPP4_ACCEPT_LOG_TABLE_NAME', 'tacpp_accept_log' );
}


if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( function_exists( 'tacppp_fs' ) ) {
    tacppp_fs()->set_basename( true, __FILE__ );
} else {
    // DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE `function_exists` CALL ABOVE TO PROPERLY WORK.
    if ( ! function_exists( 'tacppp_fs' ) ) {
        // Create a helper function for easy SDK access.
        function tacppp_fs() {
            global $tacppp_fs;

            if ( ! isset( $tacppp_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/freemius/start.php';

                $tacppp_fs = fs_dynamic_init( array(
                    'id'                  => '11429',
                    'slug'                => 'terms-and-conditions-per-product',
                    'premium_slug'        => 'terms-and-conditions-per-product-pro',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_b45440428f27613181b9ab830fa1b',
                    'is_premium'          => false,
                    'premium_suffix'      => 'Premium',
                    // If your plugin is a serviceware, set this option to false.
                    'has_premium_version' => true,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'menu'                => array(
                        'slug'       => 'tacpp',
                        'first-path' => 'admin.php?page=tacpp',
                        'contact'    => false,
                    ),
                ) );
            }

            return $tacppp_fs;
        }

        // Init Freemius.
        tacppp_fs();
        // Signal that SDK was initiated.
        do_action( 'tacppp_fs_loaded' );

        function tacppp_fs_settings_url() {
            return admin_url( 'admin.php?page=tacpp' );
        }

        tacppp_fs()->add_filter( 'connect_url', 'tacppp_fs_settings_url' );
        tacppp_fs()->add_filter( 'after_skip_url', 'tacppp_fs_settings_url' );
        tacppp_fs()->add_filter( 'after_connect_url', 'tacppp_fs_settings_url' );
        tacppp_fs()->add_filter( 'after_pending_connect_url', 'tacppp_fs_settings_url' );

        /**
         * Plugin uninstall cleanup.
         *
         * Hooked to Freemius `after_uninstall` instead of using uninstall.php so
         * that Freemius can report the uninstall event (including user feedback)
         * before the cleanup runs. Drops the acceptance-log table and removes
         * plugin options/transients/usermeta.
         */
        function tacpp_after_uninstall_cleanup() {
            global $wpdb;

            // Drop the acceptance-log table.
            $table_name = $wpdb->prefix . TACPP4_ACCEPT_LOG_TABLE_NAME;
            $wpdb->query( "DROP TABLE IF EXISTS `{$table_name}`" );

            // Remove plugin options.
            delete_option( 'tacpp_admin_settings' );

            // Remove transient cache used by the rate-me notice.
            delete_transient( 'tacpp_custom_terms_count' );

            // Remove per-user dismissed-notice tracking.
            delete_metadata( 'user', 0, 'tacpp_seen_notices', '', true );
        }

        tacppp_fs()->add_action( 'after_uninstall', 'tacpp_after_uninstall_cleanup' );
    }

    /**
     * Include files.
     */
    function tacpp_include_extra_plugin_files() {

        // Include Class files
        $files = array(
            'app/main/class-tacpp4-terms-conditions-per-product',
            'app/main/class-tacpp4-terms-conditions-variations',
            'app/main/class-tacpp4-terms-conditions-accept-log',
            'app/main/class-tacpp4-terms-conditions-modal',
            'app/main/class-tacpp4-terms-conditions-settings',
            'app/main/class-tacpp4-terms-conditions-per-categories',
            'app/main/class-tacpp4-terms-conditions-admin-notices',
            'app/main/class-tacpp4-terms-and-conditions-checkout-block',
        );


        // Include Includes files
        $includes = array();

        // Merge the two arrays
        $files = array_merge( $files, $includes );

        foreach ( $files as $file ) {
            // Include functions file.
            require TACPP4_PLUGIN_PATH . $file . '.php';
        }
    }

    add_action( 'plugins_loaded', 'tacpp_include_extra_plugin_files' );

    /**
     * Load Terms and Conditions per product textdomain.
     */
    function tacpp_language_textdomain_init() {
        // Localization
        load_plugin_textdomain( 'terms-and-conditions-per-product', false,
            dirname( plugin_basename( __FILE__ ) ) . "/languages" );
    }

    // Add actions
    add_action( 'init', 'tacpp_language_textdomain_init' );

    add_filter( 'plugin_row_meta', 'tacpp_plugin_row_meta', 10, 2 );
    function tacpp_plugin_row_meta( $links, $file ) {
        if ( plugin_basename( __FILE__ ) === $file ) {
            $row_meta = array(
                '<a href="https://tacpp-pro.com/documentation/" target="_blank">' . __( 'Docs', 'terms-and-conditions-per-product' ) . '</a>',
                '<a href="https://tacpp-pro.com/support/" target="_blank">' . __( 'Support', 'terms-and-conditions-per-product' ) . '</a>',
                '<a href="https://tacpp-pro.com/changelog/" target="_blank">' . __( 'Changelog', 'terms-and-conditions-per-product' ) . '</a>',
            );

            return array_merge( $links, $row_meta );
        }

        return (array) $links;
    }

    /**
     * Seed defaults for the plugin's option on activation. Anything already
     * stored wins, so this only matters on fresh installs and never overwrites
     * existing user choices on reactivation.
     */
    function tacpp_plugin_activate() {
        if ( ! current_user_can( 'activate_plugins' ) ) {
            return;
        }

        $defaults = array(
            'terms_on_product'   => 0,
            'terms_must_read'    => 0,
            'terms_modal'        => 0,
            'wc_terms_modal'     => 0,
            'log_acceptance'     => 0,
            'hide_default_terms' => 0,
        );

        $existing = get_option( 'tacpp_admin_settings', array() );
        if ( ! is_array( $existing ) ) {
            $existing = array();
        }

        update_option( 'tacpp_admin_settings', array_merge( $defaults, $existing ) );
    }

    register_activation_hook( __FILE__, 'tacpp_plugin_activate' );

    // INCLUDES - Need to run First
    include( TACPP4_PLUGIN_PATH . 'app/main/class-db-management.php' );
}

/**
 * WC Checkout blocks incompatibility
 */
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    }
} );
