<?php
/**
 * Plugin Name: WholesaleNode for WooCommerce
 * Plugin URI: https://demo2.wholesalenode.com/
 * Version: 1.0.2
 * Description: Essential B2B features for WooCommerce — wholesale role, registration approval, hide price for guests, product visibility, basic discount rules, product lists, and a simple quote request form. Upgrade to Pro for advanced rules, company accounts, purchase orders, and more.
 * Author: WholesaleNode for WooCommerce
 * Author URI: https://wholesalenode.com/
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 6.4
 * Tested up to: 7.0
 * Requires PHP: 7.4
 * Text Domain: wholesalenode-for-woocommerce
 * Domain Path: /languages
 * WC requires at least: 8.0
 * WC tested up to: 10.9.4
 * Requires Plugins: woocommerce
 *
 * @package WholesaleNode_For_WooCommerce
 */

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

define( 'WOOWSN_FREE_VERSION',     '1.0.0' );
define( 'WOOWSN_FREE_FILE',        __FILE__ );
define( 'WOOWSN_FREE_DIR',         plugin_dir_path( __FILE__ ) );
define( 'WOOWSN_FREE_URL',         plugin_dir_url( __FILE__ ) );
define( 'WOOWSN_FREE_PLUGIN_NAME', 'WholesaleNode for WooCommerce' );

add_action( 'plugins_loaded', 'woowsn_free_check_woocommerce', 1 );

/**
 * Deactivate and show notice when WooCommerce is missing.
 */
function woowsn_free_check_woocommerce() {
	if ( class_exists( 'WooCommerce' ) ) {
		return;
	}
	add_action( 'admin_notices', function () {
		echo '<div class="notice notice-error"><p>';
		echo wp_kses(
			'<strong>WholesaleNode for WooCommerce</strong> ' . esc_html__( 'requires WooCommerce to be installed and active.', 'wholesalenode-for-woocommerce' ),
			array( 'strong' => array() )
		);
		echo '</p></div>';
	} );
	deactivate_plugins( plugin_basename( __FILE__ ) );
}

add_action( 'before_woocommerce_init', 'woowsn_free_declare_compatibility' );

/**
 * Declare HPOS and Cart/Checkout Blocks compatibility.
 */
function woowsn_free_declare_compatibility() {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
	}
}

/**
 * Check whether a free feature is enabled.
 *
 * @param string $feature Feature key.
 * @return bool
 */
function woowsn_free_is_feature_enabled( $feature ) {
	$features = get_option( 'woowsn_free_features', array() );
	$defaults = array(
		'registration'       => true,
		'hide_price'         => true,
		'product_visibility' => true,
		'discount_rules'     => true,
		'product_lists'      => true,
		'quotes'             => true,
	);
	if ( isset( $features[ $feature ] ) ) {
		return (bool) $features[ $feature ];
	}
	return isset( $defaults[ $feature ] ) ? (bool) $defaults[ $feature ] : true;
}

register_activation_hook( __FILE__, 'woowsn_free_activate' );

/**
 * Plugin activation.
 */
function woowsn_free_activate() {
	require_once WOOWSN_FREE_DIR . 'includes/class-woowsnfree-activation.php';
	WOOWSNFree_Activation::activate();
}

register_deactivation_hook( __FILE__, 'woowsn_free_deactivate' );

/**
 * Plugin deactivation.
 */
function woowsn_free_deactivate() {
	flush_rewrite_rules();
}

add_action( 'plugins_loaded', 'woowsn_free_init', 10 );

/**
 * Initialise all free modules.
 */
function woowsn_free_init() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	$stored = get_option( 'woowsn_free_version', '0' );
	if ( version_compare( $stored, WOOWSN_FREE_VERSION, '<' ) ) {
		require_once WOOWSN_FREE_DIR . 'includes/class-woowsnfree-activation.php';
		WOOWSNFree_Activation::activate();
		update_option( 'woowsn_free_version', WOOWSN_FREE_VERSION );
	}

	if ( ! get_option( 'woowsn_quotes_settings' ) ) {
		update_option( 'woowsn_quotes_settings', array(
			'displayMode'           => 'popup',
			'displaymode'           => 'popup',
			'displayPosition'       => 'after_add_to_cart_button',
			'displayposition'       => 'after_add_to_cart_button',
			'popupButtonText'       => 'Request a Quote',
			'popupbuttontext'       => 'Request a Quote',
			'popupButtonClass'      => '',
			'enableOnSingleProduct' => true,
			'enableonsingleproduct' => true,
		) );
	}

	require_once WOOWSN_FREE_DIR . 'includes/class-woowsnfree-activation.php';
	WOOWSNFree_Activation::seed_default_registration_form();

	$inc = WOOWSN_FREE_DIR . 'includes/';

	require_once $inc . 'class-woowsnfree-admin.php';
	require_once $inc . 'class-woowsnfree-approval-admin.php';

	new WOOWSNFree_Admin();
	WOOWSNFree_Approval_Admin::init();

	if ( woowsn_free_is_feature_enabled( 'registration' ) ) {
		require_once $inc . 'class-woowsnfree-registration.php';
		WOOWSNFree_Registration::instance();
	}

	if ( woowsn_free_is_feature_enabled( 'hide_price' ) ) {
		require_once $inc . 'class-woowsnfree-hide-price.php';
		WOOWSNFree_Hide_Price::instance();
	}

	if ( woowsn_free_is_feature_enabled( 'product_visibility' ) ) {
		require_once $inc . 'class-woowsnfree-product-visibility.php';
		WOOWSNFree_Product_Visibility::instance();
	}

	if ( woowsn_free_is_feature_enabled( 'discount_rules' ) ) {
		require_once $inc . 'class-woowsnfree-discount.php';
		WOOWSNFree_Discount::instance();
	}

	if ( woowsn_free_is_feature_enabled( 'product_lists' ) ) {
		require_once $inc . 'class-woowsnfree-product-lists.php';
		WOOWSNFree_Product_Lists::instance();
	}

	if ( woowsn_free_is_feature_enabled( 'quotes' ) ) {
		require_once $inc . 'class-woowsnfree-quotes.php';
		WOOWSNFree_Quotes::instance();
	}

	require_once $inc . 'class-woowsnfree-api.php';
	WOOWSNFree_API::register();
}
