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