Skip to content

Commit 4daf073

Browse files
author
hsehszroc
committed
resolved sanitize_callback not working
1. breakdown field args accordingly to field type 2. recognizable variable naming 3. uncommented color-picker type 4. allow clear on select field with select2 5. added radio switch trigger js
1 parent e7951ef commit 4daf073

File tree

1 file changed

+104
-63
lines changed

1 file changed

+104
-63
lines changed

setting/setting-api.php

Lines changed: 104 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* -----------------------------------
1414
* DEVELOPED-MAINTAINED-SUPPPORTED BY
1515
* -----------------------------------
16-
* ███ ███╗ ████████████████
17-
* ███ ███║ ██████
18-
* ███ ███║ ╔══█████
19-
* ████████████║ ╚█████
20-
* ███║ ███║ █████
21-
* ███║ ███║ █████
16+
* ███ ███╗ ████████████████
17+
* ███ ███║ ═════════██████
18+
* ███ ███║ ╔══█████═╝
19+
* ████████████║ ╚█████
20+
* ███║═════███║ █████
21+
* ███║ ███║ █████═╝
2222
* ███║ ███║ ████████████████╗
23-
* ╚═╝ ╚═╝ ═══════════════
23+
* ╚═╝ ╚═╝ ═══════════════╝
2424
*/
2525

2626
namespace TheWebSolver\Plugin\Core\Framework;
@@ -136,25 +136,32 @@ public function register_setting() {
136136
if( $this->fields && is_array( $this->fields ) && sizeof( $this->fields ) > 0 ) {
137137

138138
// Registers settings fields
139-
foreach ( $this->fields as $section => $field ) {
139+
foreach ( $this->fields as $section_id => $fields ) {
140140

141-
foreach ( $field as $id => $option ) {
141+
foreach ( $fields as $field_id => $field_args ) {
142142

143-
// Sets callback function to display field HTML structure.
144-
$callback = isset( $option['callback'] ) ? $option['callback'] : [ __CLASS__, 'field_callback' ];
143+
// Gets and sets field data args from $field_args
144+
$args = $this->field_data( $section_id, $field_id, $field_args );
145+
$id = "{$section_id}[{$field_id}]";
146+
$callback = isset( $field_args['callback'] ) ? $field_args['callback'] : [ __CLASS__, 'field_callback' ];
145147

146-
// Gets field data args
147-
$args = $this->field_data( $section, $id, $option );
148+
148149

149150
// Adds new fields to each sections
150-
add_settings_field( "{$section}[{$id}]", $args['name'], $callback, $section, $section, $args );
151+
add_settings_field( "{$id}", $args['name'], $callback, $section_id, $section_id, $args );
151152
}
152153
}
153154
}
154155

155156
// Registers settings
156157
foreach ( $this->sections as $section ) {
157-
register_setting( $section['id'], $section['id'], [ 'sanitize_callback', [ $this, 'sanitize_options' ] ] );
158+
register_setting(
159+
$section['id'],
160+
$section['id'],
161+
[
162+
'sanitize_callback' => [ $this, 'sanitize_callback' ]
163+
]
164+
);
158165
}
159166
}
160167

@@ -198,37 +205,59 @@ private function section_callback( $section ) {
198205
*
199206
* @since 1.0
200207
*
201-
* @internal TODO: breakdown field args accordingly to field type
202-
*
203208
* @access private
204209
*/
205-
private function field_data( $section, $id, $option ) {
210+
private function field_data( $section_id, $field_id, $field_args ) {
206211

207-
$class = isset( $option['class'] ) ? $option['class'] : '';
208-
if( isset( $option['label'] ) ) {
209-
$label = $option['label'];
210-
} else {
211-
$label = 'hzfex_noLabel';
212-
}
212+
$class = isset( $field_args['class'] ) && ! empty( $field_args['class'] ) ? ' ' .$field_args['class'] : '';
213+
$label = isset( $field_args['label'] ) && ! empty( $field_args['label'] ) ? $field_args['label'] : $field_args['type'] . ' field';
213214

214215
$args = [
215-
'id' => $id,
216-
'class' => $id . ' ' . $class . ' ' .$label,
217-
'label_for' => "{$section}[{$id}]",
218-
'desc' => isset( $option['desc'] ) && ! empty( $option['desc'] ) ? $option['desc'] : '',
216+
'id' => $field_id,
217+
'class' => $field_id . '' . $class,
218+
'label_for' => "{$section_id}[{$field_id}]",
219+
'desc' => isset( $field_args['desc'] ) && ! empty( $field_args['desc'] ) ? $field_args['desc'] : '',
219220
'name' => $label,
220-
'section' => $section,
221-
'size' => isset( $option['size'] ) && ! empty( $option['size'] ) ? $option['size'] : null,
222-
'options' => isset( $option['options'] ) && ! empty( $option['options'] ) ? $option['options'] : '',
223-
'default' => isset( $option['default'] ) && ! empty( $option['default'] ) ? $option['default'] : '',
224-
'sanitize_callback' => isset( $option['sanitize_callback'] ) && ! empty( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
225-
'type' => isset( $option['type'] ) && ! empty( $option['type'] ) ? $option['type'] : 'text',
226-
'placeholder' => isset( $option['placeholder'] ) && ! empty( $option['placeholder'] ) ? $option['placeholder'] : '',
227-
'min' => isset( $option['min'] ) && ! empty( $option['min'] ) ? $option['min'] : '',
228-
'max' => isset( $option['max'] ) && ! empty( $option['max'] ) ? $option['max'] : '',
229-
'step' => isset( $option['step'] ) && ! empty( $option['step'] ) ? $option['step'] : '',
221+
'section' => $section_id,
222+
'sanitize_callback' => isset( $field_args['sanitize_callback'] ) && ! empty( $field_args['sanitize_callback'] ) ? $field_args['sanitize_callback'] : '',
223+
'type' => isset( $field_args['type'] ) && ! empty( $field_args['type'] ) ? $field_args['type'] : 'text',
224+
'placeholder' => isset( $field_args['placeholder'] ) && ! empty( $field_args['placeholder'] ) ? $field_args['placeholder'] : '',
230225
];
231226

227+
// Only set "min", "max" and "step" arg to number field type.
228+
if( $field_args['type'] == 'number' ) {
229+
$args['min'] = isset( $field_args['min'] ) && ! empty( $field_args['min'] ) ? $field_args['min'] : '';
230+
$args['max'] = isset( $field_args['max'] ) && ! empty( $field_args['max'] ) ? $field_args['max'] : '';
231+
$args['step'] = isset( $field_args['step'] ) && ! empty( $field_args['step'] ) ? $field_args['step'] : '';
232+
}
233+
234+
// Only set "rows" and "cols" arg to textarea field type.
235+
if( $field_args['type'] == 'textarea' ) {
236+
$args['rows'] = isset( $field_args['rows'] ) && ! empty( $field_args['rows'] ) ? $field_args['rows'] : '5';
237+
$args['cols'] = isset( $field_args['cols'] ) && ! empty( $field_args['cols'] ) ? $field_args['cols'] : '50';
238+
}
239+
240+
// Only set "options" arg to radio|select|multi_select|multi_checkbox field types.
241+
if(
242+
$field_args['type'] == 'radio' ||
243+
$field_args['type'] == 'select' ||
244+
$field_args['type'] == 'multi_select' ||
245+
$field_args['type'] == 'multi_checkbox'
246+
) {
247+
$args['options'] = isset( $field_args['options'] ) && ! empty( $field_args['options'] ) ? $field_args['options'] : '';
248+
}
249+
250+
// Set "default" arg to array if multi-checkbox field type, else set it to string.
251+
if( $field_args['type'] == 'multi_checkbox' ) {
252+
$args['default'] = isset( $field_args['default'] ) && is_array( $field_args['default'] ) && sizeof( $field_args['default'] ) > 0 ? $field_args['default'] : [];
253+
} else {
254+
$args['default'] = isset( $field_args['default'] ) && ! empty( $field_args['default'] ) ? $field_args['default'] : '';
255+
}
256+
257+
if( $field_args['type'] == 'wysiwyg' ) {
258+
$args['class'] = $args['class'] . ' hz_wysiwyg_field';
259+
}
260+
232261
return $args;
233262
}
234263

@@ -313,54 +342,57 @@ public static function get_field_description( $field ) {
313342
}
314343

315344
/**
316-
* Sanitize callback for Settings API
345+
* Sanitize callback for Settings fields
346+
*
347+
* @param array $pre_saved_values values that needs to be sanitized before saving
317348
*
318-
* @return array
349+
* @return array sanitized values that will be saved to database
319350
*
320351
* @since 1.0
321352
*
322353
* @access public
323354
*/
324-
public function sanitize_options( $options ) {
355+
public function sanitize_callback( $pre_saved_values = [] ) {
325356

326-
// bail early if no options
327-
if ( ! $options ) return $options;
357+
// bail early if no pre-saved values exist
358+
if ( ! $pre_saved_values || ! is_array( $pre_saved_values ) ) return $pre_saved_values;
328359

329-
foreach( $options as $slug => $value ) {
360+
// Loops through registering section fields pre-saved values that exists in $key => $value pair.
361+
foreach( $pre_saved_values as $key => $value ) {
330362

331-
$sanitize_callback = $this->get_sanitize_callback( $slug );
363+
$sanitize_callback = $this->get_sanitize_callback( $key );
332364

333-
// If callback is set, call it
365+
// If callback arg is set in each field, call it
334366
if ( $sanitize_callback ) {
335-
$options[$slug] = call_user_func( $sanitize_callback, $value ); continue;
367+
$pre_saved_values[$key] = call_user_func( $sanitize_callback, $value ); continue;
336368
}
337369
}
338370

339-
return $options;
371+
return $pre_saved_values;
340372
}
341373

342374
/**
343-
* Get sanitization callback for given option slug
375+
* Get sanitization callback for given field
344376
*
345-
* @param string $slug option slug
377+
* @param string $field_id the given field id
346378
*
347-
* @return string/bool callback name if found, false otherwise
379+
* @return string/bool callback function if found, false otherwise
348380
*
349381
* @since 1.0
350382
*
351-
* @access public
383+
* @access private
352384
*/
353-
public function get_sanitize_callback( $slug = '' ) {
385+
private function get_sanitize_callback( $key = '' ) {
354386

355387
// bail early if no slug
356-
if ( empty( $slug ) ) return false;
388+
if ( empty( $key ) ) return false;
357389

358-
// Iterate over registered fields and see if proper callback is found
359-
foreach( $this->settings_fields as $section => $options ) {
390+
// Loops through registering fields and see if proper callback arg is set
391+
foreach( $this->fields as $section_id => $options ) {
360392

361-
foreach ( $options as $option ) {
393+
foreach ( $options as $field_id => $option ) {
362394

363-
if ( $option['name'] != $slug ) continue;
395+
if ( $field_id != $key ) continue;
364396

365397
// Return the callback name
366398
return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
@@ -385,7 +417,7 @@ public function get_sanitize_callback( $slug = '' ) {
385417
*
386418
* @access public
387419
*/
388-
public static function get_option( $field, $section, $default = '' ) {
420+
public static function get_option( $field, $section, $default = false ) {
389421

390422
$options = get_option( $section );
391423

@@ -453,7 +485,7 @@ public function show_forms() {
453485
* define( 'HZFEX_SETTING_FRAMEWORK_DEBUG_MODE', true );
454486
*/
455487
if( defined( 'HZFEX_SETTING_FRAMEWORK_DEBUG_MODE' ) && HZFEX_SETTING_FRAMEWORK_DEBUG_MODE ) {
456-
echo '<div class="hzfex_debug_out"><h3>'.$section['title'].' Debug Output</h3><b>Section Data:</b><pre>', htmlspecialchars( print_r( $section, true ) ), '</pre></div>';
488+
echo '<div class="hzfex_debug_out"><h3>'.$section['tab_title'].' Debug Output</h3><b>Section Data:</b><pre>', htmlspecialchars( print_r( $section, true ) ), '</pre></div>';
457489
}
458490

459491
// // Gets section callback data.
@@ -545,7 +577,7 @@ public function script() {
545577
jQuery(document).ready(function($) {
546578

547579
//Initiate Color Picker
548-
// $('.wp-color-picker-field').wpColorPicker();
580+
$('.hz_color_picker_control').wpColorPicker();
549581

550582
// Switches option sections
551583
$('.group').hide();
@@ -620,12 +652,21 @@ function(){
620652
});
621653

622654
// enable select2 when necessary for "select" & "multi-select" field type
623-
if($('.hz_select2').length > 0) {
624-
$('.hz_select2').select2({
655+
if($('.hz_select_control .hz_select_control').length > 0) {
656+
$('.hz_select_control .hz_select_control').select2({
625657
width: '100%',
626658
placeholder: 'Select Options',
659+
allowClear: true,
627660
});
628661
}
662+
663+
// radio input field selection.
664+
$('input[type="radio"]').on('click', function() {
665+
$('input[type="radio"]').each(function(){
666+
$(this).closest('li').toggleClass('hz_radio_selected', this.checked);
667+
});
668+
});
669+
$('input:radio:checked').closest('li').addClass('hz_radio_selected');
629670
});
630671
</script>
631672

0 commit comments

Comments
 (0)