<?php
/**
 * Plugin Name: Micropedia AI Search Optimizer
 * Plugin URI: https://micropedia.it/micropedia-ai-search-optimizer/
 * Description: Non-destructive AI readability layer for WordPress: llms.txt, clean AI-readable endpoints, Markdown exports, crawler preferences and semantic metadata. Developed by Marco Ilardi. Does not guarantee AI indexing or visibility.
 * Version: 1.1.14
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: Marco Ilardi
 * Author URI: https://marcoilardi.it/
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: micropedia-ai-search-optimizer
 * Domain Path: /languages
 *
 * @package MicropediaAISearchOptimizer
 */

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

define( 'MICROPEDIA_AI_SEARCH_OPTIMIZER_VERSION', '1.1.14' );
define( 'MICROPEDIA_AI_SEARCH_OPTIMIZER_FILE', __FILE__ );
define( 'MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH', plugin_dir_path( __FILE__ ) );
define( 'MICROPEDIA_AI_SEARCH_OPTIMIZER_URL', plugin_dir_url( __FILE__ ) );
define( 'MICROPEDIA_AI_DEVELOPER_NAME', 'Marco Ilardi' );
define( 'MICROPEDIA_AI_DEVELOPER_URL', 'https://marcoilardi.it/' );
define( 'MICROPEDIA_AI_DEVELOPER_SCHEMA_ID', 'https://marcoilardi.it/biografia/#person' );

if ( ! defined( 'MICROPEDIA_AI_PRO_URL' ) ) {
	define( 'MICROPEDIA_AI_PRO_URL', 'https://micropedia.it/micropedia-ai-search-optimizer-pro/' );
}

if ( ! defined( 'MICROPEDIA_AI_PRO_ENABLED' ) ) {
	define( 'MICROPEDIA_AI_PRO_ENABLED', false );
}

require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-i18n.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-compatibility.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-settings.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-llms.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-markdown.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-robots.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-schema.php';
require_once MICROPEDIA_AI_SEARCH_OPTIMIZER_PATH . 'includes/class-micropedia-ai-score.php';

/** Boot plugin. */
function micropedia_ai_search_optimizer_init() {
	Micropedia_AI_I18n::init();
	Micropedia_AI_Settings::init();
	Micropedia_AI_Llms::init();
	Micropedia_AI_Markdown::init();
	Micropedia_AI_Robots::init();
	Micropedia_AI_Schema::init();
	Micropedia_AI_Score::init();
}
add_action( 'plugins_loaded', 'micropedia_ai_search_optimizer_init' );

/** Activation defaults. */
function micropedia_ai_search_optimizer_activate() {
	$defaults = micropedia_ai_default_options();
	$current  = get_option( 'micropedia_ai_options', array() );
	update_option( 'micropedia_ai_options', wp_parse_args( $current, $defaults ) );
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'micropedia_ai_search_optimizer_activate' );

/** Deactivation cleanup. */
function micropedia_ai_search_optimizer_deactivate() {
	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'micropedia_ai_search_optimizer_deactivate' );

/** Default plugin options. */
function micropedia_ai_default_options() {
	return array(
		'enable_llms'               => 1,
		'enable_llms_full'          => 0,
		'enable_markdown'           => 0,
		'enable_schema'             => 1,
		'enable_crawler_control'    => 1,
		'enable_ai_score'           => 0,
		'plugin_language'           => '',
		'posts_limit'               => 20,
		'cache_ttl'                 => HOUR_IN_SECONDS,
		'crawler_oai_searchbot'     => 'neutral',
		'crawler_gptbot'            => 'neutral',
		'crawler_google_extended'   => 'neutral',
		'crawler_perplexitybot'     => 'neutral',
		'crawler_claudebot'         => 'neutral',
		'schema_base_type'          => 'Organization',
		'schema_base_name'          => get_bloginfo( 'name' ),
		'schema_base_url'           => home_url( '/' ),
		'schema_base_id'            => home_url( '/#organization' ),
		'schema_base_description'   => get_bloginfo( 'description' ),
		'schema_base_image'         => '',
		'schema_base_same_as'       => '',
	);
}

/** Get a plugin option. */
function micropedia_ai_get_option( $key, $default = null ) {
	$options  = get_option( 'micropedia_ai_options', array() );
	$defaults = micropedia_ai_default_options();
	$options  = wp_parse_args( is_array( $options ) ? $options : array(), $defaults );
	return array_key_exists( $key, $options ) ? $options[ $key ] : $default;
}

/** Whether the PRO module is enabled. */
function micropedia_ai_is_pro() {
	return (bool) MICROPEDIA_AI_PRO_ENABLED;
}

/** Get the correct PRO landing page URL based on plugin/site locale. */
function micropedia_ai_get_pro_url() {
	$plugin_language = micropedia_ai_get_option( 'plugin_language', '' );
	$locale          = ! empty( $plugin_language ) ? $plugin_language : determine_locale();

	if ( 0 === strpos( $locale, 'it_' ) || 'it' === $locale ) {
		return 'https://micropedia.it/micropedia-ai-search-optimizer-pro/';
	}

	return 'https://marcoilardi.io/micropedia-ai-search-optimizer-for-wordpress/';
}
