<?php
/**
 * Plugin Name: Responsive Mobile Select Menu
 * Plugin URI: https://www.saskialund.de/responsive-mobile-select-menu/
 * Description: Mobile Menu Made Simple - Automatically converts your WordPress menu into a touch-friendly dropdown on mobile devices. No theme modifications needed.
 * Version: 2.0.4
 * Requires at least: 6.5
 * Tested up to: 6.8
 * Requires PHP: 8.0
 * Author: Saskia Teichmann
 * Author URI: https://www.saskialund.de
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: responsive-mobile-select-menu
 *
 * @package ResponsiveMobileSelect
 * @since 2.0.0
 */

// Direktzugriff verhindern.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Plugin-Konstanten.
define( 'RESPONSIVE_MOBILE_SELECT_VERSION', '2.0.4' );
define( 'RESPONSIVE_MOBILE_SELECT_PLUGIN_FILE', __FILE__ );
define( 'RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'RESPONSIVE_MOBILE_SELECT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'RESPONSIVE_MOBILE_SELECT_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

// Manuell benötigte Dateien laden (klare Abhängigkeiten, kein Autoloader vorausgesetzt).
require_once RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . 'includes/class-settings.php';
require_once RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . 'includes/class-menu-walker.php';
require_once RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . 'includes/class-asset-manager.php';
require_once RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . 'includes/class-plugin.php';
require_once RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . 'admin/class-admin.php';

/**
 * Plugin initialisieren (nur Laufzeit-Setup, keine Migrationen).
 *
 * @since 2.0.0
 */
function responsive_mobile_select_init() {
	// Haupt-Plugin-Klasse initialisieren.
	$plugin = new ResponsiveMobileSelect\Core\Plugin();
	$plugin->init();
}
add_action( 'plugins_loaded', 'responsive_mobile_select_init' );

/**
 * Einmalige Updates/Migrationen ausführen.
 * - nur im Site-Admin (nicht Netzwerk-Admin)
 * - nur bei Versionssprung
 * - strictly per-site (keine anderen Blogs anfassen)
 *
 * @since 2.0.0
 */
function responsive_mobile_select_handle_updates() {
	// Nie im Netzwerk-Admin laufen lassen.
	if ( is_network_admin() ) {
		return;
	}

	$current_version = get_option( 'responsive_mobile_select_version', '0' );
	$target_version  = RESPONSIVE_MOBILE_SELECT_VERSION;

	// Nichts zu tun, wenn DB-Version bereits aktuell.
	if ( version_compare( $current_version, $target_version, '>=' ) ) {
		return;
	}

	// Alte Optionen aus v1/vorherigen Versionen bereinigen – nur im aktuellen Blog.
	$old_options = array(
		'rmsops_responsive-mobile-select-menu-menu',
		'responsive_mobile_select_settings', // ggf. alte v2-Vorversion.
	);

	foreach ( $old_options as $old_option ) {
		delete_option( $old_option );
	}

	// Neue Default-Option nur anlegen, wenn sie fehlt (kein Überschreiben bestehender Werte).
	if ( false === get_option( 'rms_plugin_settings_v2', false ) ) {
		$default_options = array(
			'max_width'           => 960,
			'menu_locations'      => array(),
			'menu_ids'            => array(),
			'activate_all'        => '0',
			'activate_all_ids'    => '0',
			'placeholder_text'    => __( 'Navigate', 'responsive-mobile-select-menu' ),
			'indent_character'    => '— ',
			'exclude_empty_links' => '0',
			'highlight_current'   => '0',
			'max_depth'           => 0,
		);

		// Autoload bewusst 'no', damit Admin-Option nicht global geladen wird.
		add_option( 'rms_plugin_settings_v2', $default_options, '', 'no' );
	}

	// DB-Version hochziehen und „Update komplett“-Flag setzen.
	update_option( 'responsive_mobile_select_version', $target_version );
	update_option( 'responsive_mobile_select_update_completed', true );
}
add_action( 'admin_init', 'responsive_mobile_select_handle_updates' );

/**
 * Aktivierungshook (strictly per site).
 * - Falls versehentlich netzwerkweit aktiviert: brich ab, fasse nichts an.
 *
 * @since 2.0.0
 *
 * @param bool $network_wide Ob Netzwerkaktivierung versucht wurde.
 */
function responsive_mobile_select_activate( $network_wide ) {
	// Niemals netzwerkweit aktivieren/arbeiten.
	if ( is_multisite() && $network_wide ) {
		// Keine DB-Änderungen; kurzer Hinweis.
		return;
	}

	// Im aktuellen Blog alte Optionen bereinigen.
	$old_options = array(
		'rmsops_responsive-mobile-select-menu',
		'responsive_mobile_select_settings',
	);

	foreach ( $old_options as $old_option ) {
		delete_option( $old_option );
	}

	// Defaults nur anlegen, wenn Option noch nicht existiert.
	if ( false === get_option( 'rms_plugin_settings_v2', false ) ) {
		$default_options = array(
			'max_width'           => 960,
			'menu_locations'      => array(),
			'menu_ids'            => array(),
			'activate_all'        => '0',
			'activate_all_ids'    => '0',
			'placeholder_text'    => __( 'Navigate', 'responsive-mobile-select-menu' ),
			'indent_character'    => '— ',
			'exclude_empty_links' => '0',
			'highlight_current'   => '0',
			'max_depth'           => 0,
		);

		add_option( 'rms_plugin_settings_v2', $default_options, '', 'no' );
	}

	update_option( 'responsive_mobile_select_version', RESPONSIVE_MOBILE_SELECT_VERSION );
}
register_activation_hook( __FILE__, 'responsive_mobile_select_activate' );

/**
 * Deaktivierungshook.
 * – Beim Deaktivieren **keine** Optionen löschen (übliches WP-Verhalten).
 * – Für komplettes Entfernen siehe Uninstall-Hook.
 *
 * @since 2.0.0
 */
function responsive_mobile_select_deactivate() {
	// Nichts löschen – bewusst leer.
}
register_deactivation_hook( __FILE__, 'responsive_mobile_select_deactivate' );

/**
 * Uninstall-Hook: vollständiges Aufräumen **nur** im aktuellen Blog.
 *
 * @since 2.0.0
 */
function responsive_mobile_select_uninstall() {
	$plugin_options = array(
		'rms_plugin_settings_v2',
		'rmsops_responsive-mobile-select-menu',
		'responsive_mobile_select_settings',
		'responsive_mobile_select_version',
		'responsive_mobile_select_update_completed',
	);

	foreach ( $plugin_options as $option ) {
		delete_option( $option );
	}
}
register_uninstall_hook( __FILE__, 'responsive_mobile_select_uninstall' );
