<?php
/**
 * Plugin Name: Site Toolkit by WP PowerSuite Pro
 * Plugin URI: https://wppowersuite.com/
 * Description: Premium modules and licensing for Site Toolkit by WP PowerSuite. Requires the free Site Toolkit by WP PowerSuite plugin.
 * Version: 1.0.8
 * Author: Muhammad Muzammil
 * Author URI: https://wppowersuite.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: wp-powersuite
 * Domain Path: /languages
 * Requires at least: 6.8
 * Requires PHP: 8.1
 * Requires Plugins: wp-powersuite
 * Update URI: https://wppowersuite.com
 * Network: true
 *
 * Secure design (rafter-secure-design / auth + deployment):
 * - Identity: WordPress site admin. License is the existing Elite Licenser
 *   site entitlement — no new auth primitive.
 * - AuthZ: Pro module PHP lives only in this add-on. Lite never ships it.
 *   Missing licensing on Lite is deny (not entitle).
 * - Updates: commercial updater targets this file, never the wordpress.org
 *   Lite plugin (guideline 8 / remote install of premium over .org).
 * - Activation without Lite: admin notice, no module load.
 *
 * @package WP_PowerSuite_Pro
 */

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

define( 'WP_POWERSUITE_PRO_VERSION', '1.0.8' );
define( 'WP_POWERSUITE_PRO_ACTIVE', true );
define( 'WP_POWERSUITE_PRO_FILE', __FILE__ );
define( 'WP_POWERSUITE_PRO_DIR', plugin_dir_path( __FILE__ ) );
define( 'WP_POWERSUITE_PRO_MODULES_DIR', WP_POWERSUITE_PRO_DIR . 'modules' . DIRECTORY_SEPARATOR );
define( 'WP_POWERSUITE_PRO_LICENSING_DIR', WP_POWERSUITE_PRO_DIR . 'includes' . DIRECTORY_SEPARATOR . 'licensing' . DIRECTORY_SEPARATOR );
define( 'WP_POWERSUITE_PRO_FREEMIUM_DIR', WP_POWERSUITE_PRO_DIR . 'freemium' . DIRECTORY_SEPARATOR );

/**
 * Whether the free WP PowerSuite plugin is loaded.
 */
function wp_powersuite_pro_lite_is_loaded(): bool {
	return defined( 'WP_POWERSUITE_VERSION' ) && function_exists( 'wp_powersuite_init' );
}

/**
 * Register the Pro modules directory with Lite's registry.
 *
 * @param list<string> $dirs Scan roots.
 * @return list<string>
 */
function wp_powersuite_pro_register_module_dir( array $dirs ): array {
	$dirs[] = WP_POWERSUITE_PRO_MODULES_DIR;
	return $dirs;
}

/**
 * Register the freemium overlay (premium files stripped from Lite).
 *
 * @param list<string> $roots Overlay roots.
 * @return list<string>
 */
function wp_powersuite_pro_register_freemium_dir( array $roots ): array {
	$roots[] = WP_POWERSUITE_PRO_FREEMIUM_DIR;
	return $roots;
}

/**
 * Clear Lite's module cache so Pro modules appear after activate/deactivate.
 */
function wp_powersuite_pro_invalidate_module_cache(): void {
	if ( class_exists( '\WP_PowerSuite\Core\DataRegistry' ) ) {
		\WP_PowerSuite\Core\DataRegistry::clearCaches();
	}
}

/**
 * Notice when Lite is missing.
 */
function wp_powersuite_pro_missing_lite_notice(): void {
	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}

	echo '<div class="notice notice-error"><p>';
	echo esc_html__( 'WP PowerSuite Pro requires the free WP PowerSuite plugin. Install and activate it from wordpress.org, then activate Pro again.', 'wp-powersuite' );
	echo '</p></div>';
}

add_action(
	'plugins_loaded',
	static function (): void {
		if ( ! wp_powersuite_pro_lite_is_loaded() ) {
			add_action( 'admin_notices', 'wp_powersuite_pro_missing_lite_notice' );
			return;
		}

		add_filter( 'wp_powersuite_module_scan_directories', 'wp_powersuite_pro_register_module_dir' );
		add_filter( 'wp_powersuite_freemium_file_roots', 'wp_powersuite_pro_register_freemium_dir' );
	},
	1
);

register_activation_hook(
	__FILE__,
	static function (): void {
		if ( ! wp_powersuite_pro_lite_is_loaded() ) {
			return;
		}
		wp_powersuite_pro_invalidate_module_cache();
	}
);

register_deactivation_hook(
	__FILE__,
	static function (): void {
		wp_powersuite_pro_invalidate_module_cache();
	}
);
