| 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 |
]; |
| 22 |
public function __construct(Config $config) { |
| 23 |
$this->config = $config; |
| 24 |
} |
| 25 |
|
| 26 |
public function render(): void { |
| 27 |
if ('general' !== $this->getActiveTab()) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$options = $this->config->getAll(); |
| 32 |
// Extract variables for the template |
| 33 |
$settings = [ |
| 34 |
'options' => $options, // Pass all options |
| 35 |
'applyTo' => [ |
| 36 |
'the_content' => [ |
| 37 |
'label' => __('Content', 'cryptx'), |
| 38 |
'description' => __('(this can be disabled per Post by an Option)', 'cryptx') |
| 39 |
], |
| 40 |
'the_meta_key' => [ |
| 41 |
'label' => __('Custom fields', 'cryptx'), |
| 42 |
'description' => __('(works only with the_meta()!)', 'cryptx') |
| 43 |
], |
| 44 |
'the_excerpt' => [ |
| 45 |
'label' => __('Excerpt', 'cryptx'), |
| 46 |
], |
| 47 |
'comment_text' => [ |
| 48 |
'label' => __('Comments', 'cryptx'), |
| 49 |
], |
| 50 |
'widget_text' => [ |
| 51 |
'label' => __('Widgets', 'cryptx'), |
| 52 |
'description' => __('(works only on all widgets, not on a single widget!)', 'cryptx') |
| 53 |
] |
| 54 |
], |
| 55 |
'decryptionType' => [ |
| 56 |
'javascript' => [ |
| 57 |
'value' => 1, |
| 58 |
'label' => __('Use javascript to hide the Email-Link.', 'cryptx') |
| 59 |
], |
| 60 |
'unicode' => [ |
| 61 |
'value' => 0, |
| 62 |
'label' => __('Use Unicode to hide the Email-Link.', 'cryptx') |
| 63 |
] |
| 64 |
], |
| 65 |
'javascriptLocation' => [ |
| 66 |
'header' => [ |
| 67 |
'value' => 0, |
| 68 |
'label' => __('Load the javascript in the <b>header</b> of the page.', 'cryptx') |
| 69 |
], |
| 70 |
'footer' => [ |
| 71 |
'value' => 1, |
| 72 |
'label' => __('Load the javascript in the <b>footer</b> of the page.', 'cryptx') |
| 73 |
] |
| 74 |
], |
| 75 |
'excludedIds' => $options['excludedIDs'], |
| 76 |
'metaBox' => $options['metaBox'], |
| 77 |
'autolink' => $options['autolink'], |
| 78 |
'whiteList' => $options['whiteList'] |
| 79 |
]; |
| 80 |
|
| 81 |
$this->renderTemplate(self::TEMPLATE_PATH, $settings); |
| 82 |
} |
| 83 |
|
| 84 |
private function renderTemplate(string $path, array $data): void { |
| 85 |
if (!file_exists($path)) { |
| 86 |
throw new \RuntimeException(sprintf('Template file not found: %s', $path)); |
| 87 |
} |
| 88 |
|
| 89 |
extract($data); |
| 90 |
include $path; |
| 91 |
} |
| 92 |
|
| 93 |
private function getActiveTab(): string { |
| 94 |
return sanitize_text_field($_GET['tab'] ?? 'general'); |
| 95 |
} |
| 96 |
|
| 97 |
public function saveSettings(array $data): void |
| 98 |
{ |
| 99 |
if (!current_user_can('manage_options')) { |
| 100 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 101 |
} |
| 102 |
|
| 103 |
check_admin_referer('cryptX'); |
| 104 |
|
| 105 |
// Merge POST data with defaults to handle unchecked checkboxes |
| 106 |
$sanitized = array_merge( |
| 107 |
self::DEFAULTS, |
| 108 |
array_map(function($value) { |
| 109 |
if ($value === '1') return 1; // Convert string '1' to integer 1 for checkboxes |
| 110 |
return sanitize_text_field($value); |
| 111 |
}, $data) |
| 112 |
); |
| 113 |
|
| 114 |
if (!empty($_POST['cryptX_var_reset'])) { |
| 115 |
$this->config->reset(); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$this->config->update($sanitized); |
| 120 |
|
| 121 |
add_settings_error( |
| 122 |
'cryptx_messages', |
| 123 |
'settings_updated', |
| 124 |
__('Settings saved.', 'cryptx'), |
| 125 |
'updated' |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
private function sanitizeSettings(array $data): array { |
| 130 |
return array_merge( |
| 131 |
self::DEFAULTS, |
| 132 |
array_map('sanitize_text_field', $data) |
| 133 |
); |
| 134 |
} |
| 135 |
} |