From 7908f8b49625af6c5dec1a158e5f229ff57c0167 Mon Sep 17 00:00:00 2001 From: Louis Wolmarans Date: Wed, 5 Nov 2025 11:50:15 +0200 Subject: [PATCH 1/2] fix: cache issues on settings page --- src/php/settings/settings.php | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/php/settings/settings.php b/src/php/settings/settings.php index 249bdc24..45813f82 100644 --- a/src/php/settings/settings.php +++ b/src/php/settings/settings.php @@ -11,6 +11,7 @@ use Code_Snippets\Welcome_API; use function Code_Snippets\clean_snippets_cache; use function Code_Snippets\code_snippets; +use const Code_Snippets\CACHE_GROUP; const CACHE_KEY = 'code_snippets_settings'; const OPTION_GROUP = 'code-snippets'; @@ -79,7 +80,7 @@ function are_settings_unified(): bool { * @return array> */ function get_settings_values(): array { - $settings = wp_cache_get( CACHE_KEY ); + $settings = wp_cache_get( CACHE_KEY, CACHE_GROUP ); if ( $settings ) { return $settings; } @@ -93,7 +94,7 @@ function get_settings_values(): array { } } - wp_cache_set( CACHE_KEY, $settings ); + wp_cache_set( CACHE_KEY, $settings, CACHE_GROUP ); return $settings; } @@ -125,7 +126,7 @@ function update_setting( string $section, string $field, $new_value ): bool { $settings[ $section ][ $field ] = $new_value; - wp_cache_set( CACHE_KEY, $settings ); + wp_cache_set( CACHE_KEY, $settings, CACHE_GROUP ); return update_self_option( are_settings_unified(), OPTION_NAME, $settings ); } @@ -136,17 +137,11 @@ function update_setting( string $section, string $field, $new_value ): bool { */ function get_settings_sections(): array { $sections = array( - 'general' => __( 'General', 'code-snippets' ), - 'editor' => __( 'Code Editor', 'code-snippets' ), - 'debug' => __( 'Debug', 'code-snippets' ), + 'general' => __( 'General', 'code-snippets' ), + 'editor' => __( 'Code Editor', 'code-snippets' ), + 'debug' => __( 'Debug', 'code-snippets' ), ); - // Only show the Version section when the debug setting to enable version changes is enabled. - $enable_version = get_setting( 'debug', 'enable_version_change' ); - if ( $enable_version ) { - $sections['version-switch'] = __( 'Version', 'code-snippets' ); - } - return apply_filters( 'code_snippets_settings_sections', $sections ); } @@ -174,13 +169,8 @@ function register_plugin_settings() { add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' ); } - // Register settings fields. Only register fields for sections that exist (some sections may be gated by settings). - $registered_sections = get_settings_sections(); + // Register settings fields. foreach ( get_settings_fields() as $section_id => $fields ) { - if ( ! isset( $registered_sections[ $section_id ] ) ) { - continue; - } - foreach ( $fields as $field_id => $field ) { $field_object = new Setting_Field( $section_id, $field_id, $field ); add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id ); @@ -306,7 +296,7 @@ function process_settings_actions( array $input ): ?array { * @return array> The validated settings. */ function sanitize_settings( array $input ): array { - wp_cache_delete( CACHE_KEY ); + wp_cache_delete( CACHE_KEY, CACHE_GROUP ); $result = process_settings_actions( $input ); if ( ! is_null( $result ) ) { From e30a6fdc8a83b7d64814d6a907a3e8b91f65a282 Mon Sep 17 00:00:00 2001 From: Louis Wolmarans Date: Wed, 5 Nov 2025 11:53:55 +0200 Subject: [PATCH 2/2] Update settings.php --- src/php/settings/settings.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/php/settings/settings.php b/src/php/settings/settings.php index 45813f82..4d906483 100644 --- a/src/php/settings/settings.php +++ b/src/php/settings/settings.php @@ -137,11 +137,17 @@ function update_setting( string $section, string $field, $new_value ): bool { */ function get_settings_sections(): array { $sections = array( - 'general' => __( 'General', 'code-snippets' ), - 'editor' => __( 'Code Editor', 'code-snippets' ), - 'debug' => __( 'Debug', 'code-snippets' ), + 'general' => __( 'General', 'code-snippets' ), + 'editor' => __( 'Code Editor', 'code-snippets' ), + 'debug' => __( 'Debug', 'code-snippets' ), ); + // Only show the Version section when the debug setting to enable version changes is enabled. + $enable_version = get_setting( 'debug', 'enable_version_change' ); + if ( $enable_version ) { + $sections['version-switch'] = __( 'Version', 'code-snippets' ); + } + return apply_filters( 'code_snippets_settings_sections', $sections ); } @@ -169,8 +175,13 @@ function register_plugin_settings() { add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' ); } - // Register settings fields. + // Register settings fields. Only register fields for sections that exist (some sections may be gated by settings). + $registered_sections = get_settings_sections(); foreach ( get_settings_fields() as $section_id => $fields ) { + if ( ! isset( $registered_sections[ $section_id ] ) ) { + continue; + } + foreach ( $fields as $field_id => $field ) { $field_object = new Setting_Field( $section_id, $field_id, $field ); add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id );