<?php
/**
 * Plugin Name: Titanium Mail SMTP
 * Plugin URI:  https://adminplugins.com/mail-smtp/
 * Description: Rule-based WordPress email delivery with two connections and visual AND/OR Smart Routing.
 * Version: 1.0.1
 * Author: AdminPlugins
 * Author URI: https://adminplugins.com/
 * Text Domain: titanium-mail-smtp
 * Domain Path: /languages
 * Requires at least: 6.5
 * Tested up to: 7.1
 * Requires PHP: 7.4
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

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

if ( defined( 'TMSMTP_FILE' ) ) {
	if ( is_admin() ) {
		add_action(
			'admin_notices',
			static function () {
				echo '<div class="notice notice-error"><p>' . esc_html__( 'Another copy of Titanium Mail SMTP is already loaded. Deactivate and delete the duplicate plugin copy, then activate this one again.', 'titanium-mail-smtp' ) . '</p></div>';
			}
		);
	}
	return;
}

define( 'TMSMTP_VERSION', '1.0.1' );
define( 'TMSMTP_FILE', __FILE__ );
define( 'TMSMTP_DIR', plugin_dir_path( __FILE__ ) );
define( 'TMSMTP_URL', plugin_dir_url( __FILE__ ) );

if ( function_exists( 'tmsmtp_fs' ) ) {
	tmsmtp_fs()->set_basename( true, __FILE__ );
} elseif ( ! function_exists( 'tmsmtp_fs' ) ) {

	function tmsmtp_fs() {
		global $tmsmtp_fs;

		if ( ! isset( $tmsmtp_fs ) ) {
			require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

			$tmsmtp_fs = fs_dynamic_init(
				array(
					'id'                  => '34725',
					'slug'                => 'titanium-mail-smtp',
					'premium_slug'        => 'titanium-mail-smtp-pro',
					'type'                => 'plugin',
					'public_key'          => 'pk_b4dcbdd0a80850e8feb021e59c0a0',
					'is_premium'          => false,
					'premium_suffix'      => 'Pro',
					'has_premium_version' => true,
					'has_addons'          => false,
					'has_paid_plans'      => true,
					'is_org_compliant'    => true,
					'menu'                => array(
						'slug'    => 'titanium-mail-smtp',
						'account' => true,
						'support' => false,
					),
				)
			);
		}

		if ( method_exists( $tmsmtp_fs, 'add_filter' ) ) {
			$tmsmtp_fs->add_filter( 'pricing_url', 'tmsmtp_custom_freemius_pricing_url' );
		}

		return $tmsmtp_fs;
	}

	tmsmtp_fs();
	do_action( 'tmsmtp_fs_loaded' );
}

if ( ! function_exists( 'tmsmtp_custom_freemius_pricing_url' ) ) {

	function tmsmtp_custom_freemius_pricing_url( $url ) {
		if ( ! is_admin() ) {
			return $url;
		}

		return admin_url( 'admin.php?page=titanium-mail-smtp-pro' );
	}
}

if ( ! function_exists( 'tmsmtp_free_get_installed_pro_plugins' ) ) {

	function tmsmtp_free_get_installed_pro_plugins() {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		$installed_plugins = get_plugins();
		$current_plugin    = plugin_basename( __FILE__ );
		$known_pro_plugins = array(
			'titanium-mail-smtp-pro/titanium-mail-smtp-pro.php',
			'titanium-mail-smtp-premium/titanium-mail-smtp-pro.php',
		);
		$pro_plugins = array();

		foreach ( $installed_plugins as $plugin => $plugin_data ) {
			$plugin = plugin_basename( $plugin );
			if ( $current_plugin === $plugin ) {
				continue;
			}

			$text_domain = is_array( $plugin_data ) && isset( $plugin_data['TextDomain'] )
				? sanitize_key( (string) $plugin_data['TextDomain'] )
				: '';

			if (
				'titanium-mail-smtp-pro' === $text_domain
				|| in_array( $plugin, $known_pro_plugins, true )
			) {
				$pro_plugins[] = $plugin;
			}
		}

		return array_values( array_unique( $pro_plugins ) );
	}
}

if ( ! function_exists( 'tmsmtp_free_fs_uninstall_cleanup' ) ) {

	function tmsmtp_free_fs_uninstall_cleanup() {
		if ( class_exists( 'TMSMTP_Plugin' ) ) {
			TMSMTP_Plugin::uninstall(
				! empty( tmsmtp_free_get_installed_pro_plugins() )
			);
		}
	}
}

if ( function_exists( 'tmsmtp_fs' ) ) {
	tmsmtp_fs()->add_action(
		'after_uninstall',
		'tmsmtp_free_fs_uninstall_cleanup'
	);
}

if ( ! function_exists( 'tmsmtp_fs_hide_default_pricing_page' ) ) {

	function tmsmtp_fs_hide_default_pricing_page() {
		return false;
	}
}

if ( function_exists( 'tmsmtp_fs' ) ) {
	tmsmtp_fs()->add_filter( 'is_pricing_page_visible', 'tmsmtp_fs_hide_default_pricing_page' );
}

require_once TMSMTP_DIR . 'includes/titanium-suite-header/titanium-suite-header.php';
require_once TMSMTP_DIR . 'includes/class-tmsmtp-more-plugins.php';
require_once TMSMTP_DIR . 'includes/class-tmsmtp-api-mailers.php';
require_once TMSMTP_DIR . 'includes/class-tmsmtp-plugin.php';
require_once TMSMTP_DIR . 'includes/class-tmsmtp-free-routing.php';

register_activation_hook( __FILE__, array( 'TMSMTP_Plugin', 'activate' ) );
TMSMTP_Plugin::instance();
TMSMTP_Free_Routing::instance();
