This commit is contained in:
Javier Casares 2026-07-18 13:29:38 +00:00
commit 916d7845be
91 changed files with 6861 additions and 6907 deletions

View file

@ -16,15 +16,15 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Configuration validator and accessor for iDrivee2 constants.
*
* Validates and provides access to the five required configuration constants
* and the optional CDN domain constant. Configuration can come from either
* wp-config.php constants (priority) or WordPress options.
*
* @since 0.3.0
*/
/**
* Configuration validator and accessor for iDrivee2 constants.
*
* Validates and provides access to the five required configuration constants
* and the optional CDN domain constant. Configuration can come from either
* wp-config.php constants (priority) or WordPress options.
*
* @since 0.3.0
*/
class Config {
/**
* Required configuration keys.
@ -46,6 +46,25 @@ class Config {
*/
private const OPTION_NAME = 'idrivee2_media_settings';
/**
* Map of config keys to wp-config.php constant names.
*
* Centralised so is_defined_in_wp_config() and get_value() cannot drift.
*
* @since 1.3.0
*
* @var array<string, string>
*/
private const CONSTANT_MAP = array(
'host' => 'IDRIVEE2_MEDIA_HOST',
'key' => 'IDRIVEE2_MEDIA_KEY',
'secret' => 'IDRIVEE2_MEDIA_SECRET',
'bucket' => 'IDRIVEE2_MEDIA_BUCKET',
'region' => 'IDRIVEE2_MEDIA_REGION',
'domain' => 'IDRIVEE2_MEDIA_DOMAIN',
'subsizes_mode' => 'IDRIVEE2_MEDIA_SUBSIZES_MODE',
);
/**
* Logger instance.
*
@ -92,17 +111,8 @@ class Config {
* @return bool True if defined in wp-config.php, false otherwise.
*/
public function is_defined_in_wp_config( string $key ): bool {
$constant_map = array(
'host' => 'IDRIVEE2_MEDIA_HOST',
'key' => 'IDRIVEE2_MEDIA_KEY',
'secret' => 'IDRIVEE2_MEDIA_SECRET',
'bucket' => 'IDRIVEE2_MEDIA_BUCKET',
'region' => 'IDRIVEE2_MEDIA_REGION',
'domain' => 'IDRIVEE2_MEDIA_DOMAIN',
);
$constant_name = $constant_map[ $key ] ?? '';
return $constant_name && defined( $constant_name );
$constant_name = self::CONSTANT_MAP[ $key ] ?? '';
return '' !== $constant_name && defined( $constant_name );
}
/**
@ -116,18 +126,9 @@ class Config {
* @return string The configuration value, or empty string if not set.
*/
private function get_value( string $key ): string {
$constant_map = array(
'host' => 'IDRIVEE2_MEDIA_HOST',
'key' => 'IDRIVEE2_MEDIA_KEY',
'secret' => 'IDRIVEE2_MEDIA_SECRET',
'bucket' => 'IDRIVEE2_MEDIA_BUCKET',
'region' => 'IDRIVEE2_MEDIA_REGION',
'domain' => 'IDRIVEE2_MEDIA_DOMAIN',
);
// Check wp-config.php constant first (highest priority).
$constant_name = $constant_map[ $key ] ?? '';
if ( $constant_name && defined( $constant_name ) ) {
$constant_name = self::CONSTANT_MAP[ $key ] ?? '';
if ( '' !== $constant_name && defined( $constant_name ) ) {
return (string) constant( $constant_name );
}
@ -216,6 +217,29 @@ class Config {
return ! empty( $this->get_domain() );
}
/**
* Get the configured sub-sizes mode.
*
* Controls which image sub-sizes WordPress generates. Returns one of
* 'all', 'thumbnail', or 'none'; falls back to 'all' for any value that
* is not on the whitelist (including unset/empty).
*
* @since 1.3.0
*
* @return string The sub-sizes mode.
*/
public function get_subsizes_mode(): string {
$value = $this->get_value( 'subsizes_mode' );
$allowed = array(
'all',
'thumbnail',
'none',
);
return in_array( $value, $allowed, true ) ? $value : 'all';
}
/**
* Update configuration options in WordPress database.
*
@ -239,6 +263,11 @@ class Config {
'domain' => isset( $data['domain'] ) ? sanitize_text_field( $data['domain'] ) : '',
);
// Sub-sizes mode: strict whitelist, default 'all'.
$raw_mode = isset( $data['subsizes_mode'] ) ? strtolower( (string) sanitize_text_field( $data['subsizes_mode'] ) ) : '';
$allowed_modes = array( 'all', 'thumbnail', 'none' );
$options['subsizes_mode'] = in_array( $raw_mode, $allowed_modes, true ) ? $raw_mode : 'all';
// Log configuration changes.
if ( $this->logger ) {
foreach ( $options as $key => $new_value ) {