Skip to content

Commit 0131c06

Browse files
committed
Refactor fields display
- Move description output to a method - Surround checkbox and radio with fieldset Signed-off-by: Roi Dayan <roi.dayan@gmail.com>
1 parent e6d5371 commit 0131c06

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/class.settings-api.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ function admin_init() {
147147
}
148148
}
149149

150+
/**
151+
* Get field description for display
152+
*
153+
* @param array $args settings field args
154+
*/
155+
public function get_field_description( $args ) {
156+
if ( ! empty( $args['desc'] ) ) {
157+
$desc = sprintf( '<p class="description">%s</p>', $args['desc'] );
158+
} else {
159+
$desc = '';
160+
}
161+
162+
return $desc;
163+
}
164+
150165
/**
151166
* Displays a text field for a settings field
152167
*
@@ -159,7 +174,7 @@ function callback_text( $args ) {
159174
$type = isset( $args['type'] ) ? $args['type'] : 'text';
160175

161176
$html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"/>', $type, $size, $args['section'], $args['id'], $value );
162-
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );
177+
$html .= $this->get_field_description( $args );
163178

164179
echo $html;
165180
}
@@ -191,9 +206,12 @@ function callback_checkbox( $args ) {
191206

192207
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
193208

194-
$html = sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
209+
$html = '<fieldset>';
210+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s]">', $args['section'], $args['id'] );
211+
$html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
195212
$html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked( $value, 'on', false ) );
196-
$html .= sprintf( '<label for="wpuf-%1$s[%2$s]"> %3$s</label>', $args['section'], $args['id'], $args['desc'] );
213+
$html .= sprintf( '%1$s</label>', $args['desc'] );
214+
$html .= '</fieldset>';
197215

198216
echo $html;
199217
}
@@ -207,13 +225,15 @@ function callback_multicheck( $args ) {
207225

208226
$value = $this->get_option( $args['id'], $args['section'], $args['std'] );
209227

210-
$html = '';
228+
$html = '<fieldset>';
211229
foreach ( $args['options'] as $key => $label ) {
212230
$checked = isset( $value[$key] ) ? $value[$key] : '0';
213-
$html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s"%4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );
214-
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
231+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
232+
$html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );
233+
$html .= sprintf( '%1$s</label><br>', $label );
215234
}
216-
$html .= sprintf( '<span class="description"> %s</label>', $args['desc'] );
235+
$html .= $this->get_field_description( $args );
236+
$html .= '</fieldset>';
217237

218238
echo $html;
219239
}
@@ -227,12 +247,14 @@ function callback_radio( $args ) {
227247

228248
$value = $this->get_option( $args['id'], $args['section'], $args['std'] );
229249

230-
$html = '';
250+
$html = '<fieldset>';
231251
foreach ( $args['options'] as $key => $label ) {
232-
$html .= sprintf( '<input type="radio" class="radio" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s"%4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
233-
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
252+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
253+
$html .= sprintf( '<input type="radio" class="radio" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
254+
$html .= sprintf( '%1$s</label><br>', $label );
234255
}
235-
$html .= sprintf( '<span class="description"> %s</label>', $args['desc'] );
256+
$html .= $this->get_field_description( $args );
257+
$html .= '</fieldset>';
236258

237259
echo $html;
238260
}
@@ -252,7 +274,7 @@ function callback_select( $args ) {
252274
$html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
253275
}
254276
$html .= sprintf( '</select>' );
255-
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );
277+
$html .= $this->get_field_description( $args );
256278

257279
echo $html;
258280
}
@@ -268,7 +290,7 @@ function callback_textarea( $args ) {
268290
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
269291

270292
$html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', $size, $args['section'], $args['id'], $value );
271-
$html .= sprintf( '<br><span class="description"> %s</span>', $args['desc'] );
293+
$html .= $this->get_field_description( $args );
272294

273295
echo $html;
274296
}
@@ -277,9 +299,10 @@ function callback_textarea( $args ) {
277299
* Displays a textarea for a settings field
278300
*
279301
* @param array $args settings field args
302+
* @return string
280303
*/
281304
function callback_html( $args ) {
282-
echo $args['desc'];
305+
echo $this->get_field_description( $args );
283306
}
284307

285308
/**
@@ -307,7 +330,7 @@ function callback_wysiwyg( $args ) {
307330

308331
echo '</div>';
309332

310-
echo sprintf( '<br><span class="description"> %s</span>', $args['desc'] );
333+
echo $this->get_field_description( $args );
311334
}
312335

313336
/**
@@ -323,8 +346,7 @@ function callback_file( $args ) {
323346

324347
$html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
325348
$html .= '<input type="button" class="button wpsa-browse" value="'.__( 'Browse' ).'" />';
326-
327-
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );
349+
$html .= $this->get_field_description( $args );
328350

329351
echo $html;
330352
}
@@ -340,7 +362,7 @@ function callback_password( $args ) {
340362
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
341363

342364
$html = sprintf( '<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
343-
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );
365+
$html .= $this->get_field_description( $args );
344366

345367
echo $html;
346368
}
@@ -356,7 +378,7 @@ function callback_color( $args ) {
356378
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
357379

358380
$html = sprintf( '<input type="text" class="%1$s-text wp-color-picker-field" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" />', $size, $args['section'], $args['id'], $value, $args['std'] );
359-
$html .= sprintf( '<span class="description" style="display:block;"> %s</span>', $args['desc'] );
381+
$html .= $this->get_field_description( $args );
360382

361383
echo $html;
362384
}

0 commit comments

Comments
 (0)