diff --git a/config/flysystem-offload.example.php b/config/flysystem-offload.example.php index ed2c132..9e8ab33 100644 --- a/config/flysystem-offload.example.php +++ b/config/flysystem-offload.example.php @@ -1,24 +1,34 @@ 's3', - 'visibility' => 'public', + 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 's3', + 'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public', 'stream' => [ - 'protocol' => 'flysystem', - 'root_prefix' => '', - 'host' => 'uploads', + 'protocol' => getenv('FLYSYSTEM_OFFLOAD_STREAM_PROTOCOL') ?: 'flysystem', + 'root_prefix' => getenv('FLYSYSTEM_OFFLOAD_STREAM_ROOT_PREFIX') ?: '', + 'host' => getenv('FLYSYSTEM_OFFLOAD_STREAM_HOST') ?: 'uploads', ], 'uploads' => [ 'base_url' => getenv('FLYSYSTEM_OFFLOAD_BASE_URL') ?: 'https://your-bucket.s3.amazonaws.com', - 'delete_remote' => true, - 'prefer_local_for_missing' => false, + 'delete_remote' => filter_var( + getenv('FLYSYSTEM_OFFLOAD_DELETE_REMOTE') ?: 'true', + FILTER_VALIDATE_BOOLEAN + ), + 'prefer_local_for_missing' => filter_var( + getenv('FLYSYSTEM_OFFLOAD_PREFER_LOCAL_FOR_MISSING') ?: 'false', + FILTER_VALIDATE_BOOLEAN + ), ], 'admin' => [ - 'enabled' => false, + 'enabled' => filter_var( + getenv('FLYSYSTEM_OFFLOAD_ADMIN_ENABLED') ?: 'false', + FILTER_VALIDATE_BOOLEAN + ), ], 's3' => [ @@ -29,8 +39,39 @@ return [ 'bucket' => getenv('FLYSYSTEM_OFFLOAD_BUCKET') ?: 'your-bucket-name', 'prefix' => getenv('FLYSYSTEM_OFFLOAD_PREFIX') ?: null, 'endpoint' => getenv('FLYSYSTEM_OFFLOAD_ENDPOINT') ?: null, - 'use_path_style_endpoint' => (bool) (getenv('AWS_USE_PATH_STYLE_ENDPOINT') ?: false), + 'use_path_style_endpoint' => filter_var( + getenv('AWS_USE_PATH_STYLE_ENDPOINT') ?: 'false', + FILTER_VALIDATE_BOOLEAN + ), 'version' => 'latest', 'options' => [], ], + + 'webdav' => [ + 'enabled' => filter_var( + getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENABLED') ?: 'false', + FILTER_VALIDATE_BOOLEAN + ), + 'base_url' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL') ?: '', + 'endpoint' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENDPOINT') ?: '', + 'prefix' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_PREFIX') ?: 'wordpress/uploads', + 'credentials' => [ + 'username' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_USERNAME') ?: '', + 'password' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_PASSWORD') ?: '', + 'auth_type' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_AUTH_TYPE') ?: null, + ], + 'stream' => [ + 'register' => filter_var( + getenv('FLYSYSTEM_OFFLOAD_WEBDAV_STREAM_REGISTER') ?: 'true', + FILTER_VALIDATE_BOOLEAN + ), + 'protocol' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_STREAM_PROTOCOL') ?: 'webdav', + ], + 'default_headers' => [ + 'Cache-Control' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_CACHE_CONTROL') ?: 'public, max-age=31536000', + 'Expires' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_EXPIRES') + ?: gmdate('D, d M Y H:i:s \G\M\T', strtotime('+1 year')), + ], + 'default_visibility' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_VISIBILITY') ?: 'private', + ], ]; diff --git a/config/flysystem-offload.php b/config/flysystem-offload.php index 2c5ba70..9e8ab33 100644 --- a/config/flysystem-offload.php +++ b/config/flysystem-offload.php @@ -3,7 +3,7 @@ declare(strict_types=1); return [ - 'driver' => getenv('FLYSYSTEM_OFFLOAD_DRIVER') ?: 's3', + 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 's3', 'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public', 'stream' => [ diff --git a/flysystem-offload.php b/flysystem-offload.php index 0bcd768..a3640a5 100644 --- a/flysystem-offload.php +++ b/flysystem-offload.php @@ -1,58 +1,101 @@

Flysystem Offload: %s

', + esc_html__('Composer dependencies not installed. Please run: composer install', 'flysystem-offload') + ); + }); + return; } -use FlysystemOffload\Plugin; -// use Throwable; - -add_action('plugins_loaded', static function (): void { - if (! class_exists(Plugin::class)) { - error_log('[Flysystem Offload] No fue posible cargar la clase principal del plugin.'); - return; - } +require_once $autoloader; +// Inicializar el plugin cuando WordPress esté listo +add_action('plugins_loaded', function (): void { try { - Plugin::bootstrap(); - } catch (Throwable $exception) { - error_log('[Flysystem Offload] Error al iniciar el plugin: ' . $exception->getMessage()); - - add_action('admin_notices', static function () use ($exception): void { - if (! current_user_can('manage_options')) { - return; - } - - printf( - '

Flysystem Offload: %s

', - esc_html($exception->getMessage()) - ); - }); + FlysystemOffload\Plugin::bootstrap(); + } catch (Throwable $e) { + error_log('[Flysystem Offload] Error al iniciar el plugin: ' . $e->getMessage()); + + // Mostrar error solo a administradores + if (is_admin() && current_user_can('manage_options')) { + add_action('admin_notices', function () use ($e): void { + printf( + '

Flysystem Offload: %s

', + esc_html($e->getMessage()) + ); + }); + } } -}, 0); +}, 10); + +// Hook de activación +register_activation_hook(__FILE__, function (): void { + // Verificar requisitos + if (version_compare(PHP_VERSION, '8.1', '<')) { + deactivate_plugins(plugin_basename(__FILE__)); + wp_die( + esc_html__('Flysystem Offload requires PHP 8.1 or higher.', 'flysystem-offload'), + esc_html__('Plugin Activation Error', 'flysystem-offload'), + ['back_link' => true] + ); + } + + // Crear directorio de configuración si no existe + $configDir = FLYSYSTEM_OFFLOAD_CONFIG_PATH; + if (!file_exists($configDir)) { + wp_mkdir_p($configDir); + } + + // Copiar archivo de ejemplo si no existe configuración + $configFile = $configDir . '/flysystem-offload.php'; + $exampleFile = $configDir . '/flysystem-offload.example.php'; + + if (!file_exists($configFile) && file_exists($exampleFile)) { + copy($exampleFile, $configFile); + } +}); + +// Hook de desactivación +register_deactivation_hook(__FILE__, function (): void { + // Desregistrar stream wrapper si existe + if (in_array('fly', stream_get_wrappers(), true)) { + stream_wrapper_unregister('fly'); + } +}); diff --git a/src/Config/ConfigLoader.php b/src/Config/ConfigLoader.php index 91573b8..f64b001 100644 --- a/src/Config/ConfigLoader.php +++ b/src/Config/ConfigLoader.php @@ -73,9 +73,9 @@ class ConfigLoader { $config = []; - // Convertir 'driver' a 'provider' - if (isset($rawConfig['driver'])) { - $config['provider'] = $rawConfig['driver']; + // Extraer 'provider' + if (isset($rawConfig['provider'])) { + $config['provider'] = $rawConfig['provider']; } // Extraer prefijo global si existe diff --git a/src/Filesystem/FilesystemFactory.php b/src/Filesystem/FilesystemFactory.php index 495b399..1d786d3 100644 --- a/src/Filesystem/FilesystemFactory.php +++ b/src/Filesystem/FilesystemFactory.php @@ -46,7 +46,7 @@ class FilesystemFactory */ public static function create(array $config): Filesystem { - $provider = $config['driver'] ?? ''; + $provider = $config['provider'] ?? ''; if (empty($provider)) { throw new InvalidArgumentException('Provider is required in configuration'); diff --git a/src/Settings/SettingsPage.php b/src/Settings/SettingsPage.php index 07eb4c5..cef0a9a 100644 --- a/src/Settings/SettingsPage.php +++ b/src/Settings/SettingsPage.php @@ -45,8 +45,8 @@ final class SettingsPage { - - + +
config['driver'] ?? ''); ?>config['provider'] ?? ''); ?>