NavTab.php
3.61 KB
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
<?php
/**
* SearchWP NavTab.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Admin;
/**
* Class NavTab is responsible for modeling an OptionsView nav tab.
*
* @since 4.0
*/
class NavTab {
/**
* Page tag.
*
* @since 4.2.0
*
* @var string
*/
public $page = '';
/**
* Tab tag.
*
* @since 4.0
*
* @var string
*/
public $tab = '';
/**
* Tab link.
*
* @since 4.0
*
* @var string
*/
public $link = '';
/**
* Tab label.
*
* @since 4.0
*
* @var string
*/
public $label = '';
/**
* Tab icon(s) as HTML class(es).
*
* @since 4.0
*
* @var string
*/
public $icon = '';
/**
* Is it a default tab?
*
* @since 4.2.0
*
* @var bool
*/
public $is_default = false;
/**
* Tab CSS classes.
*
* @since 4.0
*
* @var string[]
*/
public $classes = [];
/**
* NavTab constructor.
*
* @since 4.0
*
* @param array $args Tab settings.
*/
public function __construct( array $args = [] ) {
$defaults = [
'page' => '',
'tab' => '',
'label' => __( 'Settings', 'searchwp' ),
'classes' => '',
'icon' => '',
];
$args = (array) wp_parse_args( $args, $defaults );
$this->link = '#';
$this->classes = [ 'searchwp-settings-nav-tab' ];
$this->page = sanitize_title_with_dashes( $args['page'] );
$this->tab = sanitize_title_with_dashes( $args['tab'] );
$this->label = sanitize_text_field( $args['label'] );
$this->icon = sanitize_text_field( $args['icon'] );
if ( isset( $args['is_default'] ) ) {
$this->is_default = (bool) $args['is_default'];
} else {
$this->is_default = $this->tab === 'default';
}
if ( ! empty( $args['classes'] ) ) {
$this->classes = array_merge( $this->classes, (array) $args['classes'] );
}
$this->check_for_active_state();
if ( ! empty( $this->page ) || ! empty( $this->tab ) ) {
$this->build_link();
}
add_action( 'searchwp\settings\nav\tab', [ $this, 'render' ] );
}
/**
* Render this Navigation Tab.
*
* @since 4.0
*
* @return void
*/
public function render() {
?>
<li class="<?php echo esc_attr( implode( '-wrapper ', $this->classes ) . '-wrapper' ); ?>">
<a href="<?php echo esc_url( $this->link ); ?>" class="<?php echo esc_attr( implode( ' ', $this->classes ) ); ?>">
<span>
<?php echo esc_html( $this->label ); ?>
<?php if ( ! empty( $this->icon ) ) : ?>
<span class="<?php echo esc_attr( $this->icon ); ?>"></span>
<?php endif; ?>
</span>
</a>
</li>
<?php
}
/**
* Build the link for this Nav Tab.
*
* @since 4.0
*
* @return void
*/
private function build_link() {
wp_create_nonce( 'searchwp-tab-' . $this->tab );
$this->classes[] = 'searchwp-settings-nav-tab-' . sanitize_title_with_dashes( $this->tab );
$this->link = add_query_arg( [] );
if ( ! empty( $this->page ) ) {
$this->link = add_query_arg(
[ 'page' => 'searchwp-' . $this->page ],
admin_url( 'admin.php' )
);
}
if ( ! empty( $this->tab ) && ! $this->is_default ) {
$this->link = add_query_arg(
[ 'tab' => $this->tab ],
$this->link
);
}
}
/**
* Check whether this Nav Tab is active and if so add CSS class.
*
* @since 4.0
*
* @return void
*/
private function check_for_active_state() {
if (
( empty( $this->tab ) && ! isset( $_GET['tab'] ) ) ||
( $this->is_default && ! isset( $_GET['tab'] ) ) ||
( isset( $_GET['tab'] ) && $this->tab === $_GET['tab'] )
) {
$this->classes[] = 'searchwp-settings-nav-tab-active postbox';
}
}
}