Skip to content

Commit 99a7ec6

Browse files
committed
feat: add debug option to enable version change section
1 parent 5e45f11 commit 99a7ec6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/php/settings/settings-fields.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ function get_default_settings(): array {
5050
'version-switch' => [
5151
'selected_version' => '',
5252
],
53+
'debug' => [
54+
'enable_version_change' => false,
55+
],
5356
];
5457

5558
$defaults = apply_filters( 'code_snippets_settings_defaults', $defaults );
@@ -83,6 +86,11 @@ function get_settings_fields(): array {
8386
'type' => 'action',
8487
'desc' => __( 'Use this button to manually clear snippets caches.', 'code-snippets' ),
8588
],
89+
'enable_version_change' => [
90+
'name' => __( 'Version Change', 'code-snippets' ),
91+
'type' => 'checkbox',
92+
'label' => __( 'Enable the ability to switch or rollback versions of the Code Snippets core plugin.', 'code-snippets' ),
93+
],
8694
];
8795

8896
$fields['version-switch'] = [

src/php/settings/settings.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,14 @@ function get_settings_sections(): array {
139139
'general' => __( 'General', 'code-snippets' ),
140140
'editor' => __( 'Code Editor', 'code-snippets' ),
141141
'debug' => __( 'Debug', 'code-snippets' ),
142-
'version-switch' => __( 'Version', 'code-snippets' ),
143142
);
144143

144+
// Only show the Version section when the debug setting to enable version changes is enabled.
145+
$enable_version = get_setting( 'debug', 'enable_version_change' );
146+
if ( $enable_version ) {
147+
$sections['version-switch'] = __( 'Version', 'code-snippets' );
148+
}
149+
145150
return apply_filters( 'code_snippets_settings_sections', $sections );
146151
}
147152

@@ -169,8 +174,13 @@ function register_plugin_settings() {
169174
add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' );
170175
}
171176

172-
// Register settings fields.
177+
// Register settings fields. Only register fields for sections that exist (some sections may be gated by settings).
178+
$registered_sections = get_settings_sections();
173179
foreach ( get_settings_fields() as $section_id => $fields ) {
180+
if ( ! isset( $registered_sections[ $section_id ] ) ) {
181+
continue;
182+
}
183+
174184
foreach ( $fields as $field_id => $field ) {
175185
$field_object = new Setting_Field( $section_id, $field_id, $field );
176186
add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id );

0 commit comments

Comments
 (0)