| 1 |
<?php |
| 2 |
// Prevent loading this file directly. |
| 3 |
defined( 'ABSPATH' ) || die; |
| 4 |
|
| 5 |
if ( ! class_exists( 'MB_Tabs' ) ) { |
| 6 |
class MB_Tabs { |
| 7 |
/** |
| 8 |
* Indicate that the instance of the class is working on a meta box that has tabs or not. |
| 9 |
* It will be set 'true' BEFORE meta box is display and 'false' AFTER. |
| 10 |
* |
| 11 |
* @var bool |
| 12 |
*/ |
| 13 |
protected $active = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Store all output of fields. |
| 17 |
* This is used to put fields in correct <div> for tabs. |
| 18 |
* The fields' output will be get via filter 'rwmb_outer_html'. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $fields_output = array(); |
| 23 |
|
| 24 |
public function __construct() { |
| 25 |
add_action( 'rwmb_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 26 |
|
| 27 |
add_action( 'rwmb_before', array( $this, 'opening_div' ), 1 ); // 1 = display first, before tab nav. |
| 28 |
add_action( 'rwmb_after', array( $this, 'closing_div' ), 100 ); // 100 = display last, after tab panels. |
| 29 |
|
| 30 |
// Change the title position of metabox |
| 31 |
add_action( 'rwmb_after', array( $this, 'show_nav' ), 20 ); |
| 32 |
add_action( 'rwmb_after', array( $this, 'show_panels' ), 30 ); |
| 33 |
|
| 34 |
add_filter( 'rwmb_outer_html', array( $this, 'capture_fields' ), 10, 2 ); |
| 35 |
} |
| 36 |
|
| 37 |
public function enqueue() { |
| 38 |
list( , $url ) = RWMB_Loader::get_path( dirname( __FILE__ ) ); |
| 39 |
wp_enqueue_style( 'rwmb-tabs', $url . 'tabs.css', '', '1.1.5' ); |
| 40 |
wp_enqueue_script( 'rwmb-tabs', $url . 'tabs.js', array( 'jquery' ), '1.1.5', true ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Display opening div for tabs for meta box. |
| 45 |
* |
| 46 |
* @param RW_Meta_Box $obj Meta Box object. |
| 47 |
*/ |
| 48 |
public function opening_div( RW_Meta_Box $obj ) { |
| 49 |
if ( empty( $obj->meta_box['tabs'] ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$class = 'rwmb-tabs'; |
| 54 |
if ( isset( $obj->meta_box['tab_style'] ) && 'default' !== $obj->meta_box['tab_style'] ) { |
| 55 |
$class .= ' rwmb-tabs-' . $obj->meta_box['tab_style']; |
| 56 |
} |
| 57 |
|
| 58 |
if ( isset( $obj->meta_box['tab_wrapper'] ) && false === $obj->meta_box['tab_wrapper'] ) { |
| 59 |
$class .= ' rwmb-tabs-no-wrapper'; |
| 60 |
} |
| 61 |
|
| 62 |
echo '<div class="' . esc_attr( $class ) . '">'; |
| 63 |
|
| 64 |
// Set 'true' to let us know that we're working on a meta box that has tabs. |
| 65 |
$this->active = true; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Display closing div for tabs for meta box. |
| 70 |
*/ |
| 71 |
public function closing_div() { |
| 72 |
if ( ! $this->active ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
echo '</div>'; |
| 77 |
|
| 78 |
// Reset to initial state to be ready for other meta boxes. |
| 79 |
$this->active = false; |
| 80 |
$this->fields_output = array(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Display tab navigation. |
| 85 |
* |
| 86 |
* @param RW_Meta_Box $obj Meta Box object. |
| 87 |
*/ |
| 88 |
public function show_nav( RW_Meta_Box $obj ) { |
| 89 |
if ( ! $this->active ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$tabs = $obj->meta_box['tabs']; |
| 94 |
$default_active = $obj->tab_default_active; |
| 95 |
|
| 96 |
echo '<ul class="rwmb-tab-nav">'; |
| 97 |
|
| 98 |
$i = 0; |
| 99 |
foreach ( $tabs as $key => $tab_data ) { |
| 100 |
if ( is_string( $tab_data ) ) { |
| 101 |
$tab_data = ['label' => $tab_data]; |
| 102 |
} |
| 103 |
$tab_data = wp_parse_args( $tab_data, [ |
| 104 |
'icon' => '', |
| 105 |
'label' => '', |
| 106 |
] ); |
| 107 |
|
| 108 |
if ( filter_var( $tab_data['icon'], FILTER_VALIDATE_URL ) ) { // If icon is an URL. |
| 109 |
$icon = '<img src="' . esc_url( $tab_data['icon'] ) . '">'; |
| 110 |
} else { // If icon is icon font. |
| 111 |
// If icon is dashicons, auto add class 'dashicons' for users. |
| 112 |
if ( false !== strpos( $tab_data['icon'], 'dashicons' ) ) { |
| 113 |
$tab_data['icon'] .= ' dashicons'; |
| 114 |
} |
| 115 |
// Remove duplicate classes. |
| 116 |
$tab_data['icon'] = array_filter( array_map( 'trim', explode( ' ', $tab_data['icon'] ) ) ); |
| 117 |
$tab_data['icon'] = implode( ' ', array_unique( $tab_data['icon'] ) ); |
| 118 |
|
| 119 |
$icon = $tab_data['icon'] ? '<i class="' . esc_attr( $tab_data['icon'] ) . '"></i>' : ''; |
| 120 |
} |
| 121 |
|
| 122 |
$class = "rwmb-tab-$key"; |
| 123 |
if ( ( $default_active && $default_active === $key ) || ( ! $default_active && ! $i ) ) { |
| 124 |
$class .= ' rwmb-tab-active'; |
| 125 |
} |
| 126 |
|
| 127 |
printf( |
| 128 |
'<li class="%s" data-panel="%s"><a href="#">%s%s</a></li>', |
| 129 |
esc_attr( $class ), |
| 130 |
esc_attr( $key ), |
| 131 |
$icon, |
| 132 |
esc_html( $tab_data['label'] ) |
| 133 |
); |
| 134 |
$i ++; |
| 135 |
} |
| 136 |
|
| 137 |
echo '</ul>'; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Display tab panels. |
| 142 |
* Note that: this public function is hooked to 'rwmb_after', when all fields are outputted. |
| 143 |
* (and captured by 'capture_fields' public function). |
| 144 |
* |
| 145 |
* @param RW_Meta_Box $obj Meta Box object. |
| 146 |
*/ |
| 147 |
public function show_panels( RW_Meta_Box $obj ) { |
| 148 |
if ( ! $this->active ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// Store all tabs. |
| 153 |
$tabs = $obj->meta_box['tabs']; |
| 154 |
|
| 155 |
echo '<div class="rwmb-tab-panels">'; |
| 156 |
foreach ( $this->fields_output as $tab => $fields ) { |
| 157 |
// Remove rendered tab. |
| 158 |
if ( isset( $tabs[ $tab ] ) ) { |
| 159 |
unset( $tabs[ $tab ] ); |
| 160 |
} |
| 161 |
|
| 162 |
echo '<div class="rwmb-tab-panel rwmb-tab-panel-' . esc_attr( $tab ) . '" data-panel="' . esc_attr( $tab ) . '">'; |
| 163 |
echo implode( '', $fields ); |
| 164 |
echo '</div>'; |
| 165 |
} |
| 166 |
|
| 167 |
// Print unrendered tabs. |
| 168 |
foreach ( $tabs as $tab_id => $tab_data ) { |
| 169 |
echo '<div class="rwmb-tab-panel rwmb-tab-panel-' . esc_attr( $tab_id ) . '">'; |
| 170 |
echo '</div>'; |
| 171 |
} |
| 172 |
|
| 173 |
echo '</div>'; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Save field output into class variable to output later. |
| 178 |
* |
| 179 |
* @param string $output Field output. |
| 180 |
* @param array $field Field configuration. |
| 181 |
* |
| 182 |
* @return string |
| 183 |
*/ |
| 184 |
public function capture_fields( $output, $field ) { |
| 185 |
// If meta box doesn't have tabs, do nothing. |
| 186 |
if ( ! $this->active || ! isset( $field['tab'] ) ) { |
| 187 |
return $output; |
| 188 |
} |
| 189 |
|
| 190 |
$tab = $field['tab']; |
| 191 |
|
| 192 |
if ( ! isset( $this->fields_output[ $tab ] ) ) { |
| 193 |
$this->fields_output[ $tab ] = array(); |
| 194 |
} |
| 195 |
$this->fields_output[ $tab ][] = $output; |
| 196 |
|
| 197 |
// Return empty string to let Meta Box plugin echoes nothing. |
| 198 |
return ''; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
new MB_Tabs(); |
| 203 |
} |
| 204 |
|