PluginProbe
CryptX / 4.0.6
CryptX v4.0.6
4.2.0 4.1.1 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.9 2.0 2.1 2.2 2.3 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 92 releases
cryptx / classes / CryptXSettingsTabs.php

CryptXSettingsTabs.php in CryptX 4.0.6, at classes/CryptXSettingsTabs.php

384 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CryptX;
4
5 use CryptX\Admin\ChangelogSettingsTab;
6 use CryptX\Admin\GeneralSettingsTab;
7 use CryptX\Admin\PresentationSettingsTab;
8 use CryptX\Util\DataSanitizer;
9
10 /**
11 * Class CryptXSettingsTabs
12 * Handles the settings tabs functionality for the CryptX plugin admin interface
13 */
14 class CryptXSettingsTabs
15 {
16 /**
17 * WordPress hook name for the CryptX settings page
18 */
19 private const SETTINGS_PAGE_HOOK = 'settings_page_cryptx';
20
21 /**
22 * @var array List of allowed tabs
23 */
24 private array $allowedTabs = ['general', 'presentation', 'howto', 'changelog'];
25
26 /**
27 * @var string Current active tab
28 */
29 private string $activeTab;
30
31 /**
32 * @var CryptX Instance of the main CryptX class
33 */
34 private CryptX $cryptX;
35
36 /**
37 * CryptXSettingsTabs constructor.
38 *
39 * @param CryptX $cryptX Instance of the main CryptX class
40 */
41 public function __construct(CryptX $cryptX)
42 {
43 $this->cryptX = $cryptX;
44 $this->activeTab = $this->determineActiveTab();
45 $this->initHooks();
46 }
47
48 private function initHooks(): void
49 {
50 // Add menu registration hook
51 if (is_admin()) {
52 add_action('admin_menu', [$this, 'registerSettingsMenu']);
53 }
54
55 // Existing hooks
56 add_action('admin_enqueue_scripts', [$this, 'enqueueAdminAssets']);
57 add_action('cryptx_settings_tab', [$this, 'renderTabNavigation']);
58 add_action('cryptx_settings_content', [$this, 'renderTabContent']);
59 }
60
61 /**
62 * Enqueues necessary CSS and JavaScript assets for the CryptX admin settings page.
63 *
64 * @param string $hook The current admin page hook suffix.
65 * @return void
66 */
67 public function enqueueAdminAssets(string $hook): void
68 {
69 if ($hook !== self::SETTINGS_PAGE_HOOK) {
70 return;
71 }
72
73 // Enqueue CSS files with version for cache busting
74 wp_enqueue_style(
75 'cryptx-admin-css',
76 CRYPTX_DIR_URL . 'css/admin.css',
77 [],
78 CRYPTX_VERSION
79 );
80
81 // Enqueue WordPress color picker assets
82 wp_enqueue_style('wp-color-picker');
83 wp_enqueue_script('wp-color-picker');
84
85 // Enqueue media uploader
86 wp_enqueue_media();
87 }
88
89 /**
90 * Register the CryptX settings menu
91 */
92 public function registerSettingsMenu(): void
93 {
94 add_submenu_page(
95 'options-general.php',
96 _x('CryptX', 'CryptX settings page', 'cryptx'),
97 _x('CryptX', 'CryptX settings menu', 'cryptx'),
98 'manage_options',
99 'cryptx',
100 [$this, 'renderSettingsPage']
101 );
102 }
103
104 /**
105 * Render the settings page
106 */
107 public function renderSettingsPage(): void
108 {
109 if (!current_user_can('manage_options')) {
110 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'cryptx'));
111 }
112
113 $this->handleFormSubmission();
114 $this->renderSettingsPageHtml();
115 }
116
117 /**
118 * Handle form submission
119 */
120 private function handleFormSubmission(): void
121 {
122 if (!empty($_POST['cryptX_var'])) {
123 if (!check_admin_referer('cryptX')) {
124 wp_die(esc_html__('Security check failed', 'cryptx'));
125 }
126
127 $saveOptions = DataSanitizer::sanitize(wp_unslash($_POST['cryptX_var']));
128
129 // Handle reset for any tab
130 if (isset($_POST['cryptX_var_reset'])) {
131 $this->cryptX->getCryptXOptionsDefaults();
132 $this->cryptX->getConfig()->reset();
133 $this->displayResetMessage();
134 return;
135 }
136
137 if (isset($_POST['cryptX_save_general_settings'])) {
138 $saveOptions = $this->parseGeneralSettings($saveOptions);
139 }
140
141 $this->cryptX->saveCryptXOptions($saveOptions);
142 $this->displaySuccessMessage();
143 }
144 }
145
146 /**
147 * Parse general settings
148 */
149 private function parseGeneralSettings(array $saveOptions): array
150 {
151 $checkboxes = [
152 'the_content' => 0,
153 'the_meta_key' => 0,
154 'the_excerpt' => 0,
155 'comment_text' => 0,
156 'widget_text' => 0,
157 'autolink' => 0,
158 'metaBox' => 0,
159 ];
160
161 return wp_parse_args($saveOptions, $checkboxes);
162 }
163
164 /**
165 * Display success message
166 */
167 private function displaySuccessMessage(): void
168 {
169 add_settings_error(
170 'cryptx_messages',
171 'cryptx_message',
172 esc_html__('Settings saved.', 'cryptx'),
173 'updated'
174 );
175 }
176
177 /**
178 * Display reset message
179 */
180 private function displayResetMessage(): void
181 {
182 add_settings_error(
183 'cryptx_messages',
184 'cryptx_reset',
185 esc_html__('Settings have been reset to defaults.', 'cryptx'),
186 'updated'
187 );
188 }
189
190 /**
191 * Render the settings page HTML
192 */
193 private function renderSettingsPageHtml(): void
194 {
195 ?>
196 <div class="cryptx-option-page">
197 <h1><?php esc_html_e("CryptX settings", 'cryptx'); ?></h1>
198 <form method="post" action="">
199 <?php
200 wp_nonce_field('cryptX');
201 settings_errors('cryptx_messages');
202 ?>
203 <h2 class="nav-tab-wrapper">
204 <?php do_action('cryptx_settings_tab'); ?>
205 </h2>
206 <div class="cryptx-tab-content-wrapper">
207 <?php do_action('cryptx_settings_content'); ?>
208 </div>
209 </form>
210 </div>
211 <?php
212 }
213
214 /**
215 * Determine the active tab from GET parameters
216 *
217 * @return string
218 */
219 private function determineActiveTab(): string
220 {
221 $tab = (isset($_GET['tab']))? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general';
222 return in_array($tab, $this->allowedTabs) ? $tab : 'general';
223 }
224
225 /**
226 * Get the current active tab
227 *
228 * @return string
229 */
230 public function getActiveTab(): string
231 {
232 return $this->activeTab;
233 }
234
235 /**
236 * Render the tab navigation
237 */
238 public function renderTabNavigation(): void
239 {
240 $tabs = [
241 'general' => esc_html__('General', 'cryptx'),
242 'presentation' => esc_html__('Presentation', 'cryptx'),
243 'howto' => esc_html__('How to&hellip;', 'cryptx'),
244 'changelog' => esc_html__('Changelog', 'cryptx')
245 ];
246
247 foreach ($tabs as $tab => $label) {
248 $this->renderTabLink($tab, $label);
249 }
250 }
251
252 /**
253 * Render individual tab link
254 *
255 * @param string $tab Tab identifier
256 * @param string $label Tab label
257 */
258 private function renderTabLink(string $tab, string $label): void
259 {
260 $isActive = $this->activeTab === $tab || ($this->activeTab === '' && $tab === 'general');
261 $activeClass = $isActive ? 'nav-tab-active' : '';
262 $url = admin_url('options-general.php?page=' . CRYPTX_BASEFOLDER . '&tab=' . $tab);
263
264 printf(
265 '<a class="nav-tab %s" href="%s">%s</a>',
266 esc_attr($activeClass),
267 esc_url($url),
268 esc_html($label)
269 );
270 }
271
272 /**
273 * Render the content for the current active tab
274 */
275 public function renderTabContent(): void
276 {
277 switch ($this->activeTab) {
278 case 'general':
279 $this->renderGeneralTab();
280 break;
281 case 'presentation':
282 $this->renderPresentationTab();
283 break;
284 case 'howto':
285 $this->renderHowtoTab();
286 break;
287 case 'changelog':
288 $this->renderChangelogTab();
289 break;
290 }
291 }
292
293 /**
294 * Render the general settings tab content
295 */
296 private function renderGeneralTab(): void
297 {
298 try {
299 // Get the Config instance from CryptX
300 $config = $this->cryptX->getConfig();
301
302 // Create and render the General Settings Tab
303 $generalTab = new GeneralSettingsTab($config);
304
305 // Handle form submission if needed
306 if (isset($_POST['cryptX_save_general_settings']) || isset($_POST['cryptX_var_reset'])) {
307 if (!empty($_POST['cryptX_var']) || isset($_POST['cryptX_var_reset'])) {
308 $generalTab->saveSettings(wp_unslash($_POST['cryptX_var']) ?? []);
309 }
310 }
311
312 // Render the tab content
313 $generalTab->render();
314
315 } catch (\Exception $e) {
316 // display admin notice
317 add_settings_error(
318 'cryptx_messages',
319 'cryptx_error',
320 esc_html__('An error occurred while loading the general settings.', 'cryptx'),
321 'error'
322 );
323 }
324 }
325
326 /**
327 * Render the presentation settings tab content
328 */
329 private function renderPresentationTab(): void
330 {
331 try {
332 // Get the Config instance from CryptX
333 $config = $this->cryptX->getConfig();
334
335 // Create and render the Presentation Settings Tab
336 $presentationTab = new PresentationSettingsTab($config);
337
338 // Handle form submission if needed
339 if (isset($_POST['cryptX_save_presentation_settings']) || isset($_POST['cryptX_var_reset'])) {
340 if (!empty($_POST['cryptX_var']) || isset($_POST['cryptX_var_reset'])) {
341 $presentationTab->saveSettings($_POST['cryptX_var'] ?? []);
342 }
343 }
344
345 // Render the tab content
346 $presentationTab->render();
347
348 } catch (\Exception $e) {
349 // display admin notice
350 add_settings_error(
351 'cryptx_messages',
352 'cryptx_error',
353 esc_html__('An error occurred while loading the presentation settings.', 'cryptx'),
354 'error'
355 );
356 }
357 }
358
359 /**
360 * Render the how-to tab content
361 */
362 private function renderHowtoTab(): void
363 {
364 require CRYPTX_DIR_PATH . '/templates/admin/tabs/howto.php';
365 }
366
367 /**
368 * Render the changelog tab content
369 */
370 private function renderChangelogTab(): void
371 {
372 try {
373 $changelogTab = new ChangelogSettingsTab($this->cryptX->getConfig());
374 $changelogTab->render();
375 } catch (\Exception $e) {
376 add_settings_error(
377 'cryptx_messages',
378 'cryptx_error',
379 esc_html__('An error occurred while loading the changelog.', 'cryptx'),
380 'error'
381 );
382 }
383 }
384 }