1
+ <?php
2
+
3
+ class MvcSettings {
4
+
5
+ public $ name = null ;
6
+ public $ title = null ;
7
+ public $ key = null ;
8
+ public $ settings = null ;
9
+
10
+ function __construct () {
11
+ $ this ->name = get_class ($ this );
12
+ $ this ->title = MvcInflector::titleize ($ this ->name );
13
+ $ this ->key = 'mvc_ ' .MvcInflector::underscore ($ this ->name );
14
+ $ this ->init_settings ();
15
+ }
16
+
17
+ public function description () {
18
+ }
19
+
20
+ public function page () {
21
+ echo '<div> ' ;
22
+ echo '<h2> ' .$ this ->title .'</h2> ' ;
23
+ echo '<form action="options.php" method="post"> ' ;
24
+ settings_fields ($ this ->key );
25
+ do_settings_sections ($ this ->key );
26
+ echo '<input name="Submit" type="submit" value=" ' .esc_attr ('Save Changes ' ).'" /> ' ;
27
+ echo '</form> ' ;
28
+ echo '</div> ' ;
29
+ }
30
+
31
+ public function display_field ($ setting_key ) {
32
+ $ setting = $ this ->settings [$ setting_key ];
33
+ $ options = get_option ($ this ->key );
34
+ $ value = isset ($ options [$ setting_key ]) ? $ options [$ setting_key ] : null ;
35
+ if (is_null ($ value )) {
36
+ if ($ setting ['default ' ]) {
37
+ $ value = $ setting ['default ' ];
38
+ } else if ($ setting ['default_method ' ]) {
39
+ $ value = $ this ->{$ setting ['default_method ' ]}();
40
+ }
41
+ }
42
+ $ input_value = $ value ;
43
+ if ($ setting ['type ' ] == 'checkbox ' ) {
44
+ $ input_value = '1 ' ;
45
+ }
46
+ $ input_options = array (
47
+ 'id ' => $ setting ['key ' ],
48
+ 'name ' => $ this ->key .'[ ' .$ setting ['key ' ].'] ' ,
49
+ 'type ' => $ setting ['type ' ],
50
+ 'value ' => $ input_value
51
+ );
52
+ if ($ setting ['type ' ] == 'checkbox ' && $ value ) {
53
+ $ input_options ['checked ' ] = 'checked ' ;
54
+ }
55
+ if ($ setting ['type ' ] == 'select ' ) {
56
+ if ($ setting ['options ' ]) {
57
+ $ input_options ['options ' ] = $ setting ['options ' ];
58
+ } else if ($ setting ['options_method ' ]) {
59
+ $ input_options ['options ' ] = $ this ->{$ setting ['options_method ' ]}();
60
+ }
61
+ }
62
+ $ html = MvcFormTagsHelper::input ($ setting_key , $ input_options );
63
+ echo $ html ;
64
+ }
65
+
66
+ public function validate_fields ($ inputs ) {
67
+ foreach ($ inputs as $ setting_key => $ value ) {
68
+ if (method_exists ($ this , 'validate_field_ ' .$ setting_key )) {
69
+ $ inputs [$ setting_key ] = $ this ->{'validate_field_ ' .$ setting_key }($ setting_key , $ value );
70
+ } else {
71
+ $ inputs [$ setting_key ] = $ this ->validate_field ($ setting_key , $ value );
72
+ }
73
+ }
74
+ return $ inputs ;
75
+ }
76
+
77
+ public function validate_field ($ setting_key , $ value ) {
78
+ return $ value ;
79
+ }
80
+
81
+ protected function init_settings () {
82
+ if (empty ($ this ->settings )) {
83
+ $ this ->settings = array ();
84
+ }
85
+ $ settings = array ();
86
+ foreach ($ this ->settings as $ key => $ setting ) {
87
+ $ defaults = array (
88
+ 'key ' => $ key ,
89
+ 'type ' => 'text ' ,
90
+ 'label ' => MvcInflector::titleize ($ key ),
91
+ 'value ' => null ,
92
+ 'value_method ' => null ,
93
+ 'default ' => null ,
94
+ 'default_method ' => null ,
95
+ 'options ' => null ,
96
+ 'options_method ' => null
97
+ );
98
+ $ setting = array_merge ($ defaults , $ setting );
99
+ $ settings [$ key ] = $ setting ;
100
+ }
101
+ $ this ->settings = $ settings ;
102
+ }
103
+
104
+ public function __call ($ method_name , $ arguments ) {
105
+ if (substr ($ method_name , 0 , 14 ) == 'display_field_ ' ) {
106
+ $ setting_key = substr ($ method_name , 14 );
107
+ $ this ->display_field ($ setting_key );
108
+ return true ;
109
+ }
110
+ if (substr ($ method_name , 0 , 15 ) == 'validate_field_ ' ) {
111
+ $ setting_key = substr ($ method_name , 15 );
112
+ $ this ->validate_field ($ setting_key , $ arguments [0 ]);
113
+ return true ;
114
+ }
115
+ MvcError::fatal ('Undefined method: ' .get_class ($ this ).':: ' .$ method_name .'. ' );
116
+ }
117
+
118
+ }
119
+
120
+ ?>
0 commit comments