<?php
/**
 * Plugin Name: Order From DM — Update checker
 * Description: Shows Order From DM update notices even when the main plugin is inactive.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @return string Absolute path to the main plugin bootstrap file.
 */
function dc_v2_mu_resolve_main_plugin() {
	$candidates = array(
		WP_PLUGIN_DIR . '/order-from-dm/amispet-direct-checkout-v2.php',
		WP_PLUGIN_DIR . '/amispet-direct-checkout-v2/amispet-direct-checkout-v2.php',
	);
	foreach ( $candidates as $path ) {
		if ( is_readable( $path ) ) {
			return $path;
		}
	}
	return '';
}

add_action(
	'plugins_loaded',
	static function () {
		$main = dc_v2_mu_resolve_main_plugin();
		if ( ! $main ) {
			return;
		}

		if ( ! defined( 'DC_V2_FILE' ) ) {
			define( 'DC_V2_FILE', $main );
		}
		if ( ! defined( 'DC_V2_VERSION' ) ) {
			if ( ! function_exists( 'get_file_data' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			$headers = get_file_data(
				$main,
				array(
					'Version' => 'Version',
				)
			);
			define( 'DC_V2_VERSION', ! empty( $headers['Version'] ) ? $headers['Version'] : '0' );
		}
		if ( ! defined( 'DC_V2_PLUGIN_SLUG' ) ) {
			define( 'DC_V2_PLUGIN_SLUG', 'order-from-dm' );
		}

		if ( class_exists( 'DC_V2_Plugin_Updater', false ) ) {
			DC_V2_Plugin_Updater::init();
			return;
		}

		$updater = dirname( $main ) . '/includes/v2/class-plugin-updater.php';
		if ( ! is_readable( $updater ) ) {
			return;
		}

		require_once $updater;
		if ( class_exists( 'DC_V2_Plugin_Updater' ) ) {
			DC_V2_Plugin_Updater::init();
		}
	},
	0
);
