1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
<?php /** * Add row, column markup to a set of fields in one row. * * @package Meta Box * @subpackage Meta Box Columns */
/** * Row class. */ class MB_Columns_Row { /** * List of fields. * * @var array */ protected $fields;
/** * Store the meta box's columns data. * * @var array */ protected $columns = array();
/** * Store the total column of a row. * * @var int */ protected $total_columns;
/** * Track current column. * * @var string */ protected $current_column = '';
/** * Track current field. * * @var array */ protected $current_field;
/** * Add hooks to meta box * * @param array $columns List of columns. * @param array $fields List of fields. */ public function __construct( array $columns, $fields ) { $this->columns = $columns; $this->fields = $fields; }
/** * Process all fields to add column markup to each one. */ public function process() { $index = 0; $count = ( is_array( $this->fields ) || is_object( $this->fields ) ? count( $this->fields ) : 0 ) - 1;
foreach ( $this->fields as &$field ) {
if ( empty( $field['column'] ) ) { continue; }
$this->process_field( $field );
if ( $count === $index ) { $this->process_last_field( $field ); }
if ( isset( $field['fields'] ) ) { $row = new self( $this->columns, $field['fields'] ); $row->process(); $field['fields'] = $row->get_fields(); }
$this->current_column = $field['column']; $this->current_field = &$field;
$index ++; } }
/** * Get all processed fields. * * @return array */ public function get_fields() { return $this->fields; }
/** * Process a field in the middle. * * @param array $field Field settings. */ public function process_field( &$field ) { if ( ! $this->is_start_column( $field ) ) { return; }
$column = $this->columns[ $field['column'] ]; $before = ''; $after = '';
$after .= '</div><!-- .rwmb-column -->';
if ( $this->is_start_row( $field ) ) { $after .= '</div><!-- .rwmb-row -->'; $before .= '<div class="rwmb-row">';
$this->total_columns = 0; }
$before .= sprintf( '<div class="rwmb-column rwmb-column-%d %s">', absint( $column['size'] ), esc_attr( $column['class'] ) );
$this->total_columns += $column['size'];
if ( $this->current_field ) { $this->current_field['after'] .= $after; } $field['before'] = $before . $field['before']; }
/** * Process the last field. * * @param array $field Field settings. */ protected function process_last_field( &$field ) { $after = '</div><!-- .rwmb-column -->'; $after .= '</div><!-- .rwmb-row -->'; $field['after'] .= $after; }
/** * Check if this field starts a column. * * @param array $field Field settings. * * @return bool */ protected function is_start_column( $field ) { return $field['column'] !== $this->current_column; }
/** * Check if this field starts a row. * * @param array $field Field settings. * * @return bool */ protected function is_start_row( $field ) { return ! $this->current_column || $this->total_columns + $this->columns[ $field['column'] ]['size'] > 12; } }
|