Atualizar src/Admin/SettingsPage.php

This commit is contained in:
Brasdrive 2025-11-04 23:13:09 -04:00
parent 192ef016e6
commit d434c4d094
1 changed files with 291 additions and 271 deletions

View File

@ -1,271 +1,291 @@
<?php <?php
namespace FlysystemOffload\Admin; namespace FlysystemOffload\Admin;
class SettingsPage class SettingsPage
{ {
private string $pluginFile; private string $pluginFile;
public function __construct(string $pluginFile) public function __construct(string $pluginFile)
{ {
$this->pluginFile = $pluginFile; $this->pluginFile = $pluginFile;
} }
public function boot(): void public function boot(): void
{ {
add_action('admin_menu', [$this, 'registerPage']); add_action('admin_menu', [$this, 'registerPage']);
add_action('admin_init', [$this, 'registerSettings']); add_action('admin_init', [$this, 'registerSettings']);
add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
} }
public function enqueueAssets(): void public function enqueueAssets(): void
{ {
$screen = get_current_screen(); $screen = get_current_screen();
if (! $screen || $screen->id !== 'settings_page_flysystem-offload') { if (! $screen || $screen->id !== 'settings_page_flysystem-offload') {
return; return;
} }
wp_enqueue_script( wp_enqueue_script(
'flysystem-offload-settings', 'flysystem-offload-settings',
plugins_url('assets/admin-settings.js', $this->pluginFile), plugins_url('assets/admin-settings.js', $this->pluginFile),
['jquery'], ['jquery'],
'0.1.0', '0.1.0',
true true
); );
} }
public function registerPage(): void public function registerPage(): void
{ {
add_options_page( add_options_page(
__('Flysystem Offload', 'flysystem-offload'), __('Flysystem Offload', 'flysystem-offload'),
__('Flysystem Offload', 'flysystem-offload'), __('Flysystem Offload', 'flysystem-offload'),
'manage_options', 'manage_options',
'flysystem-offload', 'flysystem-offload',
[$this, 'render'] [$this, 'render']
); );
} }
public function registerSettings(): void public function registerSettings(): void
{ {
register_setting( register_setting(
'flysystem_offload', 'flysystem_offload',
'flysystem_offload_settings', 'flysystem_offload_settings',
[ [
'sanitize_callback' => [$this, 'sanitizeSettings'], 'sanitize_callback' => [$this, 'sanitizeSettings'],
] ]
); );
add_settings_section( add_settings_section(
'flysystem_offload_general', 'flysystem_offload_general',
__('Configuración general', 'flysystem-offload'), __('Configuración general', 'flysystem-offload'),
'__return_false', '__return_false',
'flysystem-offload' 'flysystem-offload'
); );
add_settings_field( add_settings_field(
'adapter', 'adapter',
__('Adaptador activo', 'flysystem-offload'), __('Adaptador activo', 'flysystem-offload'),
[$this, 'renderAdapterField'], [$this, 'renderAdapterField'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_general' 'flysystem_offload_general'
); );
add_settings_field( add_settings_field(
'base_prefix', 'base_prefix',
__('Prefijo de almacenamiento', 'flysystem-offload'), __('Prefijo de almacenamiento', 'flysystem-offload'),
[$this, 'renderBasePrefixField'], [$this, 'renderBasePrefixField'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_general' 'flysystem_offload_general'
); );
add_settings_section( add_settings_section(
'flysystem_offload_s3', 'flysystem_offload_s3',
__('Amazon S3 / Compatible', 'flysystem-offload'), __('Amazon S3 / Compatible', 'flysystem-offload'),
function () { function () {
echo '<p>' . esc_html__('Proporciona credenciales de un bucket S3 o compatible (MinIO, DigitalOcean Spaces, etc.).', 'flysystem-offload') . '</p>'; echo '<p>' . esc_html__('Proporciona credenciales de un bucket S3 o compatible (MinIO, DigitalOcean Spaces, etc.).', 'flysystem-offload') . '</p>';
}, },
'flysystem-offload' 'flysystem-offload'
); );
add_settings_field( add_settings_field(
's3_access_key', 's3_access_key',
__('Access Key', 'flysystem-offload'), __('Access Key', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'access_key', 'type' => 'text'] ['key' => 'access_key', 'type' => 'text']
); );
add_settings_field( add_settings_field(
's3_secret_key', 's3_secret_key',
__('Secret Key', 'flysystem-offload'), __('Secret Key', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'secret_key', 'type' => 'password'] ['key' => 'secret_key', 'type' => 'password']
); );
add_settings_field( add_settings_field(
's3_region', 's3_region',
__('Región', 'flysystem-offload'), __('Región', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'region', 'type' => 'text', 'placeholder' => 'us-east-1'] ['key' => 'region', 'type' => 'text', 'placeholder' => 'us-east-1']
); );
add_settings_field( add_settings_field(
's3_bucket', 's3_bucket',
__('Bucket', 'flysystem-offload'), __('Bucket', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'bucket', 'type' => 'text'] ['key' => 'bucket', 'type' => 'text']
); );
add_settings_field( add_settings_field(
's3_prefix', 's3_prefix',
__('Prefijo (opcional)', 'flysystem-offload'), __('Prefijo (opcional)', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'prefix', 'type' => 'text', 'placeholder' => 'uploads'] ['key' => 'prefix', 'type' => 'text', 'placeholder' => 'uploads']
); );
add_settings_field( add_settings_field(
's3_endpoint', 's3_endpoint',
__('Endpoint personalizado', 'flysystem-offload'), __('Endpoint personalizado', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'endpoint', 'type' => 'text', 'placeholder' => 'https://nyc3.digitaloceanspaces.com'] ['key' => 'endpoint', 'type' => 'text', 'placeholder' => 'https://nyc3.digitaloceanspaces.com']
); );
add_settings_field( add_settings_field(
's3_cdn_url', 's3_cdn_url',
__('URL CDN (opcional)', 'flysystem-offload'), __('URL CDN (opcional)', 'flysystem-offload'),
[$this, 'renderS3Field'], [$this, 'renderS3Field'],
'flysystem-offload', 'flysystem-offload',
'flysystem_offload_s3', 'flysystem_offload_s3',
['key' => 'cdn_url', 'type' => 'text', 'placeholder' => 'https://cdn.midominio.com'] ['key' => 'cdn_url', 'type' => 'text', 'placeholder' => 'https://cdn.midominio.com']
); );
} }
public function sanitizeSettings(array $input): array public function sanitizeSettings(array $input): array
{ {
$current = get_option('flysystem_offload_settings', []); $current = get_option('flysystem_offload_settings', []);
$adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local'); $adapter = sanitize_key($input['adapter'] ?? $current['adapter'] ?? 'local');
$basePrefix = trim($input['base_prefix'] ?? ''); $basePrefix = trim($input['base_prefix'] ?? '');
$s3 = $current['adapters']['s3'] ?? []; $s3 = $current['adapters']['s3'] ?? [];
$inputS3 = $input['adapters']['s3'] ?? []; $inputS3 = $input['adapters']['s3'] ?? [];
$secretRaw = $inputS3['secret_key'] ?? ''; $secretRaw = $inputS3['secret_key'] ?? '';
$secret = $secretRaw === '' ? ($s3['secret_key'] ?? '') : $secretRaw; $secret = $secretRaw === '' ? ($s3['secret_key'] ?? '') : $secretRaw;
$sanitizedS3 = [ $sanitizedS3 = [
'access_key' => sanitize_text_field($inputS3['access_key'] ?? $s3['access_key'] ?? ''), 'access_key' => sanitize_text_field($inputS3['access_key'] ?? $s3['access_key'] ?? ''),
'secret_key' => sanitize_text_field($inputS3['secret_key'] ?? $s3['secret_key'] ?? ''), 'secret_key' => sanitize_text_field($inputS3['secret_key'] ?? $s3['secret_key'] ?? ''),
'region' => sanitize_text_field($inputS3['region'] ?? $s3['region'] ?? ''), 'region' => sanitize_text_field($inputS3['region'] ?? $s3['region'] ?? ''),
'bucket' => sanitize_text_field($inputS3['bucket'] ?? $s3['bucket'] ?? ''), 'bucket' => sanitize_text_field($inputS3['bucket'] ?? $s3['bucket'] ?? ''),
'prefix' => trim($inputS3['prefix'] ?? $s3['prefix'] ?? ''), 'prefix' => trim($inputS3['prefix'] ?? $s3['prefix'] ?? ''),
'endpoint' => esc_url_raw($inputS3['endpoint'] ?? $s3['endpoint'] ?? ''), 'endpoint' => esc_url_raw($inputS3['endpoint'] ?? $s3['endpoint'] ?? ''),
'cdn_url' => esc_url_raw($inputS3['cdn_url'] ?? $s3['cdn_url'] ?? ''), 'cdn_url' => esc_url_raw($inputS3['cdn_url'] ?? $s3['cdn_url'] ?? ''),
]; ];
$current['adapter'] = $adapter; $current['adapter'] = $adapter;
$current['base_prefix'] = $basePrefix; $current['base_prefix'] = $basePrefix;
$current['adapters']['s3'] = $sanitizedS3; $current['adapters']['s3'] = $sanitizedS3;
return $current; return $current;
} }
public function render(): void public function render(): void
{ {
?> ?>
<div class="wrap"> <div class="wrap">
<h1><?php esc_html_e('Flysystem Offload', 'flysystem-offload'); ?></h1> <h1><?php esc_html_e('Flysystem Offload', 'flysystem-offload'); ?></h1>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php <?php
settings_fields('flysystem_offload'); settings_fields('flysystem_offload');
do_settings_sections('flysystem-offload');
submit_button(__('Guardar cambios', 'flysystem-offload')); $sectionsHtml = $this->generateSectionsMarkup();
?> echo $sectionsHtml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
</form>
</div> submit_button(__('Guardar cambios', 'flysystem-offload'));
<?php ?>
} </form>
</div>
public function renderAdapterField(): void <?php
{ }
$settings = get_option('flysystem_offload_settings', []);
$adapter = $settings['adapter'] ?? 'local'; private function generateSectionsMarkup(): string
{
$options = [ ob_start();
'local' => __('Local (fallback)', 'flysystem-offload'), do_settings_sections('flysystem-offload');
's3' => 'Amazon S3 / Compatible', $html = ob_get_clean();
'sftp' => 'SFTP',
'webdav' => 'WebDAV', $label = preg_quote(__('Amazon S3 / Compatible', 'flysystem-offload'), '/');
'gcs' => 'Google Cloud Storage',
'azure' => 'Azure Blob Storage', $pattern = '/(<h2[^>]*>\s*' . $label . '\s*<\/h2>\s*<table[^>]*>.*?<\/table>)/is';
'googledrive' => 'Google Drive (beta)',
'onedrive' => 'OneDrive (beta)', return preg_replace(
'dropbox' => 'Dropbox (beta)', $pattern,
]; '<div class="flysystem-offload-adapter-section" data-adapter="s3">$1</div>',
?> $html
<select name="flysystem_offload_settings[adapter]" id="flysystem-offload-adapter"> );
<?php foreach ($options as $value => $label): ?> }
<option value="<?php echo esc_attr($value); ?>" <?php selected($adapter, $value); ?>>
<?php echo esc_html($label); ?> public function renderAdapterField(): void
</option> {
<?php endforeach; ?> $settings = get_option('flysystem_offload_settings', []);
</select> $adapter = $settings['adapter'] ?? 'local';
<?php
} $options = [
'local' => __('Local (fallback)', 'flysystem-offload'),
public function renderBasePrefixField(): void 's3' => 'Amazon S3 / Compatible',
{ 'sftp' => 'SFTP',
$settings = get_option('flysystem_offload_settings', []); 'webdav' => 'WebDAV',
$prefix = $settings['base_prefix'] ?? ''; 'gcs' => 'Google Cloud Storage',
?> 'azure' => 'Azure Blob Storage',
<input 'googledrive' => 'Google Drive (beta)',
type="text" 'onedrive' => 'OneDrive (beta)',
name="flysystem_offload_settings[base_prefix]" 'dropbox' => 'Dropbox (beta)',
value="<?php echo esc_attr($prefix); ?>" ];
placeholder="media/" ?>
class="regular-text" <select name="flysystem_offload_settings[adapter]" id="flysystem-offload-adapter">
/> <?php foreach ($options as $value => $label): ?>
<p class="description"> <option value="<?php echo esc_attr($value); ?>" <?php selected($adapter, $value); ?>>
<?php esc_html_e('Opcional. Se antepone a todas las rutas remotas (ej. "wordpress/uploads").', 'flysystem-offload'); ?> <?php echo esc_html($label); ?>
</p> </option>
<?php <?php endforeach; ?>
} </select>
<?php
public function renderS3Field(array $args): void }
{
$settings = get_option('flysystem_offload_settings', []); public function renderBasePrefixField(): void
$s3 = $settings['adapters']['s3'] ?? []; {
$key = $args['key']; $settings = get_option('flysystem_offload_settings', []);
$type = $args['type'] ?? 'text'; $prefix = $settings['base_prefix'] ?? '';
$placeholder = $args['placeholder'] ?? ''; ?>
$value = $s3[$key] ?? ''; <input
type="text"
if ($type === 'password') { name="flysystem_offload_settings[base_prefix]"
$value = ''; value="<?php echo esc_attr($prefix); ?>"
} placeholder="media/"
?> class="regular-text"
<input />
type="<?php echo esc_attr($type); ?>" <p class="description">
name="flysystem_offload_settings[adapters][s3][<?php echo esc_attr($key); ?>]" <?php esc_html_e('Opcional. Se antepone a todas las rutas remotas (ej. "wordpress/uploads").', 'flysystem-offload'); ?>
value="<?php echo esc_attr($value); ?>" </p>
class="regular-text" <?php
placeholder="<?php echo esc_attr($placeholder); ?>" }
autocomplete="off"
/> public function renderS3Field(array $args): void
<?php if ('secret_key' === $key): ?> {
<p class="description"> $settings = get_option('flysystem_offload_settings', []);
<?php esc_html_e('La clave no se mostrará después de guardarla.', 'flysystem-offload'); ?> $s3 = $settings['adapters']['s3'] ?? [];
</p> $key = $args['key'];
<?php endif; $type = $args['type'] ?? 'text';
} $placeholder = $args['placeholder'] ?? '';
} $value = $s3[$key] ?? '';
if ($type === 'password') {
$value = '';
}
?>
<input
type="<?php echo esc_attr($type); ?>"
name="flysystem_offload_settings[adapters][s3][<?php echo esc_attr($key); ?>]"
value="<?php echo esc_attr($value); ?>"
class="regular-text"
placeholder="<?php echo esc_attr($placeholder); ?>"
autocomplete="off"
/>
<?php if ('secret_key' === $key): ?>
<p class="description">
<?php esc_html_e('La clave no se mostrará después de guardarla.', 'flysystem-offload'); ?>
</p>
<?php endif;
}
}