Skip to content

Commit

Permalink
Fix missing 'attributes' for 'input' field. Reformat code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh committed Nov 18, 2015
1 parent bd2b2cc commit acfe5c0
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 94 deletions.
16 changes: 8 additions & 8 deletions inc/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static function normalize_field( $field )
'attributes' => array()
) );
}

/**
* Renders an attribute array into an html attributes string
*
Expand All @@ -412,16 +412,16 @@ static function normalize_field( $field )
static function render_attributes( $attributes )
{
$attr_string = '';
foreach( $attributes as $key => $value )
foreach ( $attributes as $key => $value )
{
if( $value )
if ( $value )
{
$attr_string .= sprintf(
' %s="%s"',
$key,
esc_attr( $value )
$attr_string .= sprintf(
' %s="%s"',
$key,
esc_attr( $value )
);
}
}
}
return $attr_string;
}
Expand Down
10 changes: 5 additions & 5 deletions inc/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ static function admin_enqueue_scripts()
*/
static function html( $meta, $field )
{
$attributes = $field['attributes'];
$attributes = $field['attributes'];
$attributes['value'] = 1;
return sprintf(
'<input %s value="1" %s>',
self::render_attributes( $attributes ),
checked( ! empty( $meta ), 1, false )
);
}

/**
* Normalize parameters for field
*
Expand All @@ -47,9 +47,9 @@ static function html( $meta, $field )
*/
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );
$field['attributes']['list'] = FALSE;
$field['attributes']['type'] = 'checkbox';
$field = parent::normalize_field( $field );
$field['attributes']['list'] = false;
$field['attributes']['type'] = 'checkbox';
$field['attributes']['class'] = 'rwmb-checkbox';

return $field;
Expand Down
24 changes: 12 additions & 12 deletions inc/fields/color.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ static function value( $new, $old, $post_id, $field )
static function normalize_field( $field )
{
$field = wp_parse_args( $field, array(
'size' => 7,
'maxlength' => 7,
'pattern' => '^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$',
'js_options' => array()
'size' => 7,
'maxlength' => 7,
'pattern' => '^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$',
'js_options' => array(),
) );

$field['js_options'] = wp_parse_args( $field['js_options'], array(
'defaultColor' => FALSE,
'hide' => TRUE,
'palettes' => TRUE,
'defaultColor' => false,
'hide' => true,
'palettes' => true,
) );

$field = parent::normalize_field( $field );

$field['attributes'] = wp_parse_args( $field['attributes'], array(
'data-options' => wp_json_encode( $field['js_options'] ),
'data-options' => wp_json_encode( $field['js_options'] ),
) );

$field['attributes']['class'] = 'rwmb-color';

return $field;
Expand Down
10 changes: 5 additions & 5 deletions inc/fields/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static function admin_enqueue_scripts()
$url = RWMB_CSS_URL . 'jqueryui';
wp_register_style( 'jquery-ui-core', "{$url}/jquery.ui.core.css", array(), '1.8.17' );
wp_register_style( 'jquery-ui-theme', "{$url}/jquery.ui.theme.css", array(), '1.8.17' );
wp_register_style( 'wp-datepicker', RWMB_CSS_URL ."datepicker.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'wp-datepicker', RWMB_CSS_URL . 'datepicker.css', array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_enqueue_style( 'jquery-ui-datepicker', "{$url}/jquery.ui.datepicker.css", array( 'wp-datepicker' ), '1.8.17' );

// Load localized scripts
Expand Down Expand Up @@ -62,13 +62,13 @@ static function normalize_field( $field )
'dateFormat' => empty( $field['format'] ) ? 'yy-mm-dd' : $field['format'],
'showButtonPanel' => true,
) );

$field['attributes'] = wp_parse_args( $field['attributes'], array(
'data-options' => wp_json_encode( $field['js_options'] ),
'data-options' => wp_json_encode( $field['js_options'] ),
) );

$field = parent::normalize_field( $field );

$field['attributes']['class'] = 'rwmb-date';

return $field;
Expand Down
19 changes: 10 additions & 9 deletions inc/fields/datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static function admin_enqueue_scripts()
wp_register_style( 'jquery-ui-core', "{$url}/jquery.ui.core.css", array(), '1.8.17' );
wp_register_style( 'jquery-ui-theme', "{$url}/jquery.ui.theme.css", array(), '1.8.17' );
wp_register_style( 'jquery-ui-datepicker', "{$url}/jquery.ui.datepicker.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'wp-datepicker', RWMB_CSS_URL ."datepicker.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'wp-datepicker', RWMB_CSS_URL . 'datepicker.css', array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'jquery-ui-slider', "{$url}/jquery.ui.slider.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_enqueue_style( 'jquery-ui-timepicker', "{$url}/jquery-ui-timepicker-addon.min.css", array( 'jquery-ui-datepicker', 'jquery-ui-slider', 'wp-datepicker' ), '1.5.0' );

Expand All @@ -56,7 +56,7 @@ static function admin_enqueue_scripts()
*
* Note: we use full locale (de-DE) and fallback to short locale (de)
*/
$locale = str_replace( '_', '-', get_locale() );
$locale = str_replace( '_', '-', get_locale() );
$locale_short = substr( $locale, 0, 2 );

wp_register_script( 'jquery-ui-timepicker-i18n', "{$url}/jquery-ui-timepicker-addon-i18n.min.js", array( 'jquery-ui-timepicker' ), '1.5.0', true );
Expand Down Expand Up @@ -119,8 +119,9 @@ static function value( $new, $old, $post_id, $field )
static function meta( $post_id, $saved, $field )
{
$meta = parent::meta( $post_id, $saved, $field );
if( is_array( $meta ) ) {
foreach( $meta as $key => $value )
if ( is_array( $meta ) )
{
foreach ( $meta as $key => $value )
{
$meta[$key] = $field['timestamp'] && $value ? date( self::translate_format( $field ), intval( $value ) ) : $value;
}
Expand All @@ -142,18 +143,18 @@ static function meta( $post_id, $saved, $field )
static function normalize_field( $field )
{
$field = wp_parse_args( $field, array(
'timestamp' => false,
'timestamp' => false,
) );

// Deprecate 'format', but keep it for backward compatible
// Use 'js_options' instead
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'timeFormat' => 'HH:mm',
'separator' => ' ',
'timeFormat' => 'HH:mm',
'separator' => ' ',
) );

$field = RWMB_Date_Field::normalize_field( $field );

$field['attributes']['class'] = 'rwmb-datetime';

return $field;
Expand Down
6 changes: 3 additions & 3 deletions inc/fields/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if ( ! class_exists( 'RWMB_Email_Field' ) )
{
class RWMB_Email_Field extends RWMB_Text_Field
{
{
/**
* Normalize parameters for field
*
Expand All @@ -20,12 +20,12 @@ static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field['attributes']['type'] = 'email';
$field['attributes']['type'] = 'email';
$field['attributes']['class'] = 'rwmb-email';

return $field;
}

/**
* Sanitize email
*
Expand Down
4 changes: 2 additions & 2 deletions inc/fields/hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class RWMB_Hidden_Field extends RWMB_Input_Field
static function normalize_field( $field )
{
$field['attributes'] = array(
'name' => $field['field_name'],
'id' => $field['clone'] ? FALSE : $field['id']
'name' => $field['field_name'],
'id' => $field['clone'] ? false : $field['id']
);

return $field;
Expand Down
33 changes: 17 additions & 16 deletions inc/fields/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class RWMB_Input_Field extends RWMB_Field
*/
static function html( $meta, $field )
{
$attributes = $field['attributes'];
$attributes = $field['attributes'];
$attributes['value'] = $meta;
return sprintf(
'<input %s >%s',
'<input %s>%s',
self::render_attributes( $attributes ),
self::datalist_html( $field )
);
Expand All @@ -34,27 +34,28 @@ static function html( $meta, $field )
*/
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );
$field = wp_parse_args( $field, array(
'datalist' => FALSE,
'disabled' => FALSE,
'required' => FALSE,
'readonly' => FALSE
'datalist' => false,
'disabled' => false,
'required' => false,
'readonly' => false
) );
if( $field['datalist'] )
if ( $field['datalist'] )
{
$field['datalist'] = wp_parse_args( $field['datalist'], array(
'id' => $field['id'] . '_list',
'options' => array()
'id' => $field['id'] . '_list',
'options' => array()
) );
}

$field['attributes'] = wp_parse_args( $field['attributes'], array(
'disabled' => $field['disabled'],
'list' => $field['datalist'] ? $field['datalist']['id'] : FALSE ,
'readonly' => $field['readonly'],
'required' => $field['required'],
'name' => $field['field_name'],
'id' => $field['clone'] ? FALSE : $field['id']
'disabled' => $field['disabled'],
'list' => $field['datalist'] ? $field['datalist']['id'] : false,
'readonly' => $field['readonly'],
'required' => $field['required'],
'name' => $field['field_name'],
'id' => $field['clone'] ? false : $field['id']
) );

return $field;
Expand Down
20 changes: 10 additions & 10 deletions inc/fields/number.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class RWMB_Number_Field extends RWMB_Input_Field
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field = wp_parse_args( $field, array(
'step' => 1,
'min' => 0,
'max' => FALSE,
'step' => 1,
'min' => 0,
'max' => false,
) );

$field['attributes'] = wp_parse_args( $field['attributes'], array(
'step' => $field['step'],
'max' => $field['max'],
'min' => $field['min'],
'step' => $field['step'],
'max' => $field['max'],
'min' => $field['min'],
) );
$field['attributes']['type'] = 'number';

$field['attributes']['type'] = 'number';
$field['attributes']['class'] = 'rwmb-number';

return $field;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field['attributes']['type'] = 'password';
$field['attributes']['type'] = 'password';
$field['attributes']['class'] = 'rwmb-password';

return $field;
Expand Down
17 changes: 9 additions & 8 deletions inc/fields/radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class RWMB_Radio_Field extends RWMB_Input_Field
*/
static function html( $meta, $field )
{
$html = array();
$tpl = '<label><input %s %s> %s</label>';
$html = array();
$tpl = '<label><input %s %s> %s</label>';
$attributes = $field['attributes'];

foreach ( $field['options'] as $value => $label )
{
$attributes['value'] = $value;
$html[] = sprintf(
$html[] = sprintf(
$tpl,
self::render_attributes( $attributes ),
checked( $value, $meta, false ),
Expand All @@ -36,7 +36,7 @@ static function html( $meta, $field )

return implode( ' ', $html );
}

/**
* Normalize parameters for field
*
Expand All @@ -47,9 +47,10 @@ static function html( $meta, $field )
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );
$field['attributes']['list'] = FALSE;
$field['attributes']['id'] = FALSE;
$field['attributes']['type'] = 'radio';

$field['attributes']['list'] = false;
$field['attributes']['id'] = false;
$field['attributes']['type'] = 'radio';
$field['attributes']['class'] = 'rwmb-radio';

return $field;
Expand Down
8 changes: 4 additions & 4 deletions inc/fields/range.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ static function normalize_field( $field )
'max' => 10,
'step' => 1,
) );

$field = parent::normalize_field( $field );
$field['attributes']['type'] = 'range';

$field['attributes']['type'] = 'range';
$field['attributes']['class'] = 'rwmb-range';

return $field;
}

Expand Down
10 changes: 5 additions & 5 deletions inc/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ class RWMB_Text_Field extends RWMB_Input_Field
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field = wp_parse_args( $field, array(
'size' => 30,
'maxlength' => 30,
'pattern' => FALSE,
'pattern' => false,
'placeholder' => '',
) );

$field['attributes'] = wp_parse_args( $field['attributes'], array(
'size' => $field['size'],
'maxlength' => $field['maxlength'],
'pattern' => $field['pattern'],
'placeholder' => $field['placeholder'],
) );
$field['attributes']['type'] = 'text';

$field['attributes']['type'] = 'text';
$field['attributes']['class'] = 'rwmb-text';

return $field;
Expand Down
Loading

0 comments on commit acfe5c0

Please sign in to comment.