<?php
/**
 * Plugin Name:       Eren Labs – Smart Categories for WooCommerce
 * Plugin URI:        https://erenlabs.net/smart-categories-for-woocommerce/
 * Description:       Auto-assign WooCommerce products to categories with an unlimited nested (AND/OR/NOT) rule engine — the WooCommerce answer to Shopify Smart Collections. Pro generates attribute-combination subcategories automatically.
 * Version:           1.0.3
 * Requires at least: 6.6
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Eren Labs
 * Author URI:        https://erenlabs.net
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       smart-categories-for-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 7.0
 * WC tested up to:   9.5
 *
 *
 * @package ErenLabs\SmartCategories
 */

defined( 'ABSPATH' ) || exit;

define( 'SCWC_VERSION', '1.0.3' );
define( 'SCWC_FILE', __FILE__ );
define( 'SCWC_DIR', plugin_dir_path( __FILE__ ) );
define( 'SCWC_URL', plugin_dir_url( __FILE__ ) );
define( 'SCWC_BASENAME', plugin_basename( __FILE__ ) );
// Shared registries (REST namespace, block name, CLI command, Action Scheduler
// group) all carry the plugin prefix — a generic vendor segment such as
// "smart-categories" could collide with any other plugin registering the same.
define( 'SCWC_REST_NS', 'scwc/v1' );


// Freemius SDK — ships in BOTH builds (GPL, wp.org-compliant). It powers opt-in,
// licence checks, automatic updates and the checkout/upgrade UI. is_premium is
// true only in the pro build (which defines PRO_ACTIVE); the pro licence gate is
// bound to can_use_premium_code, so a licence never flips features in the free
// build (it carries no premium code). Values are PLACEHOLDERS until the Freemius
// product is created — see docs/FREEMIUS.md.
if ( ! function_exists( 'scwc_fs' ) ) {
	/**
	 * Freemius instance singleton.
	 *
	 * @return Freemius|null
	 */
	function scwc_fs() {
		global $scwc_fs;
		if ( ! isset( $scwc_fs ) ) {
			$scwc_fs = null;
			if ( is_readable( __DIR__ . '/freemius/start.php' ) ) {
				require_once __DIR__ . '/freemius/start.php';
				$scwc_fs = fs_dynamic_init(
					array(
						'id'                  => '34510',
						'slug'                => 'smart-categories-for-woocommerce',
						'type'                => 'plugin',
						'public_key'          => 'pk_24e1eb157b87f76bae61f97c01a00',
						'is_premium'          => false,
						'premium_suffix'      => 'Pro',
						'has_premium_version' => true,
						'has_addons'          => false,
						'has_paid_plans'      => true,
						'is_org_compliant'    => true,
						'menu'                => array(
							'slug'       => 'scwc-overview',
							'first-path' => 'admin.php?page=scwc-overview',
							'account'    => true,
							'contact'    => true,
							'support'    => false,
						),
					)
				);
			}
		}
		return $scwc_fs;
	}

	// Init Freemius (only when the bundled SDK is present).
	if ( scwc_fs() ) {
		scwc_fs()->add_filter(
			'plugin_icon',
			static function () {
				return __DIR__ . '/assets/img/icon-256.png';
			}
		);
		do_action( 'scwc_fs_loaded' );

		// The SDK's English-only pricing submenu is replaced by our own localized
		// Upgrade screen; the direct-support Contact page is part of the paid offering.
		scwc_fs()->add_filter(
			'is_submenu_visible',
			static function ( $visible, $id ) {
				if ( 'pricing' === $id ) {
					return false;
				}
				if ( 'contact' === $id ) {
					return function_exists( 'scwc_is_pro' ) ? scwc_is_pro() : $visible;
				}
				return $visible;
			},
			10,
			2
		);
	}
}

/**
 * Whether Pro features are unlocked. The free build always returns false (it
 * carries no premium code); the pro build binds this to the Freemius licence.
 *
 * @return bool
 */
function scwc_is_pro() {
	$pro = false;
	return (bool) apply_filters( 'scwc_is_pro', $pro );
}

/**
 * Where upgrade buttons point.
 *
 * @return string
 */
function scwc_upgrade_url() {
	$url = 'https://erenlabs.net/smart-categories-for-woocommerce/';
	if ( function_exists( 'scwc_fs' ) && scwc_fs() ) {
		$url = scwc_fs()->get_upgrade_url();
	}
	return (string) apply_filters( 'scwc_upgrade_url', $url );
}

/**
 * Where "I need help" goes: the wp.org support forum for free users, the Freemius
 * contact page once the SDK is present and the licence is active. Filterable.
 *
 * @return string
 */
function scwc_support_url() {
	$url = 'https://wordpress.org/support/plugin/smart-categories-for-woocommerce/';
	if ( function_exists( 'scwc_fs' ) && scwc_fs() && scwc_is_pro() ) {
		$url = scwc_fs()->contact_url();
	}
	return (string) apply_filters( 'scwc_support_url', $url );
}

// Uninstall cleanup. The free build also ships uninstall.php (wp.org runs it
// standalone); the pro build ships none (the Freemius deployment processor
// requires uninstall.php be absent) — it cleans up through the SDK's
// after_uninstall hook instead. Both call the same idempotent routine.
require_once SCWC_DIR . 'includes/uninstall-cleanup.php';
if ( function_exists( 'scwc_fs' ) && scwc_fs() ) {
	scwc_fs()->add_action( 'after_uninstall', 'scwc_uninstall_cleanup' );
}

// Factory autoloader (Strauss-prefixed shared core). Optional at runtime: a dev
// tree deployed without composer still runs the free engine through the manual
// requires in SCWC_Plugin; only the Core wiring (HPOS declaration, should_boot
// guard) needs it.
$scwc_autoload = __DIR__ . '/vendor/autoload.php';
if ( is_readable( $scwc_autoload ) ) {
	require $scwc_autoload;
}


// The engine's install class powers activation/deactivation; require it up front
// so the hooks resolve even before plugins_loaded.
require_once SCWC_DIR . 'includes/class-scwc-install.php';

register_activation_hook( __FILE__, array( 'SCWC_Install', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'SCWC_Install', 'deactivate' ) );

/**
 * Declare HPOS compatibility. When the factory core is present it declares this
 * itself in Bootstrap::core(); this manual fallback covers a dev tree deployed
 * without vendor/. The plugin never touches orders, so it is compatible by design.
 */
add_action(
	'before_woocommerce_init',
	static function () {
		if ( class_exists( '\\ErenLabs\\SmartCategories\\Bootstrap' ) ) {
			return; // Core::boot() declares compatibility.
		}
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', SCWC_FILE, true );
		}
	}
);

/**
 * Boot once all plugins are loaded so WooCommerce is available.
 */
add_action(
	'plugins_loaded',
	static function () {
		// Factory core wiring: boot it (declares HPOS/blocks compat) and honour the
		// free-yields-to-pro guard. Degrades gracefully when deployed without vendor/.
		if ( class_exists( '\\ErenLabs\\SmartCategories\\Bootstrap' ) && ! \ErenLabs\SmartCategories\Bootstrap::should_boot() ) {
			return;
		}

		require_once SCWC_DIR . 'includes/class-scwc-plugin.php';
		SCWC_Plugin::instance();
	},
	11
);
