| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX\Admin; |
| 4 |
|
| 5 |
use CryptX\Config; |
| 6 |
|
| 7 |
class PresentationSettingsTab { |
| 8 |
private Config $config; |
| 9 |
private const TEMPLATE_PATH = CRYPTX_DIR_PATH . 'templates/admin/tabs/presentation.php'; |
| 10 |
|
| 11 |
public function __construct(Config $config) { |
| 12 |
$this->config = $config; |
| 13 |
} |
| 14 |
|
| 15 |
public function render(): void { |
| 16 |
if ('presentation' !== $this->getActiveTab()) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$options = $this->config->getAll(); |
| 21 |
|
| 22 |
// Extract variables for the template |
| 23 |
$settings = [ |
| 24 |
'css' => [ |
| 25 |
'id' => $options['css_id'], |
| 26 |
'class' => $options['css_class'] |
| 27 |
], |
| 28 |
'emailReplacements' => [ |
| 29 |
'at' => $options['at'], |
| 30 |
'dot' => $options['dot'] |
| 31 |
], |
| 32 |
'linkTextOptions' => [ |
| 33 |
'replacement' => [ |
| 34 |
'value' => 0, |
| 35 |
'fields' => [ |
| 36 |
'at' => [ |
| 37 |
'label' => __('Replacement for \'@\'', 'cryptx'), |
| 38 |
'value' => $options['at'] |
| 39 |
], |
| 40 |
'dot' => [ |
| 41 |
'label' => __('Replacement for \'.\'', 'cryptx'), |
| 42 |
'value' => $options['dot'] |
| 43 |
] |
| 44 |
] |
| 45 |
], |
| 46 |
'customText' => [ |
| 47 |
'value' => 1, |
| 48 |
'fields' => [ |
| 49 |
'alt_linktext' => [ |
| 50 |
'label' => __('Text for link', 'cryptx'), |
| 51 |
'value' => $options['alt_linktext'] |
| 52 |
] |
| 53 |
] |
| 54 |
], |
| 55 |
'externalImage' => [ |
| 56 |
'value' => 2, |
| 57 |
'fields' => [ |
| 58 |
'alt_linkimage' => [ |
| 59 |
'label' => __('Image-URL', 'cryptx'), |
| 60 |
'value' => $options['alt_linkimage'] |
| 61 |
], |
| 62 |
'http_linkimage_title' => [ |
| 63 |
'label' => __('Title-Tag for the Image', 'cryptx'), |
| 64 |
'value' => $options['http_linkimage_title'] |
| 65 |
] |
| 66 |
] |
| 67 |
], |
| 68 |
'uploadedImage' => [ |
| 69 |
'value' => 3, |
| 70 |
'fields' => [ |
| 71 |
'alt_uploadedimage' => [ |
| 72 |
'value' => $options['alt_uploadedimage'] |
| 73 |
], |
| 74 |
'alt_linkimage_title' => [ |
| 75 |
'label' => __('Title-Tag for the Image', 'cryptx'), |
| 76 |
'value' => $options['alt_linkimage_title'] |
| 77 |
] |
| 78 |
] |
| 79 |
], |
| 80 |
'scrambled' => [ |
| 81 |
'value' => 4, |
| 82 |
'label' => __('Text scrambled by AntiSpamBot', 'cryptx') |
| 83 |
], |
| 84 |
'pngImage' => [ |
| 85 |
'value' => 5, |
| 86 |
'label' => __('Convert Email to PNG-image', 'cryptx'), |
| 87 |
'fields' => [ |
| 88 |
'c2i_font' => [ |
| 89 |
'label' => __('Font', 'cryptx'), |
| 90 |
'value' => $options['c2i_font'], |
| 91 |
'options' => $this->getFontOptions() |
| 92 |
], |
| 93 |
'c2i_fontSize' => [ |
| 94 |
'label' => __('Font size (pixel)', 'cryptx'), |
| 95 |
'value' => $options['c2i_fontSize'] |
| 96 |
], |
| 97 |
'c2i_fontRGB' => [ |
| 98 |
'label' => __('Font color (RGB)', 'cryptx'), |
| 99 |
'value' => $options['c2i_fontRGB'] |
| 100 |
] |
| 101 |
] |
| 102 |
] |
| 103 |
], |
| 104 |
'selectedOption' => $options['opt_linktext'] |
| 105 |
]; |
| 106 |
|
| 107 |
$this->renderTemplate(self::TEMPLATE_PATH, $settings); |
| 108 |
} |
| 109 |
|
| 110 |
private function renderTemplate(string $path, array $data): void { |
| 111 |
if (!file_exists($path)) { |
| 112 |
throw new \RuntimeException(sprintf('Template file not found: %s', $path)); |
| 113 |
} |
| 114 |
|
| 115 |
extract($data); |
| 116 |
include $path; |
| 117 |
} |
| 118 |
|
| 119 |
private function getActiveTab(): string { |
| 120 |
return sanitize_text_field($_GET['tab'] ?? 'general'); |
| 121 |
} |
| 122 |
|
| 123 |
private function getFontOptions(): array { |
| 124 |
$fonts = []; |
| 125 |
$fontFiles = $this->getFilesInDirectory(CRYPTX_DIR_PATH . 'fonts', ['ttf']); |
| 126 |
|
| 127 |
foreach ($fontFiles as $font) { |
| 128 |
$fonts[$font] = str_replace('.ttf', '', $font); |
| 129 |
} |
| 130 |
|
| 131 |
return $fonts; |
| 132 |
} |
| 133 |
|
| 134 |
private function getFilesInDirectory(string $path, array $extensions): array { |
| 135 |
if (!is_dir($path)) { |
| 136 |
return []; |
| 137 |
} |
| 138 |
|
| 139 |
$files = []; |
| 140 |
$dir = new \DirectoryIterator($path); |
| 141 |
|
| 142 |
foreach ($dir as $file) { |
| 143 |
if ($file->isFile() && in_array($file->getExtension(), $extensions)) { |
| 144 |
$files[] = $file->getFilename(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return $files; |
| 149 |
} |
| 150 |
|
| 151 |
public function saveSettings(array $data): void { |
| 152 |
if (!current_user_can('manage_options')) { |
| 153 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 154 |
} |
| 155 |
|
| 156 |
check_admin_referer('cryptX'); |
| 157 |
|
| 158 |
$sanitized = $this->sanitizeSettings($data); |
| 159 |
|
| 160 |
if (!empty($_POST['cryptX_var_reset'])) { |
| 161 |
$this->config->reset(); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$this->config->update($sanitized); |
| 166 |
|
| 167 |
add_settings_error( |
| 168 |
'cryptx_messages', |
| 169 |
'settings_updated', |
| 170 |
__('Settings saved.', 'cryptx'), |
| 171 |
'updated' |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
private function sanitizeSettings(array $data): array { |
| 176 |
$sanitized = []; |
| 177 |
|
| 178 |
// CSS settings |
| 179 |
$sanitized['css_id'] = sanitize_html_class($data['css_id'] ?? ''); |
| 180 |
$sanitized['css_class'] = sanitize_html_class($data['css_class'] ?? ''); |
| 181 |
|
| 182 |
// Text replacements |
| 183 |
$sanitized['at'] = sanitize_text_field($data['at'] ?? ' [at] '); |
| 184 |
$sanitized['dot'] = sanitize_text_field($data['dot'] ?? ' [dot] '); |
| 185 |
|
| 186 |
// Link text options |
| 187 |
$sanitized['opt_linktext'] = absint($data['opt_linktext'] ?? 0); |
| 188 |
$sanitized['alt_linktext'] = sanitize_text_field($data['alt_linktext'] ?? ''); |
| 189 |
$sanitized['alt_linkimage'] = esc_url_raw($data['alt_linkimage'] ?? ''); |
| 190 |
$sanitized['http_linkimage_title'] = sanitize_text_field($data['http_linkimage_title'] ?? ''); |
| 191 |
$sanitized['alt_uploadedimage'] = absint($data['alt_uploadedimage'] ?? 0); |
| 192 |
$sanitized['alt_linkimage_title'] = sanitize_text_field($data['alt_linkimage_title'] ?? ''); |
| 193 |
|
| 194 |
// Font settings |
| 195 |
$sanitized['c2i_font'] = sanitize_file_name($data['c2i_font'] ?? ''); |
| 196 |
$sanitized['c2i_fontSize'] = absint($data['c2i_fontSize'] ?? 10); |
| 197 |
$sanitized['c2i_fontRGB'] = sanitize_hex_color($data['c2i_fontRGB'] ?? '#000000'); |
| 198 |
|
| 199 |
return $sanitized; |
| 200 |
} |
| 201 |
} |