| 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 |
/** |
| 16 |
* Renders the template for the active tab. |
| 17 |
* |
| 18 |
* The function checks if the 'presentation' tab is active. If not, it returns without further |
| 19 |
* execution. When active, it retrieves configuration options, organizes them into a structured |
| 20 |
* settings array, and renders the template with the specified settings. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function render(): void { |
| 25 |
if ('presentation' !== $this->getActiveTab()) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$options = $this->config->getAll(); |
| 30 |
|
| 31 |
// Extract variables for the template |
| 32 |
$settings = [ |
| 33 |
'css' => [ |
| 34 |
'id' => $options['css_id'], |
| 35 |
'class' => $options['css_class'] |
| 36 |
], |
| 37 |
'emailReplacements' => [ |
| 38 |
'at' => $options['at'], |
| 39 |
'dot' => $options['dot'] |
| 40 |
], |
| 41 |
'linkTextOptions' => [ |
| 42 |
'replacement' => [ |
| 43 |
'value' => 0, |
| 44 |
'fields' => [ |
| 45 |
'at' => [ |
| 46 |
'label' => __('Replacement for \'@\'', 'cryptx'), |
| 47 |
'value' => $options['at'] |
| 48 |
], |
| 49 |
'dot' => [ |
| 50 |
'label' => __('Replacement for \'.\'', 'cryptx'), |
| 51 |
'value' => $options['dot'] |
| 52 |
] |
| 53 |
] |
| 54 |
], |
| 55 |
'customText' => [ |
| 56 |
'value' => 1, |
| 57 |
'fields' => [ |
| 58 |
'alt_linktext' => [ |
| 59 |
'label' => __('Text for link', 'cryptx'), |
| 60 |
'value' => $options['alt_linktext'] |
| 61 |
] |
| 62 |
] |
| 63 |
], |
| 64 |
'externalImage' => [ |
| 65 |
'value' => 2, |
| 66 |
'fields' => [ |
| 67 |
'alt_linkimage' => [ |
| 68 |
'label' => __('Image-URL', 'cryptx'), |
| 69 |
'value' => $options['alt_linkimage'] |
| 70 |
], |
| 71 |
'http_linkimage_title' => [ |
| 72 |
'label' => __('Title-Tag for the Image', 'cryptx'), |
| 73 |
'value' => $options['http_linkimage_title'] |
| 74 |
] |
| 75 |
] |
| 76 |
], |
| 77 |
'uploadedImage' => [ |
| 78 |
'value' => 3, |
| 79 |
'fields' => [ |
| 80 |
'alt_uploadedimage' => [ |
| 81 |
'value' => $options['alt_uploadedimage'] |
| 82 |
], |
| 83 |
'alt_linkimage_title' => [ |
| 84 |
'label' => __('Title-Tag for the Image', 'cryptx'), |
| 85 |
'value' => $options['alt_linkimage_title'] |
| 86 |
] |
| 87 |
] |
| 88 |
], |
| 89 |
'scrambled' => [ |
| 90 |
'value' => 4, |
| 91 |
'label' => __('Text scrambled by AntiSpamBot (Try it and look at your site and check the html source!)', 'cryptx') |
| 92 |
], |
| 93 |
'pngImage' => [ |
| 94 |
'value' => 5, |
| 95 |
'label' => __('Convert Email to PNG-image', 'cryptx'), |
| 96 |
'fields' => [ |
| 97 |
'c2i_font' => [ |
| 98 |
'label' => __('Font', 'cryptx'), |
| 99 |
'value' => $options['c2i_font'], |
| 100 |
'options' => $this->getFontOptions() |
| 101 |
], |
| 102 |
'c2i_fontSize' => [ |
| 103 |
'label' => __('Font size (pixel)', 'cryptx'), |
| 104 |
'value' => $options['c2i_fontSize'] |
| 105 |
], |
| 106 |
'c2i_fontRGB' => [ |
| 107 |
'label' => __('Font color (RGB)', 'cryptx'), |
| 108 |
'value' => $options['c2i_fontRGB'] |
| 109 |
] |
| 110 |
] |
| 111 |
] |
| 112 |
], |
| 113 |
'selectedOption' => $options['opt_linktext'] |
| 114 |
]; |
| 115 |
|
| 116 |
$this->renderTemplate(self::TEMPLATE_PATH, $settings); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Renders a template file by including it and extracting the provided data for use within the template scope. |
| 121 |
* |
| 122 |
* @param string $path The file path to the template to be rendered. Must be a valid, existing file. |
| 123 |
* @param array $data An associative array of data to be extracted and made available to the template. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
* |
| 127 |
* @throws \RuntimeException If the specified template file does not exist. |
| 128 |
*/ |
| 129 |
private function renderTemplate(string $path, array $data): void { |
| 130 |
if (!file_exists($path)) { |
| 131 |
throw new \RuntimeException(sprintf('Template file not found: %s', $path)); |
| 132 |
} |
| 133 |
|
| 134 |
extract($data); |
| 135 |
include $path; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Retrieves the active tab from the request, defaulting to 'general' if not specified. |
| 140 |
* |
| 141 |
* @return string The sanitized active tab value from the request or 'general' if no tab is provided. |
| 142 |
*/ |
| 143 |
private function getActiveTab(): string { |
| 144 |
return sanitize_text_field($_GET['tab'] ?? 'general'); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Retrieves the available font options by scanning a directory for font files. |
| 149 |
* |
| 150 |
* @return array An associative array where the keys are the font file names and the values are the font names without the '.ttf' extension. |
| 151 |
*/ |
| 152 |
private function getFontOptions(): array { |
| 153 |
$fonts = []; |
| 154 |
$fontFiles = $this->getFilesInDirectory(CRYPTX_DIR_PATH . 'fonts', ['ttf']); |
| 155 |
|
| 156 |
foreach ($fontFiles as $font) { |
| 157 |
$fonts[$font] = str_replace('.ttf', '', $font); |
| 158 |
} |
| 159 |
|
| 160 |
return $fonts; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Retrieves a list of files in a specified directory that match the given file extensions. |
| 165 |
* |
| 166 |
* @param string $path The path to the directory to scan for files. |
| 167 |
* @param array $extensions An array of file extensions to filter the files by. |
| 168 |
* Only files matching these extensions will be included in the results. |
| 169 |
* |
| 170 |
* @return array An array of filenames from the specified directory that match the provided extensions. |
| 171 |
* If the directory does not exist, an empty array is returned. |
| 172 |
*/ |
| 173 |
private function getFilesInDirectory(string $path, array $extensions): array { |
| 174 |
if (!is_dir($path)) { |
| 175 |
return []; |
| 176 |
} |
| 177 |
|
| 178 |
$files = []; |
| 179 |
$dir = new \DirectoryIterator($path); |
| 180 |
|
| 181 |
foreach ($dir as $file) { |
| 182 |
if ($file->isFile() && in_array($file->getExtension(), $extensions)) { |
| 183 |
$files[] = $file->getFilename(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $files; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Saves and processes the settings for the application. Handles security checks, |
| 192 |
* optional reset functionality, and updates sanitized settings to the configuration. |
| 193 |
* |
| 194 |
* @param array $data The input array containing settings data to be saved. |
| 195 |
* The data is sanitized before being saved into the configuration. |
| 196 |
* |
| 197 |
* @return void This method does not return a value. |
| 198 |
*/ |
| 199 |
public function saveSettings(array $data): void { |
| 200 |
if (!current_user_can('manage_options')) { |
| 201 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 202 |
} |
| 203 |
|
| 204 |
check_admin_referer('cryptX'); |
| 205 |
|
| 206 |
// Handle reset button |
| 207 |
if (!empty($_POST['cryptX_var_reset'])) { |
| 208 |
$this->config->reset(); |
| 209 |
add_settings_error( |
| 210 |
'cryptx_messages', |
| 211 |
'settings_reset', |
| 212 |
__('Presentation settings have been reset to defaults.', 'cryptx'), |
| 213 |
'updated' |
| 214 |
); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
$sanitized = $this->sanitizeSettings($data); |
| 219 |
$this->config->update($sanitized); |
| 220 |
|
| 221 |
add_settings_error( |
| 222 |
'cryptx_messages', |
| 223 |
'settings_updated', |
| 224 |
__('Settings saved.', 'cryptx'), |
| 225 |
'updated' |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Sanitizes an array of settings data to ensure secure and valid formatting. |
| 231 |
* |
| 232 |
* @param array $data The input array containing settings data to be sanitized. |
| 233 |
* Expected keys include CSS settings, text replacements, link text options, |
| 234 |
* and font settings, each of which is sanitized according to its respective type. |
| 235 |
* |
| 236 |
* @return array The sanitized array with cleaned or validated values for all specified settings. |
| 237 |
*/ |
| 238 |
private function sanitizeSettings(array $data): array { |
| 239 |
$sanitized = []; |
| 240 |
|
| 241 |
// CSS settings |
| 242 |
$sanitized['css_id'] = sanitize_html_class($data['css_id'] ?? ''); |
| 243 |
$sanitized['css_class'] = sanitize_html_class($data['css_class'] ?? ''); |
| 244 |
|
| 245 |
// Text replacements - preserve whitespace for 'at' and 'dot' fields |
| 246 |
$sanitized['at'] = wp_kses_post($data['at'] ?? ' [at] '); |
| 247 |
$sanitized['dot'] = wp_kses_post($data['dot'] ?? ' [dot] '); |
| 248 |
|
| 249 |
// Link text options |
| 250 |
$sanitized['opt_linktext'] = absint($data['opt_linktext'] ?? 0); |
| 251 |
$sanitized['alt_linktext'] = sanitize_text_field($data['alt_linktext'] ?? ''); |
| 252 |
$sanitized['alt_linkimage'] = esc_url_raw($data['alt_linkimage'] ?? ''); |
| 253 |
$sanitized['http_linkimage_title'] = sanitize_text_field($data['http_linkimage_title'] ?? ''); |
| 254 |
$sanitized['alt_uploadedimage'] = absint($data['alt_uploadedimage'] ?? 0); |
| 255 |
$sanitized['alt_linkimage_title'] = sanitize_text_field($data['alt_linkimage_title'] ?? ''); |
| 256 |
|
| 257 |
// Font settings |
| 258 |
$sanitized['c2i_font'] = sanitize_file_name($data['c2i_font'] ?? ''); |
| 259 |
$sanitized['c2i_fontSize'] = absint($data['c2i_fontSize'] ?? 10); |
| 260 |
$sanitized['c2i_fontRGB'] = sanitize_hex_color($data['c2i_fontRGB'] ?? '#000000'); |
| 261 |
|
| 262 |
return $sanitized; |
| 263 |
} |
| 264 |
|
| 265 |
} |