<?php
/**
 * Your base production configuration goes in this file. Environment-specific
 * overrides go in their respective config/environments/{{WP_ENV}}.php file.
 *
 * A good default policy is to deviate from the production config as little as
 * possible. Try to define as much of your configuration in this file as you
 * can.
 */

use function Env\env;

use Roots\WPConfig\Config;

/**
 * Directory containing all of the site's files.
 *
 * @var string
 */
$root_dir = dirname(__DIR__);

/**
 * Document Root.
 *
 * @var string
 */
$webroot_dir = $root_dir . '/public';

/**
 * Use Dotenv to set required environment variables and load .env file in root
 * .env.local will override .env if it exists.
 */
$env_files = file_exists($root_dir . '/.env.local')
    ? ['.env', '.env.local']
    : ['.env'];

$dotenv = Dotenv\Dotenv::createUnsafeImmutable($root_dir, $env_files, false);

if (!file_exists($root_dir . '/.env')) {
    exit('Make sure you have an .env file' . PHP_EOL);
}

$dotenv->load();
$dotenv->required(['WP_HOME', 'WP_SITEURL']);

if (!env('DATABASE_URL')) {
    $dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD']);
}

/*
 * Set up our global environment constant and load its config first
 * Default: production
 */
define('WP_ENV', env('WP_ENV') ?: 'production');

/**
 * Disable Query Monitors error handler
 */
Config::define('QM_DISABLE_ERROR_HANDLER', env('QM_DISABLE_ERROR_HANDLER') ?? true);

/**
 * Disable Timmy (https://github.com/mindkomm/timmy/) by default
 */
Config::define('TIMMY', env('TIMMY') ?? false);

// Environment
Config::define('WP_LOCAL_DEV', env('WP_LOCAL_DEV') ?: false);

// URLs
Config::define('WP_HOME', env('WP_HOME'));

if (!defined('WP_CLI') && env('WP_HOME') != 'https://' . $_SERVER['HTTP_HOST']) {
    // Override WP_HOME if it's different than the one in .env
    Config::define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
}

Config::define('WP_SITEURL', env('WP_SITEURL'));

// Custom Content Directory
Config::define('CONTENT_DIR', '/content');
Config::define('WP_CONTENT_DIR', $webroot_dir . Config::get('CONTENT_DIR'));
Config::define('WP_CONTENT_URL', Config::get('WP_HOME') . Config::get('CONTENT_DIR'));

// DB settings
Config::define('DB_NAME', env('DB_NAME'));
Config::define('DB_USER', env('DB_USER'));
Config::define('DB_PASSWORD', env('DB_PASSWORD'));
Config::define('DB_HOST', env('DB_HOST') ?: 'localhost');
Config::define('DB_CHARSET', 'utf8mb4');
Config::define('DB_COLLATE', '');
$table_prefix = env('DB_PREFIX') ?: 'wp_';

if (env('DATABASE_URL')) {
    $dsn = (object) parse_url(env('DATABASE_URL'));

    Config::define('DB_NAME', substr($dsn->path, 1));
    Config::define('DB_USER', $dsn->user);
    Config::define('DB_PASSWORD', isset($dsn->pass) ? $dsn->pass : null);
    Config::define('DB_HOST', isset($dsn->port) ? "{$dsn->host}:{$dsn->port}" : $dsn->host);
}

// Setup Redis cache
Config::define('WP_REDIS_DISABLED', env('WP_REDIS_DISABLED') ?: false);
Config::define('WP_REDIS_HOST', env('WP_REDIS_HOST') ?: '127.0.0.1');
Config::define('WP_REDIS_PORT', env('WP_REDIS_PORT') ?: 6379);
Config::define('WP_REDIS_DATABASE', env('WP_REDIS_DATABASE') ?: 0);
Config::define('WP_REDIS_PASSWORD', env('WP_REDIS_PASSWORD') ?: '');
Config::define('WP_REDIS_TIMEOUT', env('WP_REDIS_TIMEOUT') ?: 1);
Config::define('WP_REDIS_READ_TIMEOUT', env('WP_REDIS_READ_TIMEOUT') ?: 1);
Config::define('WP_REDIS_PREFIX', env('WP_REDIS_PREFIX') ?: substr(sha1(basename(Config::get('WP_HOME'))), 0, 5) . '_');
Config::define('WP_REDIS_SCHEME', env('WP_REDIS_SCHEME') ?: 'tcp');
Config::define('WP_REDIS_PATH', env('WP_REDIS_PATH') ?: null);

// Authentication Unique Keys and Salts
Config::define('AUTH_KEY', env('AUTH_KEY'));
Config::define('SECURE_AUTH_KEY', env('SECURE_AUTH_KEY'));
Config::define('LOGGED_IN_KEY', env('LOGGED_IN_KEY'));
Config::define('NONCE_KEY', env('NONCE_KEY'));
Config::define('AUTH_SALT', env('AUTH_SALT'));
Config::define('SECURE_AUTH_SALT', env('SECURE_AUTH_SALT'));
Config::define('LOGGED_IN_SALT', env('LOGGED_IN_SALT'));
Config::define('NONCE_SALT', env('NONCE_SALT'));

// Custom Settings
Config::define('AUTOMATIC_UPDATER_DISABLED', true);
Config::define('DISABLE_WP_CRON', env('DISABLE_WP_CRON') ?: false);
// Disable the plugin and theme file editor in the admin
Config::define('DISALLOW_FILE_EDIT', true);
// Disable plugin and theme updates and installation from the admin
Config::define('DISALLOW_FILE_MODS', true);
// Limit the number of post revisions that Wordpress stores (true (default WP): store every revision)
Config::define('WP_POST_REVISIONS', env('WP_POST_REVISIONS') ?: true);

// Theme settings
Config::define('WPLANG', 'nl_NL');
Config::define('WP_DEFAULT_THEME', 'proudnerds');
Config::define('THEME_LANGUAGE_DOMAIN', 'proudnerds');
Config::define('USE_GUTENBERG', true);

// Performance settings
Config::define('ENFORCE_GZIP', !Config::get('WP_LOCAL_DEV'));

if (!defined('WP_CACHE')) {
    Config::define('WP_CACHE', !Config::get('WP_LOCAL_DEV'));
}

Config::define('COMPRESS_SCRIPTS', false);
Config::define('COMPRESS_CSS', false);
Config::define('DISABLE_WP_CRON', false);
Config::define('WP_MEMORY_LIMIT', '256M');

// Security settings
Config::define('WP_AUTO_UPDATE_CORE', false);
Config::define('FORCE_SSL_ADMIN', true);
Config::define('WP_ALLOW_REPAIR', false);
Config::define('CORE_UPGRADE_SKIP_NEW_BUNDLED', true);
Config::define('CONCATENATE_SCRIPTS', true);
Config::define('DISALLOW_UNFILTERED_HTML', true);

// Debugging Settings
Config::define('WP_DEBUG_DISPLAY', false);
Config::define('WP_DEBUG_LOG', false);
Config::define('SCRIPT_DEBUG', false);
ini_set('display_errors', '0');

/*
 * Allow WordPress to detect HTTPS when used behind a reverse proxy or a load balancer
 * See https://codex.wordpress.org/Function_Reference/is_ssl#Notes
 */
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) {
    $_SERVER['HTTPS'] = 'on';
}

$env_config = __DIR__ . '/environments/' . WP_ENV . '.php';

if (file_exists($env_config)) {
    require_once $env_config;
}

if (!file_exists(__DIR__ . '/salts.php')) {
    $salts_url = 'https://api.wordpress.org/secret-key/1.1/salt/';

    $file_contents = file_get_contents($salts_url);

    if (false === $file_contents) {
        throw new Exception('Error: Couldn\'t load salts URL');
    }

    $file_contents = str_replace('define', 'Config::define', $file_contents);

    $salts_file = <<<PHP
    <?php
    // Generated in ./application.php
    use Roots\\WPConfig\\Config;

    {$file_contents}
    PHP;

    file_put_contents(__DIR__ . '/salts.php', $salts_file);
}

require_once __DIR__ . '/salts.php';

Config::apply();

// Bootstrap WordPress
if (!defined('ABSPATH')) {
    define('ABSPATH', $webroot_dir . '/wp/');
}
