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