<?php
/**
 * Plugin Name: Nonprofit Manager
 * Plugin URI: https://nonprofitmanager.app/
 * Description: Manage memberships, donations, newsletters and events from one plugin.
 * Version: 2026.09.14
 * Requires at least: 6.0
 * Requires PHP: 8.1
 * Author: Rosenberg Digital LLC
 * Author URI: https://ericrosenberg.com
 * Text Domain: nonprofit-manager
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * File path: nonprofit-manager.php
 */
defined( 'ABSPATH' ) || exit;

/* -------------------------------------------------------------------------
 * Core components (always loaded)
 * ---------------------------------------------------------------------- */
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-version.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-admin-helpers.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-powered-by.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-marketing-optin.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-review-nudge.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-setup-wizard.php';
// Guided product tour + the org-identity data model it walks through. Loaded
// before the admin/general settings pages so they can render the org section.
require_once plugin_dir_path( __FILE__ ) . 'includes/onboarding/class-org-settings.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/onboarding/class-tour.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-admin-settings.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-general-settings.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-scripts.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/activation-hooks.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-captcha.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-content-types.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/email/helpers.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/email/smtp.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-dashboard-widgets.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-sentry.php';

/* -------------------------------------------------------------------------
 * Feature flags
 * ---------------------------------------------------------------------- */
$npmp_features = get_option(
	'npmp_enabled_features',
	array(
		'members'     => true,
		'newsletters' => false,
		'donations'   => true,
		'calendar'    => false,
		'social'      => false,
	)
);

/* -------------------------------------------------------------------------
 * Conditional modules
 * ---------------------------------------------------------------------- */
if ( ! empty( $npmp_features['members'] ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-members-settings.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-email-settings.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-membership-forms.php';

	// Member import wizard (CSV / XLSX / Google Sheets / Mailchimp / Constant
	// Contact). Row-capped in Free via npmp_import_max_rows(); Pro lifts the cap.
	require_once plugin_dir_path( __FILE__ ) . 'includes/import/import-cap.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/import/mailchimp-api.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/import/constant-contact-api.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/import/class-import-manager.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/import/admin-import.php';
}

if ( ! empty( $npmp_features['donations'] ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-payments-settings.php';
}

if ( ! empty( $npmp_features['newsletters'] ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-email-newsletter.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-subscription-preferences.php';
}

if ( ! empty( $npmp_features['calendar'] ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-calendar.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-convert-to-event.php';
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-calendar-blocks.php';
}

if ( ! empty( $npmp_features['social'] ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-social-sharing.php';
}

// Content blocks + shortcodes (email signup, unsubscribe, donation form, social
// share, contact form). Always loaded so the two new shortcodes are available;
// each wrapper block registers only when its feature module was loaded above.
require_once plugin_dir_path( __FILE__ ) . 'includes/npmp-content-blocks.php';

/* -------------------------------------------------------------------------
 * Plugin action links
 * ---------------------------------------------------------------------- */
add_filter(
	'plugin_action_links_' . plugin_basename( __FILE__ ),
	static function ( $links ) {
		$custom_links = array();

		// Overview link
		$custom_links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=npmp_main' ) ) . '">' . esc_html__( 'Overview', 'nonprofit-manager' ) . '</a>';

		// Plugin website link
		$custom_links[] = '<a href="https://nonprofitmanager.app/" target="_blank">' . esc_html__( 'Website', 'nonprofit-manager' ) . '</a>';

		// Upgrade to Pro link
		if ( ! npmp_is_pro() ) {
			$custom_links[] = '<a href="' . esc_url( npmp_get_upgrade_url() ) . '" target="_blank">' . esc_html__( 'Upgrade to Pro', 'nonprofit-manager' ) . '</a>';
		}

		// Support link
		$custom_links[] = '<a href="https://wordpress.org/support/plugin/nonprofit-manager/" target="_blank">' . esc_html__( 'Support', 'nonprofit-manager' ) . '</a>';

		return array_merge( $custom_links, $links );
	}
);

/* -------------------------------------------------------------------------
 * Admin-menu scaffold (module menus register their own sub-items)
 * ---------------------------------------------------------------------- */
add_action(
	'admin_menu',
	static function () use ( $npmp_features ) {

		/* Main hub */
		add_menu_page(
			'Nonprofit Manager',
			'Nonprofit Manager',
			'manage_options',
			'npmp_main',
			'npmp_render_main_plugin_page',
			'dashicons-groups',
			3.0
		);

		add_submenu_page( 'npmp_main', 'Overview', 'Overview', 'manage_options', 'npmp_main', 'npmp_render_main_plugin_page' );

		/* General Settings - Always available */
		add_submenu_page( 'npmp_main', 'General Settings', 'General Settings', 'manage_options', 'npmp_general_settings', 'npmp_render_general_settings_page' );

		/* Membership */
		if ( ! empty( $npmp_features['members'] ) ) {

			add_menu_page(
				'Membership',
				'Membership',
				'manage_options',
				'npmp_membership',
				'npmp_render_membership_dashboard',
				'dashicons-admin-users',
				3.05
			);

			add_submenu_page( 'npmp_membership', 'Member List',       'Member List',       'manage_options', 'npmp_members',            'npmp_render_members_page' );
			add_submenu_page( 'npmp_membership', 'Membership Settings',  'Membership Settings',  'manage_options', 'npmp_membership_forms',   'npmp_render_membership_forms_page' );
			add_submenu_page( 'npmp_main',       'Email Settings',    'Email Settings',    'manage_options', 'npmp_email_settings',     'npmp_render_email_settings_page' );
		}

		/* Newsletters */
		if ( ! empty( $npmp_features['newsletters'] ) ) {
			add_menu_page( 'Email Newsletters', 'Email Newsletters', 'edit_posts', 'npmp-newsletters', 'npmp_render_newsletter_editor', 'dashicons-email-alt', 3.1 );
			add_submenu_page( 'npmp-newsletters', 'New Newsletter',       'New Newsletter',       'edit_posts',    'npmp-newsletters',          'npmp_render_newsletter_editor' );
			add_submenu_page( 'npmp-newsletters', 'Newsletter Templates', 'Newsletter Templates', 'edit_posts',    'npmp_newsletter_templates', 'npmp_render_newsletter_templates' );
			add_submenu_page( 'npmp-newsletters', 'Newsletter Archive',   'Newsletter Archive',   'edit_posts',    'npmp_newsletter_archive',   'npmp_render_newsletter_archive' );
			add_submenu_page( 'npmp-newsletters', 'Newsletter Reports',   'Newsletter Reports',   'edit_posts',    'npmp_newsletter_reports',   'npmp_render_newsletter_reports' );
			add_submenu_page( 'npmp-newsletters', 'Newsletter Settings',  'Newsletter Settings',  'manage_options','npmp_newsletter_settings',  'npmp_render_newsletter_settings' );
		}

		/* Donations */
		if ( ! empty( $npmp_features['donations'] ) ) {
			add_menu_page( 'Donations', 'Donations', 'manage_options', 'npmp_donations_group', 'npmp_render_donations_dashboard', 'dashicons-money-alt', 3.2 );
			add_submenu_page( 'npmp_donations_group', 'Donation Settings', 'Donation Settings', 'manage_options', 'npmp_donation_settings', 'npmp_render_donation_settings_page' );
			add_submenu_page( 'npmp_donations_group', 'Payment Settings', 'Payment Settings', 'manage_options', 'npmp_payment_settings', 'npmp_render_payment_settings_page' );
		}

		/* Calendar menus are fully registered inside npmp-calendar.php */
	}
);

/* -------------------------------------------------------------------------
 * Email delivery
 * ---------------------------------------------------------------------- */
// Transport selection is handled dynamically at send time by
// includes/email/smtp.php (loaded unconditionally above), which reads
// npmp_email_get_settings() and hooks phpmailer_init to configure SMTP for
// whichever provider (Custom SMTP, Amazon SES, Brevo, SendGrid, Mailgun,
// Postmark, SparkPost) is selected. There used to be a second,
// provider-per-file loader here keyed on a `npmp_email_delivery_settings`
// option that nothing ever wrote, pointing at includes/email/ses.php,
// sendgrid.php, and mailgun.php, none of which exist. It was always a
// no-op (guarded by file_exists()) and has been removed.
