Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

deploy from git

git-svn-id: http://plugins.svn.wordpress.org/scb-framework/trunk@595026 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information...
commit 882694cfa79c5854285c263c096dfedce8342025 1 parent 4abfc08
@scribu authored
View
20 scb/AdminPage.php
@@ -127,7 +127,7 @@ function page_help(){}
function page_header() {
echo "<div class='wrap'>\n";
screen_icon( $this->args['screen_icon'] );
- echo "<h2>" . $this->args['page_title'] . "</h2>\n";
+ echo html( "h2", $this->args['page_title'] );
}
// This is where the page content goes
@@ -274,20 +274,6 @@ function table_row( $args, $formdata = false ) {
return $this->row_wrap( $args['title'], $this->input( $args, $formdata ) );
}
- // Wraps the given content in a <table>
- function table_wrap( $content ) {
- return
- html( 'table class="form-table"', $content );
- }
-
- // Wraps the given content in a <tr><td>
- function row_wrap( $title, $content ) {
- return
- html( 'tr',
- html( 'th scope="row"', $title )
- .html( 'td', $content ) );
- }
-
// Mimic scbForms inheritance
function __call( $method, $args ) {
if ( in_array( $method, array( 'input', 'form' ) ) ) {
@@ -303,12 +289,12 @@ function __call( $method, $args ) {
// Wraps a string in a <script> tag
function js_wrap( $string ) {
- return "\n<script type='text/javascript'>\n" . $string . "\n</script>\n";
+ return html( "script type='text/javascript'", $string );
}
// Wraps a string in a <style> tag
function css_wrap( $string ) {
- return "\n<style type='text/css'>\n" . $string . "\n</style>\n";
+ return html( "style type='text/css'", $string );
}
View
771 scb/Forms.php
@@ -6,83 +6,20 @@ class scbForms {
const TOKEN = '%input%';
- protected static $cur_name;
-
- function input_with_value( $args, $value ) {
- if ( is_null( $value ) && isset( $args['default'] ) )
- $value = $args['default'];
-
- if ( !is_null( $value ) ) {
- switch ( $args['type'] ) {
- case 'select':
- case 'radio':
- $args['selected'] = $value;
- break;
- case 'checkbox':
- if ( is_array( $value ) )
- $args['checked'] = $value;
- else
- $args['checked'] = ( $value || ( isset( $args['value'] ) && $value == $args['value'] ) );
- break;
- default:
- $args['value'] = $value;
- }
- }
+ static function input_with_value( $args, $value ) {
+ $field = scbFormField::create( $args );
- return self::input( $args );
+ return $field->render( $value );
}
- static function input( $args, $formdata = false ) {
- if ( false !== $formdata ) {
- $form = new scbForm( $formdata );
- return $form->input( $args );
- }
-
- if ( empty( $args['name'] ) ) {
- return trigger_error( 'Empty name', E_USER_WARNING );
- }
-
- $args = wp_parse_args( $args, array(
- 'desc' => '',
- 'desc_pos' => 'after',
- 'wrap' => self::TOKEN,
- 'wrap_each' => self::TOKEN,
- ) );
+ static function input( $args, $formdata = null ) {
+ $field = scbFormField::create( $args );
- if ( isset( $args['value'] ) && is_array( $args['value'] ) ) {
- $args['values'] = $args['value'];
- unset( $args['value'] );
- }
-
- if ( isset( $args['extra'] ) && !is_array( $args['extra'] ) )
- $args['extra'] = shortcode_parse_atts( $args['extra'] );
-
- self::$cur_name = self::get_name( $args['name'] );
-
- switch ( $args['type'] ) {
- case 'select':
- case 'radio':
- $input = self::_single_choice( $args );
- break;
- case 'checkbox':
- if ( isset( $args['values'] ) )
- $input = self::_multiple_choice( $args );
- else
- $input = self::_checkbox( $args );
- break;
- default:
- $input = self::_input( $args );
- }
-
- return str_replace( self::TOKEN, $input, $args['wrap'] );
+ return $field->render( scbForms::get_value( $args['name'], $formdata ) );
}
-
-// ____________UTILITIES____________
-
-
// Generates a table wrapped in a form
- static function form_table( $rows, $formdata = NULL ) {
+ static function form_table( $rows, $formdata = null ) {
$output = '';
foreach ( $rows as $row )
$output .= self::table_row( $row, $formdata );
@@ -93,7 +30,7 @@ static function form_table( $rows, $formdata = NULL ) {
}
// Generates a form
- static function form( $inputs, $formdata = NULL, $nonce ) {
+ static function form( $inputs, $formdata = null, $nonce ) {
$output = '';
foreach ( $inputs as $input )
$output .= self::input( $input, $formdata );
@@ -104,7 +41,7 @@ static function form( $inputs, $formdata = NULL, $nonce ) {
}
// Generates a table
- static function table( $rows, $formdata = NULL ) {
+ static function table( $rows, $formdata = null ) {
$output = '';
foreach ( $rows as $row )
$output .= self::table_row( $row, $formdata );
@@ -115,7 +52,7 @@ static function table( $rows, $formdata = NULL ) {
}
// Generates a table row
- static function table_row( $args, $formdata = NULL ) {
+ static function table_row( $args, $formdata = null ) {
return self::row_wrap( $args['title'], self::input( $args, $formdata ) );
}
@@ -123,165 +60,315 @@ static function table_row( $args, $formdata = NULL ) {
// ____________WRAPPERS____________
- // Wraps the given content in a <form><table>
static function form_table_wrap( $content, $nonce = 'update_options' ) {
- $output = self::table_wrap( $content );
- $output = self::form_wrap( $output, $nonce );
-
- return $output;
+ return self::form_wrap( self::table_wrap( $content ), $nonce );
}
- // Wraps the given content in a <form> tag
static function form_wrap( $content, $nonce = 'update_options' ) {
- $output = "\n<form method='post' action=''>\n";
- $output .= $content;
- $output .= wp_nonce_field( $action = $nonce, $name = "_wpnonce", $referer = true , $echo = false );
- $output .= "\n</form>\n";
-
- return $output;
+ return html( "form method='post' action=''",
+ $content,
+ wp_nonce_field( $nonce, '_wpnonce', $referer = true, $echo = false )
+ );
}
- // Wraps the given content in a <table>
static function table_wrap( $content ) {
- $output = "\n<table class='form-table'>\n" . $content . "\n</table>\n";
-
- return $output;
+ return html( "table class='form-table'", $content );
}
- // Wraps the given content in a <tr><td>
static function row_wrap( $title, $content ) {
- return "\n<tr>\n\t<th scope='row'>" . $title . "</th>\n\t<td>\n\t\t" . $content . "\t</td>\n\n</tr>";
+ return html( "tr",
+ html( "th scope='row'", $title ),
+ html( "td", $content )
+ );
}
// ____________PRIVATE METHODS____________
- private static function _single_choice( $args ) {
- $args = wp_parse_args( $args, array(
- 'numeric' => false, // use numeric array instead of associative
- 'selected' => array( 'foo' ), // hack to make default blank
- ) );
+// Utilities
- self::_expand_values( $args );
- if ( 'select' == $args['type'] )
- return self::_select( $args );
- else
- return self::_radio( $args );
+ /**
+ * Generates the proper string for a name attribute.
+ *
+ * @param array|string $name The raw name
+ *
+ * @return string
+ */
+ static function get_name( $name ) {
+ $name = (array) $name;
+
+ $name_str = array_shift( $name );
+
+ foreach ( $name as $key ) {
+ $name_str .= '[' . esc_attr( $key ) . ']';
+ }
+
+ return $name_str;
}
- private static function _multiple_choice( $args ) {
- $args = wp_parse_args( $args, array(
- 'numeric' => false, // use numeric array instead of associative
- 'checked' => null,
- ) );
+ /**
+ * Traverses the formdata and retrieves the correct value.
+ *
+ * @param array|string $name The name of the value
+ * @param array $value The data that will be traversed
+ * @param mixed $fallback The value returned when the key is not found
+ *
+ * @return mixed
+ */
+ static function get_value( $name, $value, $fallback = null ) {
+ foreach ( (array) $name as $key ) {
+ if ( !isset( $value[ $key ] ) )
+ return $fallback;
- self::$cur_name .= '[]';
+ $value = $value[$key];
+ }
- self::_expand_values( $args );
+ return $value;
+ }
- extract( $args );
+ /**
+ * Given a list of fields, validate some data.
+ *
+ * @param array $fields List of args that would be sent to scbForms::input()
+ * @param array $data The data to validate. Defaults to $_POST
+ * @param array $to_update Existing data to populate. Necessary for nested values
+ *
+ * @return array
+ */
+ static function validate_post_data( $fields, $data = null, $to_update = array() ) {
+ if ( null === $data ) {
+ $data = stripslashes_deep( $_POST );
+ }
- if ( !is_array( $checked ) )
- $checked = array();
+ foreach ( $fields as $field ) {
+ $value = scbForms::get_value( $field['name'], $data );
- $opts = '';
- foreach ( $values as $value => $title ) {
- $single_input = self::_checkbox( array(
- 'type' => 'checkbox',
- 'value' => $value,
- 'checked' => in_array( $value, $checked ),
- 'desc' => $title,
- 'desc_pos' => 'after'
- ) );
+ $fieldObj = scbFormField::create( $field );
+
+ $value = $fieldObj->validate( $value );
- $opts .= str_replace( self::TOKEN, $single_input, $args['wrap_each'] );
+ if ( null !== $value )
+ self::set_value( $to_update, $field['name'], $value );
}
- return self::add_desc( $opts, $desc, $desc_pos );
+ return $to_update;
}
- private static function _expand_values( &$args ) {
- $values =& $args['values'];
+ /**
+ * For multiple-choice fields, we can never distinguish between "never been set" and "set to none".
+ * For single-choice fields, we can't distinguish either, because of how self::update_meta() works.
+ * Therefore, the 'default' parameter is always ignored.
+ *
+ * @param array $args Field arguments.
+ * @param int $object_id The object ID the metadata is attached to
+ * @param string $meta_type
+ *
+ * @return string
+ */
+ static function input_from_meta( $args, $object_id, $meta_type = 'post' ) {
+ $single = ( 'checkbox' != $args['type'] );
- if ( !empty( $values ) && !self::is_associative( $values ) ) {
- if ( is_array( $args['desc'] ) ) {
- $values = array_combine( $values, $args['desc'] ); // back-compat
- $args['desc'] = false;
- } elseif ( !isset( $args['numeric'] ) || !$args['numeric'] ) {
- $values = array_combine( $values, $values );
+ $key = (array) $args['name'];
+ $key = end( $key );
+
+ $value = get_metadata( $meta_type, $object_id, $key, $single );
+
+ return self::input_with_value( $args, $value );
+ }
+
+ static function update_meta( $fields, $data, $object_id, $meta_type = 'post' ) {
+ foreach ( $fields as $field_args ) {
+ $key = $field_args['name'];
+
+ if ( 'checkbox' == $field_args['type'] ) {
+ $new_values = isset( $data[$key] ) ? $data[$key] : array();
+
+ $old_values = get_metadata( $meta_type, $object_id, $key );
+
+ foreach ( array_diff( $new_values, $old_values ) as $value )
+ add_metadata( $meta_type, $object_id, $key, $value );
+
+ foreach ( array_diff( $old_values, $new_values ) as $value )
+ delete_metadata( $meta_type, $object_id, $key, $value );
+ } else {
+ $value = $data[$key];
+
+ if ( '' === $value )
+ delete_metadata( $meta_type, $object_id, $key );
+ else
+ update_metadata( $meta_type, $object_id, $key, $value );
}
}
}
- private static function _radio( $args ) {
- extract( $args );
+ private static function set_value( &$arr, $name, $value ) {
+ $name = (array) $name;
- if ( array( 'foo' ) == $selected ) {
- // radio buttons should always have one option selected
- $selected = key( $values );
+ $final_key = array_pop( $name );
+
+ while ( !empty( $name ) ) {
+ $key = array_shift( $name );
+
+ if ( !isset( $arr[ $key ] ) )
+ $arr[ $key ] = array();
+
+ $arr =& $arr[ $key ];
}
- $opts = '';
- foreach ( $values as $value => $title ) {
- $single_input = self::_checkbox( array(
- 'type' => 'radio',
- 'value' => $value,
- 'checked' => ( (string) $value == (string) $selected ),
- 'desc' => $title,
- 'desc_pos' => 'after'
- ) );
+ $arr[ $final_key ] = $value;
+ }
+}
+
+
+/**
+ * A wrapper for scbForms, containing the formdata
+ */
+class scbForm {
+ protected $data = array();
+ protected $prefix = array();
- $opts .= str_replace( self::TOKEN, $single_input, $args['wrap_each'] );
+ function __construct( $data, $prefix = false ) {
+ if ( is_array( $data ) )
+ $this->data = $data;
+
+ if ( $prefix )
+ $this->prefix = (array) $prefix;
+ }
+
+ function traverse_to( $path ) {
+ $data = scbForms::get_value( $path, $this->data );
+
+ $prefix = array_merge( $this->prefix, (array) $path );
+
+ return new scbForm( $data, $prefix );
+ }
+
+ function input( $args ) {
+ $value = scbForms::get_value( $args['name'], $this->data );
+
+ if ( !empty( $this->prefix ) ) {
+ $args['name'] = array_merge( $this->prefix, (array) $args['name'] );
}
- return self::add_desc( $opts, $desc, $desc_pos );
+ return scbForms::input_with_value( $args, $value );
}
+}
- private static function _select( $args ) {
- extract( wp_parse_args( $args, array(
- 'text' => false,
- 'extra' => array()
- ) ) );
- $options = array();
+interface scbFormField_I {
- if ( false !== $text ) {
- $options[] = array(
- 'value' => '',
- 'selected' => ( $selected == array( 'foo' ) ),
- 'title' => $text
- );
+ /**
+ * Generate the corresponding HTML for a field
+ *
+ * @param mixed $value The value to use
+ *
+ * @return string
+ */
+ function render( $value = null );
+
+ /**
+ * Validates a value against a field.
+ *
+ * @param mixed $value The value to check
+ *
+ * @return mixed null if the validation failed, sanitized value otherwise.
+ */
+ function validate( $value );
+}
+
+
+abstract class scbFormField implements scbFormField_I {
+
+ protected $args;
+
+ public static function create( $args ) {
+ if ( is_a( $args, 'scbFormField_I' ) )
+ return $args;
+
+ if ( empty( $args['name'] ) ) {
+ return trigger_error( 'Empty name', E_USER_WARNING );
}
- foreach ( $values as $value => $title ) {
- $options[] = array(
- 'value' => $value,
- 'selected' => ( (string) $value == (string) $selected ),
- 'title' => $title
- );
+ if ( isset( $args['value'] ) && is_array( $args['value'] ) ) {
+ $args['choices'] = $args['value'];
+ unset( $args['value'] );
}
- $opts = '';
- foreach ( $options as $option ) {
- extract( $option );
+ if ( isset( $args['values'] ) ) {
+ $args['choices'] = $args['values'];
+ unset( $args['values'] );
+ }
- $opts .= html( 'option', compact( 'value', 'selected' ), $title );
+ if ( isset( $args['extra'] ) && !is_array( $args['extra'] ) )
+ $args['extra'] = shortcode_parse_atts( $args['extra'] );
+
+ $args = wp_parse_args( $args, array(
+ 'desc' => '',
+ 'desc_pos' => 'after',
+ 'wrap' => scbForms::TOKEN,
+ 'wrap_each' => scbForms::TOKEN,
+ ) );
+
+ // depends on $args['desc']
+ if ( isset( $args['choices'] ) )
+ self::_expand_choices( $args );
+
+ switch ( $args['type'] ) {
+ case 'radio':
+ return new scbRadiosField( $args );
+ case 'select':
+ return new scbSelectField( $args );
+ case 'checkbox':
+ if ( isset( $args['choices'] ) )
+ return new scbMultipleChoiceField( $args );
+ else
+ return new scbSingleCheckboxField( $args );
+ case 'custom':
+ return new scbCustomField( $args );
+ default:
+ return new scbTextField( $args );
}
+ }
- $extra['name'] = self::$cur_name;
+ protected function __construct( $args ) {
+ $this->args = $args;
+ }
- $input = html( 'select', $extra, $opts );
+ public function __get( $key ) {
+ return $this->args[ $key ];
+ }
- return self::add_label( $input, $desc, $desc_pos );
+ public function __isset( $key ) {
+ return isset( $this->args[ $key ] );
+ }
+
+ public function render( $value = null ) {
+ if ( null === $value && isset( $this->default ) )
+ $value = $this->default;
+
+ $args = $this->args;
+
+ if ( null !== $value )
+ $this->_set_value( $args, $value );
+
+ $args['name'] = scbForms::get_name( $args['name'] );
+
+ return str_replace( scbForms::TOKEN, $this->_render( $args ), $this->wrap );
}
+ // Mutate the field arguments so that the value passed is rendered.
+ abstract protected function _set_value( &$args, $value );
+
+ // The actual rendering
+ abstract protected function _render( $args );
+
// Handle args for a single checkbox or radio input
- private static function _checkbox( $args ) {
+ protected static function _checkbox( $args ) {
$args = wp_parse_args( $args, array(
'value' => true,
- 'desc' => NULL,
+ 'desc' => null,
'checked' => false,
'extra' => array(),
) );
@@ -298,33 +385,15 @@ private static function _checkbox( $args ) {
return self::_input_gen( $args );
}
- // Handle args for text inputs
- private static function _input( $args ) {
- $args = wp_parse_args( $args, array(
- 'value' => '',
- 'desc_pos' => 'after',
- 'extra' => array( 'class' => 'regular-text' ),
- ) );
-
- foreach ( $args as $key => &$val )
- $$key = &$val;
- unset( $val );
-
- if ( !isset( $extra['id'] ) && !is_array( $name ) && false === strpos( $name, '[' ) )
- $extra['id'] = $name;
-
- return self::_input_gen( $args );
- }
-
// Generate html with the final args
- private static function _input_gen( $args ) {
+ protected static function _input_gen( $args ) {
extract( wp_parse_args( $args, array(
- 'value' => NULL,
- 'desc' => NULL,
+ 'value' => null,
+ 'desc' => null,
'extra' => array()
) ) );
- $extra['name'] = self::$cur_name;
+ $extra['name'] = $name;
if ( 'textarea' == $type ) {
$input = html( 'textarea', $extra, esc_textarea( $value ) );
@@ -337,11 +406,11 @@ private static function _input_gen( $args ) {
return self::add_label( $input, $desc, $desc_pos );
}
- private static function add_label( $input, $desc, $desc_pos ) {
+ protected static function add_label( $input, $desc, $desc_pos ) {
return html( 'label', self::add_desc( $input, $desc, $desc_pos ) ) . "\n";
}
- private static function add_desc( $input, $desc, $desc_pos ) {
+ protected static function add_desc( $input, $desc, $desc_pos ) {
if ( empty( $desc ) )
return $input;
@@ -351,181 +420,251 @@ private static function add_desc( $input, $desc, $desc_pos ) {
return $input . ' ' . $desc;
}
+ private static function _expand_choices( &$args ) {
+ $choices =& $args['choices'];
-// Utilities
+ if ( !empty( $choices ) && !self::is_associative( $choices ) ) {
+ if ( is_array( $args['desc'] ) ) {
+ $choices = array_combine( $choices, $args['desc'] ); // back-compat
+ $args['desc'] = false;
+ } elseif ( !isset( $args['numeric'] ) || !$args['numeric'] ) {
+ $choices = array_combine( $choices, $choices );
+ }
+ }
+ }
+ private static function is_associative( $array ) {
+ $keys = array_keys( $array );
+ return array_keys( $keys ) !== $keys;
+ }
+}
- /**
- * Generates the proper string for a name attribute.
- *
- * @param array|string $name The raw name
- *
- * @return string
- */
- static function get_name( $name ) {
- $name = (array) $name;
- $name_str = array_shift( $name );
+class scbTextField extends scbFormField {
- foreach ( $name as $key ) {
- $name_str .= '[' . esc_attr( $key ) . ']';
- }
+ public function validate( $value ) {
+ $sanitize = isset( $this->sanitize ) ? $this->sanitize : 'wp_filter_kses';
- return $name_str;
+ return call_user_func( $sanitize, $value, $this );
}
- /**
- * Traverses the formdata and retrieves the correct value.
- *
- * @param array|string $name The name of the value
- * @param array $value The data that will be traversed
- * @param mixed $fallback The value returned when the key is not found
- *
- * @return mixed
- */
- static function get_value( $name, $value, $fallback = null ) {
- foreach ( (array) $name as $key ) {
- if ( !isset( $value[ $key ] ) )
- return $fallback;
+ protected function _render( $args ) {
+ $args = wp_parse_args( $args, array(
+ 'value' => '',
+ 'desc_pos' => 'after',
+ 'extra' => array( 'class' => 'regular-text' ),
+ ) );
- $value = $value[$key];
- }
+ foreach ( $args as $key => &$val )
+ $$key = &$val;
+ unset( $val );
- return $value;
+ if ( !isset( $extra['id'] ) && !is_array( $name ) && false === strpos( $name, '[' ) )
+ $extra['id'] = $name;
+
+ return scbFormField::_input_gen( $args );
}
- /**
- * Given a list of fields, extract the appropriate POST data and return it.
- *
- * @param array $fields List of args that would be sent to scbForms::input()
- * @param array $to_update Existing data to update
- *
- * @return array
- */
- static function validate_post_data( $fields, $to_update = array() ) {
- foreach ( $fields as $field ) {
- $value = scbForms::get_value( $field['name'], $_POST );
+ protected function _set_value( &$args, $value ) {
+ $args['value'] = $value;
+ }
+}
- $value = stripslashes_deep( $value );
- switch ( $field['type'] ) {
- case 'checkbox':
- if ( isset( $field['values'] ) && is_array( $field['values'] ) )
- $value = array_intersect( $field['values'], (array) $value );
- else
- $value = (bool) $value;
+abstract class scbSingleChoiceField extends scbFormField {
- break;
- case 'radio':
- case 'select':
- self::_expand_values( $field );
+ public function validate( $value ) {
+ if ( isset( $this->choices[ $value ] ) )
+ return $value;
- if ( !isset( $field['values'][ $value ] ) )
- continue 2;
- }
+ return null;
+ }
- self::set_value( $to_update, $field['name'], $value );
- }
+ protected function _render( $args ) {
+ $args = wp_parse_args( $args, array(
+ 'numeric' => false, // use numeric array instead of associative
+ 'selected' => array( 'foo' ), // hack to make default blank
+ ) );
- return $to_update;
+ return $this->_render_specific( $args );
}
- static function input_from_meta( $args, $object_id, $meta_type = 'post' ) {
- $single = ( 'checkbox' != $args['type'] );
+ protected function _set_value( &$args, $value ) {
+ $args['selected'] = $value;
+ }
- $key = (array) $args['name'];
- $key = end( $key );
+ abstract protected function _render_specific( $args );
+}
- $value = get_metadata( $meta_type, $object_id, $key );
- if ( empty( $value ) )
- $value = null;
- elseif( $single )
- $value = reset( $value );
+class scbSelectField extends scbSingleChoiceField {
- return self::input_with_value( $args, $value );
+ protected function _render_specific( $args ) {
+ extract( wp_parse_args( $args, array(
+ 'text' => false,
+ 'extra' => array()
+ ) ) );
+
+ $options = array();
+
+ if ( false !== $text ) {
+ $options[] = array(
+ 'value' => '',
+ 'selected' => ( $selected == array( 'foo' ) ),
+ 'title' => $text
+ );
+ }
+
+ foreach ( $choices as $value => $title ) {
+ $options[] = array(
+ 'value' => $value,
+ 'selected' => ( $value == $selected ),
+ 'title' => $title
+ );
+ }
+
+ $opts = '';
+ foreach ( $options as $option ) {
+ extract( $option );
+
+ $opts .= html( 'option', compact( 'value', 'selected' ), $title );
+ }
+
+ $extra['name'] = $name;
+
+ $input = html( 'select', $extra, $opts );
+
+ return scbFormField::add_label( $input, $desc, $desc_pos );
}
+}
- static function update_meta( $fields, $data, $object_id, $meta_type = 'post' ) {
- foreach ( $fields as $field_args ) {
- $key = $field_args['name'];
- if ( 'checkbox' == $field_args['type'] ) {
- $new_values = isset( $data[$key] ) ? $data[$key] : array();
+class scbRadiosField extends scbSelectField {
- $old_values = get_metadata( $meta_type, $object_id, $key );
+ protected function _render_specific( $args ) {
+ extract( $args );
- foreach ( array_diff( $new_values, $old_values ) as $value )
- add_metadata( $meta_type, $object_id, $key, $value );
+ if ( array( 'foo' ) == $selected ) {
+ // radio buttons should always have one option selected
+ $selected = key( $choices );
+ }
- foreach ( array_diff( $old_values, $new_values ) as $value )
- delete_metadata( $meta_type, $object_id, $key, $value );
- } else {
- $value = $data[$key];
+ $opts = '';
+ foreach ( $choices as $value => $title ) {
+ $single_input = scbFormField::_checkbox( array(
+ 'name' => $name,
+ 'type' => 'radio',
+ 'value' => $value,
+ 'checked' => ( $value == $selected ),
+ 'desc' => $title,
+ 'desc_pos' => 'after'
+ ) );
- if ( '' === $value )
- delete_metadata( $meta_type, $object_id, $key );
- else
- update_metadata( $meta_type, $object_id, $key, $value );
- }
+ $opts .= str_replace( scbForms::TOKEN, $single_input, $wrap_each );
}
+
+ return scbFormField::add_desc( $opts, $desc, $desc_pos );
}
+}
- private static function set_value( &$arr, $name, $value ) {
- $name = (array) $name;
- $final_key = array_pop( $name );
+class scbMultipleChoiceField extends scbFormField {
- while ( !empty( $name ) ) {
- $key = array_shift( $name );
+ public function validate( $value ) {
+ return array_intersect( array_keys( $this->choices ), (array) $value );
+ }
- if ( !isset( $arr[ $key ] ) )
- $arr[ $key ] = array();
+ protected function _render( $args ) {
+ $args = wp_parse_args( $args, array(
+ 'numeric' => false, // use numeric array instead of associative
+ 'checked' => null,
+ ) );
- $arr =& $arr[ $key ];
+ extract( $args );
+
+ if ( !is_array( $checked ) )
+ $checked = array();
+
+ $opts = '';
+ foreach ( $choices as $value => $title ) {
+ $single_input = scbFormField::_checkbox( array(
+ 'name' => $name . '[]',
+ 'type' => 'checkbox',
+ 'value' => $value,
+ 'checked' => in_array( $value, $checked ),
+ 'desc' => $title,
+ 'desc_pos' => 'after'
+ ) );
+
+ $opts .= str_replace( scbForms::TOKEN, $single_input, $wrap_each );
}
- $arr[ $final_key ] = $value;
+ return scbFormField::add_desc( $opts, $desc, $desc_pos );
}
- private static function is_associative( $array ) {
- $keys = array_keys( $array );
- return array_keys( $keys ) !== $keys;
+ protected function _set_value( &$args, $value ) {
+ $args['checked'] = (array) $value;
}
}
-/**
- * A wrapper for scbForms, containing the formdata
- */
-class scbForm {
- protected $data = array();
- protected $prefix = array();
+class scbSingleCheckboxField extends scbFormField {
- function __construct( $data, $prefix = false ) {
- if ( is_array( $data ) )
- $this->data = $data;
+ public function validate( $value ) {
+ return (bool) $value;
+ }
- if ( $prefix )
- $this->prefix = (array) $prefix;
+ protected function _render( $args ) {
+ $args = wp_parse_args( $args, array(
+ 'value' => true,
+ 'desc' => null,
+ 'checked' => false,
+ 'extra' => array(),
+ ) );
+
+ foreach ( $args as $key => &$val )
+ $$key = &$val;
+ unset( $val );
+
+ $extra['checked'] = $checked;
+
+ if ( is_null( $desc ) && !is_bool( $value ) )
+ $desc = str_replace( '[]', '', $value );
+
+ return scbFormField::_input_gen( $args );
}
- function traverse_to( $path ) {
- $data = scbForms::get_value( $path, $this->data );
+ protected function _set_value( &$args, $value ) {
+ $args['checked'] = ( $value || ( isset( $args['value'] ) && $value == $args['value'] ) );
+ }
+}
- $prefix = array_merge( $this->prefix, (array) $path );
- return new scbForm( $data, $prefix );
+class scbCustomField implements scbFormField_I {
+
+ protected $args;
+
+ function __construct( $args ) {
+ $this->args = wp_parse_args( $args, array(
+ 'render' => 'var_dump',
+ 'sanitize' => 'wp_filter_kses',
+ ) );
}
- function input( $args ) {
- $value = scbForms::get_value( $args['name'], $this->data );
+ public function __get( $key ) {
+ return $this->args[ $key ];
+ }
- if ( !empty( $this->prefix ) ) {
- $args['name'] = array_merge( $this->prefix, (array) $args['name'] );
- }
+ public function __isset( $key ) {
+ return isset( $this->args[ $key ] );
+ }
- return scbForms::input_with_value( $args, $value );
+ public function render( $value = null ) {
+ return call_user_func( $this->render, $value, $this );
+ }
+
+ public function validate( $value ) {
+ return call_user_func( $this->sanitize, $value, $this );
}
}
View
34 scb/Options.php
@@ -35,27 +35,25 @@ public function get_key() {
}
/**
- * Get option values for one, many or all fields
+ * Get option values for one or all fields
*
- * @param string|array $field The field(s) to get
+ * @param string|array $field The field to get
* @return mixed Whatever is in those fields
*/
- public function get( $field = '' ) {
- $data = get_option( $this->key, array() );
+ public function get( $field = null, $default = null ) {
+ $data = array_merge( $this->defaults, get_option( $this->key, array() ) );
- $data = array_merge( $this->defaults, $data );
-
- return $this->_get( $field, $data );
+ return scbForms::get_value( $field, $data, $default );
}
/**
- * Get default values for one, many or all fields
+ * Get default values for one or all fields
*
- * @param string|array $field The field( s ) to get
+ * @param string|array $field The field to get
* @return mixed Whatever is in those fields
*/
- public function get_defaults( $field = '' ) {
- return $this->_get( $field, $this->defaults );
+ public function get_defaults( $field = null ) {
+ return scbForms::get_value( $field, $this->defaults );
}
/**
@@ -89,7 +87,7 @@ public function reset() {
* @return bool
*/
public function cleanup() {
- $this->update( $this->_clean( $this->get() ) );
+ $this->update( $this->get(), true );
}
/**
@@ -129,19 +127,7 @@ private function _clean( $data ) {
return wp_array_slice_assoc( $data, array_keys( $this->defaults ) );
}
- // Get one, more or all fields from an array
private function &_get( $field, $data ) {
- if ( empty( $field ) )
- return $data;
-
- if ( is_string( $field ) )
- return $data[$field];
-
- foreach ( $field as $key )
- if ( isset( $data[$key] ) )
- $result[] = $data[$key];
-
- return $result;
}
// Magic method: $options->field
View
18 scb/Util.php
@@ -119,7 +119,7 @@ static function split_at( $delim, $str ) {
// Return a standard admin notice
function scb_admin_notice( $msg, $class = 'updated' ) {
- return "<div class='$class fade'><p>$msg</p></div>\n";
+ return html( "div class='$class fade'", html( "p", $msg ) );
}
// Transform a list of objects into an associative array
@@ -145,6 +145,8 @@ function scb_list_fold( $list, $key, $value ) {
*/
if ( ! function_exists( 'html' ) ):
function html( $tag ) {
+ static $SELF_CLOSING_TAGS = array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' );
+
$args = func_get_args();
$tag = array_shift( $args );
@@ -165,7 +167,7 @@ function html( $tag ) {
list( $closing ) = explode( ' ', $tag, 2 );
}
- if ( in_array( $closing, array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' ) ) ) {
+ if ( in_array( $closing, $SELF_CLOSING_TAGS ) ) {
return "<{$tag} />";
}
@@ -185,6 +187,18 @@ function html_link( $url, $title = '' ) {
}
endif;
+function scb_get_query_flags( $wp_query = null ) {
+ if ( !$wp_query )
+ $wp_query = $GLOBALS['wp_query'];
+
+ $flags = array();
+ foreach ( get_object_vars( $wp_query ) as $key => $val ) {
+ if ( 'is_' == substr( $key, 0, 3 ) && $val )
+ $flags[] = substr( $key, 3 );
+ }
+
+ return $flags;
+}
//_____Compatibility layer_____
View
8 scb/load.php
@@ -1,6 +1,6 @@
<?php
-$GLOBALS['_scb_data'] = array( 53, __FILE__, array(
+$GLOBALS['_scb_data'] = array( 57, __FILE__, array(
'scbUtil', 'scbOptions', 'scbForms', 'scbTable',
'scbWidget', 'scbAdminPage', 'scbBoxesPage',
'scbCron', 'scbHooks',
@@ -32,8 +32,10 @@ static function init( $callback = '' ) {
add_action( 'activate_plugin', array( __CLASS__, 'delayed_activation' ) );
}
- // TODO: don't load when activating a plugin ?
- add_action( 'plugins_loaded', array( __CLASS__, 'load' ), 9, 0 );
+ if ( did_action( 'plugins_loaded' ) )
+ self::load();
+ else
+ add_action( 'plugins_loaded', array( __CLASS__, 'load' ), 9, 0 );
}
static function delayed_activation( $plugin ) {
Please sign in to comment.
Something went wrong with that request. Please try again.