From d434c4d094c753aec66d86d5d869aec1b372077a Mon Sep 17 00:00:00 2001 From: Brasdrive Date: Tue, 4 Nov 2025 23:13:09 -0400 Subject: [PATCH] Atualizar src/Admin/SettingsPage.php --- src/Admin/SettingsPage.php | 562 +++++++++++++++++++------------------ 1 file changed, 291 insertions(+), 271 deletions(-) diff --git a/src/Admin/SettingsPage.php b/src/Admin/SettingsPage.php index e0da506..7ba3118 100644 --- a/src/Admin/SettingsPage.php +++ b/src/Admin/SettingsPage.php @@ -1,271 +1,291 @@ -pluginFile = $pluginFile; - } - - public function boot(): void - { - add_action('admin_menu', [$this, 'registerPage']); - add_action('admin_init', [$this, 'registerSettings']); - add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); - } - - public function enqueueAssets(): void - { - $screen = get_current_screen(); - if (! $screen || $screen->id !== 'settings_page_flysystem-offload') { - return; - } - - wp_enqueue_script( - 'flysystem-offload-settings', - plugins_url('assets/admin-settings.js', $this->pluginFile), - ['jquery'], - '0.1.0', - true - ); - } - - public function registerPage(): void - { - add_options_page( - __('Flysystem Offload', 'flysystem-offload'), - __('Flysystem Offload', 'flysystem-offload'), - 'manage_options', - 'flysystem-offload', - [$this, 'render'] - ); - } - - public function registerSettings(): void - { - register_setting( - 'flysystem_offload', - 'flysystem_offload_settings', - [ - 'sanitize_callback' => [$this, 'sanitizeSettings'], - ] - ); - - add_settings_section( - 'flysystem_offload_general', - __('Configuración general', 'flysystem-offload'), - '__return_false', - 'flysystem-offload' - ); - - add_settings_field( - 'adapter', - __('Adaptador activo', 'flysystem-offload'), - [$this, 'renderAdapterField'], - 'flysystem-offload', - 'flysystem_offload_general' - ); - - add_settings_field( - 'base_prefix', - __('Prefijo de almacenamiento', 'flysystem-offload'), - [$this, 'renderBasePrefixField'], - 'flysystem-offload', - 'flysystem_offload_general' - ); - - add_settings_section( - 'flysystem_offload_s3', - __('Amazon S3 / Compatible', 'flysystem-offload'), - function () { - echo '

' . esc_html__('Proporciona credenciales de un bucket S3 o compatible (MinIO, DigitalOcean Spaces, etc.).', 'flysystem-offload') . '

'; - }, - 'flysystem-offload' - ); - - add_settings_field( - 's3_access_key', - __('Access Key', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'access_key', 'type' => 'text'] - ); - - add_settings_field( - 's3_secret_key', - __('Secret Key', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'secret_key', 'type' => 'password'] - ); - - add_settings_field( - 's3_region', - __('Región', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'region', 'type' => 'text', 'placeholder' => 'us-east-1'] - ); - - add_settings_field( - 's3_bucket', - __('Bucket', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'bucket', 'type' => 'text'] - ); - - add_settings_field( - 's3_prefix', - __('Prefijo (opcional)', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'prefix', 'type' => 'text', 'placeholder' => 'uploads'] - ); - - add_settings_field( - 's3_endpoint', - __('Endpoint personalizado', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'endpoint', 'type' => 'text', 'placeholder' => 'https://nyc3.digitaloceanspaces.com'] - ); - - add_settings_field( - 's3_cdn_url', - __('URL CDN (opcional)', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'cdn_url', 'type' => 'text', 'placeholder' => 'https://cdn.midominio.com'] - ); - } - - public function sanitizeSettings(array $input): array - { - $current = get_option('flysystem_offload_settings', []); - - $adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local'); - $basePrefix = trim($input['base_prefix'] ?? ''); - - $s3 = $current['adapters']['s3'] ?? []; - $inputS3 = $input['adapters']['s3'] ?? []; - - $secretRaw = $inputS3['secret_key'] ?? ''; - $secret = $secretRaw === '' ? ($s3['secret_key'] ?? '') : $secretRaw; - - $sanitizedS3 = [ - 'access_key' => sanitize_text_field($inputS3['access_key'] ?? $s3['access_key'] ?? ''), - 'secret_key' => sanitize_text_field($inputS3['secret_key'] ?? $s3['secret_key'] ?? ''), - 'region' => sanitize_text_field($inputS3['region'] ?? $s3['region'] ?? ''), - 'bucket' => sanitize_text_field($inputS3['bucket'] ?? $s3['bucket'] ?? ''), - 'prefix' => trim($inputS3['prefix'] ?? $s3['prefix'] ?? ''), - 'endpoint' => esc_url_raw($inputS3['endpoint'] ?? $s3['endpoint'] ?? ''), - 'cdn_url' => esc_url_raw($inputS3['cdn_url'] ?? $s3['cdn_url'] ?? ''), - ]; - - $current['adapter'] = $adapter; - $current['base_prefix'] = $basePrefix; - $current['adapters']['s3'] = $sanitizedS3; - - return $current; - } - - public function render(): void - { - ?> -
-

-
- -
-
- __('Local (fallback)', 'flysystem-offload'), - 's3' => 'Amazon S3 / Compatible', - 'sftp' => 'SFTP', - 'webdav' => 'WebDAV', - 'gcs' => 'Google Cloud Storage', - 'azure' => 'Azure Blob Storage', - 'googledrive' => 'Google Drive (beta)', - 'onedrive' => 'OneDrive (beta)', - 'dropbox' => 'Dropbox (beta)', - ]; - ?> - - - -

- -

- - - -

- -

- pluginFile = $pluginFile; + } + + public function boot(): void + { + add_action('admin_menu', [$this, 'registerPage']); + add_action('admin_init', [$this, 'registerSettings']); + add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); + } + + public function enqueueAssets(): void + { + $screen = get_current_screen(); + if (! $screen || $screen->id !== 'settings_page_flysystem-offload') { + return; + } + + wp_enqueue_script( + 'flysystem-offload-settings', + plugins_url('assets/admin-settings.js', $this->pluginFile), + ['jquery'], + '0.1.0', + true + ); + } + + public function registerPage(): void + { + add_options_page( + __('Flysystem Offload', 'flysystem-offload'), + __('Flysystem Offload', 'flysystem-offload'), + 'manage_options', + 'flysystem-offload', + [$this, 'render'] + ); + } + + public function registerSettings(): void + { + register_setting( + 'flysystem_offload', + 'flysystem_offload_settings', + [ + 'sanitize_callback' => [$this, 'sanitizeSettings'], + ] + ); + + add_settings_section( + 'flysystem_offload_general', + __('Configuración general', 'flysystem-offload'), + '__return_false', + 'flysystem-offload' + ); + + add_settings_field( + 'adapter', + __('Adaptador activo', 'flysystem-offload'), + [$this, 'renderAdapterField'], + 'flysystem-offload', + 'flysystem_offload_general' + ); + + add_settings_field( + 'base_prefix', + __('Prefijo de almacenamiento', 'flysystem-offload'), + [$this, 'renderBasePrefixField'], + 'flysystem-offload', + 'flysystem_offload_general' + ); + + add_settings_section( + 'flysystem_offload_s3', + __('Amazon S3 / Compatible', 'flysystem-offload'), + function () { + echo '

' . esc_html__('Proporciona credenciales de un bucket S3 o compatible (MinIO, DigitalOcean Spaces, etc.).', 'flysystem-offload') . '

'; + }, + 'flysystem-offload' + ); + + add_settings_field( + 's3_access_key', + __('Access Key', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'access_key', 'type' => 'text'] + ); + + add_settings_field( + 's3_secret_key', + __('Secret Key', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'secret_key', 'type' => 'password'] + ); + + add_settings_field( + 's3_region', + __('Región', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'region', 'type' => 'text', 'placeholder' => 'us-east-1'] + ); + + add_settings_field( + 's3_bucket', + __('Bucket', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'bucket', 'type' => 'text'] + ); + + add_settings_field( + 's3_prefix', + __('Prefijo (opcional)', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'prefix', 'type' => 'text', 'placeholder' => 'uploads'] + ); + + add_settings_field( + 's3_endpoint', + __('Endpoint personalizado', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'endpoint', 'type' => 'text', 'placeholder' => 'https://nyc3.digitaloceanspaces.com'] + ); + + add_settings_field( + 's3_cdn_url', + __('URL CDN (opcional)', 'flysystem-offload'), + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + ['key' => 'cdn_url', 'type' => 'text', 'placeholder' => 'https://cdn.midominio.com'] + ); + } + + public function sanitizeSettings(array $input): array + { + $current = get_option('flysystem_offload_settings', []); + + $adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local'); + $basePrefix = trim($input['base_prefix'] ?? ''); + + $s3 = $current['adapters']['s3'] ?? []; + $inputS3 = $input['adapters']['s3'] ?? []; + + $secretRaw = $inputS3['secret_key'] ?? ''; + $secret = $secretRaw === '' ? ($s3['secret_key'] ?? '') : $secretRaw; + + $sanitizedS3 = [ + 'access_key' => sanitize_text_field($inputS3['access_key'] ?? $s3['access_key'] ?? ''), + 'secret_key' => sanitize_text_field($inputS3['secret_key'] ?? $s3['secret_key'] ?? ''), + 'region' => sanitize_text_field($inputS3['region'] ?? $s3['region'] ?? ''), + 'bucket' => sanitize_text_field($inputS3['bucket'] ?? $s3['bucket'] ?? ''), + 'prefix' => trim($inputS3['prefix'] ?? $s3['prefix'] ?? ''), + 'endpoint' => esc_url_raw($inputS3['endpoint'] ?? $s3['endpoint'] ?? ''), + 'cdn_url' => esc_url_raw($inputS3['cdn_url'] ?? $s3['cdn_url'] ?? ''), + ]; + + $current['adapter'] = $adapter; + $current['base_prefix'] = $basePrefix; + $current['adapters']['s3'] = $sanitizedS3; + + return $current; + } + + public function render(): void + { + ?> +
+

+
+ generateSectionsMarkup(); + echo $sectionsHtml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + + submit_button(__('Guardar cambios', 'flysystem-offload')); + ?> +
+
+ ]*>\s*' . $label . '\s*<\/h2>\s*]*>.*?<\/table>)/is'; + + return preg_replace( + $pattern, + '
$1
', + $html + ); + } + + public function renderAdapterField(): void + { + $settings = get_option('flysystem_offload_settings', []); + $adapter = $settings['adapter'] ?? 'local'; + + $options = [ + 'local' => __('Local (fallback)', 'flysystem-offload'), + 's3' => 'Amazon S3 / Compatible', + 'sftp' => 'SFTP', + 'webdav' => 'WebDAV', + 'gcs' => 'Google Cloud Storage', + 'azure' => 'Azure Blob Storage', + 'googledrive' => 'Google Drive (beta)', + 'onedrive' => 'OneDrive (beta)', + 'dropbox' => 'Dropbox (beta)', + ]; + ?> + + + +

+ +

+ + + +

+ +

+