/
options.php
330 lines (266 loc) · 9.03 KB
/
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
class Struts_Options {
protected $_sections, $_all_options, $_name, $_slug, $_stranded_options, $_is_initialized;
public function __construct( $slug, $name ) {
$this->sections( array() );
$this->all_options( array() );
$this->stranded_options( array() );
$this->slug( $slug );
$this->name( $name );
$this->register_hooks();
}
/***** Attribute accessors *****/
// Sections are containers for options
public function sections( $sections = NULL ) {
if ( NULL === $sections )
return $this->_sections;
$this->_sections = $sections;
return $this;
}
// Every option added, regardless of whether it was added to a section or not.
// This is useful for efficiently setting/getting option values without scanning all sections.
public function all_options( $all_options = NULL ) {
if ( NULL === $all_options )
return $this->_all_options;
$this->_all_options = $all_options;
return $this;
}
// All options without a section.
public function stranded_options( $stranded_options = NULL ) {
if ( NULL === $stranded_options )
return $this->_stranded_options;
$this->_stranded_options = $stranded_options;
return $this;
}
public function slug( $slug = NULL ) {
if ( NULL === $slug )
return $this->_slug;
$this->_slug = $slug;
return $this;
}
public function name( $name = NULL ) {
if ( NULL === $name )
return $this->_name;
$this->_name = $name;
return $this;
}
public function is_initialized( $is_initialized = NULL ) {
if ( NULL === $is_initialized )
return $this->_is_initialized;
$this->_is_initialized = $is_initialized;
return $this;
}
/***** WordPress setup *****/
public function register_hooks() {
// Load the Admin Options page
add_action( 'admin_menu', array( &$this, 'add_theme_options_page' ) );
// Register the sections and options
add_action( 'admin_init', array( &$this, 'register' ) );
// Enqueue the styles and scripts
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
// For the theme customizer
add_action( 'customize_register', array( &$this,'register_customizer' ) );
// Initialize options on wp_loaded, AFTER the customizer has a chance to register filters
add_action( 'wp_loaded', array( &$this,'initialize' ), 20 );
}
public function enqueue_scripts() {
$enqueue_scripts =
is_admin()
&&
current_user_can( 'edit_theme_options' )
&&
isset( $_GET['page'] )
&&
$_GET['page'] == $this->slug();
if ( $enqueue_scripts ) {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_script(
'struts-admin',
Struts::config( 'struts_root_uri' ) . '/javascripts/struts.js',
array( 'jquery', 'media-upload' ),
null
);
add_thickbox();
wp_enqueue_style( 'farbtastic' );
if ( Struts::config( 'use_struts_skin' ) ) {
wp_enqueue_style(
'struts-admin',
Struts::config( 'struts_root_uri' ) . '/stylesheets/struts.css'
);
}
}
}
public function initialize() {
// We only want to initialize the options once.
if ( $this->is_initialized() ) {
throw new StrutsOptionsAlreadyInitializedException( __( 'Options already initialized. Struts no longer requires manual initialization.', 'struts' ) );
} else {
$this->is_initialized( true );
}
$option_values = get_option( $this->name() );
if ( false === $option_values || empty( $option_values ) ) {
$option_values = $this->defaults();
}
update_option( $this->name(), $option_values );
foreach ( $this->all_options() as $option ) {
if ( isset( $option_values[$option->name()] ) ) {
$option->value( $option_values[$option->name()] );
} elseif ( ! isset( $option_values[$option->name()] ) && $option->default_value() === true ) {
// In the case of checkboxes if the value is empty and the default is true we need
// to explicitly set the false value here to prevent the default from always being set.
$option->value( false );
} else {
$option->value( $option->default_value() );
}
}
}
public function add_theme_options_page() {
add_theme_page(
__( 'Theme Options', 'struts' ),
__( 'Theme Options', 'struts' ),
'edit_theme_options',
$this->slug(),
array( &$this, 'echo_form_html' ) );
}
public function register() {
register_setting( $this->name(), $this->name(), array( &$this, 'validate' ) );
$this->register_sections();
$this->register_stranded_options();
}
protected function register_stranded_options() {
foreach( $this->stranded_options() as $option ) {
$option->register();
}
}
protected function register_sections() {
foreach( $this->sections() as $section ) {
$section->register();
}
}
public function register_customizer( $wp_customize ) {
if ( Struts::config( 'preview_javascript' ) && $wp_customize->is_preview() && ! is_admin() ) {
$preview_javascript_dependencies = Struts::config( 'preview_javascript_dependencies' ) ? Struts::config( 'preview_javascript_dependencies' ) : array();
wp_enqueue_script(
'struts-preview-js',
Struts::config( 'preview_javascript' ),
$preview_javascript_dependencies,
null
);
}
foreach( $this->sections() as $section ) {
$section->register_customizer( $wp_customize );
}
}
public function validate( $inputs ) {
$validated_input = array();
if ( isset( $inputs['struts_reset'] ) ) {
$validated_input = $this->defaults();
} else {
$all_options = $this->all_options();
foreach ( $inputs as $key => $value ) {
if ( isset( $all_options[$key] ) ) {
$option = $all_options[$key];
$validated_input[$key] = $option->validate( $value );
}
}
}
return $validated_input;
}
public function add_section( $id, $title, $description = NULL ) {
$this->_sections[$id] = new Struts_Section( $id, $title, $description, $this->name() );
}
/**
* Adds an option with the given name and type to this collection
* Sets the option's parent_name to this collection's name, and returns the option
*
* @param $name - unique (within the collection ) name for this option
* @param $type - type of option (text/select/checkbox/etc)
* @param $section - name of the section this option goes in
*
* @return Struts_Option
*/
public function add_option( $name, $type, $section = NULL ) {
$option_class = 'Struts_Option_' . ucfirst( $type );
$option = new $option_class;
$option->name( $name );
$option->parent_name( $this->name() );
$option->type( $type );
if ( NULL !== $section ) {
$sections = $this->sections();
if ( ! isset( $sections[$section] ) ) {
throw new SectionNotFoundException( sprintf( __( 'Section with name ‘%1$s’ not defined', 'struts' ), esc_html( $section ) ) );
}
$option->section( $section );
$sections[$section]->add_option($option);
} else {
// No section provided
$this->_stranded_options[$name] = $option;
}
$this->_all_options[$name] = $option;
return $option;
}
public function get_value( $option_name ) {
$options = $this->all_options();
if ( isset( $options[$option_name] ) ) {
$option = $options[$option_name];
return $option->value();
} else {
return NULL;
}
}
/**
* Returns the default values of all options in this collection as a hash
*
* @return array
*/
public function defaults() {
$defaults = array();
$options = $this->all_options();
foreach ( $options as $option ) {
$defaults[ $option->name() ] = $option->default_value();
}
return $defaults;
}
/***** HTML Output *****/
public function echo_form_html() { ?>
<div id="struts-options" class="wrap">
<div id="icon-themes" class="icon32"><br></div>
<h2><?php _e( 'Theme Options', 'struts' ); ?></h2>
<div id="struts-options" class="wrap">
<div id="struts-options-body">
<?php echo $this->settings_updated_html(); ?>
<form action="options.php" method="post">
<?php
settings_fields( $this->name() );
$this->do_options_html();
?>
<div class="struts-buttons-container">
<input name="<?php echo $this->name(); ?>[struts_submit]" type="submit" class="button-primary struts-save-button" value="<?php esc_attr_e( 'Save Settings', 'struts' ); ?>" />
<input name="<?php echo $this->name(); ?>[struts_reset]" type="submit" class="button-secondary struts-reset-button" value="<?php esc_attr_e( 'Reset Defaults', 'struts' ); ?>" onclick="return confirm('Click OK if you want to reset your theme options to the defaults.')" />
</div>
</form>
</div>
</div>
</div>
<?php }
public function settings_updated_html() {
if ( isset( $_GET['settings-updated'] ) )
return "<div class='updated'><p>" . __( "Theme settings updated successfully.", 'struts' ) . "</p></div>";
}
public function do_options_html() {
if ( ! Struts::config( 'use_struts_skin' ) ) {
do_settings_sections( $this->name() );
return;
}
foreach ( $this->sections() as $section ) {
$section->to_html();
}
foreach ( $this->stranded_options() as $option ) {
$option->to_html();
}
}
}
class SectionNotFoundException extends Exception {}
class StrutsOptionsAlreadyInitializedException extends Exception {}