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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
<?php // Prevent loading this file directly. defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'MB_Tabs' ) ) { /** * Main plugin class. * * @package Meta Box * @subpackage Meta Box Tabs * @author Tran Ngoc Tuan Anh <rilwis@gmail.com> */ class MB_Tabs { /** * Indicate that the instance of the class is working on a meta box that has tabs or not. * It will be set 'true' BEFORE meta box is display and 'false' AFTER. * * @var bool */ protected $active = false;
/** * Store all output of fields. * This is used to put fields in correct <div> for tabs. * The fields' output will be get via filter 'rwmb_outer_html'. * * @var array */ protected $fields_output = array();
/** * Initialize. Add hooks to meta box. */ public function init() { add_action( 'rwmb_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'rwmb_before', array( $this, 'opening_div' ), 1 ); // 1 = display first, before tab nav. add_action( 'rwmb_after', array( $this, 'closing_div' ), 100 ); // 100 = display last, after tab panels.
add_action( 'rwmb_before', array( $this, 'show_nav' ) ); add_action( 'rwmb_after', array( $this, 'show_panels' ) );
add_filter( 'rwmb_outer_html', array( $this, 'capture_fields' ), 10, 2 ); }
/** * Enqueue scripts and styles for tabs. */ public function enqueue() { list( , $url ) = RWMB_Loader::get_path( dirname( __FILE__ ) ); wp_enqueue_style( 'rwmb-tabs', $url . 'tabs.css', '', '1.0.3' ); wp_enqueue_script( 'rwmb-tabs', $url . 'tabs.js', array( 'jquery' ), '1.0.0', true ); }
/** * Display opening div for tabs for meta box. * * @param RW_Meta_Box $obj Meta Box object. */ public function opening_div( RW_Meta_Box $obj ) { if ( empty( $obj->meta_box['tabs'] ) ) { return; }
$class = 'rwmb-tabs'; if ( isset( $obj->meta_box['tab_style'] ) && 'default' !== $obj->meta_box['tab_style'] ) { $class .= ' rwmb-tabs-' . $obj->meta_box['tab_style']; }
if ( isset( $obj->meta_box['tab_wrapper'] ) && false === $obj->meta_box['tab_wrapper'] ) { $class .= ' rwmb-tabs-no-wrapper'; }
echo '<div class="' . esc_attr( $class ) . '">';
// Set 'true' to let us know that we're working on a meta box that has tabs. $this->active = true; }
/** * Display closing div for tabs for meta box. */ public function closing_div() { if ( ! $this->active ) { return; }
echo '</div>';
// Reset to initial state to be ready for other meta boxes. $this->active = false; $this->fields_output = array(); }
/** * Display tab navigation. * * @param RW_Meta_Box $obj Meta Box object. */ public function show_nav( RW_Meta_Box $obj ) { if ( ! $this->active ) { return; }
$tabs = $obj->meta_box['tabs'];
echo '<ul class="rwmb-tab-nav">';
$i = 0; foreach ( $tabs as $key => $tab_data ) { if ( is_string( $tab_data ) ) { $tab_data = array( 'label' => $tab_data, ); } $tab_data = wp_parse_args( $tab_data, array( 'icon' => '', 'label' => '', ) );
if ( filter_var( $tab_data['icon'], FILTER_VALIDATE_URL ) ) { // If icon is an URL. $icon = '<img src="' . esc_url( $tab_data['icon'] ) . '">'; } else { // If icon is icon font. // If icon is dashicons, auto add class 'dashicons' for users. if ( false !== strpos( $tab_data['icon'], 'dashicons' ) ) { $tab_data['icon'] .= ' dashicons'; } // Remove duplicate classes. $tab_data['icon'] = array_filter( array_map( 'trim', explode( ' ', $tab_data['icon'] ) ) ); $tab_data['icon'] = implode( ' ', array_unique( $tab_data['icon'] ) );
$icon = $tab_data['icon'] ? '<i class="' . esc_attr( $tab_data['icon'] ) . '"></i>' : ''; }
$class = "rwmb-tab-$key"; if ( ! $i ) { $class .= ' rwmb-tab-active'; }
printf( // WPCS: XSS OK. '<li class="%s" data-panel="%s"><a href="#">%s%s</a></li>', esc_attr( $class ), esc_attr( $key ), $icon, $tab_data['label'] ); $i ++; } // End foreach().
echo '</ul>'; }
/** * Display tab panels. * Note that: this public function is hooked to 'rwmb_after', when all fields are outputted. * (and captured by 'capture_fields' public function). * * @param RW_Meta_Box $obj Meta Box object. */ public function show_panels( RW_Meta_Box $obj ) { if ( ! $this->active ) { return; }
// Store all tabs. $tabs = $obj->meta_box['tabs'];
echo '<div class="rwmb-tab-panels">'; foreach ( $this->fields_output as $tab => $fields ) { // Remove rendered tab. if ( isset( $tabs[ $tab ] ) ) { unset( $tabs[ $tab ] ); }
echo '<div class="rwmb-tab-panel rwmb-tab-panel-' . esc_attr( $tab ) . '">'; echo implode( '', $fields ); // WPCS: XSS OK. echo '</div>'; }
// Print unrendered tabs. foreach ( $tabs as $tab_id => $tab_data ) { echo '<div class="rwmb-tab-panel rwmb-tab-panel-' . esc_attr( $tab_id ) . '">'; echo '</div>'; }
echo '</div>'; }
/** * Save field output into class variable to output later. * * @param string $output Field output. * @param array $field Field configuration. * * @return string */ public function capture_fields( $output, $field ) { // If meta box doesn't have tabs, do nothing. if ( ! $this->active || ! isset( $field['tab'] ) ) { return $output; }
$tab = $field['tab'];
if ( ! isset( $this->fields_output[ $tab ] ) ) { $this->fields_output[ $tab ] = array(); } $this->fields_output[ $tab ][] = $output;
// Return empty string to let Meta Box plugin echoes nothing. return ''; } }
$mb_tabs = new MB_Tabs; $mb_tabs->init(); } // End if().
|