config = $config;
}
public function render(): void {
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD']
&& isset( $_POST['toc_settings_nonce'] )
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['toc_settings_nonce'] ) ), 'contai_toc_settings_save' )
) {
$this->handleSave();
}
$current = $this->config->getAll();
?>
References -
Chapter * -
* Summary -
config->reset();
$this->purgePageCaches();
$this->showNotice(__('Settings reset to defaults successfully.', '1platform-content-ai'), 'success');
return;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- each field is sanitized individually below
$raw_post = wp_unslash( $_POST );
$data = [
'enabled' => isset($raw_post['enabled']),
'post_types' => isset($raw_post['post_types']) ? array_map('sanitize_text_field', (array) $raw_post['post_types']) : [],
'heading_levels' => isset($raw_post['heading_levels']) ? array_map('intval', (array) $raw_post['heading_levels']) : [],
'min_headings' => intval($raw_post['min_headings'] ?? 4),
'position' => sanitize_text_field($raw_post['position'] ?? 'before_first_heading'),
'title' => sanitize_text_field($raw_post['title'] ?? 'Table of Contents'),
'show_title' => isset($raw_post['show_title']),
'show_toggle' => isset($raw_post['show_toggle']),
'initial_state' => sanitize_text_field($raw_post['initial_state'] ?? 'show'),
'show_hierarchy' => isset($raw_post['show_hierarchy']),
'numbered_list' => isset($raw_post['numbered_list']),
'exclude_patterns' => sanitize_textarea_field($raw_post['exclude_patterns'] ?? ''),
'theme' => sanitize_text_field($raw_post['theme'] ?? 'grey'),
'lowercase_anchors' => isset($raw_post['lowercase_anchors']),
'hyphenate_anchors' => isset($raw_post['hyphenate_anchors']),
'smooth_scroll' => isset($raw_post['smooth_scroll']),
];
if ($this->config->update($data)) {
$this->purgePageCaches();
$this->showNotice(__('Settings saved successfully.', '1platform-content-ai'), 'success');
} else {
$this->showNotice(__('Failed to save settings.', '1platform-content-ai'), 'error');
}
}
private function purgePageCaches(): void {
// LiteSpeed Cache
if (has_action('litespeed_purge_all')) {
do_action('litespeed_purge_all');
}
// WP Rocket
if (function_exists('rocket_clean_domain')) {
rocket_clean_domain();
}
// WP Super Cache
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache();
}
// W3 Total Cache
if (function_exists('w3tc_flush_posts')) {
w3tc_flush_posts();
}
// Cachify
if (has_action('cachify_flush_cache')) {
do_action('cachify_flush_cache');
}
// WP Fastest Cache
if (function_exists('wpfc_clear_all_cache')) {
wpfc_clear_all_cache();
}
// Autoptimize
if (class_exists('autoptimizeCache') && method_exists('autoptimizeCache', 'clearall')) {
\autoptimizeCache::clearall();
}
}
private function showNotice(string $message, string $type = 'success'): void {
add_settings_error('toc_settings', 'toc_settings_message', $message, $type);
settings_errors('toc_settings');
}
private function getAvailablePostTypes(): array {
$types = get_post_types(['public' => true], 'objects');
$result = [];
foreach ($types as $type) {
$result[$type->name] = $type->label;
}
return $result;
}
private function getThemes(): array {
return [
'grey' => __('Grey', '1platform-content-ai'),
'light-blue' => __('Light Blue', '1platform-content-ai'),
'white' => __('White', '1platform-content-ai'),
'black' => __('Black', '1platform-content-ai'),
'transparent' => __('Transparent', '1platform-content-ai'),
];
}
}