<?php
/**
 * Plugin Name: Content Miner
 * Plugin URI: https://www.corristonconsulting.com/products/content-miner
 * Description: Mine your WordPress content library for overlap, search and AI readiness, refresh opportunities, localization candidates, and pre-publish collisions.
 * Version: 2.0.7
 * Requires at least: 6.2
 * Requires PHP: 7.4
 * Author: Corriston Consulting
 * Author URI: https://www.corristonconsulting.com/
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: content-miner
 *
 * @fs_premium_only /pro/, /includes/class-content-miner-secret-vault.php, /includes/class-content-miner-openai.php, /includes/class-content-miner-vector-store.php, /includes/class-content-miner-embedding-indexer.php, /includes/class-content-miner-digest.php, /includes/class-content-miner-collision-verdicts.php, /includes/class-content-miner-search-console.php, /includes/class-content-miner-google-analytics.php, /includes/class-content-miner-cloud-connector.php
 */

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

define( 'CONTENT_MINER_VERSION', '2.0.7' );
define( 'CONTENT_MINER_FILE', __FILE__ );
define( 'CONTENT_MINER_DIR', plugin_dir_path( __FILE__ ) );
define( 'CONTENT_MINER_URL', plugin_dir_url( __FILE__ ) );
define( 'CONTENT_MINER_CLOUD_DEFAULT_ENDPOINT', 'https://www.corristonconsulting.com' );

$content_miner_freemius_config = CONTENT_MINER_DIR . 'config-freemius.php';
if ( file_exists( $content_miner_freemius_config ) ) {
	require_once $content_miner_freemius_config;
}
if ( ! defined( 'CONTENT_MINER_FS_ID' ) ) {
	$content_miner_freemius_public_config = CONTENT_MINER_DIR . 'includes/content-miner-freemius-public.php';
	if ( file_exists( $content_miner_freemius_public_config ) ) {
		require_once $content_miner_freemius_public_config;
	}
}

$content_miner_freemius_secret_key    = null;
$content_miner_freemius_secret_config = CONTENT_MINER_DIR . 'config-freemius-secret.php';
if ( file_exists( $content_miner_freemius_secret_config ) ) {
	$content_miner_freemius_secret_key = require $content_miner_freemius_secret_config;
}

$content_miner_pro_bootstrap = CONTENT_MINER_DIR . 'pro/bootstrap.php';
if ( file_exists( $content_miner_pro_bootstrap ) ) {
	require_once $content_miner_pro_bootstrap;
}

if ( ! defined( 'CONTENT_MINER_FS_ID' ) ) {
	define( 'CONTENT_MINER_FS_ID', 0 );
}
if ( ! defined( 'CONTENT_MINER_FS_PUBLIC_KEY' ) ) {
	define( 'CONTENT_MINER_FS_PUBLIC_KEY', '' );
}
foreach ( array( 'PRO', 'AGENCY', 'ENTERPRISE' ) as $content_miner_plan_name ) {
	foreach ( array( 'PLAN_ID', 'ANNUAL_PRICING_ID' ) as $content_miner_identifier_type ) {
		$content_miner_constant = 'CONTENT_MINER_FS_' . $content_miner_plan_name . '_' . $content_miner_identifier_type;
		if ( ! defined( $content_miner_constant ) ) {
			define( $content_miner_constant, 0 );
		}
	}
}
unset( $content_miner_plan_name, $content_miner_identifier_type, $content_miner_constant );
foreach ( array( 'PRO', 'AGENCY' ) as $content_miner_stripe_plan_name ) {
	foreach ( array( 'PRODUCT_ID', 'ANNUAL_PRICE_ID' ) as $content_miner_stripe_identifier_type ) {
		$content_miner_stripe_constant = 'CONTENT_MINER_STRIPE_' . $content_miner_stripe_plan_name . '_' . $content_miner_stripe_identifier_type;
		if ( ! defined( $content_miner_stripe_constant ) ) {
			define( $content_miner_stripe_constant, '' );
		}
	}
}
unset( $content_miner_stripe_plan_name, $content_miner_stripe_identifier_type, $content_miner_stripe_constant );

require_once CONTENT_MINER_DIR . 'includes/class-content-miner-license.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-free-scan-limit.php';
require_once CONTENT_MINER_DIR . 'includes/content-miner-uninstall-cleanup.php';

/** Return whether real Freemius product identifiers are configured. */
function content_miner_freemius_configured() {
	return (int) CONTENT_MINER_FS_ID > 0 && '' !== trim( (string) CONTENT_MINER_FS_PUBLIC_KEY );
}

/** Return the Freemius singleton or null when public identifiers are unavailable. */
function content_miner_fs() {
	global $content_miner_fs, $content_miner_freemius_secret_key;

	if ( isset( $content_miner_fs ) ) {
		return $content_miner_fs;
	}

	if ( ! content_miner_freemius_configured() ) {
		$content_miner_fs = null;
		return $content_miner_fs;
	}

	require_once CONTENT_MINER_DIR . 'vendor/freemius/start.php';
	$content_miner_freemius_init = array(
		'id'                  => CONTENT_MINER_FS_ID,
		'slug'                => 'content-miner',
		'premium_slug'        => 'content-miner-pro',
		'type'                => 'plugin',
		'public_key'          => CONTENT_MINER_FS_PUBLIC_KEY,
		'is_premium'          => defined( 'CONTENT_MINER_PRO' ) && true === CONTENT_MINER_PRO,
		'is_premium_only'     => false,
		'has_addons'          => false,
		'has_paid_plans'      => true,
		'has_premium_version' => true,
		'is_org_compliant'    => true,
		'enable_anonymous'    => true,
		'anonymous_mode'      => false,
		'menu'                => array(
			'slug'    => 'content-miner',
			'account' => true,
			'pricing' => true,
			'contact' => false,
			'support' => false,
		),
	);

	if ( is_string( $content_miner_freemius_secret_key ) && '' !== trim( $content_miner_freemius_secret_key ) ) {
		$content_miner_freemius_init['secret_key'] = $content_miner_freemius_secret_key;
	}

	$content_miner_fs = fs_dynamic_init( $content_miner_freemius_init );
	unset( $content_miner_freemius_init, $content_miner_freemius_secret_key );

	return $content_miner_fs;
}

/** Return whether this source package contains premium implementation files. */
function content_miner_premium_code_available() {
	return defined( 'CONTENT_MINER_PRO' ) && true === CONTENT_MINER_PRO;
}

/** Return the license-policy singleton. */
function content_miner_license() {
	global $content_miner_license;
	if ( ! isset( $content_miner_license ) || ! $content_miner_license instanceof Content_Miner_License ) {
		$content_miner_license = new Content_Miner_License( content_miner_fs(), content_miner_freemius_configured(), content_miner_premium_code_available() );
	}
	return $content_miner_license;
}

/** Authoritative runtime paid-license gate. */
function content_miner_is_pro() {
	return content_miner_license()->is_pro();
}

/** Backward-compatible alias; runtime behavior is license-backed. */
function content_miner_pro_enabled() {
	return content_miner_is_pro();
}

/** Return whether the current plan unlocks a named feature. */
function content_miner_has_feature( $feature ) {
	return content_miner_license()->has_feature( $feature );
}

// Initialize Freemius before plugin hooks are registered.
content_miner_fs();
do_action( 'content_miner_fs_loaded' );
$content_miner_freemius = content_miner_fs();
if ( $content_miner_freemius && method_exists( $content_miner_freemius, 'add_action' ) ) {
	$content_miner_freemius->add_action( 'after_uninstall', 'content_miner_uninstall_cleanup' );
}
unset( $content_miner_freemius );

require_once CONTENT_MINER_DIR . 'includes/class-content-miner-collision-recommendation.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-prompt-page-evidence.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-prompt-builder.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-revue-workflow-bundle.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-consultation-cta.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-ai-response-renderer.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-analysis-content.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-analyzer.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-content-extractor.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-scoring-exclusions.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-repository.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-family-classifier.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-readiness-analyzer.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-memory.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-ingestion.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-candidate-union.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-analysis-job.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-analysis-handoff.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-decision-ledger.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-rendered-verification.php';
require_once CONTENT_MINER_DIR . 'includes/class-content-miner-admin.php';

if ( content_miner_premium_code_available() ) {
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-cloud-connector.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-secret-vault.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-openai.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-vector-store.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-embedding-indexer.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-digest.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-collision-verdicts.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-search-console.php';
	require_once CONTENT_MINER_DIR . 'includes/class-content-miner-google-analytics.php';
}

/**
 * Start the plugin after WordPress has loaded all plugins.
 */
function content_miner_boot() {
	$extractor          = new Content_Miner_Content_Extractor();
	$scoring_exclusions = new Content_Miner_Scoring_Exclusions();
	$repository         = new Content_Miner_Repository( $extractor, null, $scoring_exclusions );
	$analyzer   = new Content_Miner_Analyzer();
	$classifier = new Content_Miner_Family_Classifier();
	$readiness  = new Content_Miner_Readiness_Analyzer( $classifier );
	$openai     = null;
	$vectors    = null;
	$search_console = null;
	$analytics = null;
	$semantic_enabled = content_miner_premium_code_available() && content_miner_has_feature( Content_Miner_License::FEATURE_SEMANTIC_ANALYSIS );
	$google_enabled   = content_miner_premium_code_available() && content_miner_has_feature( Content_Miner_License::FEATURE_GOOGLE_EVIDENCE );
	if ( $semantic_enabled || $google_enabled ) {
		$vector_class = 'Content_Miner_' . 'Vector_Store';
		$vault_class = 'Content_Miner_' . 'Secret_Vault';
		$vault      = new $vault_class();
		if ( $semantic_enabled ) {
			$openai_class = 'Content_Miner_' . 'OpenAI';
			$vector_class::maybe_install();
			$openai  = new $openai_class( $vault );
			$vectors = new $vector_class();
		}
		if ( $google_enabled ) {
			$search_class      = 'Content_Miner_' . 'Search_Console';
			$analytics_class   = 'Content_Miner_' . 'Google_Analytics';
			$search_console    = new $search_class( $vault );
			$analytics         = new $analytics_class( $search_console );
		}
	}
	$memory     = new Content_Miner_Memory( $classifier, $readiness, $vectors, $analyzer );
	$embedding_class = 'Content_Miner_' . 'Embedding_Indexer';
	$embedding_indexer = $semantic_enabled ? new $embedding_class( $openai, $vectors ) : null;
	$verification = new Content_Miner_Rendered_Verification( $repository, $memory );
	$ingestion  = new Content_Miner_Ingestion( $repository, $memory, $embedding_indexer, $verification );
	$free_scan_limit = new Content_Miner_Free_Scan_Limit( content_miner_license(), content_miner_fs() );
	$analysis   = new Content_Miner_Analysis_Job( $repository, $analyzer, $memory, $vectors, $openai, $free_scan_limit );
	$analysis_handoff = new Content_Miner_Analysis_Handoff( $analysis );
	$ledger     = new Content_Miner_Decision_Ledger();
	Content_Miner_Decision_Ledger::maybe_install();
	$digest     = null;
	$verdicts   = null;
	if ( content_miner_premium_code_available() && content_miner_is_pro() ) {
		$digest_class = 'Content_Miner_' . 'Digest';
		$verdict_class = 'Content_Miner_' . 'Collision_Verdicts';
		if ( content_miner_has_feature( Content_Miner_License::FEATURE_DIGEST ) ) {
			$digest = new $digest_class( $memory, $ledger );
			$digest_class::install();
		}
		if ( content_miner_has_feature( Content_Miner_License::FEATURE_LLM_VERDICTS ) && $openai ) {
			$verdicts = new $verdict_class( $repository, $openai );
		}
	}
	$prompt_builder = new Content_Miner_Prompt_Builder();
	$consultation = new Content_Miner_Consultation_CTA();
	$cloud_enabled   = content_miner_premium_code_available() && content_miner_has_feature( Content_Miner_License::FEATURE_CLOUD_BRIDGE );
	$cloud_connector = $cloud_enabled && class_exists( 'Content_Miner_Cloud_Connector' ) ? new Content_Miner_Cloud_Connector( $memory, $analysis, $repository ) : null;

	new Content_Miner_Admin( $repository, $analyzer, $readiness, $memory, $ingestion, $analysis, $openai, $search_console, $analytics, $ledger, $digest, $verdicts, $verification, $prompt_builder, $consultation, $scoring_exclusions, $free_scan_limit, $analysis_handoff, $cloud_connector );
}
add_action( 'plugins_loaded', 'content_miner_boot' );

/** Install local persistence used by the plugin. */
function content_miner_activate() {
	if ( content_miner_premium_code_available() && content_miner_has_feature( Content_Miner_License::FEATURE_SEMANTIC_ANALYSIS ) ) {
		$vector_class = 'Content_Miner_' . 'Vector_Store';
		$vector_class::install();
	}
	Content_Miner_Decision_Ledger::install();
}
register_activation_hook( CONTENT_MINER_FILE, 'content_miner_activate' );
