Permalink
Browse files

Merge pull request #265 from venca163/rangefield

Rangefield: Range field added
  • Loading branch information...
2 parents 2f7b6af + 919e867 commit e604b3af34b8bb71af7c4daad93fc6021e960451 @rilwis rilwis committed Mar 31, 2013
Showing with 88 additions and 0 deletions.
  1. +11 −0 demo/demo.php
  2. +77 −0 inc/fields/range.php
View
@@ -214,6 +214,17 @@
'type' => 'email',
'std' => 'http://google.com',
),
+ // RANGE
+ array(
+ 'name' => 'Range',
+ 'id' => "{$prefix}range",
+ 'desc' => 'range description',
+ 'type' => 'range',
+ 'min' => 0,
+ 'max' => 100,
+ 'step' => 5,
+ 'std' => 0
+ ),
// URL
array(
'name' => 'URL',
View
@@ -0,0 +1,77 @@
+<?php
+// Prevent loading this file directly
+defined( 'ABSPATH' ) || exit;
+
+
+if ( !class_exists( 'RWMB_Range_Field' ) )
+{
+ class RWMB_Range_Field
+ {
+ /**
+ * Get field HTML
+ *
+ * @param string $html
+ * @param mixed $meta
+ * @param array $field
+ *
+ * @return string
+ */
+ static function html( $html, $meta, $field )
+ {
+ return sprintf(
+ '<input type="range" class="rwmb-range" name="%s" id="%s" value="%s" min="%s" max="%s" step="%s"/>',
+ $field['field_name'],
+ $field['id'],
+ $meta,
+ $field['min'],
+ $field['max'],
+ $field['step']
+ );
+ }
+
+ /**
+ * Normalize parameters for field.
+ *
+ * @param array $field
+ *
+ * @return array
+ */
+ static function normalize_field( $field )
+ {
+ $field = wp_parse_args( $field, array(
+ 'min' => 0,
+ 'max' => 10,
+ 'step' => 1
+ ) );
+ return $field;
+ }
+
+
+ /**
+ * Ensure number in range.
+ *
+ * @param mixed $new
+ * @param mixed $old
+ * @param int $post_id
+ * @param array $field
+ *
+ * @return int
+ */
+ static function value( $new, $old, $post_id, $field )
+ {
+ $new = intval($new);
+ $min = intval($field['min']);
+ $max = intval($field['max']);
+
+ if ($new < $min) {
+ return $min;
+ }
+ else if ($new > $max) {
+ return $max;
+ }
+
+ return $new;
+ }
+
+ }
+}

0 comments on commit e604b3a

Please sign in to comment.