diff --git a/src/Admin/SettingsPage.php b/src/Admin/SettingsPage.php index 7ba3118..5fc001b 100644 --- a/src/Admin/SettingsPage.php +++ b/src/Admin/SettingsPage.php @@ -17,22 +17,6 @@ class SettingsPage 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( @@ -86,96 +70,118 @@ class SettingsPage 'flysystem-offload' ); - add_settings_field( - 's3_access_key', - __('Access Key', 'flysystem-offload'), - [$this, 'renderS3Field'], - 'flysystem-offload', - 'flysystem_offload_s3', - ['key' => 'access_key', 'type' => 'text'] + $this->registerS3Fields(); + } + + private function registerS3Fields(): void + { + $fields = [ + [ + 'id' => 's3_access_key', + 'label' => __('Access Key', 'flysystem-offload'), + 'args' => ['key' => 'access_key', 'type' => 'text'], + ], + [ + 'id' => 's3_secret_key', + 'label' => __('Secret Key', 'flysystem-offload'), + 'args' => ['key' => 'secret_key', 'type' => 'password'], + ], + [ + 'id' => 's3_region', + 'label' => __('Región', 'flysystem-offload'), + 'args' => ['key' => 'region', 'type' => 'text', 'placeholder' => 'us-east-1'], + ], + [ + 'id' => 's3_bucket', + 'label' => __('Bucket', 'flysystem-offload'), + 'args' => ['key' => 'bucket', 'type' => 'text'], + ], + [ + 'id' => 's3_prefix', + 'label' => __('Prefijo (opcional)', 'flysystem-offload'), + 'args' => ['key' => 'prefix', 'type' => 'text', 'placeholder' => 'uploads'], + ], + [ + 'id' => 's3_endpoint', + 'label' => __('Endpoint personalizado', 'flysystem-offload'), + 'args' => ['key' => 'endpoint', 'type' => 'text', 'placeholder' => 'https://nyc3.digitaloceanspaces.com'], + ], + [ + 'id' => 's3_cdn_url', + 'label' => __('URL CDN (opcional)', 'flysystem-offload'), + 'args' => ['key' => 'cdn_url', 'type' => 'text', 'placeholder' => 'https://cdn.midominio.com'], + ], + ]; + + foreach ($fields as $field) { + $args = array_merge( + $field['args'], + ['class' => 'flysystem-offload-field flysystem-offload-field--s3'] + ); + + add_settings_field( + $field['id'], + $field['label'], + [$this, 'renderS3Field'], + 'flysystem-offload', + 'flysystem_offload_s3', + $args + ); + } + } + + 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.1', + true ); - 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'] + wp_localize_script( + 'flysystem-offload-settings', + 'flysystemOffloadSettings', + [ + 'labels' => [ + 's3' => __('Amazon S3 / Compatible', 'flysystem-offload'), + ], + ] ); } public function sanitizeSettings(array $input): array { - $current = get_option('flysystem_offload_settings', []); - - $adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local'); + $current = get_option('flysystem_offload_settings', []); + $adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local'); $basePrefix = trim($input['base_prefix'] ?? ''); - $s3 = $current['adapters']['s3'] ?? []; + $s3 = $current['adapters']['s3'] ?? []; $inputS3 = $input['adapters']['s3'] ?? []; $secretRaw = $inputS3['secret_key'] ?? ''; - $secret = $secretRaw === '' ? ($s3['secret_key'] ?? '') : $secretRaw; + $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'] ?? ''), + 'secret_key' => sanitize_text_field($secret), + '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; + $current['adapter'] = $adapter; + $current['base_prefix'] = $basePrefix; + $current['adapters']['s3'] = $sanitizedS3; return $current; } @@ -188,10 +194,7 @@ class SettingsPage
generateSectionsMarkup(); - echo $sectionsHtml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - + do_settings_sections('flysystem-offload'); submit_button(__('Guardar cambios', 'flysystem-offload')); ?>
@@ -199,42 +202,25 @@ class SettingsPage ]*>\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'; + $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)', + 'local' => __('Local (fallback)', 'flysystem-offload'), + 's3' => __('Amazon S3 / Compatible', 'flysystem-offload'), + 'sftp' => 'SFTP', + 'webdav' => 'WebDAV', + 'gcs' => 'Google Cloud Storage', + 'azure' => 'Azure Blob Storage', + 'googledrive'=> 'Google Drive (beta)', + 'onedrive' => 'OneDrive (beta)', + 'dropbox' => 'Dropbox (beta)', ]; ?> - +