Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@retlehs
Copy link
Member

@retlehs retlehs commented Feb 16, 2025

Replace the static Config class with a new fluent API for wp-config v2. This new API provides a more expressive and maintainable way to manage WordPress configuration while maintaining the same strong safety guarantees of v1.

Key changes:

  • Introduce instance-based Config class with fluent methods
  • Add when() helper for conditional configuration
  • Add env() helper for setting environment variables as constants
  • Add WordPress-style hook system for extensible configuration
  • Include built-in environment variable loading
  • Remove need for separate environment config files
  • Bump minimum PHP version to 8.1

Breaking changes:

  • Static Config methods are removed
  • Config directory structure is simplified

New config file example

<?php

use function Env\env;
use Roots\WPConfig\Config;

$rootDir = dirname(__DIR__);
$webrootDir = $rootDir . '/web';
$config = new Config($rootDir);
$config->bootstrapEnv()
       ->do_action('config_loaded');

$config
    /**
     * DB settings
     */
    ->env(['DB_NAME', 'DB_USER', 'DB_PASSWORD'])
    ->env('DB_HOST', 'localhost')
    ->when(env('DB_SSL'), fn ($config) => $config->set('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL))
    ->set([
        'DB_CHARSET' => 'utf8mb4',
        'DB_COLLATE' => '',
    ])
    ->do_action('database_configured')

    /**
     * URLs
     */
    ->env('WP_HOME')
    ->env('WP_SITEURL', $config->get('WP_HOME') . '/wp')
    ->do_action('urls_configured')

    /**
     * Environment
     */
    ->env('WP_ENV', 'production')
    ->env('WP_ENVIRONMENT_TYPE', $config->get('WP_ENV'))
    ->do_action('environment_loaded')

    /**
     * Content directory
     */
    ->set([
        'CONTENT_DIR' => '/app',
        'WP_CONTENT_DIR' => $webrootDir . '/app',
        'WP_CONTENT_URL' => $config->get('WP_HOME') . '/app',
    ])

    /**
     * Authentication unique keys and salts
     */
    ->env([
        'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY',
        'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT',
    ])

    /**
     * Custom settings
     */
    ->set('AUTOMATIC_UPDATER_DISABLED', true)
    ->env('DISABLE_WP_CRON', false)
    ->set('DISALLOW_FILE_EDIT', true)
    ->env('DISALLOW_FILE_MODS', true)
    ->env('WP_POST_REVISIONS', true)

    /**
     * Performance settings
     */
    ->set('CONCATENATE_SCRIPTS', false)

    /**
     * Default debug settings
     */
    ->set('WP_DEBUG', (bool) env('WP_DEBUG', false))
    ->set('WP_DEBUG_DISPLAY', false)
    ->set('WP_DEBUG_LOG', false)
    ->set('SCRIPT_DEBUG', false)

    /**
     * Development settings
     */
    ->when($config->get('WP_ENV') === 'development', function($config) {
        $config->env('WP_DEBUG_LOG', true)->set([
            'SAVEQUERIES' => true,
            'WP_DEBUG' => true,
            'WP_DEBUG_DISPLAY' => true,
            'WP_DISABLE_FATAL_ERROR_HANDLER' => true,
            'SCRIPT_DEBUG' => true,
            'DISALLOW_INDEXING' => true,
            'DISALLOW_FILE_MODS' => false,
        ]);
    })

    /**
     * Handle reverse proxy settings
     */
    ->when(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https', function() {
        $_SERVER['HTTPS'] = 'on';
    })

    ->do_action('before_apply')
    ->apply();

$config->do_action('after_apply');

$table_prefix = env('DB_PREFIX') ?: 'wp_';

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

require_once ABSPATH . 'wp-settings.php';

@retlehs retlehs self-assigned this Feb 16, 2025
@retlehs retlehs changed the title 💥 Introduce Fluent config API 💥 Introduce fluent config API Feb 16, 2025
@retlehs retlehs mentioned this pull request Feb 20, 2025
5 tasks
@rvola
Copy link

rvola commented Apr 16, 2025

Hello @retlehs ,

I noticed two errors in this code.
The first is related to the example; this causes an error.

/**
 * Development settings
 */
->when($config->get('WP_ENV') === 'development', function($config) {
	$config->env('WP_DEBUG_LOG', true)->set([
		'SAVEQUERIES' => true,
		'WP_DEBUG', true,
		'WP_DEBUG_DISPLAY', true,
		'WP_DISABLE_FATAL_ERROR_HANDLER', true,
		'SCRIPT_DEBUG', true,
		'DISALLOW_INDEXING', true,
		'DISALLOW_FILE_MODS', false,
	]);
})

replace by :

/**
 * Development settings
 */
->when($config->get('WP_ENV') === 'development', function($config) {
	$config->env('WP_DEBUG_LOG', true)->set([
		'SAVEQUERIES' => true,
		'WP_DEBUG' => true,
		'WP_DEBUG_DISPLAY' => true,
		'WP_DISABLE_FATAL_ERROR_HANDLER '=> true,
		'SCRIPT_DEBUG' => true,
		'DISALLOW_INDEXING' => true,
		'DISALLOW_FILE_MODS' => false,
	]);
})

The second may be a misunderstanding or a bug, but if I want to set a default value in my configuration that can be modified via the .env file, such as WP_MEMORY_LIMIT

wp-config/src/Config.php

Lines 97 to 102 in 4fec115

$value = match (true) {
function_exists('env') => env($key, $default),
function_exists('\Env\env') => \Env\env($key, $default),
default => getenv($key) ?? $_ENV[$key] ?? $default,
};

I will add the following to my configuration file:
$config->env('WP_MEMORY_LIMIT','256M')

But if I need to modify this value in an environment, I would add the following to my .env file:
WP_MEMORY_LIMIT='512M'

But in its current state, this value will never be read. WP_MEMORY_LIMIT will be NULL from the start.

@rvola
Copy link

rvola commented Apr 16, 2025

I have identified and proposed a correction regarding the normal operation of the $config->env() method
#23

@retlehs
Copy link
Member Author

retlehs commented Jul 26, 2025

@rvola Thanks, fixed with ee8e8cf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants