<?php
/**
 * Plugin Name: WordAI
 * Plugin URI:  https://softcoy.com/products/wordai-pro
 * Description: AI-powered content creation with HD image generation—automate humanlike posts, pages, and product descriptions with built-in SEO for maximum impact.
 * Version:     3.4.0
 * Author:      softcoy
 * Author URI:  https://softcoy.com/
 * License:     GPL2
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 * Requires PHP: 7.4
 * Text Domain: wordai
 * Domain Path: /languages
 */

 /*  
  Copyright 2026 softcoy.com
 */ 

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

if ( ! function_exists( 'sftcy_wordai_free_pro_plugin_path' ) ) {
	function sftcy_wordai_free_pro_plugin_path() {
		return 'wordai-pro/wordai-pro.php';
	}
}

if ( ! function_exists( 'sftcy_wordai_free_is_activation_request' ) ) {
	function sftcy_wordai_free_is_activation_request() {
		return is_admin()
			&& isset( $_GET['action'], $_GET['plugin'] )
			&& 'activate' === sanitize_key( wp_unslash( $_GET['action'] ) )
			&& plugin_basename( __FILE__ ) === sanitize_text_field( wp_unslash( $_GET['plugin'] ) );
	}
}

if ( ! function_exists( 'sftcy_wordai_free_pro_exists' ) ) {
	function sftcy_wordai_free_pro_exists() {
		return file_exists( WP_PLUGIN_DIR . '/' . sftcy_wordai_free_pro_plugin_path() );
	}
}

if ( sftcy_wordai_free_pro_exists() ) {
	if ( is_admin() ) {
		if ( ! function_exists( 'deactivate_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		deactivate_plugins( plugin_basename( __FILE__ ) );

		add_action(
			'admin_notices',
			function() {
				echo '<div class="notice notice-warning"><p>' . esc_html__( 'WordAI Pro is installed on this site. WordAI free was kept inactive because the Pro version should be used instead.', 'wordai' ) . '</p></div>';
			}
		);

		if ( sftcy_wordai_free_is_activation_request() ) {
			wp_die(
				esc_html__( 'WordAI Pro is installed on this site. Please activate WordAI Pro. WordAI free cannot be activated while the Pro version exists.', 'wordai' ),
				esc_html__( 'WordAI Pro Already Installed', 'wordai' ),
				array( 'back_link' => true )
			);
		}
	}

	return;
}

if ( ! defined( 'WORDAI_PRODUCT_TIER' ) ) {
	define( 'WORDAI_PRODUCT_TIER', 'basic' );
}
if ( ! defined( 'SFTCY_WORDAI_TEXT_DOMAIN' ) ) {
	define( 'SFTCY_WORDAI_TEXT_DOMAIN', 'wordai' );
}
if ( ! function_exists( 'sftcy_wordai_get_minimum_php_version' ) ) {
	function sftcy_wordai_get_minimum_php_version() {
		$readme_file = __DIR__ . '/readme.txt';

		if ( is_readable( $readme_file ) ) {
			$readme_contents = file_get_contents( $readme_file );

			if ( is_string( $readme_contents ) && preg_match( '/^Requires PHP:\s*([0-9]+(?:\.[0-9]+){1,2})\s*$/mi', $readme_contents, $matches ) ) {
				return $matches[1];
			}
		}

		return '7.4';
	}
}
if ( ! defined( 'SFTCY_WORDAI_MINIMUM_PHP_VERSION' ) ) {
	define( 'SFTCY_WORDAI_MINIMUM_PHP_VERSION', sftcy_wordai_get_minimum_php_version() );
}
if ( ! defined( 'SFTCY_WORDAI_MINIMUM_WP_VERSION' ) ) {
	define( 'SFTCY_WORDAI_MINIMUM_WP_VERSION', '6.0' );
}
if ( ! defined( 'SFTCY_WORDAI_PLUGIN_DIR' ) ) {
	define( 'SFTCY_WORDAI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'SFTCY_WORDAI_PLUGIN_FILE' ) ) {
	define( 'SFTCY_WORDAI_PLUGIN_FILE', __FILE__ );
}
if ( ! function_exists( 'sftcy_wordai_get_stable_version' ) ) {
	function sftcy_wordai_get_stable_version() {
		$default_version = '1.0.0';
		$readme_file     = SFTCY_WORDAI_PLUGIN_DIR . 'readme.txt';

		if ( is_readable( $readme_file ) ) {
			$readme_contents = file_get_contents( $readme_file );

			if ( is_string( $readme_contents ) && preg_match( '/^Stable tag:\s*([0-9]+(?:\.[0-9]+){1,3})\s*$/mi', $readme_contents, $matches ) ) {
				return $matches[1];
			}
		}

		return $default_version;
	}
}
if ( ! defined( 'SFTCY_WORDAI_VERSION' ) ) {
	define( 'SFTCY_WORDAI_VERSION', sftcy_wordai_get_stable_version() );
}
if ( ! defined( 'SFTCY_WORDAI_PLUGIN_INC' ) ) {
	define( 'SFTCY_WORDAI_PLUGIN_INC', plugin_dir_path( __FILE__ ) . '/includes/' );
}
if ( ! defined( 'SFTCY_WORDAI_PLUGIN_URL' ) ) {
	define( 'SFTCY_WORDAI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'SFTCY_WORDAI_PRODUCT_URL' ) ) {
	define( 'SFTCY_WORDAI_PRODUCT_URL', 'https://softcoy.com/products/wordai-pro/checkout' );
}
if ( ! defined( 'SFTCY_SOFTCOY_API_BASE' ) ) {
	define( 'SFTCY_SOFTCOY_API_BASE', 'https://softcoy.com/api/v1' );
}
if ( ! defined( 'SFTCY_PRODUCT_REGISTRATION_API' ) ) {
	define( 'SFTCY_PRODUCT_REGISTRATION_API', SFTCY_SOFTCOY_API_BASE . '/license-registration' );
}
if ( ! defined( 'SFTCY_PRODUCT_UPGRADE_API' ) ) {
	define( 'SFTCY_PRODUCT_UPGRADE_API', SFTCY_SOFTCOY_API_BASE . '/product-upgrade' );
}
if ( ! defined( 'SFTCY_PRODUCT_DOWNLOAD_API' ) ) {
	define( 'SFTCY_PRODUCT_DOWNLOAD_API', SFTCY_SOFTCOY_API_BASE . '/releases/download' );
}

require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-features.php';
require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-seo-standards.php';
require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-seo-integration.php';
require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-service-telemetry.php';
require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-seo-scan.php';

function sftcy_wordai_autoloader( $class ) {
	static $classmap = null;
	$classmap_file = SFTCY_WORDAI_PLUGIN_DIR . 'classmap.json';
	$lower_class   = strtolower( $class );

	if ( $lower_class === 'sftcywordaiclassmapgenerator' ) {
		return;
	}

	try {
		if ( null === $classmap ) {
			$classmap = sftcy_wordai_get_classmap();
		}

		if ( isset( $classmap['classes'][ $lower_class ] ) ) {
			require_once SFTCY_WORDAI_PLUGIN_DIR . $classmap['classes'][ $lower_class ];
		}
	} catch ( Exception $e ) {
		error_log( '[WordAI Autoloader] ' . $e->getMessage() );
	}
}
spl_autoload_register( 'sftcy_wordai_autoloader' );

function sftcy_wordai_load_classmap_generator() {
	spl_autoload_unregister( 'sftcy_wordai_autoloader' );
	require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-classmap-generator.php';
	spl_autoload_register( 'sftcy_wordai_autoloader' );
}

function sftcy_wordai_get_classmap(): array {
	$classmap_file = SFTCY_WORDAI_PLUGIN_DIR . 'classmap.json';
	$needs_regen   = ! file_exists( $classmap_file );

	if ( ! $needs_regen ) {
		$raw = file_get_contents( $classmap_file );
		$map = json_decode( $raw, true );
		$needs_regen = ! is_array( $map ) || empty( $map['version'] ) || empty( $map['classes'] );

		if ( ! $needs_regen ) {
			if ( version_compare( $map['version'], SFTCY_WORDAI_VERSION, '!=' ) ) {
				$needs_regen = true;
			}
		}
	}

	if ( $needs_regen ) {
		sftcy_wordai_load_classmap_generator();
		try {
			SftcyWordaiClassmapGenerator::generate();
		} catch ( Throwable $e ) {
			error_log( '[WordAI] classmap regeneration failed: ' . $e->getMessage() );
		}
	}

	$raw_map = @file_get_contents( $classmap_file );
	$map     = @json_decode( $raw_map, true );

	return is_array( $map ) ? $map : array( 'classes' => array(), 'bootstrap' => array() );
}

register_activation_hook(
	__FILE__,
	function() {
		try {
			sftcy_wordai_load_classmap_generator();
			SftcyWordaiClassmapGenerator::generate();

			if ( class_exists( 'SftcyWordaiBase' ) ) {
				SftcyWordaiBase::activate();
			}
		} catch ( Throwable $e ) {
			error_log( '[WordAI] Activation failed: ' . $e->getMessage() );
		}
	}
);

add_action(
	'plugins_loaded',
	function() {
		$map = sftcy_wordai_get_classmap();
		require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-hosted-services-client.php';
		require_once SFTCY_WORDAI_PLUGIN_INC . 'class-sftcy-wordai-search-console.php';
		SftcyWordaiSeoIntegrationManager::init();
		SftcyWordaiServiceTelemetry::init();
		SftcyWordaiSearchConsole::init();

		if ( ! empty( $map['bootstrap'] ) && is_array( $map['bootstrap'] ) ) {
			foreach ( $map['bootstrap'] as $classname ) {
				if ( class_exists( $classname ) ) {
					try {
						new $classname();
					} catch ( Exception $e ) {
						error_log( "[WordAI Bootstrap] Failed to instantiate {$classname}: " . $e->getMessage() );
					} catch ( Error $e ) {
						error_log( "[WordAI Bootstrap] Error during {$classname} instantiation: " . $e->getMessage() );
					}
				}
			}
		}

		if ( class_exists( 'SftcyWordaiSeoScan' ) ) {
			new SftcyWordaiSeoScan();
		}
	},
	1
);
