PluginProbe
WP Ultimate Post Grid / 2.6.0
WP Ultimate Post Grid v2.6.0
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.6.0, at vendor/vafpress/classes/option.php

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