Skip to content

Commit 375df7c

Browse files
committed
Add max and min attributes to number settings
1 parent 12403f3 commit 375df7c

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

includes/settings/class-settings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private static function set_fields() {
6868
'type' => 'number',
6969
'label' => 'rows',
7070
'default' => 5,
71+
'min' => 0,
7172
),
7273

7374
array(
@@ -114,6 +115,7 @@ private static function set_fields() {
114115
'desc' => __( 'The width of a tab character.', 'code-snippets' ),
115116
'default' => 4,
116117
'codemirror' => 'tabSize',
118+
'min' => 0,
117119
),
118120

119121
array(
@@ -123,6 +125,7 @@ private static function set_fields() {
123125
'desc' => __( 'How many spaces a block should be indented.', 'code-snippets' ),
124126
'default' => 2,
125127
'codemirror' => 'indentUnit',
128+
'min' => 0,
126129
),
127130

128131
array(

includes/settings/settings-fields.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ function code_snippets_checkbox_field( $atts ) {
3939
function code_snippets_number_field( $atts ) {
4040

4141
printf(
42-
'<input type="number" name="code_snippets_settings[%s][%s]" value="%s">',
42+
'<input type="number" name="code_snippets_settings[%s][%s]" value="%s"',
4343
$atts['section'],
4444
$atts['id'],
4545
code_snippets_get_setting( $atts['section'], $atts['id'] )
4646
);
4747

48+
if ( isset( $atts['min'] ) ) {
49+
printf( ' min="%d"', $atts['min'] );
50+
}
51+
52+
if ( isset( $atts['max'] ) ) {
53+
printf( ' max="%d"', $atts['max'] );
54+
}
55+
56+
echo '>';
57+
4858
if ( ! empty( $atts['label'] ) ) {
4959
echo ' ' . $atts['label'];
5060
}

includes/settings/settings.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,18 @@ function code_snippets_register_settings() {
121121
}
122122

123123
/* Register settings fields */
124-
foreach ( code_snippets_get_settings_fields() as $section_id => $fields ) {
125-
124+
foreach ( Code_Snippets_Settings::get_fields() as $section_id => $fields ) {
126125
foreach ( $fields as $field ) {
126+
$atts = $field;
127+
$atts['section'] = $section_id;
128+
127129
add_settings_field(
128130
'code_snippets_' . $field['id'],
129131
$field['name'],
130132
"code_snippets_{$field['type']}_field",
131133
'code-snippets',
132134
'code-snippets-' . $section_id,
133-
array_merge( $field, array( 'section' => $section_id ) )
135+
$atts
134136
);
135137
}
136138
}

0 commit comments

Comments
 (0)