<?php
/**
 * Plugin Name: Effortless Taiwan Mandarin Helper
 * Description: Translates any text into Taiwanese Mandarin with Pinyin and Bopomofo annotations.
 * Version:     1.0.34
 * Author:      domclic
 * Donate link: https://id7.dev/donate/
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: effortless-taiwan-mandarin-helper
 *
 * @package Effortless_Taiwan_Mandarin_Helper
 */

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

define( 'ELTMH_VERSION', '1.0.34' );
define( 'ELTMH_FILE', __FILE__ );
define( 'ELTMH_DIR', plugin_dir_path( __FILE__ ) );
define( 'ELTMH_URL', plugin_dir_url( __FILE__ ) );

spl_autoload_register(
	function ( $class_name ) {
		if ( 0 !== strncmp( 'ELTMH_', $class_name, 6 ) ) {
			return;
		}
		$file = ELTMH_DIR . 'includes/class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
		if ( file_exists( $file ) ) {
			require $file;
		}
	}
);

add_action( 'plugins_loaded', 'eltmh_init' );

/**
 * Initializes all plugin components.
 */
function eltmh_init(): void {
	$settings = (array) get_option( 'eltmh_settings', array() );
	$cache    = new ELTMH_Cache();
	$api      = new ELTMH_API( $settings );

	( new ELTMH_Settings( $cache ) )->init();
	( new ELTMH_Rest( $cache, $api ) )->init();
	( new ELTMH_Admin() )->init();
	( new ELTMH_Shortcode( $cache, $settings['provider'] ?? 'openai' ) )->init();
}

if ( is_admin() ) {
	require_once ELTMH_DIR . 'includes/class-donate-notice.php';
	new ELTMH_Donate_Notice( __FILE__, array( 'settings_page_eltmh-settings' ) );
}

/**
 * Keep the plugin's name untranslated in the Plugins list.
 *
 * WordPress.org translators can translate the "Plugin Name" header via
 * GlotPress, and core's get_plugins() runs it through translate() before
 * this filter fires. Force it back to the original English name so the
 * brand name stays consistent across all locales.
 *
 * @param array $plugins List of plugins keyed by plugin basename.
 * @return array
 */
function eltmh_force_untranslated_plugin_name( $plugins ) {
	$basename = plugin_basename( __FILE__ );
	if ( isset( $plugins[ $basename ] ) ) {
		$plugins[ $basename ]['Name'] = 'Effortless Taiwan Mandarin Helper';
	}
	return $plugins;
}
add_filter( 'all_plugins', 'eltmh_force_untranslated_plugin_name' );
