1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Tabify_Edit_Screen_Tabs { |
4
|
|
|
private $base_url = ''; |
5
|
|
|
private $get_arg = 'tab'; |
6
|
|
|
|
7
|
|
|
private $active = ''; |
8
|
|
|
|
9
|
|
|
private $items = array(); |
10
|
|
|
private $type; |
11
|
|
|
private $javascript_support; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* construct method |
15
|
|
|
* |
16
|
|
|
* @param array $items All the tab items |
17
|
|
|
* |
18
|
|
|
* @since 0.1.0 |
19
|
|
|
*/ |
20
|
|
|
public function __construct( $items, $type = 'horizontal', $get_arg = 'tab', $javascript_support = true ) { |
|
|
|
|
21
|
|
|
if ( is_array( $items ) ) { |
22
|
|
|
$this->items = apply_filters( 'tabify_tabs', $items, $this, $type ); |
23
|
|
|
$this->type = $type; |
24
|
|
|
$this->get_arg = $get_arg; |
25
|
|
|
$this->javascript_support = $javascript_support; |
26
|
|
|
|
27
|
|
|
if ( isset( $_REQUEST[ $this->get_arg ] ) ) { |
28
|
|
|
$this->active = sanitize_text_field( $_REQUEST[ $this->get_arg ] ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->base_url = remove_query_arg( $this->get_arg, $_SERVER["REQUEST_URI"] ); |
32
|
|
|
|
33
|
|
|
if ( empty( $this->active ) || ! isset( $items[ $this->active ] ) ) { |
34
|
|
|
$this->active = key( $items ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the current tab |
41
|
|
|
* |
42
|
|
|
* @return string Current tab name |
43
|
|
|
* |
44
|
|
|
* @since 0.1.0 |
45
|
|
|
*/ |
46
|
|
|
public function get_current_tab() { |
47
|
|
|
return $this->active; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the HTML code of the tabs container including the tabs |
52
|
|
|
* |
53
|
|
|
* @return string The HTML of the tabs |
54
|
|
|
* |
55
|
|
|
* @since 0.1.0 |
56
|
|
|
*/ |
57
|
|
|
public function get_tabs_with_container() { |
58
|
|
|
$class = 'tabify-tabs tab-' . $this->type; |
59
|
|
|
|
60
|
|
|
if ( ! $this->javascript_support ) { |
61
|
|
|
$class .= ' js-disabled'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$return = '<div class="' . $class . '">'; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
if ( 'horizontal' == $this->type ) { |
68
|
|
|
$return .= '<h2 class="nav-tab-wrapper">'; |
69
|
|
|
} |
70
|
|
|
else { |
71
|
|
|
$return .= '<h2>'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$return .= $this->get_tabs_current_tab_input(); |
75
|
|
|
$return .= $this->get_tabs(); |
76
|
|
|
|
77
|
|
|
$return .= '</h2>'; |
78
|
|
|
|
79
|
|
|
$return .= apply_filters( 'tabify_tabs_under', '', $this->type ); |
80
|
|
|
|
81
|
|
|
$return .= '</div>'; |
82
|
|
|
|
83
|
|
|
//When tabs are requested also enqueue the javascript and css code |
84
|
|
|
$required = array( 'jquery' ); |
85
|
|
|
|
86
|
|
|
if ( 'post' == get_current_screen()->base ) { |
87
|
|
|
$required[] = 'postbox'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$suffix = SCRIPT_DEBUG ? '' : '.min'; |
91
|
|
|
|
92
|
|
|
wp_register_script( 'tabify-edit-screen', plugins_url( '/js/tabs' . $suffix . '.js', dirname( __FILE__ ) ), $required, '1.0' ); |
93
|
|
|
wp_register_style( 'tabify-edit-screen', plugins_url( '/css/tabs' . $suffix . '.css', dirname( __FILE__ ) ), array(), '1.0' ); |
94
|
|
|
|
95
|
|
|
wp_enqueue_script( 'tabify-edit-screen' ); |
96
|
|
|
wp_enqueue_style( 'tabify-edit-screen' ); |
97
|
|
|
|
98
|
|
|
$this->add_active_state_style(); |
99
|
|
|
|
100
|
|
|
return $return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Update switch background color for active state |
105
|
|
|
* |
106
|
|
|
* @since 0.9.6 |
107
|
|
|
*/ |
108
|
|
|
private function add_active_state_style() { |
109
|
|
|
global $_wp_admin_css_colors; |
110
|
|
|
|
111
|
|
|
$color = get_user_option('admin_color'); |
112
|
|
|
|
113
|
|
|
if ( empty( $color ) || ! isset($_wp_admin_css_colors[ $color ] ) ) { |
114
|
|
|
$color = 'fresh'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$background = $_wp_admin_css_colors[$color]->colors[3]; |
118
|
|
|
|
119
|
|
|
wp_add_inline_style( |
120
|
|
|
'tabify-edit-screen', |
121
|
|
|
'.switch-checkbox:checked + .switch-label { background: ' . $background . '; }' |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the hidden input code for current tab |
127
|
|
|
* |
128
|
|
|
* @return string The HTML of the hidden input field |
129
|
|
|
* |
130
|
|
|
* @since 0.2.0 |
131
|
|
|
*/ |
132
|
|
|
public function get_tabs_current_tab_input() { |
133
|
|
|
return '<input type="hidden" class="current_tab" name="' . $this->get_arg . '" value="' . $this->active . '" />'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the HTML code of all the tabs |
138
|
|
|
* |
139
|
|
|
* @return string The HTML of the hidden input field |
140
|
|
|
* |
141
|
|
|
* @since 0.1.0 |
142
|
|
|
*/ |
143
|
|
|
private function get_tabs() { |
144
|
|
|
$return = ''; |
145
|
|
|
|
146
|
|
|
foreach ( $this->items as $key => $args ) { |
147
|
|
|
if ( ! is_array( $args ) ) { |
148
|
|
|
$args = array( |
149
|
|
|
'title' => $args |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$title = apply_filters( 'tabify_tabs_tab_title', $args['title'], $args, $this ); |
154
|
|
|
$url = esc_url( add_query_arg( $this->get_arg, $key, $this->base_url ) ); |
155
|
|
|
$classes = 'tabify-tab nav-tab'; |
156
|
|
|
|
157
|
|
|
if ( $this->active == $key ) { |
158
|
|
|
$classes .= ' nav-tab-active'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$return .= '<a id="tab-' . $key . '" href="' . $url . '" class="' . $classes . '">' . $title . '</a>'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: