@@ -14,7 +14,7 @@ class Custom_Featured_Image_Metabox_Settings {
14
14
*/
15
15
protected $ plugin_slug = null ;
16
16
17
- protected $ plugin_options = null ;
17
+ protected $ supported_post_types = null ;
18
18
19
19
/**
20
20
* Instance of this class.
@@ -35,11 +35,7 @@ private function __construct() {
35
35
36
36
$ plugin = Custom_Featured_Image_Metabox::get_instance ();
37
37
$ this ->plugin_slug = $ plugin ->get_plugin_slug ();
38
-
39
- if ( false == get_option ( $ this ->plugin_slug ) ) {
40
- add_option ( $ this ->plugin_slug , $ this ->default_settings () );
41
- }
42
- $ this ->plugin_options = $ plugin ->get_plugin_options ();
38
+ $ this ->supported_post_types = $ plugin ->supported_post_types ();
43
39
44
40
// Add settings page
45
41
add_action ( 'admin_init ' , array ( $ this , 'admin_init ' ) );
@@ -70,25 +66,36 @@ public static function get_instance() {
70
66
*/
71
67
public function admin_init () {
72
68
73
- $ post_types = $ this ->supported_post_types ();
74
- $ options = $ this ->plugin_options ;
69
+ $ post_types = $ this ->supported_post_types ;
70
+ $ defaults = array (
71
+ 'title ' => '' ,
72
+ 'instruction ' => '' ,
73
+ 'set_text ' => '' ,
74
+ 'remove_text ' => '' ,
75
+ );
75
76
76
77
foreach ( $ post_types as $ pt ) {
77
78
$ post_object = get_post_type_object ( $ pt );
78
- $ args = array ( $ pt , $ options [$ pt ] );
79
+ $ section = $ this ->plugin_slug . '_ ' . $ pt ;
80
+
81
+ if ( false == get_option ( $ section ) ) {
82
+ add_option ( $ section , apply_filters ( $ section . '_default_settings ' , $ defaults ) );
83
+ }
84
+
85
+ $ args = array ( $ section , get_option ( $ section ) );
79
86
80
87
add_settings_section (
81
88
$ pt ,
82
89
sprintf ( __ ( 'Featured Image Metabox in %s ' , $ this ->plugin_slug ), $ post_object ->labels ->name ),
83
90
'' ,
84
- $ this -> plugin_slug
91
+ $ section
85
92
);
86
93
87
94
add_settings_field (
88
95
'title ' ,
89
96
__ ( 'Title Text ' , $ this ->plugin_slug ),
90
97
array ( $ this , 'title_callback ' ),
91
- $ this -> plugin_slug ,
98
+ $ section ,
92
99
$ pt ,
93
100
$ args
94
101
);
@@ -97,7 +104,7 @@ public function admin_init() {
97
104
'instruction ' ,
98
105
__ ( 'Instruction ' , $ this ->plugin_slug ),
99
106
array ( $ this , 'instruction_callback ' ),
100
- $ this -> plugin_slug ,
107
+ $ section ,
101
108
$ pt ,
102
109
$ args
103
110
);
@@ -106,7 +113,7 @@ public function admin_init() {
106
113
'set_text ' ,
107
114
__ ( 'Set Text ' , $ this ->plugin_slug ),
108
115
array ( $ this , 'set_text_callback ' ),
109
- $ this -> plugin_slug ,
116
+ $ section ,
110
117
$ pt ,
111
118
$ args
112
119
);
@@ -115,71 +122,25 @@ public function admin_init() {
115
122
'remove_text ' ,
116
123
__ ( 'Remove Text ' , $ this ->plugin_slug ),
117
124
array ( $ this , 'remove_text_callback ' ),
118
- $ this -> plugin_slug ,
125
+ $ section ,
119
126
$ pt ,
120
127
$ args
121
128
);
122
- }
123
-
124
- register_setting (
125
- $ this ->plugin_slug ,
126
- $ this ->plugin_slug ,
127
- array ( $ this , 'validate_inputs ' )
128
- );
129
-
130
- } // end admin_init
131
-
132
- /**
133
- * Provides default values for the plugin settings.
134
- *
135
- * @return array<string> Default settings
136
- */
137
- public function default_settings () {
138
129
139
- $ post_types = $ this ->supported_post_types ();
140
- $ keys = array (
141
- 'title ' => '' ,
142
- 'instruction ' => '' ,
143
- 'set_text ' => '' ,
144
- 'remove_text ' => '' ,
130
+ register_setting (
131
+ $ section ,
132
+ $ section ,
133
+ array ( $ this , 'validate_inputs ' )
145
134
);
146
- $ defaults = array ();
147
-
148
- foreach ( $ post_types as $ pt ) {
149
- $ defaults [$ pt ] = $ keys ;
150
- }
151
-
152
- return apply_filters ( 'cfim_default_settings ' , $ defaults );
153
-
154
- } // end default_settings
155
-
156
- /**
157
- * Get post types with thumbnail support
158
- *
159
- * @return array supported post types
160
- *
161
- * @since 0.6.0
162
- */
163
- public function supported_post_types () {
164
-
165
- $ post_types = get_post_types ();
166
- $ results = array ();
167
-
168
- foreach ( $ post_types as $ pt ) {
169
- if ( post_type_supports ( $ pt , 'thumbnail ' ) ) {
170
- $ results [] = $ pt ;
171
- }
172
135
}
173
136
174
- return $ results ;
175
-
176
- } // end supported_post_types
137
+ } // end admin_init
177
138
178
139
public function title_callback ( $ args ) {
179
140
180
141
$ value = isset ( $ args [1 ]['title ' ] ) ? $ args [1 ]['title ' ] : '' ;
181
142
182
- $ html = '<input type"text" id="title" name=" ' . $ this -> plugin_slug . ' [ ' . $ args [0 ] . '] [title]" value=" ' . $ value . '" class="regular-text" /> ' ;
143
+ $ html = '<input type"text" id="title" name=" ' . $ args [0 ] . '[title]" value=" ' . $ value . '" class="regular-text" /> ' ;
183
144
$ html .= '<p class="description"> ' . __ ( 'Enter your custom title for Featured Image Metabox. ' , $ this ->plugin_slug ) . '</p> ' ;
184
145
185
146
echo $ html ;
@@ -190,7 +151,7 @@ public function instruction_callback( $args ) {
190
151
191
152
$ value = isset ( $ args [1 ]['instruction ' ] ) ? $ args [1 ]['instruction ' ] : '' ;
192
153
193
- $ html = '<input type"text" id="instruction" name=" ' . $ this -> plugin_slug . ' [ ' . $ args [0 ] . '] [instruction]" value=" ' . $ value . '" class="regular-text" /> ' ;
154
+ $ html = '<input type"text" id="instruction" name=" ' . $ args [0 ] . '[instruction]" value=" ' . $ value . '" class="regular-text" /> ' ;
194
155
$ html .= '<p class="description"> ' . __ ( 'Enter the instruction for Featured Image, like image dimensions. ' , $ this ->plugin_slug ) . '</p> ' ;
195
156
196
157
echo $ html ;
@@ -201,7 +162,7 @@ public function set_text_callback( $args ) {
201
162
202
163
$ value = isset ( $ args [1 ]['set_text ' ] ) ? $ args [1 ]['set_text ' ] : '' ;
203
164
204
- $ html = '<input type"text" id="set_text" name=" ' . $ this -> plugin_slug . ' [ ' . $ args [0 ] . '] [set_text]" value=" ' . $ value . '" class="regular-text" /> ' ;
165
+ $ html = '<input type"text" id="set_text" name=" ' . $ args [0 ] . '[set_text]" value=" ' . $ value . '" class="regular-text" /> ' ;
205
166
$ html .= '<p class="description"> ' . sprintf ( __ ( 'Enter the custom text to replace the default "%s". ' , $ this ->plugin_slug ), __ ( 'Set featured image ' ) ) . '</p> ' ;
206
167
207
168
echo $ html ;
@@ -212,7 +173,7 @@ public function remove_text_callback( $args ) {
212
173
213
174
$ value = isset ( $ args [1 ]['remove_text ' ] ) ? $ args [1 ]['remove_text ' ] : '' ;
214
175
215
- $ html = '<input type"text" id="remove_text" name=" ' . $ this -> plugin_slug . ' [ ' . $ args [0 ] . '] [remove_text]" value=" ' . $ value . '" class="regular-text" /> ' ;
176
+ $ html = '<input type"text" id="remove_text" name=" ' . $ args [0 ] . '[remove_text]" value=" ' . $ value . '" class="regular-text" /> ' ;
216
177
$ html .= '<p class="description"> ' . sprintf ( __ ( 'Enter the custom text to replace the default "%s". ' , $ this ->plugin_slug ), __ ( 'Remove featured image ' ) ) . '</p> ' ;
217
178
218
179
echo $ html ;
@@ -231,20 +192,12 @@ public function validate_inputs( $inputs ) {
231
192
$ outputs = array ();
232
193
233
194
foreach ( $ inputs as $ key => $ value ) {
234
- if ( is_array ( $ value ) ) {
235
- foreach ( $ value as $ k => $ v ) {
236
- $ outputs [$ key ][$ k ] = sanitize_text_field ( $ v );
237
- }
238
- } else {
239
- $ outputs [$ key ] = sanitize_text_field ( $ value );
240
- }
241
-
195
+ $ outputs [$ key ] = sanitize_text_field ( $ value );
242
196
}
243
197
244
198
return apply_filters ( 'cfim_validate_inputs ' , $ outputs , $ inputs );
245
199
246
200
} // end validate_inputs
247
201
}
248
202
249
- Custom_Featured_Image_Metabox_Settings::get_instance ();
250
- ?>
203
+ Custom_Featured_Image_Metabox_Settings::get_instance ();
0 commit comments