-
Notifications
You must be signed in to change notification settings - Fork 12
/
gust-options.php
211 lines (171 loc) · 5.43 KB
/
gust-options.php
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
<?php
/**
* Gust Options
*/
class Gust_Options {
protected $data = array();
protected $o = array();
public function __construct( $data = array() ) {
$this->data = $data;
$this->load();
add_action( 'admin_init', array( $this,'options' ) );
add_action( 'admin_menu', array( $this,'pages' ) );
}
/**
* Register menu
*/
public function pages() {
add_options_page(
$this->data['page_title'],
$this->data['menu_title'],
$this->data['permission'],
$this->data['menu_slug'],
array( $this,'page' )
);
}
public function page(){
?>
<div class="wrap">
<h2><?php echo esc_html( $this->data['page_title'] ); ?></h2>
<?php if ( ! empty( $this->data['page_description'] ) ) : ?>
<?php echo wpautop( $this->data['page_description'] ); ?>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( $this->data['option_name'] ); ?>
<?php do_settings_sections( $this->data['plugin_path'] ); ?>
<?php submit_button() ?>
</form>
</div>
<?php
}
/**
* Register option
*/
public function options() {
register_setting(
$this->data['option_name'],
$this->data['option_name'],
array( $this,'validate' )
);
foreach ( $this->data['section'] as $section ) {
add_settings_section( $section['slug'], $section['title'], '', $this->data['plugin_path'] );
foreach ( $section['field'] as $field ) {
$option_id = $this->get_field_id( $section['slug'], $field['slug'] );
add_settings_field(
$option_id,
sprintf(
'<label for="%s">%s</label>',
esc_attr( $option_id ),
esc_html( $field['title'] )
),
array( $this, 'the_field' ),
$this->data['plugin_path'],
$section['slug'],
array(
'option_type' => $field['type'],
'option_id' => $option_id,
'description' => ! empty( $field['description'] ) ? $field['description'] : '',
'label' => ! empty( $field['label'] ) ? $field['label'] : '',
'options' => isset( $field['options'] ) ? $field['options'] : false,
'value' => isset( $this->o[ "{$section['slug']}_{$field['slug']}" ] ) ? $this->o[ "{$section['slug']}_{$field['slug']}" ] : false,
)
);
}
}
}
public function get() {
return get_option( $this->data['option_name'] );
}
public function load() {
$this->o = get_option( $this->data['option_name'] );
// First time, save defaults
if ( false === $this->o ) {
$this->defaults();
}
}
public function defaults() {
update_option( $this->data['option_name'], $this->data['defaults'] );
$this->o = $this->data['defaults'];
}
public function get_field_id( $section_slug, $field_slug ) {
return sprintf(
'%s[%s_%s]',
$this->data['option_name'],
$section_slug,
$field_slug
);
}
public function validate( $values ) {
foreach ( $this->data['section'] as $section ) {
foreach ( $section['field'] as $field ) {
$name = "{$section['slug']}_{$field['slug']}";
$type = $field['type'];
if ( ! isset( $values[ $name ] ) ) {
$values[ $name ] = false;
}
// TODO: sanitize for every type;
}
}
return $values;
}
public function the_field( $args ) {
call_user_func( array( $this, $args['option_type'] ), $args );
if ( ! empty( $args['description'] ) ) :
?>
<p class="description"><?php echo $args['description'] ?></p>
<?php endif;
}
public function text( $args ) {
printf(
'<input class="regular-text" id="%1$s" name="%1$s" type="text" value="%2$s" />',
esc_attr( $args['option_id'] ),
esc_attr( $args['value'] )
);
}
public function textarea( $args ) {
printf(
'<textarea class="all-options" id="%1$s" name="%1$s" rows="5">%2$s</textarea>',
esc_attr( $args['option_id'] ),
esc_html( $args['value'] )
);
}
public function select( $args ) {
printf( '<select id="%1$s" name="%1$s">', esc_attr( $args['option_id'] ) );
foreach( $args['options']['list'] as $value => $label ) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr( $value ),
selected( $args['value'], $value, false ),
esc_attr( $label )
);
}
printf( '</select>' );
}
public function checkbox( $args ) {
if ( ! empty( $args['label'] ) ) :
printf(
'<label><input id="%1$s" name="%1$s" type="checkbox" value="true"%2$s /> %3$s</label>',
esc_attr( $args['option_id'] ),
checked( $args['value'], 'true', false ),
esc_html( $args['label'] )
);
else :
printf(
'<input id="%1$s" name="%1$s" type="checkbox" value="true"%2$s />',
esc_attr( $args['option_id'] ),
checked( $args['value'], 'true', false )
);
endif;
}
public function checkbox_list( $args ) {
foreach( $args['options']['list'] as $value => $label ) {
$is_checked = ( isset( $args['value'][ $value ] ) && $args['value'][ $value ] );
printf(
'<label for="%1$s"><input id="%1$s" name="%1$s" type="checkbox" value="true"%2$s /> %3$s</label><br />',
esc_attr( "{$args['option_id']}[{$value}]" ),
checked( $is_checked, true, false ),
esc_html( $label )
);
}
}
}