<?php
/**
 * Loader — Panelra Free.
 *
 * Requires all module files and instantiates them.
 * The Pro add-on hooks into the 'pnlrdb_loaded' action
 * to register its own modules without modifying this file.
 *
 * @package Panelra
 */

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

/**
 * Class Pnlrdb_Loader
 */
class Pnlrdb_Loader {

	/**
	 * Load module files and run the plugin.
	 */
	public function __construct() {
		$this->load_dependencies();
	}

	/**
	 * Require all module class files.
	 *
	 * @return void
	 */
	private function load_dependencies() {
		require_once PNLRDB_PLUGIN_DIR . 'admin/class-pnlrdb-admin.php';

		$modules = array(
			'dashboard-colors/class-pnlrdb-dashboard-colors.php',
			'menu-manager/class-pnlrdb-menu-manager.php',
			'admin-bar/class-pnlrdb-admin-bar.php',
			'notice-manager/class-pnlrdb-notice-manager.php',
			'maintenance-mode/class-pnlrdb-maintenance-mode.php',
			'log-manager/class-pnlrdb-log-manager.php',
			'role-manager/class-pnlrdb-role-manager.php',
			'dark-mode/class-pnlrdb-dark-mode.php',
			'theme-presets/class-pnlrdb-theme-presets.php',
			'dashboard-manager/class-pnlrdb-dashboard-manager.php',
			'dashboard-builder/class-pnlrdb-dashboard-builder.php',
		);

		foreach ( $modules as $file ) {
			$path = PNLRDB_PLUGIN_DIR . 'modules/' . $file;
			if ( file_exists( $path ) ) {
				require_once $path;
			}
		}
	}

	/**
	 * Instantiate all modules and fire the Pro hook.
	 *
	 * @return void
	 */
	public function run() {
		if ( is_admin() && class_exists( 'Pnlrdb_Admin' ) ) {
			new Pnlrdb_Admin();
		}

		if ( class_exists( 'Pnlrdb_Dashboard_Colors' ) )  { new Pnlrdb_Dashboard_Colors(); }
		if ( class_exists( 'Pnlrdb_Menu_Manager' ) )       { new Pnlrdb_Menu_Manager(); }
		if ( class_exists( 'Pnlrdb_Admin_Bar' ) )          { new Pnlrdb_Admin_Bar(); }
		if ( class_exists( 'Pnlrdb_Notice_Manager' ) )     { new Pnlrdb_Notice_Manager(); }
		if ( class_exists( 'Pnlrdb_Maintenance_Mode' ) )   { new Pnlrdb_Maintenance_Mode(); }
		if ( class_exists( 'Pnlrdb_Log_Manager' ) )        { new Pnlrdb_Log_Manager(); }
		if ( class_exists( 'Pnlrdb_Role_Manager' ) )       { new Pnlrdb_Role_Manager(); }
		if ( class_exists( 'Pnlrdb_Dark_Mode' ) )          { new Pnlrdb_Dark_Mode(); }
		if ( class_exists( 'Pnlrdb_Theme_Presets' ) )      { new Pnlrdb_Theme_Presets(); }
		if ( class_exists( 'Pnlrdb_Dashboard_Manager' ) )  { new Pnlrdb_Dashboard_Manager(); }
		if ( class_exists( 'Pnlrdb_Dashboard_Builder' ) )  { new Pnlrdb_Dashboard_Builder(); }

		/**
		 * Fires after all Free modules are loaded.
		 * The Pro add-on uses this hook to register its own modules.
		 *
		 * @since 1.0.0
		 */
		do_action( 'pnlrdb_loaded' );
	}
}
