| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX\Admin; |
| 4 |
|
| 5 |
use CryptX\Config; |
| 6 |
|
| 7 |
class GeneralSettingsTab { |
| 8 |
private Config $config; |
| 9 |
private const TEMPLATE_PATH = CRYPTX_DIR_PATH . 'templates/admin/tabs/general.php'; |
| 10 |
private const DEFAULTS = [ |
| 11 |
'the_content' => 0, |
| 12 |
'the_meta_key' => 0, |
| 13 |
'the_excerpt' => 0, |
| 14 |
'comment_text' => 0, |
| 15 |
'widget_text' => 0, |
| 16 |
'autolink' => 0, |
| 17 |
'metaBox' => 0, |
| 18 |
'disable_rss' => 0, |
| 19 |
'java' => 1, |
| 20 |
'load_java' => 1, |
| 21 |
'use_secure_encryption' => 0, |
| 22 |
'encryption_mode' => 'legacy' |
| 23 |
]; |
| 24 |
|
| 25 |
public function __construct(Config $config) { |
| 26 |
$this->config = $config; |
| 27 |
} |
| 28 |
|
| 29 |
public function render(): void { |
| 30 |
if ('general' !== $this->getActiveTab()) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$options = $this->config->getAll(); |
| 35 |
// Extract variables for the template |
| 36 |
$settings = [ |
| 37 |
'options' => $options, // Pass all options |
| 38 |
'securitySettings' => [ |
| 39 |
'use_secure_encryption' => [ |
| 40 |
'label' => __('Use Secure Encryption', 'cryptx'), |
| 41 |
'description' => __('Enable AES-256-GCM encryption for enhanced security (recommended for new installations)', 'cryptx'), |
| 42 |
'value' => $options['use_secure_encryption'] ?? 0 |
| 43 |
], |
| 44 |
'encryption_mode' => [ |
| 45 |
'label' => __('Encryption Mode', 'cryptx'), |
| 46 |
'description' => __('Choose encryption method. Legacy mode maintains backward compatibility with existing encrypted emails.', 'cryptx'), |
| 47 |
'value' => $options['encryption_mode'] ?? 'legacy', |
| 48 |
'options' => [ |
| 49 |
'legacy' => __('Legacy (Compatible)', 'cryptx'), |
| 50 |
'secure' => __('Secure (AES-256-GCM)', 'cryptx') |
| 51 |
] |
| 52 |
] |
| 53 |
], |
| 54 |
'applyTo' => [ |
| 55 |
'the_content' => [ |
| 56 |
'label' => __('Content', 'cryptx'), |
| 57 |
'description' => __('(this can be disabled per Post by an Option)', 'cryptx') |
| 58 |
], |
| 59 |
'the_meta_key' => [ |
| 60 |
'label' => __('Custom fields', 'cryptx'), |
| 61 |
'description' => __('(works only with the_meta()!)', 'cryptx') |
| 62 |
], |
| 63 |
'the_excerpt' => [ |
| 64 |
'label' => __('Excerpt', 'cryptx'), |
| 65 |
], |
| 66 |
'comment_text' => [ |
| 67 |
'label' => __('Comments', 'cryptx'), |
| 68 |
], |
| 69 |
'widget_text' => [ |
| 70 |
'label' => __('Widgets', 'cryptx'), |
| 71 |
'description' => __('(applies to all text and HTML widgets)', 'cryptx') |
| 72 |
] |
| 73 |
], |
| 74 |
'decryptionType' => [ |
| 75 |
'javascript' => [ |
| 76 |
'value' => 1, |
| 77 |
'label' => __('Use javascript to hide the Email-Link.', 'cryptx') |
| 78 |
], |
| 79 |
'unicode' => [ |
| 80 |
'value' => 0, |
| 81 |
'label' => __('Use Unicode to hide the Email-Link.', 'cryptx') |
| 82 |
] |
| 83 |
], |
| 84 |
'javascriptLocation' => [ |
| 85 |
'header' => [ |
| 86 |
'value' => 0, |
| 87 |
'label' => __('Load the javascript in the <b>header</b> of the page.', 'cryptx') |
| 88 |
], |
| 89 |
'footer' => [ |
| 90 |
'value' => 1, |
| 91 |
'label' => __('Load the javascript in the <b>footer</b> of the page.', 'cryptx') |
| 92 |
] |
| 93 |
], |
| 94 |
'excludedIds' => $options['excludedIDs'], |
| 95 |
'metaBox' => $options['metaBox'], |
| 96 |
'autolink' => $options['autolink'], |
| 97 |
'whiteList' => $options['whiteList'] |
| 98 |
]; |
| 99 |
|
| 100 |
$this->renderTemplate(self::TEMPLATE_PATH, $settings); |
| 101 |
} |
| 102 |
|
| 103 |
private function renderTemplate(string $path, array $data): void { |
| 104 |
if (!file_exists($path)) { |
| 105 |
throw new \RuntimeException(sprintf('Template file not found: %s', $path)); |
| 106 |
} |
| 107 |
|
| 108 |
extract($data); |
| 109 |
include $path; |
| 110 |
} |
| 111 |
|
| 112 |
private function getActiveTab(): string { |
| 113 |
return sanitize_text_field($_GET['tab'] ?? 'general'); |
| 114 |
} |
| 115 |
|
| 116 |
public function saveSettings(array $data): void |
| 117 |
{ |
| 118 |
if (!current_user_can('manage_options')) { |
| 119 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 120 |
} |
| 121 |
|
| 122 |
check_admin_referer('cryptX'); |
| 123 |
|
| 124 |
// Merge POST data with defaults to handle unchecked checkboxes |
| 125 |
$sanitized = array_merge( |
| 126 |
self::DEFAULTS, |
| 127 |
array_map(function($value) { |
| 128 |
if ($value === '1') return 1; // Convert string '1' to integer 1 for checkboxes |
| 129 |
return sanitize_text_field($value); |
| 130 |
}, $data) |
| 131 |
); |
| 132 |
|
| 133 |
if (!empty($_POST['cryptX_var_reset'])) { |
| 134 |
$this->config->reset(); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$this->config->update($sanitized); |
| 139 |
|
| 140 |
add_settings_error( |
| 141 |
'cryptx_messages', |
| 142 |
'settings_updated', |
| 143 |
__('Settings saved.', 'cryptx'), |
| 144 |
'updated' |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
private function sanitizeSettings(array $data): array { |
| 149 |
return array_merge( |
| 150 |
self::DEFAULTS, |
| 151 |
array_map('sanitize_text_field', $data) |
| 152 |
); |
| 153 |
} |
| 154 |
} |