<?php
define('WEBIEN_PLUGIN_VERSION', '0.8.9');

/**
 * Plugin Name: Webien
 * Plugin URI: https://plugins.webien.io
 * Author: Haus
 * Description: Setup improvements
 * Author URI: https://plugins.webien.io
 * Version: 0.8.9
 * License: GPL
 * Primary Branch: main
 */


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

if (!class_exists('webienPlugin')) {

    class webienPlugin
    {
        public function __construct()
        {

            define('WEBIEN_PLUGIN_PATH', __DIR__);
            define('WEBIEN_PLUGIN_URL', plugin_dir_url(__FILE__));

            if (file_exists(WEBIEN_PLUGIN_PATH . '/vendor/autoload.php')) {
                require WEBIEN_PLUGIN_PATH . '/vendor/autoload.php';
            }

            // Add options page
            add_filter('acf/init', [new \Webien\Setup\OptionPage, 'init']);
            //add_action('init', [(new \Webien\Setup\PrintPdf\OptionPage()), 'add_options'], 100);


            // Detect plugin updates
            if(is_admin() || (defined('WP_CLI') && WP_CLI)) {

                $version = WEBIEN_PLUGIN_VERSION;
                $slug = 'webien-plugin';
                $indexFile = $slug . '/webien.php';

                $info = 'https://plugins.webien.io/plugins/webien-plugin/info.php?key=xxx';
                $updater = new \Webien\Setup\PluginUpdater($info, $indexFile, $slug, $version);
            }


            // Classes
            \Webien\Setup\General::init();
            \Webien\Setup\CleanUp::init();
            \Webien\Setup\WCAG::init();
            // \Webien\Setup\PluginCheck::init();
            \Webien\Setup\Scheduling\Scheduling::init();
            \Webien\Setup\Cron\Cron::init();

            add_action('init', function () {
                (new \Webien\Elementor\Setup())->init();
                (new \Webien\Setup\Jet\Setup())->init();
            });


            // Add WP CLI for AWS Setup [wp aws-setup sitename.se]
            if (defined('WP_CLI') && WP_CLI) {
                require_once WEBIEN_PLUGIN_PATH . '/src/AwsSetup.php';
                require_once WEBIEN_PLUGIN_PATH . '/src/Performance/Performance.php';
                require_once WEBIEN_PLUGIN_PATH . '/src/Scheduling/CLI.php';
            }


            // Define AWS variables from .env if exists and not already defined
            if (!defined('AWS_BUCKET') && isset($_ENV['AWS_BUCKET'])) define('AWS_BUCKET', $_ENV['AWS_BUCKET']);
            if (!defined('AWS_KEY') && isset($_ENV['AWS_KEY'])) define('AWS_KEY', $_ENV['AWS_KEY']);
            if (!defined('AWS_SECRET') && isset($_ENV['AWS_SECRET'])) define('AWS_SECRET', $_ENV['AWS_SECRET']);
            if (!defined('AWS_DISTRIBUTION') && isset($_ENV['AWS_DISTRIBUTION'])) define('AWS_DISTRIBUTION', $_ENV['AWS_DISTRIBUTION']);
            if (!defined('AWS_ENABLE_UPLOAD') && isset($_ENV['AWS_ENABLE_UPLOAD'])) define('AWS_ENABLE_UPLOAD', $_ENV['AWS_ENABLE_UPLOAD']);
            if (!defined('FORCE_LOCAL_S3_UPLOAD') && isset($_ENV['FORCE_LOCAL_S3_UPLOAD'])) define('FORCE_LOCAL_S3_UPLOAD', $_ENV['FORCE_LOCAL_S3_UPLOAD']);


            // AWS defines for S3 plugin
            $awsBucket = defined('AWS_BUCKET') ? AWS_BUCKET : false;
            $awsKey = defined('AWS_KEY') ? AWS_KEY : false;
            $awsSecret = defined('AWS_SECRET') ? AWS_SECRET : false;
            $awsDistribution = defined('AWS_DISTRIBUTION') ? AWS_DISTRIBUTION : false;
            $enableS3Upload = defined('AWS_ENABLE_UPLOAD') ? filter_var(AWS_ENABLE_UPLOAD, FILTER_VALIDATE_BOOLEAN) : false;

            // Not very smart to use s3 upload locally if it's the same bucket etc as live site but here's a chance to use it anyway
            $forceInDevMode = defined('FORCE_LOCAL_S3_UPLOAD') ? filter_var(FORCE_LOCAL_S3_UPLOAD, FILTER_VALIDATE_BOOLEAN) : false;
            // If forced by .env, set to true
            $devPass = \Webien\Setup\General::isDev() ? $forceInDevMode : true;

            define('USE_S3_LOCALLY', $devPass);

            if ($awsBucket && $awsKey && $awsSecret && $awsDistribution && $enableS3Upload && $devPass) {
                $site = str_replace('webien-', '', $awsBucket);
                define('S3_UPLOADS_BUCKET', $awsBucket . '/' . $site . '/app');
                define('S3_UPLOADS_REGION', 'eu-north-1');
                define('S3_UPLOADS_KEY', $awsKey);
                define('S3_UPLOADS_SECRET', $awsSecret);
                define('S3_UPLOADS_HTTP_CACHE_CONTROL', 30 * 24 * 60 * 60);
                define('S3_UPLOADS_HTTP_EXPIRES', gmdate('D, d M Y H:i:s', time() + (10 * 365 * 24 * 60 * 60)) . ' GMT');
                define('S3_UPLOADS_BUCKET_URL', 'https://' . $awsDistribution . '/' . $site . '/app');
                define('S3_UPLOADS_AUTOENABLE', $enableS3Upload);
            }
        }

    }

    new webienPlugin();

}