<?php
/**
 * Plugin Name: WordForm
 * Plugin URI:  https://softcoy.com/products/wordform-pro
 * Description: WordForm Pro is a drag-and-drop WordPress form builder for contact forms, bookings, surveys, uploads, payments, and connected workflows.
 * Version:     3.0.1
 * Author:      softcoy
 * Author URI:  https://softcoy.com/
 * License:     GPL2
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 * Requires PHP: 7.4
 * Text Domain: wordform
 * Domain Path: /languages
 */

/*
	Copyright 2026 softcoy.com
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! function_exists( 'sftcy_wordform_free_pro_plugin_path' ) ) {
	function sftcy_wordform_free_pro_plugin_path() {
		return 'wordform-pro/wordform-pro.php';
	}
}

if ( ! function_exists( 'sftcy_wordform_free_is_activation_request' ) ) {
	function sftcy_wordform_free_is_activation_request() {
		return is_admin()
			&& isset( $_GET['action'], $_GET['plugin'] )
			&& 'activate' === sanitize_key( wp_unslash( $_GET['action'] ) )
			&& plugin_basename( __FILE__ ) === sanitize_text_field( wp_unslash( $_GET['plugin'] ) );
	}
}

if ( ! function_exists( 'sftcy_wordform_free_pro_exists' ) ) {
	function sftcy_wordform_free_pro_exists() {
		return file_exists( WP_PLUGIN_DIR . '/' . sftcy_wordform_free_pro_plugin_path() );
	}
}

if ( sftcy_wordform_free_pro_exists() ) {
	if ( is_admin() ) {
		if ( ! function_exists( 'deactivate_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		deactivate_plugins( plugin_basename( __FILE__ ) );

		add_action(
			'admin_notices',
			function() {
				echo '<div class="notice notice-warning"><p>' . esc_html__( 'WordForm Pro is installed on this site. WordForm free was kept inactive because the Pro version should be used instead.', 'wordform' ) . '</p></div>';
			}
		);

		if ( sftcy_wordform_free_is_activation_request() ) {
			wp_die(
				esc_html__( 'WordForm Pro is installed on this site. Please activate WordForm Pro. WordForm free cannot be activated while the Pro version exists.', 'wordform' ),
				esc_html__( 'WordForm Pro Already Installed', 'wordform' ),
				array( 'back_link' => true )
			);
		}
	}

	return;
}

if ( ! defined( 'WORDFORM_PRODUCT_TIER' ) ) {
	define( 'WORDFORM_PRODUCT_TIER', 'basic' );
}
if ( ! function_exists( 'sftcy_wordform_get_plugin_version_from_readme' ) ) {
	function sftcy_wordform_get_plugin_version_from_readme() {
		$readme_path = __DIR__ . '/readme.txt';

		if ( is_readable( $readme_path ) ) {
			$readme_contents = file_get_contents( $readme_path );

			if ( is_string( $readme_contents ) && preg_match( '/^Stable tag:\s*([^\r\n]+)/mi', $readme_contents, $matches ) ) {
				$stable_tag = trim( $matches[1] );

				if ( '' !== $stable_tag && 'trunk' !== strtolower( $stable_tag ) ) {
					return preg_replace( '/[^0-9A-Za-z.\-_+]/', '', $stable_tag );
				}
			}
		}

		if ( function_exists( 'get_file_data' ) ) {
			$plugin_data = get_file_data(
				__FILE__,
				array(
					'Version' => 'Version',
				),
				'plugin'
			);

			if ( ! empty( $plugin_data['Version'] ) ) {
				return $plugin_data['Version'];
			}
		}

		return '1.0.0';
	}
}
if ( ! defined( 'SFTCY_WORDFORM_VERSION' ) ) {
	define( 'SFTCY_WORDFORM_VERSION', sftcy_wordform_get_plugin_version_from_readme() );
}
if ( ! function_exists( 'sftcy_wordform_sync_plugins_page_version' ) ) {
	function sftcy_wordform_sync_plugins_page_version( $plugins ) {
		$plugin_basename = plugin_basename( __FILE__ );

		if ( isset( $plugins[ $plugin_basename ] ) ) {
			$plugins[ $plugin_basename ]['Version'] = SFTCY_WORDFORM_VERSION;
		}

		return $plugins;
	}
}
add_filter( 'all_plugins', 'sftcy_wordform_sync_plugins_page_version' );
if ( ! function_exists( 'sftcy_wordform_get_minimum_php_version_from_readme' ) ) {
	function sftcy_wordform_get_minimum_php_version_from_readme() {
		$readme_path = __DIR__ . '/readme.txt';

		if ( is_readable( $readme_path ) ) {
			$readme_contents = file_get_contents( $readme_path );

			if ( is_string( $readme_contents ) && preg_match( '/^Requires PHP:\s*([0-9]+(?:\.[0-9]+){1,2})\s*$/mi', $readme_contents, $matches ) ) {
				return $matches[1];
			}
		}

		return '7.4';
	}
}
if ( ! defined( 'SFTCY_WORDFORM_MINIMUM_PHP_VERSION' ) ) {
	define( 'SFTCY_WORDFORM_MINIMUM_PHP_VERSION', sftcy_wordform_get_minimum_php_version_from_readme() );
}
if ( ! defined( 'SFTCY_WORDFORM_MINIMUM_WP_VERSION' ) ) {
	define( 'SFTCY_WORDFORM_MINIMUM_WP_VERSION', '6.2' );
}
if ( ! defined( 'SFTCY_WORDFORM_PLUGIN_DIR' ) ) {
	define( 'SFTCY_WORDFORM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'SFTCY_WORDFORM_PLUGIN_FILE' ) ) {
	define( 'SFTCY_WORDFORM_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/wordform.php' );
}
if ( ! defined( 'SFTCY_WORDFORM_PLUGIN_INC' ) ) {
	define( 'SFTCY_WORDFORM_PLUGIN_INC', plugin_dir_path( __FILE__ ) . '/includes/' );
}
if ( ! defined( 'SFTCY_WORDFORM_PLUGIN_LIBS' ) ) {
	define( 'SFTCY_WORDFORM_PLUGIN_LIBS', plugin_dir_path( __FILE__ ) . '/libs/' );
}
if ( ! defined( 'SFTCY_WORDFORM_PRODUCT_URL' ) ) {
	define( 'SFTCY_WORDFORM_PRODUCT_URL', 'https://softcoy.com/products/wordform-pro' );
}
if ( ! defined( 'SFTCY_SOFTCOY_API_BASE' ) ) {
	define( 'SFTCY_SOFTCOY_API_BASE', 'https://softcoy.com/api/v1' );
}
if ( ! defined( 'SFTCY_PRODUCT_REGISTRATION_API' ) ) {
	define( 'SFTCY_PRODUCT_REGISTRATION_API', SFTCY_SOFTCOY_API_BASE . '/license-registration' );
}
if ( ! defined( 'SFTCY_PRODUCT_UPGRADE_API' ) ) {
	define( 'SFTCY_PRODUCT_UPGRADE_API', SFTCY_SOFTCOY_API_BASE . '/product-upgrade' );
}
if ( ! defined( 'SFTCY_PRODUCT_DOWNLOAD_API' ) ) {
	define( 'SFTCY_PRODUCT_DOWNLOAD_API', SFTCY_SOFTCOY_API_BASE . '/releases/download' );
}

function sftcy_wordform_autoloader( $class ) {
	static $classmap = null;

	$classmap_file = SFTCY_WORDFORM_PLUGIN_DIR . 'classmap.json';
	$lower_class   = strtolower( $class );

	if ( $lower_class === 'sftcywordformclassmapgenerator' ) {
		return;
	}

	try {
		if ( $classmap === null ) {
			$needs_regen = false;

			if ( ! file_exists( $classmap_file ) ) {
				$needs_regen = true;
			} else {
				$map_json = file_get_contents( $classmap_file );
				$classmap = json_decode( $map_json, true );

				if ( ! is_array( $classmap ) || empty( $classmap['version'] ) || empty( $classmap['classes'] ) ) {
					$needs_regen = true;
				} else {
					if ( version_compare( $classmap['version'], SFTCY_WORDFORM_VERSION, '!=' ) ) {
						$needs_regen = true;
					}
				}
			}

			if ( $needs_regen ) {
				sftcy_wordform_load_classmap_generator();
				SftcyWordformClassmapGenerator::generate();
				$classmap = json_decode( file_get_contents( $classmap_file ), true );
			}
		}

		if ( isset( $classmap['classes'][ $lower_class ] ) ) {
			require_once SFTCY_WORDFORM_PLUGIN_DIR . $classmap['classes'][ $lower_class ];
		}
	} catch ( Exception $e ) {
		error_log( '[WordForm Autoloader] ' . $e->getMessage() );
	}
}
spl_autoload_register( 'sftcy_wordform_autoloader' );

register_activation_hook( __FILE__, function () {
	try {
		sftcy_wordform_load_classmap_generator();
		SftcyWordformClassmapGenerator::generate();

		if ( class_exists( 'SftcyWordformBase' ) ) {
			SftcyWordformBase::activate();
		}
	} catch ( Exception $e ) {
		error_log( '[WordForm] Activation Error: ' . $e->getMessage() );
	}
});

function sftcy_wordform_load_classmap_generator() {
	spl_autoload_unregister( 'sftcy_wordform_autoloader' );
	require_once SFTCY_WORDFORM_PLUGIN_INC . 'class-sftcy-wordform-classmap-generator.php';
	spl_autoload_register( 'sftcy_wordform_autoloader' );
}

function sftcy_wordform_get_classmap(): array {
	$classmap_file = SFTCY_WORDFORM_PLUGIN_DIR . 'classmap.json';

	$needs_regen = ! file_exists( $classmap_file );
	if ( ! $needs_regen ) {
		$raw = file_get_contents( $classmap_file );
		$map = json_decode( $raw, true );
		$needs_regen = ! is_array( $map ) || empty( $map['version'] ) || empty( $map['classes'] );

		if ( ! $needs_regen ) {
			if ( version_compare( $map['version'], SFTCY_WORDFORM_VERSION, '!=' ) ) {
				$needs_regen = true;
			}
		}
	}

	if ( $needs_regen ) {
		sftcy_wordform_load_classmap_generator();
		try {
			SftcyWordformClassmapGenerator::generate();
		} catch ( Throwable $e ) {
			error_log( '[WordForm] classmap regeneration failed: ' . $e->getMessage() );
		}
	}

	$raw2 = @file_get_contents( $classmap_file );
	$map2 = @json_decode( $raw2, true );
	return is_array( $map2 ) ? $map2 : array( 'classes' => array(), 'bootstrap' => array() );
}

add_action( 'plugins_loaded', function () {
	$map = sftcy_wordform_get_classmap();

	if ( ! empty( $map['bootstrap'] ) && is_array( $map['bootstrap'] ) ) {
		foreach ( $map['bootstrap'] as $class_lc ) {
			$classname = $class_lc;

			if ( class_exists( $classname ) ) {
				try {
					new $classname();
				} catch ( Throwable $e ) {
					error_log( "[WordForm Bootstrap] Failed to init {$classname}: " . $e->getMessage() );
				}
			} elseif ( ! empty( $map['classes'][ $class_lc ] ) ) {
				$path = SFTCY_WORDFORM_PLUGIN_DIR . $map['classes'][ $class_lc ];
				if ( file_exists( $path ) ) {
					require_once $path;
					if ( class_exists( $classname ) ) {
						try {
							new $classname();
						} catch ( Throwable $e ) {
							error_log( "[WordForm Bootstrap] Failed to init {$classname}: " . $e->getMessage() );
						}
					}
				}
			}
		}
	}
}, 1 );
