<?php
/**
 * PSR-4 fallback autoloader when Composer vendor/ is not present.
 *
 * @package PhoenixWP\PriceLabels
 */

defined( 'ABSPATH' ) || exit;

/**
 * Registers the extension PSR-4 autoloader.
 */
function phoenix_wp_price_labels_register_autoload_fallback(): void {
	static $registered = false;

	if ( $registered ) {
		return;
	}

	$registered = true;

	spl_autoload_register(
		static function ( string $class ): void {
			$prefix = 'PhoenixWP\\PriceLabels\\';

			if ( ! str_starts_with( $class, $prefix ) ) {
				return;
			}

			$relative  = substr( $class, strlen( $prefix ) );
			$free_file = PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_PATH . 'src/' . str_replace( '\\', '/', $relative ) . '.php';

			if ( is_readable( $free_file ) ) {
				require_once $free_file;
			}
		}
	);

	require_once PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_PATH . 'src/functions.php';
}
