<?php

// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
/**
 * Plugin Name: Form Enhancer for Fluent Forms
 * Version: 1.3.1
 * Description: A lightweight add-on that extends Fluent Forms and Fluent SMTP with new fields, admin tools, and features to boost form functionality without the bloat.
 * Requires at least: 6.4
 * Requires PHP: 8.1
 * Author: Karolína Vyskočilová
 * Author URI: https://kybernaut.cz
 * Developer: Karolína Vyskočilová
 * Developer URI: https://kybernaut.cz
 * Plugin URI: https://formenhancer.com
 * Text Domain: formenhancer
 * Domain Path: /languages
 *
 *
 * Requires Plugins: fluentform
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package FormEnhancer
 */
use FormEnhancer\Plugin;
use FormEnhancerDeps\DI\ContainerBuilder;
use FormEnhancerDeps\DI\Container;
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Exit if accessed directly.
}
if ( !defined( 'FORMENHANCER_VERSION' ) ) {
    define( 'FORMENHANCER_VERSION', '1.3.1' );
}
if ( !defined( 'FORMENHANCER_MIN_PHP_VERSION' ) ) {
    define( 'FORMENHANCER_MIN_PHP_VERSION', '8.1' );
}
if ( !defined( 'FORMENHANCER_FILE' ) ) {
    define( 'FORMENHANCER_FILE', __FILE__ );
}
if ( !defined( 'FORMENHANCER_DIR' ) ) {
    define( 'FORMENHANCER_DIR', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'FORMENHANCER_BASENAME' ) ) {
    define( 'FORMENHANCER_BASENAME', plugin_basename( __FILE__ ) );
}
if ( !defined( 'FORMENHANCER_URL' ) ) {
    define( 'FORMENHANCER_URL', plugin_dir_url( __FILE__ ) . '/' );
}
if ( !defined( 'FORMENHANCER_FLUENTFORM_VERSION' ) ) {
    define( 'FORMENHANCER_FLUENTFORM_VERSION', '5.0.0' );
}
// Skip plugin initialization in WP-CLI environment.
if ( defined( 'WP_CLI' ) && constant( 'WP_CLI' ) ) {
    return;
}
if ( !function_exists( 'formenhancer_plugin_is_active' ) ) {
    /**
     * Check if required plugin is active
     *
     * @param string $plugin Plugin basename.
     *
     * @return bool
     */
    function formenhancer_plugin_is_active(  string $plugin  ) : bool {
        if ( is_multisite() ) {
            $plugins = get_site_option( 'active_sitewide_plugins' );
            if ( isset( $plugins[$plugin] ) ) {
                return true;
            }
        }
        if ( in_array( $plugin, (array) get_option( 'active_plugins', [] ), true ) ) {
            return true;
        }
        return false;
    }

}
if ( !function_exists( 'formenhancer_files_corrupted_notice' ) ) {
    /**
     * Show admin notice if plugin files are corrupted.
     *
     * @return void
     *
     * @throws Exception
     */
    function formenhancer_files_corrupted_notice() : void {
        echo '<div class="notice is-dismissible notice-error">';
        echo '<p>' . esc_html__( 'The plugin files are corrupted. Please re-install the plugin.', 'formenhancer' ) . '</p>';
        echo '</div>';
    }

}
if ( !function_exists( 'formenhancer_php_version_notice' ) ) {
    /**
     * Show admin notice if PHP version is lower than required.
     *
     * @return void
     *
     * @throws Exception
     */
    function formenhancer_php_version_notice() : void {
        echo '<div class="notice is-dismissible notice-error">';
        echo '<p>' . wp_kses( sprintf(
            /* translators: 1: Required PHP version number, 2: Current PHP version number, 3: URL of PHP update help page */
            __( 'The plugin requires PHP version %1$s or higher. This site is running PHP version %2$s. <a href="%3$s">Learn about updating PHP</a>.', 'formenhancer' ),
            FORMENHANCER_MIN_PHP_VERSION,
            PHP_VERSION,
            'https://wordpress.org/support/update-php/'
        ), [
            'a' => [
                'href' => [],
            ],
        ] ) . '</p>';
        echo '</div>';
    }

}
if ( !function_exists( 'formenhancer_fluentform_version_notice' ) ) {
    /**
     * Show admin notice if FluentForm version is lower than required.
     *
     * @return void
     */
    function formenhancer_fluentform_version_notice() : void {
        if ( !defined( 'FLUENTFORM_VERSION' ) ) {
            echo '<div class="notice is-dismissible notice-error">';
            echo '<p>' . esc_html__( 'Form Enhancer Pro requires Fluent Forms plugin to be installed and activated.', 'formenhancer' ) . '</p>';
            echo '</div>';
            return;
        }
        if ( version_compare( FLUENTFORM_VERSION, FORMENHANCER_FLUENTFORM_VERSION, '<' ) ) {
            echo '<div class="notice is-dismissible notice-error">';
            echo '<p>' . wp_kses( sprintf( 
                /* translators: 1: Required Fluent Forms version number, 2: Current Fluent Forms version number */
                __( 'Form Enhancer Pro requires Fluent Forms version %1$s or higher. This site is running Fluent Forms version %2$s. Please update Fluent Forms to the latest version.', 'formenhancer' ),
                FORMENHANCER_FLUENTFORM_VERSION,
                FLUENTFORM_VERSION
             ), [] ) . '</p>';
            echo '</div>';
        }
    }

}
/**
 * Check plugin requirements: PHP version, WooCommerce plugin, FluentForm plugin. IF not met, show admin notice and stop loading plugin.
 */
if ( version_compare( PHP_VERSION, FORMENHANCER_MIN_PHP_VERSION, '<' ) ) {
    add_action( 'admin_notices', 'formenhancer_php_version_notice' );
    return;
} elseif ( !file_exists( __DIR__ . '/vendor/deps/scoper-autoload.php' ) ) {
    add_action( 'admin_notices', 'formenhancer_files_corrupted_notice' );
    return;
} elseif ( defined( 'FLUENTFORM_VERSION' ) && version_compare( FLUENTFORM_VERSION, FORMENHANCER_FLUENTFORM_VERSION, '<' ) ) {
    add_action( 'admin_notices', 'formenhancer_fluentform_version_notice' );
    return;
}
/**
 * Load Composer dependencies.
 */
if ( !class_exists( 'FormEnhancerDeps\\DI\\Container' ) ) {
    require_once 'vendor/deps/scoper-autoload.php';
}
if ( file_exists( __DIR__ . '/vendor/depsPro/scoper-autoload.php' ) && !class_exists( 'FormEnhancerDepsPro\\DI\\Container' ) ) {
    require_once 'vendor/depsPro/scoper-autoload.php';
}
if ( !class_exists( 'FormEnhancer\\Plugin' ) ) {
    require_once 'vendor/autoload.php';
}
if ( function_exists( 'formenhancer_fs' ) ) {
    formenhancer_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( !function_exists( 'formenhancer_fs' ) ) {
        /**
         * Create a helper function for easy SDK access.
         *
         * @return object
         */
        function formenhancer_fs() {
            global $formenhancer_fs;
            if ( !isset( $formenhancer_fs ) ) {
                // Activate multisite network integration.
                if ( !defined( 'WP_FS__PRODUCT_18735_MULTISITE' ) ) {
                    define( 'WP_FS__PRODUCT_18735_MULTISITE', true );
                }
                // Include Freemius SDK.
                // SDK is auto-loaded through composer.
                $formenhancer_fs = fs_dynamic_init( [
                    'id'               => '18735',
                    'slug'             => 'formenhancer',
                    'premium_slug'     => 'formenhancer-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_0765179795545747938a2566010c6',
                    'is_premium'       => false,
                    'premium_suffix'   => 'PRO',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => [
                        'slug'    => 'formenhancer',
                        'support' => false,
                        'parent'  => [
                            'slug' => 'options-general.php',
                        ],
                    ],
                    'navigation'       => 'tabs',
                    'is_live'          => true,
                ] );
            }
            return $formenhancer_fs;
        }

        // Init Freemius.
        formenhancer_fs();
        // Signal that SDK was initiated.
        do_action( 'formenhancer_fs_loaded' );
    }
    // ... Your plugin's main file logic ...
    formenhancer_fs()->add_action( 'after_uninstall', 'formenhancer_uninstall' );
    formenhancer_fs()->add_filter( 'plugin_icon', function () {
        return FORMENHANCER_DIR . 'assets/icon.png';
    } );
}
if ( !function_exists( 'formenhancer_container' ) ) {
    /**
     * Creates and returns DI container.
     * If DI container is already created, returns it.
     *
     * @return Container
     * @throws Exception
     */
    function formenhancer_container() : Container {
        static $container;
        if ( empty( $container ) ) {
            $containerBuilder = new ContainerBuilder();
            $containerBuilder->useAutowiring( true );
            $containerBuilder->addDefinitions( [] );
            $container = $containerBuilder->build();
        }
        return $container;
    }

}
if ( !function_exists( 'formenhancer' ) ) {
    /**
     * Returns the main plugin class from DI container.
     *
     * @return Plugin
     * @throws NotFoundException
     * @throws Exception
     */
    function formenhancer() : Plugin {
        return formenhancer_container()->get( Plugin::class );
    }

}
if ( !function_exists( 'formenhancer_init' ) ) {
    /**
     * Init
     *
     * @return void
     * @throws NotFoundException
     * @throws Exception
     */
    function formenhancer_init() : void {
        formenhancer()->init();
    }

}
if ( !function_exists( 'formenhancer_activate' ) ) {
    /**
     * Method called on plugin activation.
     *
     * @param bool $network_wide Activated network wide.
     *
     * @return void
     * @throws NotFoundException
     * @throws Exception
     */
    function formenhancer_activate(  bool $network_wide  ) : void {
        formenhancer()->activate( $network_wide );
    }

}
if ( !function_exists( 'formenhancer_deactivate' ) ) {
    /**
     * Method called on plugin deactivation.
     *
     * @param bool $network_wide Deactivated network wide.
     *
     * @return void
     * @throws NotFoundException|Exception
     */
    function formenhancer_deactivate(  bool $network_wide  ) : void {
        formenhancer()->deactivate( $network_wide );
    }

}
if ( !function_exists( 'formenhancer_uninstall' ) ) {
    /**
     * Method called on plugin uninstall.
     *
     * @return void
     * @throws NotFoundException|Exception
     */
    function formenhancer_uninstall() : void {
        formenhancer()->uninstall();
    }

}
/**
 * Init plugin.
 */
add_action( 'plugins_loaded', 'formenhancer_init', 9 );
/**
 * Register activation, deactivation and uninstall hooks.
 */
register_activation_hook( __FILE__, 'formenhancer_activate' );
register_deactivation_hook( __FILE__, 'formenhancer_deactivate' );