PluginProbe
WP Ultimate Post Grid / 2.8.2
WP Ultimate Post Grid v2.8.2
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / vafpress / classes / option.php

option.php in WP Ultimate Post Grid 2.8.2, at vendor/vafpress/classes/option.php

795 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class VP_Option
4 {
5
6 private $_option_key;
7
8 private $_page_slug;
9
10 private $_template;
11
12 private $_is_dev_mode;
13
14 private $_use_util_menu;
15
16 private $_use_auto_group_naming;
17
18 private $_role;
19
20 private $_menu_page;
21
22 private $_page_title;
23
24 private $_menu_label;
25
26 private $_layout;
27
28 private $_options_set = NULL;
29
30 private $_options = NULL;
31
32 private $_hook_suffix;
33
34 public static $pool;
35
36 public function __construct(array $configs)
37 {
38
39 // merge configs with default value
40 $configs = array_merge(array(
41 'is_dev_mode' => false,
42 'use_auto_group_naming' => true,
43 'use_util_menu' => true,
44 'minimum_role' => 'edit_theme_options',
45 'menu_page' => 'themes.php',
46 'layout' => 'fixed',
47 'page_title' => __( 'Vafpress Options', 'wp-ultimate-recipe' ),
48 'menu_label' => __( 'Vafpress Options', 'wp-ultimate-recipe' ),
49 'priority' => 10,
50 ), $configs);
51
52 // options config filter
53 $configs = apply_filters('vp_option_configuration_array' , $configs, $configs['option_key']);
54 $configs = apply_filters('vp_option_configuration_array-' . $configs['option_key'], $configs);
55
56 // extract the configs
57 extract($configs);
58
59 // check and set required configs
60 if(isset($option_key)) $this->set_option_key($option_key);
61 else throw new Exception(__( 'Option Key is required', 'wp-ultimate-recipe' ), 1);
62 if(isset($template)) $this->set_template($template);
63 else throw new Exception(__( 'Template Array/File is required', 'wp-ultimate-recipe' ), 1);
64 if(isset($page_slug)) $this->set_page_slug($page_slug);
65 else throw new Exception(__( 'Page Slug is required', 'wp-ultimate-recipe' ), 1);
66
67 // swim in the pool
68 self::$pool[$this->get_option_key()] = &$this;
69
70 // check and set the remaining configs
71 if(isset($menu_page)) $this->set_menu_page($menu_page);
72 if(isset($is_dev_mode)) $this->is_dev_mode($is_dev_mode);
73 if(isset($use_util_menu)) $this->use_util_menu($use_util_menu);
74 if(isset($use_auto_group_naming)) $this->use_auto_group_naming($use_auto_group_naming);
75 if(isset($minimum_role)) $this->set_minimum_role($minimum_role);
76 if(isset($page_title)) $this->set_page_title($page_title);
77 if(isset($layout)) $this->set_layout($layout);
78 if(isset($menu_label)) $this->set_menu_label($menu_label);
79
80 // add first_activation hook to save initial values to db
81 add_action('vp_option_first_activation', array($this, 'initial_db_setup'));
82
83 // check if option key not existed init data from default values
84 $options = get_option( $this->get_option_key() );
85 if( $options === FALSE )
86 {
87 do_action('vp_option_first_activation');
88 }
89
90 // init options from db and expose to the api
91 $this->init_options_from_db();
92
93 // setup ajax
94 add_action('wp_ajax_vp_ajax_' . $this->get_option_key() . '_export_option', array($this, 'ajax_export_option'));
95 add_action('wp_ajax_vp_ajax_' . $this->get_option_key() . '_import_option', array($this, 'ajax_import_option'));
96 add_action('wp_ajax_vp_ajax_' . $this->get_option_key() . '_save' , array($this, 'ajax_save'));
97 add_action('wp_ajax_vp_ajax_' . $this->get_option_key() . '_restore' , array($this, 'ajax_restore'));
98
99 // register menu page
100 add_action( 'admin_menu', array($this, 'register_menu_page'), $priority );
101 }
102
103 public static function get_pool()
104 {
105 return self::$pool;
106 }
107
108 public function init_options_from_db()
109 {
110 $options = get_option( $this->get_option_key() );
111 if( $options !== FALSE )
112 {
113 $this->set_options($options);
114 }
115 }
116
117 // register menu page as configured
118 public function register_menu_page()
119 {
120 if( is_array( $this->get_menu_page() ) )
121 {
122 $menu_page = $this->get_menu_page();
123
124 // check and set required configs
125 if(!isset($menu_page['icon_url'])) $menu_page['icon_url'] = '';
126 if(!isset($menu_page['position'])) $menu_page['position'] = null;
127
128 $hook_suffix = add_menu_page(
129 $this->get_page_title(),
130 $this->get_menu_label(),
131 $this->get_minimum_role(),
132 $this->get_page_slug(),
133 array($this, 'option_page_display'),
134 $menu_page['icon_url'],
135 $menu_page['position']
136 );
137 }
138 else
139 {
140 $hook_suffix = add_submenu_page(
141 $this->get_menu_page(),
142 $this->get_page_title(),
143 $this->get_menu_label(),
144 $this->get_minimum_role(),
145 $this->get_page_slug(),
146 array($this, 'option_page_display')
147 );
148 }
149 $this->set_hook_suffix($hook_suffix);
150
151 // register option page load
152 add_action( 'load-' . $this->get_hook_suffix(), array($this, 'setup') );
153 }
154
155 public function setup()
156 {
157 $this->init_options_set();
158 $this->init_options();
159 $this->enqueue_scripts_and_styles();
160 // show dev mode notice
161 if( $this->is_dev_mode() )
162 add_action( 'admin_notices', array( $this, 'dev_mode_notice' ) );
163 }
164
165 public function dev_mode_notice()
166 {
167 VP_WP_Util::admin_notice(__("Development Mode is Active, options' values won't be saved into database.", 'wp-ultimate-recipe'), false);
168 }
169
170 public function enqueue_scripts_and_styles()
171 {
172 $opt_loader = VP_WP_Loader::instance();
173 $opt_loader->add_types( $this->get_field_types(), 'option' );
174 $opt_loader->add_main_js( 'vp-option' );
175 $opt_loader->add_main_css( 'vp-option' );
176 $opt_loader->add_js_data( 'vp-option', 'custom_local.name', $this->_option_key );
177 $opt_loader->add_js_data( 'vp-option', 'custom_local.SAVE_SUCCESS', VP_Option_Control_Set::SAVE_SUCCESS );
178 $opt_loader->add_js_data( 'vp-option', 'custom_local.SAVE_NOCHANGES', VP_Option_Control_Set::SAVE_NOCHANGES );
179 $opt_loader->add_js_data( 'vp-option', 'custom_local.SAVE_FAILED', VP_Option_Control_Set::SAVE_FAILED );
180 }
181
182 function save_and_reinit()
183 {
184 // do saving
185 $result = $this->get_options_set()->save($this->get_option_key());
186
187 // re-init $opt
188 $this->init_options_from_db();
189
190 // get the new $opt
191 $opt = $this->get_options_set()->get_values();
192
193 // save and re-init action hook
194 do_action('vp_option_save_and_reinit', $opt, $result['status'], $this->get_option_key());
195
196 // option key specific save and re-init action hook
197 do_action('vp_option_save_and_reinit-' . $this->get_option_key(), $opt, $result['status']);
198
199 return $result;
200 }
201
202 function ajax_save()
203 {
204 $result = $this->vp_verify_nonce();
205
206 if($result['status'])
207 {
208 $this->init_options_set();
209 $this->init_options();
210
211 $option = $_POST['option'];
212 $nonce = $_POST['nonce'];
213
214 $option = VP_Util_Array::unite( $option, 'name', 'value' );
215 $option = $this->get_options_set()->normalize_values($option);
216
217 // stripslashes added by WP in $_GET / $_POST
218 $option = stripslashes_deep($option);
219
220 // get old options from set
221 $old_opt = $this->get_options_set()->get_values();
222
223 $this->get_options_set()->populate_values($option, true);
224
225 // get back options from set
226 $opt = $this->get_options_set()->get_values();
227
228 // before ajax save action hook
229 do_action('vp_option_before_ajax_save', $opt);
230
231 // save and re-init options
232 $result = $this->save_and_reinit();
233
234 // after ajax save action hook
235 do_action('vp_option_after_ajax_save', $opt, $old_opt, $result['status'], $this->get_option_key());
236
237 // option key specific after ajax save action hook
238 do_action('vp_option_after_ajax_save-' . $this->get_option_key(), $opt, $old_opt, $result['status']);
239 }
240
241 if (ob_get_length()) ob_clean();
242 header('Content-type: application/json');
243 echo json_encode($result);
244 die();
245 }
246
247 function ajax_restore()
248 {
249 $result = $this->vp_verify_nonce();
250
251 if( $result['status'] )
252 {
253 $this->init_options_set();
254 $set = $this->get_options_set();
255 $options = $set->get_defaults();
256
257 // get old options from set
258 $old_opt = $this->get_options_set()->get_values();
259
260 // set options so that default value can be accessed in binding done in `setup`
261 $this->set_options($options);
262
263 // setup and process values
264 $set->setup($options);
265
266 // before ajax save action hook
267 do_action('vp_option_before_ajax_restore', $options);
268
269 // save and re-init options
270 $result = $this->save_and_reinit();
271
272 $options = $this->get_options_set()->get_values();
273
274 // after ajax restore action hook
275 do_action('vp_option_after_ajax_restore', $options, $old_opt, $result['status'], $this->get_option_key());
276
277 // after ajax restore action hook
278 do_action('vp_option_after_ajax_restore-' . $this->get_option_key(), $options, $old_opt, $result['status']);
279 }
280
281 if (ob_get_length()) ob_clean();
282 header('Content-type: application/json');
283 echo json_encode($result);
284 die();
285 }
286
287 function ajax_import_option()
288 {
289 global $vp_set, $vp_config;
290
291 $options = null;
292 $old_opt = null;
293
294 $result = $this->vp_verify_nonce();
295
296 if($result['status'])
297 {
298 $this->init_options_set();
299 $this->init_options();
300
301 $option = $_POST['option'];
302
303 if(empty($option))
304 {
305 $result['status'] = false;
306 $result['message'] = __("Can not be empty", 'wp-ultimate-recipe');
307 }
308 else
309 {
310 $option = json_decode(stripslashes($option), true);
311
312 if( is_array($option) )
313 {
314 $set = $this->get_options_set();
315
316 // get old options from set
317 $old_opt = $this->get_options_set()->get_values();
318
319 // populate new values
320 $set->populate_values($option, false);
321
322 // save and re-init options
323 $result = $this->save_and_reinit();
324
325 // get new options
326 $options = $this->get_options_set()->get_values();
327 }
328 else
329 {
330 $result['status'] = false;
331 $result['message'] = __("Invalid data", 'wp-ultimate-recipe');
332 }
333 }
334 }
335
336 // after ajax import action hook
337 do_action('vp_option_after_ajax_import', $options, $old_opt, $result['status'], $this->get_option_key());
338
339 // after ajax import action hook
340 do_action('vp_option_after_ajax_import-' . $this->get_option_key(), $options, $old_opt, $result['status']);
341
342 if (ob_get_length()) ob_clean();
343 header('Content-type: application/json');
344 echo json_encode($result);
345 die();
346 }
347
348 function ajax_export_option()
349 {
350 global $wpdb;
351
352 $sr_options = null;
353 $db_options = null;
354
355 $result = $this->vp_verify_nonce();
356
357 if($result['status'])
358 {
359 $db_options = get_option($this->get_option_key());
360 $sr_options = json_encode($db_options);
361
362 $result = array(
363 'status' => true,
364 'message'=> __("Successful", 'wp-ultimate-recipe'),
365 'option' => $sr_options,
366 );
367 }
368
369 // after ajax export action hook
370 do_action('vp_option_after_ajax_export', $db_options, $sr_options, $result['status'], $this->get_option_key());
371
372 // after ajax export action hook
373 do_action('vp_option_after_ajax_export-' . $this->get_option_key(), $db_options, $sr_options, $result['status']);
374
375
376 if (ob_get_length()) ob_clean();
377 header('Content-type: application/json');
378 echo json_encode($result);
379 die();
380 }
381
382 function vp_verify_nonce()
383 {
384 $nonce = $_POST['nonce'];
385 $verify = check_ajax_referer('vafpress', 'nonce', false);
386 if($verify)
387 {
388 $result['status'] = true;
389 $result['message'] = __("Successful", 'wp-ultimate-recipe');
390 }
391 else
392 {
393 $result['status'] = false;
394 $result['message'] = __("Unverified Access", 'wp-ultimate-recipe');
395 }
396 return $result;
397 }
398
399 function initial_db_setup()
400 {
401 // init set and options
402 $this->init_options();
403 $set = $this->get_options_set();
404
405 // get baked values from options set
406 $opt = $set->get_values();
407
408 // before db options db action hook
409 do_action('vp_option_before_db_init', $opt);
410
411 // save to db
412 $result = $this->save_and_reinit();
413
414 // after db options db action hook
415 do_action('vp_option_after_db_init', $opt, $result['status'], $this->get_option_key());
416 do_action('vp_option_after_db_init-' . $this->get_option_key(), $opt, $result['status']);
417 }
418
419 public function init_options()
420 {
421 $this->init_options_set();
422 $set = $this->get_options_set();
423
424 // try load option from DB
425 $db_options = get_option($this->get_option_key());
426 $default = $set->get_defaults();
427 if (!empty($db_options))
428 {
429 // unify, preserve option from DB but appends anything new from default
430 $options = $db_options;
431 $options = $options + $default;
432 }
433 else
434 {
435 $options = $set->get_defaults();
436 }
437
438 // If dev mode, always use default, no db interaction
439 if($this->is_dev_mode())
440 $options = $set->get_defaults();
441
442 // set options so that default value can be accessed in binding done in `setup`
443 $this->set_options($options);
444
445 // setup and process values
446 $set->setup($options);
447
448 }
449
450 public function init_options_set()
451 {
452 if(!is_null($this->get_options_set()))
453 return;
454
455 if( is_string($this->get_template()) and is_file($this->get_template()) )
456 $template = include $this->get_template();
457 else if(is_array($this->get_template()))
458 $template = $this->get_template();
459 else
460 throw new Exception(__( 'Invalid template supplied', 'wp-ultimate-recipe' ), 1);
461
462 $parser = new VP_Option_Parser();
463 $set = $parser->parse_array_options($template, $this->use_auto_group_naming());
464 $set->set_layout($this->get_layout());
465
466 // assign set object
467 $this->set_options_set($set);
468
469 if( $this->use_util_menu() )
470 {
471 // setup utility menu
472 $util_menu = new VP_Option_Control_Group_Menu();
473 $util_menu->set_title(__('Utility', 'wp-ultimate-recipe'));
474 $util_menu->set_name('menu_util');
475 $util_menu->set_icon('font-awesome:fa-ambulance');
476
477 // setup restore default section
478 $restore_section = new VP_Option_Control_Group_Section();
479 $restore_section->set_title(__('Restore Default', 'wp-ultimate-recipe'));
480 $restore_section->set_name('section_restore');
481
482 // setup restore button
483 $restore_button = new VP_Option_Control_Field_Restore();
484 $restore_section->add_field($restore_button);
485
486 // setup exim section
487 $exim_section = new VP_Option_Control_Group_Section();
488 $exim_section->set_title(__('Export/Import', 'wp-ultimate-recipe'));
489 $exim_section->set_name('section_exim');
490
491 // setup exim field
492 $exim_field = new VP_Option_Control_Field_ImpExp();
493 $exim_section->add_field($exim_field);
494
495 // add exim section
496 $util_menu->add_control($exim_section);
497
498 $util_menu->add_control($restore_section);
499 $set->add_menu($util_menu);
500 }
501 }
502
503 public function option_page_display()
504 {
505 echo $this->get_options_set()->render();
506 }
507
508 public function get_field_types()
509 {
510 // $this->init_options_set();
511 return $this->get_options_set()->get_field_types();
512 }
513
514 // @todo return `vp_option` like function
515 public function create_get_option_helper()
516 {
517
518 }
519
520 //////////////////////////////
521 // GETTER AND SETTER CHUNKS //
522 //////////////////////////////
523
524 /**
525 * Get _hook_suffix
526 *
527 * @return String _hook_suffix
528 */
529 public function get_hook_suffix()
530 {
531 return $this->_hook_suffix;
532 }
533
534 /**
535 * Set _hook_suffix
536 *
537 * @param String $_hook_suffix _hook_suffix
538 */
539 public function set_hook_suffix($_hook_suffix)
540 {
541 $this->_hook_suffix = $_hook_suffix;
542 return $this;
543 }
544
545 /**
546 * Get _template
547 *
548 * @return String _template
549 */
550 public function get_template()
551 {
552 return $this->_template;
553 }
554
555 /**
556 * Set _template
557 *
558 * @param String $_template _template
559 */
560 public function set_template($_template)
561 {
562 $this->_template = $_template;
563 return $this;
564 }
565
566 /**
567 * Get _options
568 *
569 * @return String _options
570 */
571 public function get_options()
572 {
573 return $this->_options;
574 }
575
576 /**
577 * Set _options
578 *
579 * @param String $_options _options
580 */
581 public function set_options($_options)
582 {
583 $this->_options = $_options;
584 return $this;
585 }
586
587 /**
588 * Get _options_set
589 *
590 * @return String _options_set
591 */
592 public function get_options_set()
593 {
594 return $this->_options_set;
595 }
596
597 /**
598 * Set _options_set
599 *
600 * @param String $_options_set _options_set
601 */
602 public function set_options_set($_options_set)
603 {
604 $this->_options_set = $_options_set;
605 return $this;
606 }
607
608 /**
609 * Get _menu_page
610 *
611 * @return String _menu_page
612 */
613 public function get_menu_page()
614 {
615 return $this->_menu_page;
616 }
617
618 /**
619 * Set _menu_page
620 *
621 * @param String $_menu_page _menu_page
622 */
623 public function set_menu_page($_menu_page)
624 {
625 $this->_menu_page = $_menu_page;
626 return $this;
627 }
628
629 /**
630 * Set _layout
631 *
632 * @return String _layout
633 */
634 public function get_layout()
635 {
636 return $this->_layout;
637 }
638
639 /**
640 * Get _layout
641 *
642 * @param String $_layout _layout
643 */
644 public function set_layout($_layout)
645 {
646 $this->_layout = $_layout;
647 return $this;
648 }
649
650 /**
651 * Get _menu_page_slug
652 *
653 * @return String _menu_page_slug
654 */
655 public function get_page_slug()
656 {
657 return $this->_page_slug;
658 }
659
660 /**
661 * Set _page_slug
662 *
663 * @param String $_page_slug _page_slug
664 */
665 public function set_page_slug($_page_slug)
666 {
667 $this->_page_slug = $_page_slug;
668 return $this;
669 }
670
671 /**
672 * Get _menu_label
673 *
674 * @return String _menu_label
675 */
676 public function get_menu_label()
677 {
678 return $this->_menu_label;
679 }
680
681 /**
682 * Set _menu_label
683 *
684 * @param String $_menu_label _menu_label
685 */
686 public function set_menu_label($_menu_label)
687 {
688 $this->_menu_label = $_menu_label;
689 return $this;
690 }
691
692 /**
693 * Get _page_title value
694 *
695 * @return String _page_title
696 */
697 public function get_page_title()
698 {
699 return $this->_page_title;
700 }
701
702 /**
703 * Set _page_title
704 *
705 * @param String $_page_title _page_title
706 */
707 public function set_page_title($_page_title)
708 {
709 $this->_page_title = $_page_title;
710 return $this;
711 }
712
713 /**
714 * Get _minimum_role value
715 *
716 * @return String $_minimum_role
717 */
718 public function get_minimum_role()
719 {
720 return $this->_minimum_role;
721 }
722
723 /**
724 * Set _minimum_role value
725 *
726 * @param String $_minimum_role _minimum_role
727 */
728 public function set_minimum_role($_minimum_role)
729 {
730 $this->_minimum_role = $_minimum_role;
731 return $this;
732 }
733
734 /**
735 * Get _option_key value
736 *
737 * @return String $_option_key
738 */
739 public function get_option_key()
740 {
741 return $this->_option_key;
742 }
743
744 /**
745 * Set _option_key value
746 *
747 * @param String $_option_key $_option_key
748 */
749 public function set_option_key($_option_key)
750 {
751 $this->_option_key = $_option_key;
752 return $this;
753 }
754
755 /**
756 * Get/Set whether to use auto group naming or not
757 *
758 * @return bool $_use_auto_group_naming
759 */
760 public function use_auto_group_naming($_use_auto_group_naming = NULL)
761 {
762 if(is_null($_use_auto_group_naming))
763 return $this->_use_auto_group_naming;
764 $this->_use_auto_group_naming = $_use_auto_group_naming;
765 }
766
767 /**
768 * Get/Set whether to use export import menu or not
769 *
770 * @return bool $_use_util_menu
771 */
772 public function use_util_menu($_use_util_menu = NULL)
773 {
774 if(is_null($_use_util_menu))
775 return $this->_use_util_menu;
776 $this->_use_util_menu = $_use_util_menu;
777 }
778
779 /**
780 * Get/Set whether it's development mode or not
781 *
782 * @return bool $_dev_mode
783 */
784 public function is_dev_mode($_dev_mode = NULL)
785 {
786 if(is_null($_dev_mode))
787 return $this->_dev_mode;
788 $this->_dev_mode = $_dev_mode;
789 }
790
791 }
792
793 /**
794 * EOF
795 */