<?php
/**
 * Plugin Name:       EntryFoundry - PDF Builder for Formidable Forms
 * Plugin URI:        https://entryfoundry.com/
 * Description:       Visual PDF builder for Formidable Forms certificates, invoices, reports, and email attachments.
 * Version:           0.1.4
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Author:            EntryFoundry
 * Author URI:        https://entryfoundry.com/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       entryfoundry-pdf-builder-for-formidable-forms
 *
 * @package EntryFoundry
 */

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

if ( ! function_exists( 'entryfoundry_fs' ) ) {
	/** Initialize and return the EntryFoundry parent Freemius instance. */
	function entryfoundry_fs() {
		global $entryfoundry_fs;

		if ( ! isset( $entryfoundry_fs ) ) {
			require_once __DIR__ . '/vendor/freemius/start.php';

			$entryfoundry_fs = fs_dynamic_init(
				array(
					'id'               => '36406',
					'slug'             => 'entryfoundry-pdf-builder-for-formidable-forms',
					'type'             => 'plugin',
					'public_key'       => 'pk_21e6affa80466a9863fd8348905bb',
					'is_premium'       => false,
					'has_addons'       => true,
					'has_paid_plans'   => false,
					'is_org_compliant' => true,
					'menu'             => array(
						'slug'       => 'edit.php?post_type=entryfoundry_tpl',
						'first-path' => 'plugins.php',
						'account'    => true,
						'support'    => false,
					),
				)
			);
		}

		return $entryfoundry_fs;
	}

	entryfoundry_fs();
	do_action( 'entryfoundry_fs_loaded' );
}

define( 'ENTRYFOUNDRY_VERSION', '0.1.4' );
define( 'ENTRYFOUNDRY_PLUGIN_FILE', __FILE__ );
define( 'ENTRYFOUNDRY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

$entryfoundry_autoloader = ENTRYFOUNDRY_PLUGIN_DIR . 'vendor/autoload.php';

if ( file_exists( $entryfoundry_autoloader ) ) {
	require_once $entryfoundry_autoloader;
	require_once ENTRYFOUNDRY_PLUGIN_DIR . 'src/Core/Support/functions.php';

	foreach ( array( 'text', 'field', 'image', 'shape', 'table' ) as $entryfoundry_free_element_type ) {
		entryfoundry_register_element_type( $entryfoundry_free_element_type );
	}
	entryfoundry_register_hydrate_step( 'field', new \EntryFoundry\Core\Services\Hydrate\FieldHydrateStep(), 10 );
	entryfoundry_register_hydrate_step( 'flow', new \EntryFoundry\Core\Services\Hydrate\FlowLayoutStep(), 20 );
	foreach ( array( 'certificate', 'document', 'receipt' ) as $entryfoundry_free_profile ) {
		entryfoundry_register_profile(
			array(
				'id'   => $entryfoundry_free_profile,
				'tier' => 'lite',
			)
		);
	}

	add_action( 'init', array( \EntryFoundry\Core\Controllers\PluginController::class, 'register_template_post_type' ) );

	$entryfoundry_formidable_data = new \EntryFoundry\Formidable\FormidableData();
	$entryfoundry_field_policy    = new \EntryFoundry\Formidable\FormidableFieldPolicy( $entryfoundry_formidable_data );
	$entryfoundry_templates       = new \EntryFoundry\Core\Models\Template\TemplateRepository(
		new \EntryFoundry\Core\Models\Template\TemplateValidator( \EntryFoundry\Core\Services\Extension\RegistryStore::elements() )
	);
	$entryfoundry_generation      = new \EntryFoundry\Core\Services\Generation\DocumentGenerationService(
		$entryfoundry_templates,
		$entryfoundry_formidable_data,
		new \EntryFoundry\Core\Services\Hydrate\HydratePipeline( \EntryFoundry\Core\Services\Extension\RegistryStore::hydrate() ),
		ENTRYFOUNDRY_PLUGIN_DIR,
		$entryfoundry_field_policy
	);
	$entryfoundry_site_salt       = defined( 'AUTH_SALT' ) ? (string) AUTH_SALT : ( defined( 'SECURE_AUTH_SALT' ) ? (string) SECURE_AUTH_SALT : ( defined( 'NONCE_SALT' ) ? (string) NONCE_SALT : ( defined( 'AUTH_KEY' ) ? (string) AUTH_KEY : ENTRYFOUNDRY_PLUGIN_FILE ) ) );
	$entryfoundry_signing_secret  = hash( 'sha256', 'entryfoundry-document|' . $entryfoundry_site_salt );
	$entryfoundry_signer          = new \EntryFoundry\Core\Services\Security\SignedUrlService( $entryfoundry_signing_secret );
	unset( $entryfoundry_site_salt );

	$entryfoundry_formidable_rest_controller  = new \EntryFoundry\Core\Controllers\Rest\FormidableRestController(
		$entryfoundry_formidable_data,
		new \EntryFoundry\Core\Services\Security\RestPermissionService()
	);
	$entryfoundry_template_rest_controller    = new \EntryFoundry\Core\Controllers\Rest\TemplateRestController(
		$entryfoundry_templates,
		new \EntryFoundry\Core\Services\Security\RestPermissionService(),
		$entryfoundry_field_policy
	);
	$entryfoundry_builder_admin_controller    = new \EntryFoundry\Core\Controllers\Admin\BuilderAdminController(
		new \EntryFoundry\Core\Views\Admin\BuilderView(),
		$entryfoundry_templates,
		$entryfoundry_formidable_data
	);
	$entryfoundry_shortcode_controller        = new \EntryFoundry\Core\Controllers\ShortcodeController( $entryfoundry_generation, $entryfoundry_signer, $entryfoundry_templates );
	$entryfoundry_document_controller         = new \EntryFoundry\Core\Controllers\DocumentController( $entryfoundry_generation, $entryfoundry_signer, $entryfoundry_templates );
	$entryfoundry_submission_controller       = new \EntryFoundry\Core\Controllers\SubmissionController( $entryfoundry_templates, $entryfoundry_generation );
	$entryfoundry_generation_admin_controller = new \EntryFoundry\Core\Controllers\Admin\GenerationAdminController( $entryfoundry_generation, $entryfoundry_signer );

	add_action( 'rest_api_init', array( $entryfoundry_formidable_rest_controller, 'register_routes' ) );
	add_action( 'rest_api_init', array( $entryfoundry_template_rest_controller, 'register_routes' ) );
	add_action( 'admin_menu', array( $entryfoundry_builder_admin_controller, 'register_menu' ) );
	add_action( 'admin_menu', array( $entryfoundry_generation_admin_controller, 'register_menu' ) );
	add_action( 'admin_init', array( $entryfoundry_builder_admin_controller, 'redirect_native_editor' ) );
	add_action( 'admin_enqueue_scripts', array( $entryfoundry_builder_admin_controller, 'enqueue_assets' ) );
	add_action( 'admin_print_footer_scripts', array( $entryfoundry_builder_admin_controller, 'restore_wordpress_underscore' ), 999 );
	add_filter( 'post_row_actions', array( $entryfoundry_builder_admin_controller, 'add_row_action' ), 10, 2 );
	add_filter( 'get_edit_post_link', array( $entryfoundry_builder_admin_controller, 'filter_edit_link' ), 10, 2 );
	add_filter( 'manage_' . \EntryFoundry\Core\Models\Template\TemplateRepository::POST_TYPE . '_posts_columns', array( $entryfoundry_builder_admin_controller, 'add_template_columns' ) );
	add_action( 'manage_' . \EntryFoundry\Core\Models\Template\TemplateRepository::POST_TYPE . '_posts_custom_column', array( $entryfoundry_builder_admin_controller, 'render_template_column' ), 10, 2 );
	add_action( 'init', array( $entryfoundry_shortcode_controller, 'register' ) );
	add_action( 'init', array( $entryfoundry_document_controller, 'register_routes' ) );
	add_filter( 'query_vars', array( $entryfoundry_document_controller, 'register_query_vars' ) );
	add_action( 'template_redirect', array( $entryfoundry_document_controller, 'handle_pretty_route' ) );
	add_action( 'admin_post_entryfoundry_document', array( $entryfoundry_document_controller, 'handle' ) );
	add_action( 'admin_post_nopriv_entryfoundry_document', array( $entryfoundry_document_controller, 'handle' ) );
	$entryfoundry_submission_controller->register();

} else {
	add_action(
		'admin_notices',
		static function () {
			printf(
				'<div class="notice notice-error"><p>%s</p></div>',
				esc_html__( 'EntryFoundry dependencies are missing. Install or bundle the Composer vendor directory.', 'entryfoundry-pdf-builder-for-formidable-forms' )
			);
		}
	);
}
