cryptX = $cryptX;
$this->activeTab = $this->determineActiveTab();
$this->initHooks();
}
private function initHooks(): void
{
// Add menu registration hook
if (is_admin()) {
add_action('admin_menu', [$this, 'registerSettingsMenu']);
}
// Existing hooks
add_action('admin_enqueue_scripts', [$this, 'enqueueAdminAssets']);
add_action('rw_cryptx_settings_tab', [$this, 'renderTabNavigation']);
add_action('rw_cryptx_settings_content', [$this, 'renderTabContent']);
}
/**
* Enqueues necessary CSS and JavaScript assets for the CryptX admin settings page.
*
* @param string $hook The current admin page hook suffix.
* @return void
*/
public function enqueueAdminAssets(string $hook): void
{
if ($hook !== self::SETTINGS_PAGE_HOOK) {
return;
}
// Enqueue CSS files with version for cache busting
wp_enqueue_style(
'cryptx-admin-css',
CRYPTX_DIR_URL . 'css/admin.css',
[],
CRYPTX_VERSION
);
// Enqueue WordPress color picker assets
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wp-color-picker');
// Enqueue media uploader
wp_enqueue_media();
}
/**
* Register the CryptX settings menu
*/
public function registerSettingsMenu(): void
{
add_submenu_page(
'options-general.php',
_x('CryptX', 'CryptX settings page', 'cryptx'),
_x('CryptX', 'CryptX settings menu', 'cryptx'),
'manage_options',
'cryptx',
[$this, 'renderSettingsPage']
);
}
/**
* Render the settings page
*/
public function renderSettingsPage(): void
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$this->handleFormSubmission();
$this->renderSettingsPageHtml();
}
/**
* Handle form submission
*/
private function handleFormSubmission(): void
{
if (!empty($_POST['cryptX_var'])) {
if (!check_admin_referer('cryptX')) {
wp_die(__('Security check failed'));
}
$saveOptions = DataSanitizer::sanitize($_POST['cryptX_var']);
// Handle reset for any tab
if (isset($_POST['cryptX_var_reset'])) {
$this->cryptX->getCryptXOptionsDefaults();
$this->cryptX->getConfig()->reset();
$this->displayResetMessage();
return;
}
if (isset($_POST['cryptX_save_general_settings'])) {
$saveOptions = $this->parseGeneralSettings($saveOptions);
}
$this->cryptX->saveCryptXOptions($saveOptions);
$this->displaySuccessMessage();
}
}
/**
* Parse general settings
*/
private function parseGeneralSettings(array $saveOptions): array
{
$checkboxes = [
'the_content' => 0,
'the_meta_key' => 0,
'the_excerpt' => 0,
'comment_text' => 0,
'widget_text' => 0,
'autolink' => 0,
'metaBox' => 0,
];
return wp_parse_args($saveOptions, $checkboxes);
}
/**
* Display success message
*/
private function displaySuccessMessage(): void
{
add_settings_error(
'cryptx_messages',
'cryptx_message',
__('Settings saved.', 'cryptx'),
'updated'
);
}
/**
* Display reset message
*/
private function displayResetMessage(): void
{
add_settings_error(
'cryptx_messages',
'cryptx_reset',
__('Settings have been reset to defaults.', 'cryptx'),
'updated'
);
}
/**
* Render the settings page HTML
*/
private function renderSettingsPageHtml(): void
{
?>
allowedTabs) ? $tab : 'general';
}
/**
* Get the current active tab
*
* @return string
*/
public function getActiveTab(): string
{
return $this->activeTab;
}
/**
* Render the tab navigation
*/
public function renderTabNavigation(): void
{
$tabs = [
'general' => __('General', 'cryptx'),
'presentation' => __('Presentation', 'cryptx'),
'howto' => __('How to…', 'cryptx'),
'changelog' => __('Changelog', 'cryptx')
];
foreach ($tabs as $tab => $label) {
$this->renderTabLink($tab, $label);
}
}
/**
* Render individual tab link
*
* @param string $tab Tab identifier
* @param string $label Tab label
*/
private function renderTabLink(string $tab, string $label): void
{
$isActive = $this->activeTab === $tab || ($this->activeTab === '' && $tab === 'general');
$activeClass = $isActive ? 'nav-tab-active' : '';
$url = admin_url('options-general.php?page=' . CRYPTX_BASEFOLDER . '&tab=' . $tab);
printf(
'%s',
esc_attr($activeClass),
esc_url($url),
esc_html($label)
);
}
/**
* Render the content for the current active tab
*/
public function renderTabContent(): void
{
switch ($this->activeTab) {
case 'general':
$this->renderGeneralTab();
break;
case 'presentation':
$this->renderPresentationTab();
break;
case 'howto':
$this->renderHowtoTab();
break;
case 'changelog':
$this->renderChangelogTab();
break;
}
}
/**
* Render the general settings tab content
*/
private function renderGeneralTab(): void
{
try {
// Get the Config instance from CryptX
$config = $this->cryptX->getConfig();
// Create and render the General Settings Tab
$generalTab = new GeneralSettingsTab($config);
// Handle form submission if needed
if (isset($_POST['cryptX_save_general_settings']) || isset($_POST['cryptX_var_reset'])) {
if (!empty($_POST['cryptX_var']) || isset($_POST['cryptX_var_reset'])) {
$generalTab->saveSettings($_POST['cryptX_var'] ?? []);
}
}
// Render the tab content
$generalTab->render();
} catch (\Exception $e) {
// Log error and display admin notice
error_log('CryptX General Settings Tab Error: ' . $e->getMessage());
add_settings_error(
'cryptx_messages',
'cryptx_error',
__('An error occurred while loading the general settings.', 'cryptx'),
'error'
);
}
}
/**
* Render the presentation settings tab content
*/
private function renderPresentationTab(): void
{
try {
// Get the Config instance from CryptX
$config = $this->cryptX->getConfig();
// Create and render the Presentation Settings Tab
$presentationTab = new PresentationSettingsTab($config);
// Handle form submission if needed
if (isset($_POST['cryptX_save_presentation_settings']) || isset($_POST['cryptX_var_reset'])) {
if (!empty($_POST['cryptX_var']) || isset($_POST['cryptX_var_reset'])) {
$presentationTab->saveSettings($_POST['cryptX_var'] ?? []);
}
}
// Render the tab content
$presentationTab->render();
} catch (\Exception $e) {
// Log error and display admin notice
error_log('CryptX Presentation Settings Tab Error: ' . $e->getMessage());
add_settings_error(
'cryptx_messages',
'cryptx_error',
__('An error occurred while loading the presentation settings.', 'cryptx'),
'error'
);
}
}
/**
* Render the how-to tab content
*/
private function renderHowtoTab(): void
{
require CRYPTX_DIR_PATH . '/templates/admin/tabs/howto.php';
}
/**
* Render the changelog tab content
*/
private function renderChangelogTab(): void
{
try {
$changelogTab = new ChangelogSettingsTab($this->cryptX->getConfig());
$changelogTab->render();
} catch (\Exception $e) {
error_log('CryptX Changelog Tab Error: ' . $e->getMessage());
add_settings_error(
'cryptx_messages',
'cryptx_error',
__('An error occurred while loading the changelog.', 'cryptx'),
'error'
);
}
}
}