This commit is contained in:
Brasdrive 2025-11-09 20:51:02 -04:00
parent cb0316fab6
commit 0ab69f79c8
6 changed files with 140 additions and 56 deletions

View File

@ -1,24 +1,34 @@
<?php
declare(strict_types=1);
return [
'driver' => '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',
],
];

View File

@ -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' => [

View File

@ -1,58 +1,101 @@
<?php
/**
* Plugin Name: Flysystem Offload
* Plugin URI: https://git.brasdrive.com.br/Brasdrive/flysystem-offload
* Description: Reemplaza el filesystem local de WordPress con almacenamiento remoto transparente usando Flysystem v3.
* Version: 3.0.0
* Author: Brasdrive
* License: GPLv2 or later
* Plugin URI: https://git.brasdrive.com.br/Brasdrive/flysystem-offload
* Description: Universal storage offloading for WordPress via Flysystem
* Version: 3.0.0
* Requires at least: 6.0
* Requires PHP: 8.1
* Author: Brasdrive
* Author URI: https://brasdrive.com.br
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: flysystem-offload
*/
declare(strict_types=1);
if (! defined('ABSPATH')) {
// Evitar acceso directo
if (!defined('ABSPATH')) {
exit;
}
if (! defined('FLYSYSTEM_OFFLOAD_PATH')) {
define('FLYSYSTEM_OFFLOAD_PATH', __DIR__);
}
if (! defined('FLYSYSTEM_OFFLOAD_FILE')) {
define('FLYSYSTEM_OFFLOAD_FILE', __FILE__);
}
if (! defined('FLYSYSTEM_OFFLOAD_CONFIG_PATH')) {
define('FLYSYSTEM_OFFLOAD_CONFIG_PATH', FLYSYSTEM_OFFLOAD_PATH . '/config');
// Definir constantes del plugin
define('FLYSYSTEM_OFFLOAD_VERSION', '3.0.0');
define('FLYSYSTEM_OFFLOAD_PLUGIN_FILE', __FILE__);
define('FLYSYSTEM_OFFLOAD_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('FLYSYSTEM_OFFLOAD_PLUGIN_URL', plugin_dir_url(__FILE__));
// Definir ruta de configuración (buscar en config/ del plugin)
if (!defined('FLYSYSTEM_OFFLOAD_CONFIG_PATH')) {
define('FLYSYSTEM_OFFLOAD_CONFIG_PATH', FLYSYSTEM_OFFLOAD_PLUGIN_DIR . 'config');
}
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
// Cargar autoloader de Composer
$autoloader = FLYSYSTEM_OFFLOAD_PLUGIN_DIR . 'vendor/autoload.php';
if (!file_exists($autoloader)) {
add_action('admin_notices', function (): void {
printf(
'<div class="notice notice-error"><p><strong>Flysystem Offload:</strong> %s</p></div>',
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(
'<div class="notice notice-error"><p><strong>Flysystem Offload:</strong> %s</p></div>',
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(
'<div class="notice notice-error"><p><strong>Flysystem Offload:</strong> %s</p></div>',
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');
}
});

View File

@ -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

View File

@ -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');

View File

@ -45,8 +45,8 @@ final class SettingsPage {
<table class="widefat striped">
<tbody>
<tr>
<th scope="row"><?php esc_html_e('Driver', 'flysystem-offload'); ?></th>
<td><?php echo esc_html($this->config['driver'] ?? ''); ?></td>
<th scope="row"><?php esc_html_e('Provider', 'flysystem-offload'); ?></th>
<td><?php echo esc_html($this->config['provider'] ?? ''); ?></td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('Protocolo del Stream Wrapper', 'flysystem-offload'); ?></th>