Skip to content

Commit 96f4373

Browse files
committed
version 1.0 launched
- Added "wpuf" prefix on input id's - wysiwyg bug fix where the rich text editor wasn't showing - removed the sanitize_text_field default sanitize callback removed. Was stripping off the html/line breaks from textarea and wysiwyg - some formatting
1 parent c066544 commit 96f4373

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

class.settings-api.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/**
44
* weDevs Settings API wrapper class
55
*
6+
* @version 1.0
7+
*
68
* @author Tareq Hasan <tareq@weDevs.com>
79
* @link http://tareq.weDevs.com Tareq's Planet
810
* @example settings-api.php How to use the class
@@ -172,8 +174,8 @@ function callback_checkbox( $args ) {
172174
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
173175

174176
$html = sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
175-
$html .= sprintf( '<input type="checkbox" class="checkbox" id="%1$s[%2$s]" name="%1$s[%2$s]" value="on"%4$s />', $args['section'], $args['id'], $value, checked( $value, 'on', false ) );
176-
$html .= sprintf( '<label for="%1$s[%2$s]"> %3$s</label>', $args['section'], $args['id'], $args['desc'] );
177+
$html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on"%4$s />', $args['section'], $args['id'], $value, checked( $value, 'on', false ) );
178+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s]"> %3$s</label>', $args['section'], $args['id'], $args['desc'] );
177179

178180
echo $html;
179181
}
@@ -190,8 +192,8 @@ function callback_multicheck( $args ) {
190192
$html = '';
191193
foreach ( $args['options'] as $key => $label ) {
192194
$checked = isset( $value[$key] ) ? $value[$key] : '0';
193-
$html .= sprintf( '<input type="checkbox" class="checkbox" id="%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 ) );
194-
$html .= sprintf( '<label for="%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
195+
$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 ) );
196+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
195197
}
196198
$html .= sprintf( '<span class="description"> %s</label>', $args['desc'] );
197199

@@ -209,8 +211,8 @@ function callback_radio( $args ) {
209211

210212
$html = '';
211213
foreach ( $args['options'] as $key => $label ) {
212-
$html .= sprintf( '<input type="radio" class="radio" id="%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 ) );
213-
$html .= sprintf( '<label for="%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
214+
$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 ) );
215+
$html .= sprintf( '<label for="wpuf-%1$s[%2$s][%4$s]"> %3$s</label><br>', $args['section'], $args['id'], $label, $key );
214216
}
215217
$html .= sprintf( '<span class="description"> %s</label>', $args['desc'] );
216218

@@ -269,12 +271,12 @@ function callback_html( $args ) {
269271
*/
270272
function callback_wysiwyg( $args ) {
271273

272-
$value = wpautop( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
274+
$value = $this->get_option( $args['id'], $args['section'], $args['std'] );
273275
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
274276

275277
echo '<div style="width: ' . $size . ';">';
276278

277-
wp_editor( $value, $args['section'] . '[' . $args['id'] . ']', array( 'teeny' => true, 'textarea_rows' => 10 ) );
279+
wp_editor( $value, $args['section'] . '-' . $args['id'] . '', array( 'teeny' => true, 'textarea_name' => $args['section'] . '[' . $args['id'] . ']', 'textarea_rows' => 10 ) );
278280

279281
echo '</div>';
280282

@@ -361,13 +363,8 @@ function sanitize_options( $options ) {
361363
$options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
362364
continue;
363365
}
364-
365-
// Treat everything that's not an array as a string
366-
if ( !is_array( $option_value ) ) {
367-
$options[ $option_slug ] = sanitize_text_field( $option_value );
368-
continue;
369-
}
370366
}
367+
371368
return $options;
372369
}
373370

@@ -379,17 +376,22 @@ function sanitize_options( $options ) {
379376
* @return mixed string or bool false
380377
*/
381378
function get_sanitize_callback( $slug = '' ) {
382-
if ( empty( $slug ) )
379+
if ( empty( $slug ) ) {
383380
return false;
381+
}
382+
384383
// Iterate over registered fields and see if we can find proper callback
385384
foreach( $this->settings_fields as $section => $options ) {
386385
foreach ( $options as $option ) {
387-
if ( $option['name'] != $slug )
386+
if ( $option['name'] != $slug ) {
388387
continue;
388+
}
389+
389390
// Return the callback name
390391
return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
391392
}
392393
}
394+
393395
return false;
394396
}
395397

0 commit comments

Comments
 (0)