<?php
/**
 * Plugin Name:     GravityExport
 * Version:         1.6.0
 * Plugin URI:      https://www.gravitykit.com/products/gravityexport/
 * Description:     Export Gravity Forms entries to multiple formats locally or to remote locations.
 * Author:          GravityKit
 * Author URI:      https://www.gravitykit.com
 * License:         GPL2
 * License URI:     https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:     gk-gravityexport
 */

use GFExcel\Addon\GravityExportAddon;
use GravityKit\GravityExport\GravityExport;

defined( 'ABSPATH' ) or exit;

require_once __DIR__ . '/vendor_prefixed/gravitykit/foundation/src/preflight_check.php';

if ( ! GravityKit\GravityExport\Foundation\should_load( __FILE__ ) ) {
	return;
}

if ( ! GravityKit\GravityExport\Foundation\meets_min_php_version_requirement( __FILE__, '7.3.0' ) ) {
	return;
}

if ( ! defined( 'GK_GRAVITYEXPORT_PLUGIN_VERSION' ) ) {
	define( 'GK_GRAVITYEXPORT_PLUGIN_VERSION', '1.6.0' );
}

if ( ! defined( 'GK_GRAVITYEXPORT_MIN_LITE_VERSION' ) ) {
	define( 'GK_GRAVITYEXPORT_MIN_LITE_VERSION', '2.1.0' );
}

if ( ! defined( 'GK_GRAVITYEXPORT_PLUGIN_FILE' ) ) {
	define( 'GK_GRAVITYEXPORT_PLUGIN_FILE', __FILE__ );
}

if ( ! defined( 'GK_GRAVITYEXPORT_PLUGIN_SLUG' ) ) {
	define( 'GK_GRAVITYEXPORT_PLUGIN_SLUG', 'gravityexport' );
}

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor_prefixed/autoload.php';

GravityKit\GravityExport\Foundation\Core::register( GK_GRAVITYEXPORT_PLUGIN_FILE );

function gk_gravityexport_load() {
	if ( ! defined( 'GFEXCEL_PLUGIN_FILE' ) ||
	     ! defined( 'GFEXCEL_PLUGIN_VERSION' ) ||
	     version_compare( GFEXCEL_PLUGIN_VERSION, GK_GRAVITYEXPORT_MIN_LITE_VERSION, '<' )
	) {
		$show_incorrect_lite_plugin_version_message = function () {
			$message = wpautop( strtr(
				// translators: Do not translate the words inside the [] square brackets; they are replaced.
				esc_html__( 'GravityExport requires [url]GravityExport Lite[/url] version [version] or newer.', 'gk-gravityexport' ),
				[
					'[url]'     => '<a href="' . esc_url( admin_url( 'plugin-install.php?s=GravityExport%2520Lite&tab=search&type=term' ) ) . '">',
					'[/url]'    => '</a>',
					'[version]' => GK_GRAVITYEXPORT_MIN_LITE_VERSION
				]
			) );

			echo "<div class='error'>$message</div>";
		};

		add_action( 'admin_notices', $show_incorrect_lite_plugin_version_message );

		return;
	}

	if ( ! defined( 'GK_GRAVITYEXPORT_PLUGIN_NAME' ) ) {
		define( 'GK_GRAVITYEXPORT_PLUGIN_NAME', 'GravityExport' );
	}

	if ( ! defined( 'GK_GRAVITYEXPORT_PLUGIN_NAME_SHORT' ) ) {
		define( 'GK_GRAVITYEXPORT_PLUGIN_NAME_SHORT', 'GravityExport' );
	}

    $assets_dir = plugin_dir_url( __FILE__ ) . 'public';

    $container_loaded = false;
    add_action( 'gfexcel_container_loaded', static function ( $container ) use ( $assets_dir, &$container_loaded ) {
        // Makes sure to register service providers before the services are requested.
        GravityExport::get_instance( $container, $assets_dir )->registerServiceProviders();
        $container_loaded = true;
    } );

    add_action( 'gfexcel_loaded', static function ( $container ) use ( $assets_dir, &$container_loaded ) {
        // Bootstrap and catch exceptions.
        try {
            $plugin = GravityExport::get_instance( $container, $assets_dir );
            if ( ! $container_loaded ) {
                // Backward compatibility for older versions of GravityExport Lite.
                $plugin->registerServiceProviders();
            }

            // Initialize add-ons.
            $plugin->registerAddOns();

            new GravityKit\GravityExport\MultiRow\Plugin();
            new GravityKit\GravityExport\PdfRenderer\Plugin( GravityExportAddon::get_instance() );
        } catch ( \Exception $e ) {
            $show_incomplete_install_message = function () use ( $e ) {
                $message = wpautop(
                    strtr(
                        esc_html__(
                            'GravityExport failed to initialize: [error]. Please re-install the plugin or [url]contact support[/url].',
                            'gk-gravityexport'
                        ),
                        [
                            '[url]'   => '<a href="https://www.gravitykit.com/support/">',
                            '[/url]'  => '</a>',
                            '[error]' => $e->getMessage(),
                        ]
                    )
                );

                echo "<div class='error'>$message</div>";
            };

            add_action( 'admin_notices', $show_incomplete_install_message );
        }
    } );
}

add_action( 'plugins_loaded', 'gk_gravityexport_load', 1 );
