-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-theme-customize.php
More file actions
executable file
·353 lines (256 loc) · 10.1 KB
/
class-theme-customize.php
File metadata and controls
executable file
·353 lines (256 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* Contains methods for customizing the theme customization screen.
*
* @link http://codex.wordpress.org/Theme_Customization_API
* @since MyTheme 1.0
*/
class customSection{
public $custom_section_name;
public $custom_section_args;
public $custom_section_fields;
/**
* __construct sets everything in motion.
*
* @return $this
* @author Michael Chacon
**/
function __construct( $name, $args = array())
{
$this->custom_section_fields = array();
$this->setSectionArgs($args)
->setSectionID($name)
->setSectionDefaultArgs()
->customize_register(array(&$this, "addSection"));
}
/**
* setSectionArgs - sets the section argument variable to whatever the user passed in
*
* @return $this
* @author Michael Chacon
**/
function setSectionArgs($args){
$this->custom_section_args = (array)$args;
return $this;
}
/**
* setSectionDefaultArgs - sets any values the user left out of the section args with defaults
*
* @return $this
* @author Michael Chacon
**/
function setSectionDefaultArgs(){
$defaults = array(
'title' => __( 'MyTheme Options', $name), //Visible title of section
'priority' => 35, //Determines what order this appears in
'default' => TRUE,
'capability' => 'edit_theme_options', //Capability needed to tweak
'description' => __('Allows you to customize some example settings for MyTheme.', $name ), //Descriptive tooltip
);
$args = array_merge($this->custom_section_args, $defaults);
return $this;
}
/**
* setSectionID - this sets the unique ID for each section
*
* @return $this
* @author Michael Chacon
**/
function setSectionID($name){
$this->custom_section_name = $name;
$this->custom_section_name = strtolower($custom_section_name);
return $this;
}
/**
* addSection - this sets the unique ID for each section
*
* @return $this
* @author Michael Chacon
**/
function addSection($wp_customize){
$wp_customize->add_section( $this->custom_section_name, $this->custom_section_args );
$this->addFields($wp_customize);
}
/**
* registerField
*
* @return $this
* @author Michael Chacon
**/
function registerField($name, $options = array()){
$field = array($name => $options);
$this->custom_section_fields = array_merge($this->custom_section_fields, $field);
}
/**
* addFields
*
* @return $this
* @author Michael Chacon
**/
function addFields($wp_customize){
foreach($this->custom_section_fields as $name => $options){
//add_settings
$wp_customize->add_setting( $name . '[' . $name . ']' , //Give it a SERIALIZED name (so all theme settings can live under one db record)
array(
'default' => $options['default'],
'type' => 'option', //Is this an 'option' or a 'theme_mod'?
'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
'transport' => 'postMessage', //What triggers a refresh of the setting? 'refresh' or 'postMessage' (instant)?
));
$this->setControl($wp_customize);
}//foreach
}//addFields
/**
* setControl
*
* @return $this
* @author Michael Chacon
**/
function setControl($wp_customize){
foreach($this->custom_section_fields as $name => $options){
switch ($options['type']) {
case 'colorpicker':
$wp_customize->add_control( new WP_Customize_Color_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
$name . '[' . $name . ']', //Set a unique ID for the control
array(
'label' => __( $name, 'mytheme' ), //Admin-visible name of the control
'section' => $this->custom_section_name, //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => $name . '[' . $name . ']', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
break;
case 'upload':
$wp_customize->add_control( new WP_Customize_Upload_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
$name . '[' . $name . ']', //Set a unique ID for the control
array(
'label' => __( $name , 'mytheme' ), //Admin-visible name of the control
'section' => $this->custom_section_name, //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => $name . '[' . $name . ']', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
break;
case 'image':
$wp_customize->add_control( new WP_Customize_Image_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
$name . '[' . $name . ']', //Set a unique ID for the control
array(
'label' => __($name, 'mytheme' ), //Admin-visible name of the control
'section' => $this->custom_section_name, //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => $name . '[' . $name . ']', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
break;
case 'background_image':
$wp_customize->add_control( new WP_Customize_Background_Image_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
$name . '[' . $name . ']', //Set a unique ID for the control
array(
'label' => __( $name, 'mytheme' ), //Admin-visible name of the control
'section' => $this->custom_section_name, //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => $name . '[' . $name . ']', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
break;
case 'header_image':
$wp_customize->add_control( new WP_Customize_Header_Image_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
$name . '[' . $name . ']', //Set a unique ID for the control
array(
'label' => __( $name, 'mytheme' ), //Admin-visible name of the control
'section' => $this->custom_section_name, //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => $name . '[' . $name . ']', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
break;
default:
$wp_customize->add_control( $name . '[' . $name . ']' ,
array(
'label' => $name,
'type' => $options['type'],
'section' => $this->custom_section_name,
'choices' => $options['choices']
));
break;
}
}//foreach
}//setControl
function getFieldType($wp_customize){
foreach($this->custom_section_fields as $name => $options){
}//foreach
return $type;
}
/**
* Helper method, that attaches a passed function to the 'init' WP action
* @param function $cb Passed callback function.
*/
function customize_register($cb)
{
add_action("customize_register", $cb);
}
}
/*Example Use
$exampleSection = new customSection('example',
array(
'title' => __( 'Contact Information', 'mytheme' ), //Visible title of section
'priority' => 35, //Determines what order this appears in
'capability' => 'edit_theme_options', //Capability needed to tweak
'description' => __('Allows you to customize some example settings for MyTheme.', 'mytheme'), //Descriptive tooltip
));
$exampleSection->registerField('Upload',
array(
'type'=>'upload',
'default'=>'Good2!',
'choices' => array('12' => '1','21' => '21','1' => '221'))
);
$exampleSection->registerField('colorpicker',
array(
'type'=>'colorpicker',
'default'=>'Good!')
);
$exampleSection->registerField('Image',
array(
'type'=>'image',
'default'=>'Good!')
);
$exampleSection->registerField('backgroundimage',
array(
'type'=>'background_image',
'default'=>'Good!')
);
$exampleSection->registerField('headerimage',
array(
'type'=>'header_image',
'default'=>'Good!')
);
$exampleSection->registerField('radio',
array(
'type'=>'radio',
'default'=>'Good!',
'choices' => array('12' => '1','21' => '21','1' => '221'))
);
$exampleSection->registerField('select',
array(
'type'=>'select',
'default'=>'Good!',
'choices' => array('12' => '1','21' => '21','1' => '221'))
);
$exampleSection->registerField('checkbox',
array(
'type'=>'checkbox',
'default'=>'Good!',
'choices' => array('12' => '1','21' => '21','1' => '221'))
);
$exampleSection->registerField('dropdown-pages',
array(
'type'=>'dropdown-pages',
'default'=>'Good!',
'choices' => array('12' => '1','21' => '21','1' => '221'))
);
*/