<?php
/**
 * Plugin Name:       Frontierline Forms – Confirmation & Multi-Step for Contact Form 7
 * Plugin URI:        https://frontierline.co.jp/en/plugins/frontierline-forms/
 * Description:       Add multi-step forms, a confirmation screen, a completion screen, conditional logic, and submission history (saved to your database) to Contact Form 7.
 * Version:           2.1.1
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Requires Plugins:  contact-form-7
 * Author:            Frontierline
 * Author URI:        https://frontierline.co.jp/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       frontierline-forms
 * Domain Path:       /languages
 *
 * @package CF7MasterPro
 */

defined( 'ABSPATH' ) || exit;

// プラグイン定数
define( 'CF7MP_VERSION', '2.1.1' );
define( 'CF7MP_PLUGIN_FILE', __FILE__ );
define( 'CF7MP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CF7MP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CF7MP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'CF7MP_TEXT_DOMAIN', 'frontierline-forms' );
define( 'CF7MP_DB_PREFIX', 'cf7mp_' );
// LP URL：サイト言語で出し分け（日本語=日本語LP／それ以外=英語LP）。海外サイトの管理画面から英語LP・英語決済へ誘導。
$cf7mp_locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale();
define(
	'CF7MP_LP_URL',
	( 0 === strpos( (string) $cf7mp_locale, 'ja' ) )
		? 'https://frontierline.co.jp/plugins/frontierline-forms/'
		: 'https://frontierline.co.jp/en/plugins/frontierline-forms/'
);

// 翻訳読込（同梱 languages/。WP.org言語パックがあれば優先）
add_action(
	'init',
	function () {
		load_plugin_textdomain( 'frontierline-forms', false, dirname( CF7MP_PLUGIN_BASENAME ) . '/languages' );
	}
);

// Composer オートロード
if ( file_exists( CF7MP_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
	require_once CF7MP_PLUGIN_DIR . 'vendor/autoload.php';
}

// 簡易PSR-4オートローダー（Composer未実行時の保険）
spl_autoload_register(
	function ( $class ) {
		$prefix   = 'CF7MasterPro\\';
		$base_dir = CF7MP_PLUGIN_DIR . 'src/';
		$len      = strlen( $prefix );
		if ( strncmp( $prefix, $class, $len ) !== 0 ) {
			return;
		}
		$relative_class = substr( $class, $len );
		$file           = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
		if ( file_exists( $file ) ) {
			require $file;
		}
	}
);

// Freemius（有料版マネタイズ層）
require_once CF7MP_PLUGIN_DIR . 'src/License/freemius-init.php';
if ( function_exists( 'cf7mp_fs' ) && cf7mp_fs() ) {
	cf7mp_fs()->set_basename( true, __FILE__ );
}

// 有効化・無効化フック
register_activation_hook( __FILE__, array( 'CF7MasterPro\\Core\\Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'CF7MasterPro\\Core\\Deactivator', 'deactivate' ) );

// CF7存在確認後にプラグイン起動
add_action(
	'plugins_loaded',
	function () {
		if ( ! class_exists( 'WPCF7' ) ) {
			add_action(
				'admin_notices',
				function () {
					$class   = 'notice notice-error';
					$message = sprintf(
						/* translators: %s: Contact Form 7 plugin name */
						esc_html__( '%s is not active. Frontierline Forms for Contact Form 7 requires Contact Form 7.', 'frontierline-forms' ),
						'<strong>Contact Form 7</strong>'
					);
					printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), wp_kses_post( $message ) );
				}
			);
			return;
		}
		\CF7MasterPro\Core\Plugin::instance()->run();
	},
	20
);
