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 <?php
declare(strict_types=1); declare(strict_types=1);
return [ return [
'driver' => 's3', 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 's3',
'visibility' => 'public', 'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public',
'stream' => [ 'stream' => [
'protocol' => 'flysystem', 'protocol' => getenv('FLYSYSTEM_OFFLOAD_STREAM_PROTOCOL') ?: 'flysystem',
'root_prefix' => '', 'root_prefix' => getenv('FLYSYSTEM_OFFLOAD_STREAM_ROOT_PREFIX') ?: '',
'host' => 'uploads', 'host' => getenv('FLYSYSTEM_OFFLOAD_STREAM_HOST') ?: 'uploads',
], ],
'uploads' => [ 'uploads' => [
'base_url' => getenv('FLYSYSTEM_OFFLOAD_BASE_URL') ?: 'https://your-bucket.s3.amazonaws.com', 'base_url' => getenv('FLYSYSTEM_OFFLOAD_BASE_URL') ?: 'https://your-bucket.s3.amazonaws.com',
'delete_remote' => true, 'delete_remote' => filter_var(
'prefer_local_for_missing' => false, 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' => [ 'admin' => [
'enabled' => false, 'enabled' => filter_var(
getenv('FLYSYSTEM_OFFLOAD_ADMIN_ENABLED') ?: 'false',
FILTER_VALIDATE_BOOLEAN
),
], ],
's3' => [ 's3' => [
@ -29,8 +39,39 @@ return [
'bucket' => getenv('FLYSYSTEM_OFFLOAD_BUCKET') ?: 'your-bucket-name', 'bucket' => getenv('FLYSYSTEM_OFFLOAD_BUCKET') ?: 'your-bucket-name',
'prefix' => getenv('FLYSYSTEM_OFFLOAD_PREFIX') ?: null, 'prefix' => getenv('FLYSYSTEM_OFFLOAD_PREFIX') ?: null,
'endpoint' => getenv('FLYSYSTEM_OFFLOAD_ENDPOINT') ?: 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', 'version' => 'latest',
'options' => [], '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); declare(strict_types=1);
return [ return [
'driver' => getenv('FLYSYSTEM_OFFLOAD_DRIVER') ?: 's3', 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 's3',
'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public', 'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public',
'stream' => [ 'stream' => [

View File

@ -1,58 +1,101 @@
<?php <?php
/** /**
* Plugin Name: Flysystem Offload * Plugin Name: Flysystem Offload
* Plugin URI: https://git.brasdrive.com.br/Brasdrive/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. * Description: Universal storage offloading for WordPress via Flysystem
* Version: 3.0.0 * Version: 3.0.0
* Author: Brasdrive * Requires at least: 6.0
* License: GPLv2 or later * 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 * Text Domain: flysystem-offload
*/ */
declare(strict_types=1); declare(strict_types=1);
if (! defined('ABSPATH')) { // Evitar acceso directo
if (!defined('ABSPATH')) {
exit; exit;
} }
if (! defined('FLYSYSTEM_OFFLOAD_PATH')) { // Definir constantes del plugin
define('FLYSYSTEM_OFFLOAD_PATH', __DIR__); define('FLYSYSTEM_OFFLOAD_VERSION', '3.0.0');
} define('FLYSYSTEM_OFFLOAD_PLUGIN_FILE', __FILE__);
if (! defined('FLYSYSTEM_OFFLOAD_FILE')) { define('FLYSYSTEM_OFFLOAD_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('FLYSYSTEM_OFFLOAD_FILE', __FILE__); define('FLYSYSTEM_OFFLOAD_PLUGIN_URL', plugin_dir_url(__FILE__));
}
if (! defined('FLYSYSTEM_OFFLOAD_CONFIG_PATH')) { // Definir ruta de configuración (buscar en config/ del plugin)
define('FLYSYSTEM_OFFLOAD_CONFIG_PATH', FLYSYSTEM_OFFLOAD_PATH . '/config'); if (!defined('FLYSYSTEM_OFFLOAD_CONFIG_PATH')) {
define('FLYSYSTEM_OFFLOAD_CONFIG_PATH', FLYSYSTEM_OFFLOAD_PLUGIN_DIR . 'config');
} }
$autoload = __DIR__ . '/vendor/autoload.php'; // Cargar autoloader de Composer
if (file_exists($autoload)) { $autoloader = FLYSYSTEM_OFFLOAD_PLUGIN_DIR . 'vendor/autoload.php';
require_once $autoload;
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; require_once $autoloader;
// 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;
}
// Inicializar el plugin cuando WordPress esté listo
add_action('plugins_loaded', function (): void {
try { try {
Plugin::bootstrap(); FlysystemOffload\Plugin::bootstrap();
} catch (Throwable $exception) { } catch (Throwable $e) {
error_log('[Flysystem Offload] Error al iniciar el plugin: ' . $exception->getMessage()); error_log('[Flysystem Offload] Error al iniciar el plugin: ' . $e->getMessage());
add_action('admin_notices', static function () use ($exception): void { // Mostrar error solo a administradores
if (! current_user_can('manage_options')) { if (is_admin() && current_user_can('manage_options')) {
return; add_action('admin_notices', function () use ($e): void {
} printf(
'<div class="notice notice-error"><p><strong>Flysystem Offload:</strong> %s</p></div>',
printf( esc_html($e->getMessage())
'<div class="notice notice-error"><p><strong>Flysystem Offload:</strong> %s</p></div>', );
esc_html($exception->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 = []; $config = [];
// Convertir 'driver' a 'provider' // Extraer 'provider'
if (isset($rawConfig['driver'])) { if (isset($rawConfig['provider'])) {
$config['provider'] = $rawConfig['driver']; $config['provider'] = $rawConfig['provider'];
} }
// Extraer prefijo global si existe // Extraer prefijo global si existe

View File

@ -46,7 +46,7 @@ class FilesystemFactory
*/ */
public static function create(array $config): Filesystem public static function create(array $config): Filesystem
{ {
$provider = $config['driver'] ?? ''; $provider = $config['provider'] ?? '';
if (empty($provider)) { if (empty($provider)) {
throw new InvalidArgumentException('Provider is required in configuration'); throw new InvalidArgumentException('Provider is required in configuration');

View File

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