<?php
/**
 * Pixelio Cart Abandon Recovery More Autoloader.
 *
 * This class handles the automatic loading of all classes for the
 * PixelioCartAbandonRecoveryMore plugin. It registers a PSR-4 style
 * autoloader that loads classes based on their namespace and class name.
 *
 * PHP version 8.0+
 *
 * @package   PixelioCartAbandonRecoveryMore
 * @version   0.1
 */

/**
 * Pixelio Cart Abandon Recovery More Autoloader.
 *
 * This class handles the automatic loading of all classes for the
 * PixelioCartAbandonRecoveryMore plugin. It registers a PSR-4 style
 * autoloader that loads classes based on their namespace and class name.
 *
 * PHP version 8.0+
 *
 * @package   PixelioCartAbandonRecoveryMore
 * @version   0.1
 */
class Pixelio_Cart_Abandon_Recovery_More_Autoloader {

	/**
	 * Registers the autoloader for the plugin classes.
	 *
	 * This method hooks the autoload function to PHP's SPL autoloader stack.
	 *
	 * @return void
	 */
	public static function pixelio_cart_abandon_recovery_more_register() {
		spl_autoload_register( array( __CLASS__, 'pixelio_cart_abandon_recovery_more_autoload' ) );
	}

	/**
	 * Autoloads classes based on namespace and class name.
	 *
	 * Only classes within the PixelioCartAbandonRecoveryMore namespace
	 * will be loaded automatically. Converts the class name to a file
	 * path and includes the file if it exists.
	 *
	 * @param string $class_name The fully qualified class name to load.
	 * @return void
	 */
	public static function pixelio_cart_abandon_recovery_more_autoload( $class_name ) {
		$prefix = 'PixelioCartAbandonRecoveryMore';
		if ( strpos( $class_name, $prefix ) < 0 ) {
			return;
		}

		$class_name               = str_replace( $prefix . '\\', '', $class_name );
		$class_name               = str_replace( '\\', DIRECTORY_SEPARATOR, strtolower( $class_name ) );
		$position_file_name_start = strrpos( $class_name, DIRECTORY_SEPARATOR );
		$file_name                = is_int( $position_file_name_start ) ? substr( $class_name, $position_file_name_start + 1 ) : $class_name;

		$file_path     = str_replace( $file_name, '', $class_name );
		$new_file_name = 'class-' . str_replace( '_', '-', $file_name ) . '.php';
		$file          = str_replace( '\\', DIRECTORY_SEPARATOR, plugin_dir_path( __FILE__ ) . $file_path . strtolower( $new_file_name ) );

		if ( file_exists( $file ) ) {

			require_once $file;
		}
	}
}
