<?php
/**
 * Plugin Name:       TradeSchool Manager
 * Plugin URI:        https://tradeschoolmanager.com/
 * Description:       Complete LMS for trade schools and vocational programs: students, classes, gradebook, attendance, school years, portals, reports, CSV import/export and email (Brevo built-in). Extend with paid add-ons for Certificates, Reports & Transcripts, Workshops, Applications, Invoicing & Contracts, and Custom Fields. Learn more at tradeschoolmanager.com.
 * Version: 1.1.20
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Author:            CJs Web Service
 * Author URI:        https://profiles.wordpress.org/cburleigh/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       tradeschool-manager
 *
 * @package TradeSchoolManager
 */

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

/* ---------- Plugin constants ---------- */
define( 'TSM_VERSION', '1.1.20' );
define( 'TSM_DB_VERSION', '1.19.67' );

define( 'TSM_PLUGIN_FILE', __FILE__ );
define( 'TSM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TSM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TSM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

/* ---------- Legacy "Pro" constants kept for back-compat ---------- */
define( 'TSMP_VERSION',     '1.9.4' );
define( 'TSMP_PLUGIN_FILE', __FILE__ );
define( 'TSMP_PLUGIN_DIR',  TSM_PLUGIN_DIR );
define( 'TSMP_PLUGIN_URL',  TSM_PLUGIN_URL );

/* ---------- Free core ---------- */
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-addons.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-activator.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-deactivator.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-db.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-roles.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-settings.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-date-utils.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-inline-assets.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-portal-theme.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-portal-pages.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-students.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-teachers.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-classes.php';
/* Certificates are now a separate add-on plugin (tradeschool-certificates).
 * Schema is still installed by class-tsm-db.php so existing data survives
 * uninstall, but the engine classes only load when the add-on is present. */
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-school-years.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-active-year.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-assignments.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-gradebook.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-reports.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-shortcodes.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-teacher-shortcodes.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-programs.php';
if ( is_admin() ) {
	require_once TSM_PLUGIN_DIR . 'includes/class-tsm-feedback.php';
}
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-announcements.php';
// Applications engine now ships in the standalone "TradeSchool Manager  -  Applications" add-on.
// Workshops engine (class-tsm-workshops.php + class-tsm-workshop-gradebook.php)
// now ships in the standalone "TradeSchool Manager  -  Workshops" add-on.
// Tuition Contracts engine (class-tsm-contracts.php) now ships in the standalone
// "TradeSchool Manager  -  Invoicing & Contracts" add-on.
// Student Documents engine (class-tsm-student-docs.php) now ships in the standalone
// "TradeSchool Manager  -  Custom Fields & Student Documents" add-on.
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-license.php';
require_once TSM_PLUGIN_DIR . 'includes/functions.php';
require_once TSM_PLUGIN_DIR . 'admin/class-tsm-admin.php';
require_once TSM_PLUGIN_DIR . 'public/class-tsm-public.php';
require_once TSM_PLUGIN_DIR . 'public/class-tsm-teacher-portal.php';
require_once TSM_PLUGIN_DIR . 'public/class-tsm-parent-portal.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-page-title-hider.php';
TSM_Page_Title_Hider::init();
TSM_Portal_Pages::init();

/* ---------- Pro feature classes (no license / no updater) ---------- */
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-attendance.php';
// Payments tracking (class-tsmp-payments.php) now ships in the standalone
// "TradeSchool Manager  -  Invoicing & Contracts" add-on.
// Custom Fields engine (class-tsmp-custom-fields.php) now ships in the
// standalone "TradeSchool Manager  -  Custom Fields & Student Documents" add-on.
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-audit.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-audience.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-notes.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-emails.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-mail.php';
TSMP_Mail::init();
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-brevo.php';
TSMP_Brevo::init();
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-focus-mode.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-admin-menu-trim.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsm-parents.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-csv.php';
// class-tsmp-reports.php now lives in the Reports & Transcripts add-on.
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-pdf.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-archive-theme.php';
require_once TSM_PLUGIN_DIR . 'includes/class-tsmp-features.php';
// Invoicing engine (class-tsmp-invoices.php), Stripe integration (class-tsmp-stripe.php),
// and the daily invoice cron (class-tsmp-invoice-cron.php) now ship in the standalone
// "TradeSchool Manager  -  Invoicing & Contracts" add-on.

register_activation_hook( __FILE__, array( 'TSM_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'TSM_Deactivator', 'deactivate' ) );

/**
 * Bootstrap.
 */
function tradeschool_manager_init() {
	// Translations are auto-loaded by WordPress 4.6+ when the plugin is hosted on WordPress.org.



	TSM_DB::maybe_upgrade();

	if ( is_admin() ) {
		TSM_Admin::instance();
	}
	TSM_Public::instance();
	TSM_Teacher_Portal::instance();
	if ( class_exists( 'TSM_Parent_Portal' ) ) {
		TSM_Parent_Portal::instance();
	}
	TSM_Shortcodes::instance();
	TSM_Teacher_Shortcodes::instance();

	if ( class_exists( 'TSM_Workshops' ) ) {
		TSM_Workshops::init();
	}
	if ( class_exists( 'TSM_Contracts' ) ) {
		TSM_Contracts::init();
	}
	if ( class_exists( 'TSM_Student_Docs' ) ) {
		TSM_Student_Docs::init();
	}

	do_action( 'tradeschool_manager_loaded' );

	// Boot the shared feature dispatcher.
	if ( class_exists( 'TSMP_Features' ) ) {
		TSMP_Features::instance();
	}
}
add_action( 'plugins_loaded', 'tradeschool_manager_init' );

/**
 * Self-heal: ensure the administrator role always has the TradeSchool caps.
 * Prior versions only granted these on activation, so users upgrading via
 * "Replace ZIP" without a deactivate/reactivate cycle saw "You are not
 * allowed to access this page" on every TS admin screen. Stamp the version
 * so this only runs once per upgrade.
 */
function tradeschool_manager_ensure_caps() {
	$stamp = get_option( 'tsm_caps_version' );
	if ( $stamp === TSM_VERSION ) {
		return;
	}
	if ( class_exists( 'TSM_Roles' ) ) {
		TSM_Roles::add_roles();
	}
	update_option( 'tsm_caps_version', TSM_VERSION, false );
}
add_action( 'admin_init', 'tradeschool_manager_ensure_caps' );

/**
 * Register add-on shortcode sections into the central Shortcodes admin page
 * (TradeSchool > Shortcodes). Each section is gated on whether the add-on
 * is active so users only see what they actually have installed.
 */
add_filter( 'tradeschool_manager_shortcode_registry', function( $sections ) {
	if ( function_exists( 'tsm_addon_active' ) && tsm_addon_active( 'applications' ) ) {
		$sections[ __( 'Applications', 'tradeschool-manager' ) ] = array(
			array( 'code' => 'tsm_applications',          'desc' => __( 'Lists open programs and links to each application form.', 'tradeschool-manager' ) ),
			array( 'code' => 'tsm_application_form',      'desc' => __( 'Renders the application form for a given program.', 'tradeschool-manager' ), 'attrs' => array( 'program_id' => __( 'Program ID (required).', 'tradeschool-manager' ) ) ),
			array( 'code' => 'tsm_application_thank_you', 'desc' => __( 'Thank-you / status confirmation page after an application is submitted.', 'tradeschool-manager' ) ),
		);
	}
	if ( function_exists( 'tsm_addon_active' ) && tsm_addon_active( 'invoicing' ) ) {
		$sections[ __( 'Invoicing & Contracts', 'tradeschool-manager' ) ] = array(
			array( 'code' => 'tsm_program_price', 'desc' => __( 'Inline price display for a program.', 'tradeschool-manager' ), 'attrs' => array( 'program_id' => __( 'Program ID (required).', 'tradeschool-manager' ) ) ),
			array( 'code' => 'tsm_contract_sign', 'desc' => __( 'Public contract e-sign page. Sign links from emails resolve here.', 'tradeschool-manager' ) ),
			array( 'code' => 'tsm_pay_invoice',   'desc' => __( 'Public invoice payment page for a given invoice token.', 'tradeschool-manager' ) ),
		);
	}
	if ( function_exists( 'tsm_addon_active' ) && tsm_addon_active( 'reports_transcripts' ) ) {
		$sections[ __( 'Reports & Transcripts', 'tradeschool-manager' ) ] = array(
			array( 'code' => 'tsr_my_reports', 'desc' => __( 'Student-facing list of available report cards and transcripts.', 'tradeschool-manager' ) ),
		);
	}
	return $sections;
} );

/**
 * Welcome notice  -  pointed at the Setup Walkthrough on the About page.
 * Set on first activation by TSM_Activator. Dismissible per-user, and also
 * auto-suppressed once the user has actually visited the About page.
 */
add_action( 'admin_notices', function () {
	if ( ! current_user_can( 'manage_tradeschool' ) ) {
		return;
	}
	if ( ! get_transient( 'tsm_show_welcome_notice' ) ) {
		return;
	}
	if ( get_user_meta( get_current_user_id(), 'tsm_welcome_notice_dismissed', true ) ) {
		return;
	}
	$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
	if ( $screen && false === strpos( (string) $screen->id, 'tsm' ) ) {
		return;
	}
	if ( $screen && false !== strpos( (string) $screen->id, 'tsm-about' ) ) {
		// They're already here.
		return;
	}
	$about_url   = admin_url( 'admin.php?page=tsm-about' );
	$dismiss_url = wp_nonce_url( add_query_arg( 'tsm_dismiss_welcome', '1' ), 'tsm_dismiss_welcome' );
	echo '<div class="notice notice-info is-dismissible" style="border-left-color:#1f6feb;">';
	echo '<p style="font-size:14px;"><strong>' . esc_html__( 'Welcome to TradeSchool Manager!', 'tradeschool-manager' ) . '</strong> ';
	echo esc_html__( 'A live setup walkthrough is waiting for you  -  it tracks each step automatically as you configure your school.', 'tradeschool-manager' ) . '</p>';
	echo '<p><a class="button button-primary" href="' . esc_url( $about_url ) . '">' . esc_html__( 'Open Setup Walkthrough', 'tradeschool-manager' ) . '</a> ';
	echo '<a class="button-link" href="' . esc_url( $dismiss_url ) . '" style="margin-left:10px;color:#64748b;">' . esc_html__( 'Dismiss', 'tradeschool-manager' ) . '</a></p>';
	echo '</div>';
} );

add_action( 'admin_init', function () {
	if ( isset( $_GET['tsm_dismiss_welcome'] ) && check_admin_referer( 'tsm_dismiss_welcome' ) ) {
		update_user_meta( get_current_user_id(), 'tsm_welcome_notice_dismissed', 1 );
		update_option( 'tsm_first_run_seen', 1, false );
		delete_transient( 'tsm_show_welcome_notice' );
		wp_safe_redirect( remove_query_arg( array( 'tsm_dismiss_welcome', '_wpnonce' ) ) );
		exit;
	}
} );

/**
 * Once a user lands on the About page, count first-run as "seen" so the
 * welcome notice doesn't keep nagging other admins on the same site.
 */
add_action( 'current_screen', function ( $screen ) {
	if ( $screen && false !== strpos( (string) $screen->id, 'tsm-about' ) ) {
		if ( ! get_option( 'tsm_first_run_seen' ) ) {
			update_option( 'tsm_first_run_seen', 1, false );
		}
		delete_transient( 'tsm_show_welcome_notice' );
	}
} );
