0
|
1 |
<?php if ( ! defined( 'OT_VERSION') ) exit( 'No direct script access allowed' ); |
|
2 |
/** |
|
3 |
* OptionTree Settings API |
|
4 |
* |
|
5 |
* This class loads all the methods and helpers specific to a Settings page. |
|
6 |
* |
|
7 |
* @package OptionTree |
|
8 |
* @author Derek Herman <derek@valendesigns.com> |
|
9 |
* @copyright Copyright (c) 2013, Derek Herman |
|
10 |
*/ |
|
11 |
if ( ! class_exists( 'OT_Settings' ) ) { |
|
12 |
|
|
13 |
class OT_Settings { |
|
14 |
|
|
15 |
/* the options array */ |
|
16 |
private $options; |
|
17 |
|
|
18 |
/* hooks for targeting admin pages */ |
|
19 |
private $page_hook; |
|
20 |
|
|
21 |
/** |
|
22 |
* Constructor |
|
23 |
* |
|
24 |
* @param array An array of options |
|
25 |
* @return void |
|
26 |
* |
|
27 |
* @access public |
|
28 |
* @since 2.0 |
|
29 |
*/ |
|
30 |
public function __construct( $args ) { |
|
31 |
|
|
32 |
$this->options = $args; |
|
33 |
|
|
34 |
/* return early if not viewing an admin page or no options */ |
|
35 |
if ( ! is_admin() || ! is_array( $this->options ) ) |
|
36 |
return false; |
|
37 |
|
|
38 |
/* load everything */ |
|
39 |
$this->hooks(); |
|
40 |
|
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* Execute the WordPress Hooks |
|
45 |
* |
|
46 |
* @return void |
|
47 |
* |
|
48 |
* @access public |
|
49 |
* @since 2.0 |
|
50 |
*/ |
|
51 |
public function hooks() { |
|
52 |
|
|
53 |
/* add pages & menu items */ |
|
54 |
add_action( 'admin_menu', array( $this, 'add_page' ) ); |
|
55 |
|
|
56 |
/* register sections */ |
|
57 |
add_action( 'admin_init', array( $this, 'add_sections' ) ); |
|
58 |
|
|
59 |
/* register settings */ |
|
60 |
add_action( 'admin_init', array( $this, 'add_settings' ) ); |
|
61 |
|
|
62 |
/* reset options */ |
|
63 |
add_action( 'admin_init', array( $this, 'reset_options' ), 10 ); |
|
64 |
|
|
65 |
/* initialize settings */ |
|
66 |
add_action( 'admin_init', array( $this, 'initialize_settings' ), 11 ); |
|
67 |
|
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* Loads each admin page |
|
72 |
* |
|
73 |
* @return void |
|
74 |
* |
|
75 |
* @access public |
|
76 |
* @since 2.0 |
|
77 |
*/ |
|
78 |
public function add_page() { |
|
79 |
|
|
80 |
/* loop through options */ |
|
81 |
foreach( (array) $this->options as $option ) { |
|
82 |
|
|
83 |
/* loop through pages */ |
|
84 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
85 |
|
|
86 |
/** |
|
87 |
* Theme Check... stop nagging me about this kind of stuff. |
|
88 |
* The damn admin pages are required for OT to function, duh! |
|
89 |
*/ |
|
90 |
$theme_check_bs = 'add_menu_page'; |
|
91 |
$theme_check_bs2 = 'add_submenu_page'; |
|
92 |
|
|
93 |
/* load page in WP top level menu */ |
|
94 |
if ( ! isset( $page['parent_slug'] ) || empty( $page['parent_slug'] ) ) { |
|
95 |
$page_hook = $theme_check_bs( |
|
96 |
$page['page_title'], |
|
97 |
$page['menu_title'], |
|
98 |
$page['capability'], |
|
99 |
$page['menu_slug'], |
|
100 |
array( $this, 'display_page' ), |
|
101 |
$page['icon_url'], |
|
102 |
$page['position'] |
|
103 |
); |
|
104 |
/* load page in WP sub menu */ |
|
105 |
} else { |
|
106 |
$page_hook = $theme_check_bs2( |
|
107 |
$page['parent_slug'], |
|
108 |
$page['page_title'], |
|
109 |
$page['menu_title'], |
|
110 |
$page['capability'], |
|
111 |
$page['menu_slug'], |
|
112 |
array( $this, 'display_page' ) |
|
113 |
); |
|
114 |
} |
|
115 |
|
|
116 |
/* only load if not a hidden page */ |
|
117 |
if ( ! isset( $page['hidden_page'] ) ) { |
|
118 |
|
|
119 |
/* associate $page_hook with page id */ |
|
120 |
$this->page_hook[$page['id']] = $page_hook; |
|
121 |
|
|
122 |
/* add scripts */ |
|
123 |
add_action( 'admin_print_scripts-' . $page_hook, array( $this, 'scripts' ) ); |
|
124 |
|
|
125 |
/* add styles */ |
|
126 |
add_action( 'admin_print_styles-' . $page_hook, array( $this, 'styles' ) ); |
|
127 |
|
|
128 |
/* add contextual help */ |
|
129 |
add_action( 'load-' . $page_hook, array( $this, 'help' ) ); |
|
130 |
|
|
131 |
} |
|
132 |
|
|
133 |
} |
|
134 |
|
|
135 |
} |
|
136 |
|
|
137 |
return false; |
|
138 |
} |
|
139 |
|
|
140 |
/** |
|
141 |
* Loads the scripts |
|
142 |
* |
|
143 |
* @return void |
|
144 |
* |
|
145 |
* @access public |
|
146 |
* @since 2.0 |
|
147 |
*/ |
|
148 |
public function scripts() { |
|
149 |
ot_admin_scripts(); |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* Loads the styles |
|
154 |
* |
|
155 |
* @return void |
|
156 |
* |
|
157 |
* @access public |
|
158 |
* @since 2.0 |
|
159 |
*/ |
|
160 |
public function styles() { |
|
161 |
ot_admin_styles(); |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Loads the contextual help for each page |
|
166 |
* |
|
167 |
* @return void |
|
168 |
* |
|
169 |
* @access public |
|
170 |
* @since 2.0 |
|
171 |
*/ |
|
172 |
public function help() { |
|
173 |
$screen = get_current_screen(); |
|
174 |
|
|
175 |
/* loop through options */ |
|
176 |
foreach( (array) $this->options as $option ) { |
|
177 |
|
|
178 |
/* loop through pages */ |
|
179 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
180 |
|
|
181 |
/* verify page */ |
|
182 |
if ( ! isset( $page['hidden_page'] ) && $screen->id == $this->page_hook[$page['id']] ) { |
|
183 |
|
|
184 |
/* set up the help tabs */ |
|
185 |
if ( ! empty( $page['contextual_help']['content'] ) ) { |
|
186 |
foreach( $page['contextual_help']['content'] as $contextual_help ) { |
|
187 |
$screen->add_help_tab( |
|
188 |
array( |
|
189 |
'id' => esc_attr( $contextual_help['id'] ), |
|
190 |
'title' => esc_attr( $contextual_help['title'] ), |
|
191 |
'content' => htmlspecialchars_decode( $contextual_help['content'] ), |
|
192 |
) |
|
193 |
); |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
/* set up the help sidebar */ |
|
198 |
if ( ! empty( $page['contextual_help']['sidebar'] ) ) { |
|
199 |
$screen->set_help_sidebar( htmlspecialchars_decode( $page['contextual_help']['sidebar'] ) ); |
|
200 |
} |
|
201 |
|
|
202 |
} |
|
203 |
|
|
204 |
} |
|
205 |
|
|
206 |
} |
|
207 |
|
|
208 |
return false; |
|
209 |
} |
|
210 |
|
|
211 |
/** |
|
212 |
* Loads the content for each page |
|
213 |
* |
|
214 |
* @return string |
|
215 |
* |
|
216 |
* @access public |
|
217 |
* @since 2.0 |
|
218 |
*/ |
|
219 |
public function display_page() { |
|
220 |
$screen = get_current_screen(); |
|
221 |
|
|
222 |
/* loop through settings */ |
|
223 |
foreach( (array) $this->options as $option ) { |
|
224 |
|
|
225 |
/* loop through pages */ |
|
226 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
227 |
|
|
228 |
/* verify page */ |
|
229 |
if ( ! isset( $page['hidden_page'] ) && $screen->id == $this->page_hook[$page['id']] ) { |
|
230 |
|
|
231 |
$show_buttons = isset( $page['show_buttons'] ) && $page['show_buttons'] == false ? false : true; |
|
232 |
|
|
233 |
/* update active layout content */ |
|
234 |
if ( isset( $_REQUEST['settings-updated'] ) && $_REQUEST['settings-updated'] == 'true' ) { |
|
235 |
|
|
236 |
$layouts = get_option( 'option_tree_layouts' ); |
|
237 |
|
|
238 |
/* has active layout */ |
|
239 |
if ( isset( $layouts['active_layout'] ) ) { |
|
240 |
$option_tree = get_option( $option['id'] ); |
|
241 |
$layouts[$layouts['active_layout']] = ot_encode( serialize( $option_tree ) ); |
|
242 |
update_option( 'option_tree_layouts', $layouts ); |
|
243 |
} |
|
244 |
|
|
245 |
} |
|
246 |
|
|
247 |
echo '<div class="wrap settings-wrap" id ="page-' . $page['id'] . '">'; |
|
248 |
|
|
249 |
screen_icon( ( isset( $page['screen_icon'] ) ? $page['screen_icon'] : 'options-general' ) ); |
|
250 |
echo '<h2>' . $page['page_title'] . '</h2>'; |
|
251 |
|
|
252 |
echo ot_alert_message( $page ); |
|
253 |
|
|
254 |
settings_errors( 'option-tree' ); |
|
255 |
|
|
256 |
/* Header */ |
|
257 |
echo '<div id="option-tree-header-wrap">'; |
|
258 |
|
|
259 |
echo '<ul id="option-tree-header">'; |
|
260 |
|
|
261 |
echo '<li id="option-tree-logo"><a href="http://wordpress.org/extend/plugins/option-tree/" target="_blank">OptionTree</a></li>'; |
|
262 |
|
|
263 |
echo '<li id="option-tree-version"><span>Version ' . OT_VERSION . '</span></li>'; |
|
264 |
|
|
265 |
echo '</ul>'; |
|
266 |
|
|
267 |
/* layouts form */ |
|
268 |
if ( $page['id'] == 'ot_theme_options' && OT_SHOW_NEW_LAYOUT == true ) |
|
269 |
ot_theme_options_layouts_form(); |
|
270 |
|
|
271 |
echo '</div>'; |
|
272 |
|
|
273 |
/* remove forms on the custom settings pages */ |
|
274 |
if ( $show_buttons ) { |
|
275 |
|
|
276 |
echo '<form action="options.php" method="post" id="option-tree-settings-api">'; |
|
277 |
|
|
278 |
settings_fields( $option['id'] ); |
|
279 |
|
|
280 |
} else { |
|
281 |
|
|
282 |
echo '<div id="option-tree-settings-api">'; |
|
283 |
|
|
284 |
} |
|
285 |
|
|
286 |
/* Sub Header */ |
|
287 |
echo '<div id="option-tree-sub-header">'; |
|
288 |
|
|
289 |
if ( $show_buttons ) |
|
290 |
echo '<button class="option-tree-ui-button blue right">' . $page['button_text'] . '</button>'; |
|
291 |
|
|
292 |
echo '</div>'; |
|
293 |
|
|
294 |
/* Navigation */ |
|
295 |
echo '<div class="ui-tabs">'; |
|
296 |
|
|
297 |
/* check for sections */ |
|
298 |
if ( isset( $page['sections'] ) && count( $page['sections'] ) > 0 ) { |
|
299 |
|
|
300 |
echo '<ul class="ui-tabs-nav">'; |
|
301 |
|
|
302 |
/* loop through page sections */ |
|
303 |
foreach( (array) $page['sections'] as $section ) { |
|
304 |
echo '<li id="tab_' . $section['id'] . '"><a href="#section_' . $section['id'] . '">' . $section['title'] . '</a></li>'; |
|
305 |
} |
|
306 |
|
|
307 |
echo '</ul>'; |
|
308 |
|
|
309 |
} |
|
310 |
|
|
311 |
/* sections */ |
|
312 |
echo '<div id="poststuff" class="metabox-holder">'; |
|
313 |
|
|
314 |
echo '<div id="post-body">'; |
|
315 |
|
|
316 |
echo '<div id="post-body-content">'; |
|
317 |
|
|
318 |
$this->do_settings_sections( $_GET['page'] ); |
|
319 |
|
|
320 |
echo '</div>'; |
|
321 |
|
|
322 |
echo '</div>'; |
|
323 |
|
|
324 |
echo '</div>'; |
|
325 |
|
|
326 |
echo '<div class="clear"></div>'; |
|
327 |
|
|
328 |
echo '</div>'; |
|
329 |
|
|
330 |
/* buttons */ |
|
331 |
if ( $show_buttons ) { |
|
332 |
|
|
333 |
echo '<div class="option-tree-ui-buttons">'; |
|
334 |
|
|
335 |
echo '<button class="option-tree-ui-button blue right">' . $page['button_text'] . '</button>'; |
|
336 |
|
|
337 |
echo '</div>'; |
|
338 |
|
|
339 |
} |
|
340 |
|
|
341 |
echo $show_buttons ? '</form>' : '</div>'; |
|
342 |
|
|
343 |
/* reset button */ |
|
344 |
if ( $show_buttons ) { |
|
345 |
|
|
346 |
echo '<form method="post" action="' . str_replace( '&settings-updated=true', '', $_SERVER["REQUEST_URI"] ) . '">'; |
|
347 |
|
|
348 |
/* form nonce */ |
|
349 |
wp_nonce_field( 'option_tree_reset_form', 'option_tree_reset_nonce' ); |
|
350 |
|
|
351 |
echo '<input type="hidden" name="action" value="reset" />'; |
|
352 |
|
|
353 |
echo '<button type="submit" class="option-tree-ui-button red left reset-settings" title="' . __( 'Reset Options', 'option-tree' ) . '">' . __( 'Reset Options', 'option-tree' ) . '</button>'; |
|
354 |
|
|
355 |
echo '</form>'; |
|
356 |
|
|
357 |
} |
|
358 |
|
|
359 |
echo '</div>'; |
|
360 |
|
|
361 |
} |
|
362 |
|
|
363 |
} |
|
364 |
|
|
365 |
} |
|
366 |
|
|
367 |
return false; |
|
368 |
} |
|
369 |
|
|
370 |
/** |
|
371 |
* Adds sections to the page |
|
372 |
* |
|
373 |
* @return void |
|
374 |
* |
|
375 |
* @access public |
|
376 |
* @since 2.0 |
|
377 |
*/ |
|
378 |
public function add_sections() { |
|
379 |
|
|
380 |
/* loop through options */ |
|
381 |
foreach( (array) $this->options as $option ) { |
|
382 |
|
|
383 |
/* loop through pages */ |
|
384 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
385 |
|
|
386 |
/* loop through page sections */ |
|
387 |
foreach( (array) $this->get_sections( $page ) as $section ) { |
|
388 |
|
|
389 |
/* add each section */ |
|
390 |
add_settings_section( |
|
391 |
$section['id'], |
|
392 |
$section['title'], |
|
393 |
array( $this, 'display_section' ), |
|
394 |
$page['menu_slug'] |
|
395 |
); |
|
396 |
|
|
397 |
} |
|
398 |
|
|
399 |
} |
|
400 |
|
|
401 |
} |
|
402 |
|
|
403 |
return false; |
|
404 |
} |
|
405 |
|
|
406 |
/** |
|
407 |
* Callback for add_settings_section() |
|
408 |
* |
|
409 |
* @return string |
|
410 |
* |
|
411 |
* @access public |
|
412 |
* @since 2.0 |
|
413 |
*/ |
|
414 |
public function display_section() { |
|
415 |
/* currently pointless */ |
|
416 |
} |
|
417 |
|
|
418 |
/** |
|
419 |
* Add settings the the page |
|
420 |
* |
|
421 |
* @return void |
|
422 |
* |
|
423 |
* @access public |
|
424 |
* @since 2.0 |
|
425 |
*/ |
|
426 |
public function add_settings() { |
|
427 |
|
|
428 |
/* loop through options */ |
|
429 |
foreach( (array) $this->options as $option ) { |
|
430 |
|
|
431 |
register_setting( $option['id'], $option['id'], array ( $this, 'sanitize_callback' ) ); |
|
432 |
|
|
433 |
/* loop through pages */ |
|
434 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
435 |
|
|
436 |
/* loop through page settings */ |
|
437 |
foreach( (array) $this->get_the_settings( $page ) as $setting ) { |
|
438 |
|
|
439 |
/* skip if no setting ID */ |
|
440 |
if ( ! isset( $setting['id'] ) ) |
|
441 |
continue; |
|
442 |
|
|
443 |
/* add get_option param to the array */ |
|
444 |
$setting['get_option'] = $option['id']; |
|
445 |
|
|
446 |
/* add each setting */ |
|
447 |
add_settings_field( |
|
448 |
$setting['id'], |
|
449 |
$setting['label'], |
|
450 |
array( $this, 'display_setting' ), |
|
451 |
$page['menu_slug'], |
|
452 |
$setting['section'], |
|
453 |
$setting |
|
454 |
); |
|
455 |
|
|
456 |
} |
|
457 |
|
|
458 |
} |
|
459 |
|
|
460 |
} |
|
461 |
|
|
462 |
return false; |
|
463 |
} |
|
464 |
|
|
465 |
/** |
|
466 |
* Callback for add_settings_field() to build each setting by type |
|
467 |
* |
|
468 |
* @param array Setting object array |
|
469 |
* @return string |
|
470 |
* |
|
471 |
* @access public |
|
472 |
* @since 2.0 |
|
473 |
*/ |
|
474 |
public function display_setting( $args = array() ) { |
|
475 |
extract( $args ); |
|
476 |
|
|
477 |
/* get current saved data */ |
|
478 |
$options = get_option( $get_option, false ); |
|
479 |
|
|
480 |
// Set field value |
|
481 |
$field_value = isset( $options[$id] ) ? $options[$id] : ''; |
|
482 |
|
|
483 |
/* set standard value */ |
|
484 |
if ( isset( $std ) ) { |
|
485 |
$field_value = ot_filter_std_value( $field_value, $std ); |
|
486 |
} |
|
487 |
|
|
488 |
/* build the arguments array */ |
|
489 |
$_args = array( |
|
490 |
'type' => $type, |
|
491 |
'field_id' => $id, |
|
492 |
'field_name' => $get_option . '[' . $id . ']', |
|
493 |
'field_value' => $field_value, |
|
494 |
'field_desc' => isset( $desc ) ? $desc : '', |
|
495 |
'field_std' => isset( $std ) ? $std : '', |
|
496 |
'field_rows' => isset( $rows ) && ! empty( $rows ) ? $rows : 15, |
|
497 |
'field_post_type' => isset( $post_type ) && ! empty( $post_type ) ? $post_type : 'post', |
|
498 |
'field_taxonomy' => isset( $taxonomy ) && ! empty( $taxonomy ) ? $taxonomy : 'category', |
|
499 |
'field_min_max_step'=> isset( $min_max_step ) && ! empty( $min_max_step ) ? $min_max_step : '0,100,1', |
|
500 |
'field_class' => isset( $class ) ? $class : '', |
|
501 |
'field_choices' => isset( $choices ) && ! empty( $choices ) ? $choices : array(), |
|
502 |
'field_settings' => isset( $settings ) && ! empty( $settings ) ? $settings : array(), |
|
503 |
'post_id' => ot_get_media_post_ID(), |
|
504 |
'get_option' => $get_option, |
|
505 |
); |
|
506 |
|
|
507 |
/* get the option HTML */ |
|
508 |
echo ot_display_by_type( $_args ); |
|
509 |
} |
|
510 |
|
|
511 |
/** |
|
512 |
* Sets the option standards if nothing yet exists. |
|
513 |
* |
|
514 |
* @return void |
|
515 |
* |
|
516 |
* @access public |
|
517 |
* @since 2.0 |
|
518 |
*/ |
|
519 |
public function initialize_settings() { |
|
520 |
|
|
521 |
/* loop through options */ |
|
522 |
foreach( (array) $this->options as $option ) { |
|
523 |
|
|
524 |
/* skip if option is already set */ |
|
525 |
if ( isset( $option['id'] ) && get_option( $option['id'], false ) ) { |
|
526 |
return false; |
|
527 |
} |
|
528 |
|
|
529 |
$defaults = array(); |
|
530 |
|
|
531 |
/* loop through pages */ |
|
532 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
533 |
|
|
534 |
/* loop through page settings */ |
|
535 |
foreach( (array) $this->get_the_settings( $page ) as $setting ) { |
|
536 |
|
|
537 |
if ( isset( $setting['std'] ) ) { |
|
538 |
|
|
539 |
$defaults[$setting['id']] = ot_validate_setting( $setting['std'], $setting['type'], $setting['id'] ); |
|
540 |
|
|
541 |
} |
|
542 |
|
|
543 |
} |
|
544 |
|
|
545 |
} |
|
546 |
|
|
547 |
update_option( $option['id'], $defaults ); |
|
548 |
|
|
549 |
} |
|
550 |
|
|
551 |
return false; |
|
552 |
} |
|
553 |
|
|
554 |
/** |
|
555 |
* Sanitize callback for register_setting() |
|
556 |
* |
|
557 |
* @return string |
|
558 |
* |
|
559 |
* @access public |
|
560 |
* @since 2.0 |
|
561 |
*/ |
|
562 |
public function sanitize_callback( $input ) { |
|
563 |
|
|
564 |
/* loop through options */ |
|
565 |
foreach( (array) $this->options as $option ) { |
|
566 |
|
|
567 |
/* loop through pages */ |
|
568 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
569 |
|
|
570 |
/* loop through page settings */ |
|
571 |
foreach( (array) $this->get_the_settings( $page ) as $setting ) { |
|
572 |
|
|
573 |
/* verify setting has a type & value */ |
|
574 |
if ( isset( $setting['type'] ) && isset( $input[$setting['id']] ) ) { |
|
575 |
|
|
576 |
/* get the defaults */ |
|
577 |
$current_settings = get_option( 'option_tree_settings' ); |
|
578 |
$current_options = get_option( $option['id'] ); |
|
579 |
|
|
580 |
/* validate setting */ |
|
581 |
if ( is_array( $input[$setting['id']] ) && in_array( $setting['type'], array( 'list-item', 'slider' ) ) ) { |
|
582 |
|
|
583 |
/* required title setting */ |
|
584 |
$required_setting = array( |
|
585 |
array( |
|
586 |
'id' => 'title', |
|
587 |
'label' => __( 'Title', 'option-tree' ), |
|
588 |
'desc' => '', |
|
589 |
'std' => '', |
|
590 |
'type' => 'text', |
|
591 |
'rows' => '', |
|
592 |
'class' => 'option-tree-setting-title', |
|
593 |
'post_type' => '', |
|
594 |
'choices' => array() |
|
595 |
) |
|
596 |
); |
|
597 |
|
|
598 |
/* get the settings array */ |
|
599 |
$settings = isset( $_POST[$setting['id'] . '_settings_array'] ) ? unserialize( ot_decode( $_POST[$setting['id'] . '_settings_array'] ) ) : array(); |
|
600 |
|
|
601 |
/* settings are empty for some odd ass reason get the defaults */ |
|
602 |
if ( empty( $settings ) ) { |
|
603 |
$settings = 'slider' == $setting['type'] ? |
|
604 |
ot_slider_settings( $setting['id'] ) : |
|
605 |
ot_list_item_settings( $setting['id'] ); |
|
606 |
} |
|
607 |
|
|
608 |
/* merge the two settings array */ |
|
609 |
$settings = array_merge( $required_setting, $settings ); |
|
610 |
|
|
611 |
/* create an empty WPML id array */ |
|
612 |
$wpml_ids = array(); |
|
613 |
|
|
614 |
foreach( $input[$setting['id']] as $k => $setting_array ) { |
|
615 |
|
|
616 |
foreach( $settings as $sub_setting ) { |
|
617 |
|
|
618 |
/* setup the WPML ID */ |
|
619 |
$wpml_id = $setting['id'] . '_' . $sub_setting['id'] . '_' . $k; |
|
620 |
|
|
621 |
/* add id to array */ |
|
622 |
$wpml_ids[] = $wpml_id; |
|
623 |
|
|
624 |
/* verify sub setting has a type & value */ |
|
625 |
if ( isset( $sub_setting['type'] ) && isset( $input[$setting['id']][$k][$sub_setting['id']] ) ) { |
|
626 |
|
|
627 |
/* validate setting */ |
|
628 |
$input[$setting['id']][$k][$sub_setting['id']] = ot_validate_setting( $input[$setting['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'], $wpml_id ); |
|
629 |
|
|
630 |
} |
|
631 |
|
|
632 |
} |
|
633 |
|
|
634 |
} |
|
635 |
|
|
636 |
} else { |
|
637 |
|
|
638 |
$input[$setting['id']] = ot_validate_setting( $input[$setting['id']], $setting['type'], $setting['id'], $setting['id'] ); |
|
639 |
|
|
640 |
} |
|
641 |
|
|
642 |
} |
|
643 |
|
|
644 |
/* unregister WPML strings that were deleted from lists and sliders */ |
|
645 |
if ( isset( $current_settings['settings'] ) && isset( $setting['type'] ) && in_array( $setting['type'], array( 'list-item', 'slider' ) ) ) { |
|
646 |
|
|
647 |
if ( ! isset( $wpml_ids ) ) |
|
648 |
$wpml_ids = array(); |
|
649 |
|
|
650 |
foreach( $current_settings['settings'] as $check_setting ) { |
|
651 |
|
|
652 |
if ( $setting['id'] == $check_setting['id'] && ! empty( $current_options[$setting['id']] ) ) { |
|
653 |
|
|
654 |
foreach( $current_options[$setting['id']] as $key => $value ) { |
|
655 |
|
|
656 |
foreach( $value as $ckey => $cvalue ) { |
|
657 |
|
|
658 |
$id = $setting['id'] . '_' . $ckey . '_' . $key; |
|
659 |
|
|
660 |
if ( ! in_array( $id, $wpml_ids ) ) { |
|
661 |
|
|
662 |
ot_wpml_unregister_string( $id ); |
|
663 |
|
|
664 |
} |
|
665 |
|
|
666 |
} |
|
667 |
|
|
668 |
} |
|
669 |
|
|
670 |
} |
|
671 |
|
|
672 |
} |
|
673 |
|
|
674 |
} |
|
675 |
|
|
676 |
} |
|
677 |
|
|
678 |
} |
|
679 |
|
|
680 |
} |
|
681 |
|
|
682 |
return $input; |
|
683 |
|
|
684 |
} |
|
685 |
|
|
686 |
/** |
|
687 |
* Helper function to get the pages array for an option |
|
688 |
* |
|
689 |
* @param array Option array |
|
690 |
* @return mixed |
|
691 |
* |
|
692 |
* @access public |
|
693 |
* @since 2.0 |
|
694 |
*/ |
|
695 |
public function get_pages( $option = array() ) { |
|
696 |
|
|
697 |
if ( empty( $option ) ) |
|
698 |
return false; |
|
699 |
|
|
700 |
/* check for pages */ |
|
701 |
if ( isset( $option['pages'] ) && ! empty( $option['pages'] ) ) { |
|
702 |
|
|
703 |
/* return pages array */ |
|
704 |
return $option['pages']; |
|
705 |
|
|
706 |
} |
|
707 |
|
|
708 |
return false; |
|
709 |
} |
|
710 |
|
|
711 |
/** |
|
712 |
* Helper function to get the sections array for a page |
|
713 |
* |
|
714 |
* @param array Page array |
|
715 |
* @return mixed |
|
716 |
* |
|
717 |
* @access public |
|
718 |
* @since 2.0 |
|
719 |
*/ |
|
720 |
public function get_sections( $page = array() ) { |
|
721 |
|
|
722 |
if ( empty( $page ) ) |
|
723 |
return false; |
|
724 |
|
|
725 |
/* check for sections */ |
|
726 |
if ( isset( $page['sections'] ) && ! empty( $page['sections'] ) ) { |
|
727 |
|
|
728 |
/* return sections array */ |
|
729 |
return $page['sections']; |
|
730 |
|
|
731 |
} |
|
732 |
|
|
733 |
return false; |
|
734 |
} |
|
735 |
|
|
736 |
/** |
|
737 |
* Helper function to get the settings array for a page |
|
738 |
* |
|
739 |
* @param array Page array |
|
740 |
* @return mixed |
|
741 |
* |
|
742 |
* @access public |
|
743 |
* @since 2.0 |
|
744 |
*/ |
|
745 |
public function get_the_settings( $page = array() ) { |
|
746 |
|
|
747 |
if ( empty( $page ) ) |
|
748 |
return false; |
|
749 |
|
|
750 |
/* check for settings */ |
|
751 |
if ( isset( $page['settings'] ) && ! empty( $page['settings'] ) ) { |
|
752 |
|
|
753 |
/* return settings array */ |
|
754 |
return $page['settings']; |
|
755 |
|
|
756 |
} |
|
757 |
|
|
758 |
return false; |
|
759 |
} |
|
760 |
|
|
761 |
/** |
|
762 |
* Prints out all settings sections added to a particular settings page |
|
763 |
* |
|
764 |
* @global $wp_settings_sections Storage array of all settings sections added to admin pages |
|
765 |
* @global $wp_settings_fields Storage array of settings fields and info about their pages/sections |
|
766 |
* |
|
767 |
* @param string The slug name of the page whos settings sections you want to output |
|
768 |
* @return string |
|
769 |
* |
|
770 |
* @access public |
|
771 |
* @since 2.0 |
|
772 |
*/ |
|
773 |
public function do_settings_sections( $page ) { |
|
774 |
global $wp_settings_sections, $wp_settings_fields; |
|
775 |
|
|
776 |
if ( ! isset( $wp_settings_sections ) || ! isset( $wp_settings_sections[$page] ) ) { |
|
777 |
return false; |
|
778 |
} |
|
779 |
|
|
780 |
foreach ( (array) $wp_settings_sections[$page] as $section ) { |
|
781 |
|
|
782 |
if ( ! isset( $section['id'] ) ) |
|
783 |
continue; |
|
784 |
|
|
785 |
echo '<div id="section_' . $section['id'] . '" class="postbox ui-tabs-panel">'; |
|
786 |
|
|
787 |
call_user_func( $section['callback'], $section ); |
|
788 |
|
|
789 |
if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[$page] ) || ! isset( $wp_settings_fields[$page][$section['id']] ) ) |
|
790 |
continue; |
|
791 |
|
|
792 |
echo '<div class="inside">'; |
|
793 |
|
|
794 |
$this->do_settings_fields( $page, $section['id'] ); |
|
795 |
|
|
796 |
echo '</div>'; |
|
797 |
|
|
798 |
echo '</div>'; |
|
799 |
|
|
800 |
} |
|
801 |
|
|
802 |
} |
|
803 |
|
|
804 |
/** |
|
805 |
* Print out the settings fields for a particular settings section |
|
806 |
* |
|
807 |
* @global $wp_settings_fields Storage array of settings fields and their pages/sections |
|
808 |
* |
|
809 |
* @param string $page Slug title of the admin page who's settings fields you want to show. |
|
810 |
* @param string $section Slug title of the settings section who's fields you want to show. |
|
811 |
* @return string |
|
812 |
* |
|
813 |
* @access public |
|
814 |
* @since 2.0 |
|
815 |
*/ |
|
816 |
public function do_settings_fields( $page, $section ) { |
|
817 |
global $wp_settings_fields; |
|
818 |
|
|
819 |
if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section]) ) |
|
820 |
return; |
|
821 |
|
|
822 |
foreach ( (array) $wp_settings_fields[$page][$section] as $field ) { |
|
823 |
|
|
824 |
echo '<div id="setting_' . $field['id'] . '" class="format-settings">'; |
|
825 |
|
|
826 |
echo '<div class="format-setting-wrap">'; |
|
827 |
|
|
828 |
if ( $field['args']['type'] != 'textblock' && ! empty( $field['title'] ) ) { |
|
829 |
|
|
830 |
echo '<div class="format-setting-label">'; |
|
831 |
|
|
832 |
echo '<h3 class="label">' . $field['title'] . '</h3>'; |
|
833 |
|
|
834 |
echo '</div>'; |
|
835 |
|
|
836 |
} |
|
837 |
|
|
838 |
call_user_func( $field['callback'], $field['args'] ); |
|
839 |
|
|
840 |
echo '</div>'; |
|
841 |
|
|
842 |
echo '</div>'; |
|
843 |
|
|
844 |
} |
|
845 |
|
|
846 |
} |
|
847 |
|
|
848 |
/** |
|
849 |
* Resets page options before the screen is displayed |
|
850 |
* |
|
851 |
* @return void |
|
852 |
* |
|
853 |
* @access public |
|
854 |
* @since 2.0 |
|
855 |
*/ |
|
856 |
public function reset_options() { |
|
857 |
|
|
858 |
/* check for reset action */ |
|
859 |
if ( isset( $_POST['option_tree_reset_nonce'] ) && wp_verify_nonce( $_POST['option_tree_reset_nonce'], 'option_tree_reset_form' ) ) { |
|
860 |
|
|
861 |
/* loop through options */ |
|
862 |
foreach( (array) $this->options as $option ) { |
|
863 |
|
|
864 |
/* loop through pages */ |
|
865 |
foreach( (array) $this->get_pages( $option ) as $page ) { |
|
866 |
|
|
867 |
/* verify page */ |
|
868 |
if ( isset( $_GET['page'] ) && $_GET['page'] == $page['menu_slug'] ) { |
|
869 |
|
|
870 |
/* reset options */ |
|
871 |
delete_option( $option['id'] ); |
|
872 |
|
|
873 |
} |
|
874 |
|
|
875 |
} |
|
876 |
|
|
877 |
} |
|
878 |
|
|
879 |
} |
|
880 |
|
|
881 |
return false; |
|
882 |
|
|
883 |
} |
|
884 |
|
|
885 |
} |
|
886 |
|
|
887 |
} |
|
888 |
|
|
889 |
/** |
|
890 |
* This method instantiates the settings class & builds the UI. |
|
891 |
* |
|
892 |
* @uses OT_Settings() |
|
893 |
* |
|
894 |
* @param array Array of arguments to create settings |
|
895 |
* @return void |
|
896 |
* |
|
897 |
* @access public |
|
898 |
* @since 2.0 |
|
899 |
*/ |
|
900 |
if ( ! function_exists( 'ot_register_settings' ) ) { |
|
901 |
|
|
902 |
function ot_register_settings( $args ) { |
|
903 |
if ( ! $args ) |
|
904 |
return; |
|
905 |
|
|
906 |
$ot_settings = new OT_Settings( $args ); |
|
907 |
} |
|
908 |
|
|
909 |
} |
|
910 |
|
|
911 |
/* End of file ot-settings-api.php */ |
|
912 |
/* Location: ./includes/ot-settings-api.php */ |