choice.php (1885B)
1 <?php 2 /** 3 * The abstract choice field. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Abstract class for any kind of choice field. 10 */ 11 abstract class RWMB_Choice_Field extends RWMB_Field { 12 /** 13 * Get field HTML. 14 * 15 * @param mixed $meta Meta value. 16 * @param array $field Field parameters. 17 * @return string 18 */ 19 public static function html( $meta, $field ) { 20 return ''; 21 } 22 23 /** 24 * Normalize parameters for field. 25 * 26 * @param array $field Field parameters. 27 * @return array 28 */ 29 public static function normalize( $field ) { 30 $field = parent::normalize( $field ); 31 $field = wp_parse_args( 32 $field, 33 array( 34 'flatten' => true, 35 'options' => array(), 36 ) 37 ); 38 39 return $field; 40 } 41 42 /** 43 * Transform field options into the verbose format. 44 * 45 * @param array $options Field options. 46 * 47 * @return array 48 */ 49 public static function transform_options( $options ) { 50 $transformed = array(); 51 $options = (array) $options; 52 foreach ( $options as $value => $label ) { 53 $option = is_array( $label ) ? $label : array( 54 'label' => (string) $label, 55 'value' => (string) $value, 56 ); 57 if ( isset( $option['label'] ) && isset( $option['value'] ) ) { 58 $transformed[ $option['value'] ] = (object) $option; 59 } 60 } 61 return $transformed; 62 } 63 64 /** 65 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. 66 * 67 * @param array $field Field parameters. 68 * @param string $value The value. 69 * @param array $args Additional arguments. Rarely used. See specific fields for details. 70 * @param int|null $post_id Post ID. null for current post. Optional. 71 * 72 * @return string 73 */ 74 public static function format_single_value( $field, $value, $args, $post_id ) { 75 $options = self::transform_options( $field['options'] ); 76 return isset( $options[ $value ] ) ? $options[ $value ]->label : ''; 77 } 78 }