<?php
/**
 * Plugin Name: ProofShield
 * Plugin URI: https://www.softwaregarten-plugins.de/proofshield
 * Description: Self-hosted, privacy-friendly Proof-of-Work bot protection for WordPress login and comments.
 * Version: 1.0.28
 * Author: Miriams Softwaregarten by Miriam Walter
 * Author URI: https://www.miriam-walter.de
 * Requires at least: 6.4
 * Requires PHP: 8.0
 * License: GPL-2.0-or-later
 * Text Domain: proofshield
 */

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

define( 'WPSH_VERSION', '1.0.28' );
define( 'WPSH_FILE', __FILE__ );
define( 'WPSH_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPSH_URL', plugin_dir_url( __FILE__ ) );
define( 'WPSH_OPTION_KEY', 'wpsh_settings' );
define( 'WPSH_STATS_KEY', 'wpsh_stats' );
define( 'WPSH_RUNTIME_STATE_KEY', 'wpsh_runtime_state' );



spl_autoload_register(
	static function ( string $class ): void {
		$prefix = 'WPProofShield\\';
		if ( strncmp( $class, $prefix, strlen( $prefix ) ) !== 0 ) {
			return;
		}

		$relative = substr( $class, strlen( $prefix ) );
		$relative_path = str_replace( '\\', '/', $relative );
		$path          = WPSH_DIR . 'src/' . $relative_path . '.php';
		if ( is_readable( $path ) ) {
			require_once $path;
		}
	}
);

register_activation_hook(
	__FILE__,
	static function (): void {
		$defaults         = \WPProofShield\Support\Settings::defaults();
		$current_settings = get_option( WPSH_OPTION_KEY, array() );
		update_option( WPSH_OPTION_KEY, wp_parse_args( (array) $current_settings, $defaults ) );

		if ( ! wp_next_scheduled( 'wpsh_cleanup_logs' ) ) {
			wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpsh_cleanup_logs' );
		}
	}
);

register_deactivation_hook(
	__FILE__,
	static function (): void {
		wp_clear_scheduled_hook( 'wpsh_cleanup_logs' );
		wp_clear_scheduled_hook( 'wpsh_weekly_stats_report' );
	}
);

add_action(
	'plugins_loaded',
	static function (): void {
		$plugin_instance = new \WPProofShield\Plugin();
		$plugin_instance->boot();
	}
);

// Compatibility redirect while legacy menu integration is deferred:
// older plugin state can still redirect to `page=proofshield` after activation.
add_action(
	'admin_init',
	static function (): void {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$page = sanitize_key( (string) filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW ) );
		if ( $page !== 'proofshield' && $page !== 'wp-proofshield' ) {
			return;
		}

		wp_safe_redirect( admin_url( 'admin.php?page=wpsh-settings' ) );
		exit;
	},
	1
);
