<?php
/**
 * Plugin Name:       Wohno – Immobilien-Inserate
 * Plugin URI:        https://wohno.de/wordpress-plugin
 * Description:       Zeigt deine Wohno-Inserate nativ auf deiner WordPress-Seite (SEO-fähig). Detailseite & Bewerbung laufen first-party auf wohno.de.
 * Version:           0.16.1
 * Requires at least: 6.4
 * Requires PHP:      8.1
 * Author:            WOHNO UG (haftungsbeschränkt)
 * Author URI:        https://wohno.de
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       wohno
 * Domain Path:       /languages
 *
 * @package Wohno\Plugin
 */

declare(strict_types=1);

namespace Wohno\Plugin;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // No direct access.
}

/**
 * Plugin version (semver).
 *
 * @var string
 */
const VERSION = '0.16.1';

/**
 * Wohno Embed-/API-Contract-Version this plugin is built against.
 *
 * Checked against the `X-Wohno-Contract-Version` response header. On a
 * mismatch the plugin surfaces an "update required" admin notice.
 *
 * @var int
 */
const EMBED_CONTRACT_VERSION = 1;

/**
 * Default API base URL. Override via the `WOHNO_API_BASE` constant
 * (wp-config.php) for staging/self-hosted setups.
 *
 * @var string
 */
const DEFAULT_API_BASE = 'https://wohno.de';

define( 'WOHNO_PLUGIN_FILE', __FILE__ );
define( 'WOHNO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WOHNO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Minimal PSR-4 autoloader so the plugin works without `composer install`
 * on the target WordPress site. Maps `Wohno\Plugin\Foo\Bar` → `src/Foo/Bar.php`.
 *
 * @param string $class_name Fully-qualified class name being loaded.
 * @return void
 */
spl_autoload_register(
	static function ( string $class_name ): void {
		$prefix = __NAMESPACE__ . '\\';
		if ( strncmp( $class_name, $prefix, strlen( $prefix ) ) !== 0 ) {
			return;
		}
		$relative = substr( $class_name, strlen( $prefix ) );
		$path     = WOHNO_PLUGIN_DIR . 'src/' . str_replace( '\\', '/', $relative ) . '.php';
		if ( is_readable( $path ) ) {
			require $path;
		}
	}
);

/**
 * Boot the plugin once all plugins are loaded.
 */
add_action(
	'plugins_loaded',
	static function (): void {
		Plugin::instance()->boot();
	}
);

/**
 * Clear scheduled background jobs on deactivation.
 */
register_deactivation_hook(
	__FILE__,
	static function (): void {
		\Wohno\Plugin\Cron\Scheduler::clear();
	}
);
