-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter-theme-customizer.php
448 lines (391 loc) · 12.3 KB
/
better-theme-customizer.php
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Plugin Name: Better Theme Customizer
* Description: A plugin that extends default customizer functionality.
* Author: Sideways8 Interactive, LLC
* Author URI: http://sideways8.com/
* Version: 1.0.0
*/
namespace S8\BetterThemeCustomizer;
use S8\BetterThemeCustomizer\CustomizerControls\Media_Library_Control;
use S8\BetterThemeCustomizer\CustomizerControls\Dropdown_Posts_Control;
use S8\BetterThemeCustomizer\CustomizerControls\Dropdown_gForm_Control;
use S8\BetterThemeCustomizer\CustomizerControls\Dropdown_Taxonomies_Control;
use S8\BetterThemeCustomizer\CustomizerControls\Dropdown_Terms_Control;
use S8\BetterThemeCustomizer\CustomizerControls\Dropdown_Post_Types_Control;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
/**
* Class BetterThemeCustomizer
* @package S8\BetterThemeCustomizer
*/
class Better_Theme_Customizer {
const SITE_OPTION = 'btc_site_option';
/**
* This prefix is used on panels and sections, options are NEVER prefixed!
*
* @var string
*/
protected $prefix = 'btc_';
/**
* This is the unprocessed options array.
*
* @var array
*/
protected $raw_options = array();
/**
* Stores top level sections and panels.
*
* @var array
*/
protected $top_panels_sections = array();
/**
* Stores sub-level sections
*
* @var array
*/
protected $sub_sections = array();
/**
* Stores options (combination of settings and controls)
*
* @var array
*/
protected $options = array();
/**
* Allowed options for panels.
*
* @var array
*/
protected $allowed_panel_options = array(
'id' => 'id',
'title' => 'title',
'priority' => 'priority',
'capability' => 'capability',
'description' => 'description',
'theme_supports' => 'theme_supports',
'active_callback' => 'active_callback',
);
/**
* Allowed options for sections.
*
* @var array
*/
protected $allowed_section_options = array(
'id' => 'id',
'title' => 'title',
'priority' => 'priority',
'capability' => 'capability',
'description' => 'description',
'theme_supports' => 'theme_supports',
'active_callback' => 'active_callback',
'panel' => 'panel',
);
/**
* Allowed options for settings.
*
* @var array
*/
protected $allowed_setting_options = array(
'type' => 'storage_type',
'default' => 'default',
'transport' => 'transport',
'capability' => 'capability',
'theme_supports' => 'theme_supports',
'sanitize_callback' => 'sanitize_callback',
'sanitize_js_callback' => 'sanitize_js_callback',
);
/**
* Allowed options for controls.
*
* @var array
*/
protected $allowed_control_options = array(
'type' => 'type',
'label' => 'label',
'choices' => 'choices',
'section' => 'section',
'settings' => 'settings',
'priority' => 'priority',
'description' => 'description',
);
/**
* BetterThemeCustomizer constructor.
*/
public function __construct() {
require_once __DIR__ . '/vendor/autoload.php';
add_action( 'init', array( $this, 'init' ) );
add_action( 'customize_register', array( $this, 'customize_register' ) );
if ( is_multisite() ) {
add_action( 'customize_update_' . self::SITE_OPTION, array( $this, 'save_multi_site_setting' ), 10, 2 );
}
}
/**
* Finds the template where options are stored.
*/
public function init() {
$this->get_custom_theme_options();
require_once( __DIR__ . '/helpers/class-customizer-helpers.php' );
require_once( __DIR__ . '/helper-functions.php' );
}
/**
* Fires on the customize_register action, registers all our options
*
* @param \WP_Customize_Manager $wp_customize
*/
public function customize_register( $wp_customize ) {
require_once __DIR__ . '/customizer-controls/class-media-library-control.php';
require_once __DIR__ . '/customizer-controls/class-dropdown-posts-control.php';
require_once __DIR__ . '/customizer-controls/class-dropdown-terms-control.php';
require_once __DIR__ . '/customizer-controls/class-dropdown-taxonomies-control.php';
require_once __DIR__ . '/customizer-controls/class-dropdown-post-types-control.php';
require_once __DIR__ . '/customizer-controls/class-dropdown-gform-control.php';
// Get and setup options
$this->raw_options = apply_filters( 'btc_options_register', array() );
$this->process_raw_options( $this->raw_options );
// Register our options
$this->register_top_level( $wp_customize );
$this->register_sub_sections( $wp_customize );
$this->register_settings_controls( $wp_customize );
}
/**
* Saves a multi-site theme option as a site option.
*
* @param mixed $value
* @param mixed $wp_customize_setting
*
* @return bool
*/
public function save_multi_site_setting( $value, $wp_customize_setting ) {
return update_site_option( $wp_customize_setting->id, $value );
}
/**
* Include custom theme options.
*/
protected function get_custom_theme_options() {
$theme_dir = get_template_directory();
/**
* Top down priority
*/
$check_yaml = [
$theme_dir . '/includes/btc-options.yml',
$theme_dir . '/inc/btc-options.yml',
$theme_dir . '/btc-options.yml',
__DIR__ . '/options/btc-options.yml', // always load last
];
foreach ( $check_yaml as $file ) {
if ( file_exists( $file ) ) {
add_filter( 'btc_options_register', function ( $settings ) use ( $file ) {
try {
$yaml = Yaml::parse( file_get_contents( $file ) );
} catch ( ParseException $e ) {
echo $e->getMessage();
}
return array_merge( $settings, $yaml );
}, 1 );
return;
}
}
}
/**
* Process our options
*/
protected function process_raw_options( $raw_options ) {
if ( ! empty( $raw_options ) && is_array( $raw_options ) ) {
foreach ( $raw_options as $item_id => $settings ) {
if ( isset( $settings['_panel'] ) || isset( $settings['_section'] ) ) {
if ( isset( $settings['_panel'] ) ) {
$panel_settings = $this->extract_args_to_array( $settings['_panel'],
$this->allowed_panel_options );
$panel_settings['is_panel'] = true;
$this->top_panels_sections[ $this->prefix . $item_id ] = $panel_settings;
unset( $settings['_panel'] );
$this->process_raw_panel( $settings, $this->prefix . $item_id );
} else {
$section_settings = $this->extract_args_to_array( $settings['_section'],
$this->allowed_section_options );
$section_settings['is_panel'] = false;
$this->_top_panels_sections[ $this->prefix . $item_id ] = $section_settings;
unset( $settings['_section'] );
$this->process_raw_section( $settings, $this->prefix . $item_id );
}
} else {
$this->process_raw_section( $settings, false );
}
}
}
}
/**
* @param array $options
* @param string $panel_id
*/
protected function process_raw_panel( $options, $panel_id ) {
foreach ( $options as $item_id => $settings ) {
if ( isset( $settings['_section'] ) ) {
$section_settings = $this->extract_args_to_array( $settings['_section'],
$this->allowed_section_options );
if ( ! isset( $section_settings['panel'] ) ) {
$section_settings['panel'] = $panel_id;
}
$this->sub_sections[ $this->prefix . $item_id ] = $section_settings;
unset( $settings['_section'] );
$this->process_raw_section( $settings, $this->prefix . $item_id );
} else {
$this->process_raw_section( $settings, false );
}
}
}
/**
* @param array $options
* @param string|bool $section_id
*/
protected function process_raw_section( $options, $section_id ) {
foreach ( $options as $item_id => $settings ) {
if ( false === $section_id ) {
$settings['priority'] = false;
} else {
$settings['section'] = $section_id;
}
$this->options[ $item_id ] = $settings;
}
}
/**
* @param \WP_Customize_Manager $wp_customize
*/
protected function register_top_level( $wp_customize ) {
$priority = 0;
foreach ( $this->top_panels_sections as $id => $settings ) {
if ( ! isset( $settings['priority'] ) ) {
$priority ++;
$settings['priority'] = $priority;
}
if ( $settings['is_panel'] ) {
$wp_customize->add_panel( $id, $settings );
} else {
$wp_customize->add_section( $id, $settings );
}
}
}
/**
* @param \WP_Customize_Manager $wp_customize
*/
protected function register_sub_sections( $wp_customize ) {
$priority = 0;
foreach ( $this->sub_sections as $id => $settings ) {
if ( ! isset( $settings['priority'] ) ) {
$priority ++;
$settings['priority'] = $priority;
}
$wp_customize->add_section( $id, $settings );
}
}
/**
* @param \WP_Customize_Manager $wp_customize
*/
protected function register_settings_controls( $wp_customize ) {
foreach ( $this->options as $setting_id => $args ) {
$setting_args = $this->extract_args_to_array( $args, $this->allowed_setting_options );
// Setup code to allow retrieving site option on multi-site (if used)
if ( isset( $setting_args['type'] ) && self::SITE_OPTION == $setting_args['type'] ) {
if ( is_multisite() ) {
add_filter( "customize_value_{$setting_id}", function ( $default ) use ( $setting_id ) {
return get_site_option( $setting_id, $default );
} );
} else {
// Not multi-site! Use alternate saving method!
$setting_args['type'] = 'option';
}
}
$wp_customize->add_setting( $setting_id, $setting_args );
$control_args = $this->extract_args_to_array( $args, $this->allowed_control_options );
if ( ! isset( $control_args['type'] ) ) {
$control_args['type'] = 'text';
}
switch ( $control_args['type'] ) {
case 'image':
case 'media-library':
$wp_customize->add_control( new Media_Library_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'dropdown-gform':
$wp_customize->add_control( new Dropdown_gForm_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'dropdown-taxonomies':
$wp_customize->add_control( new Dropdown_Taxonomies_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'dropdown-post-types':
$wp_customize->add_control( new Dropdown_Post_Types_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'dropdown-terms':
$control_args = array_merge(
$control_args,
$this->extract_args_to_array( $args, array(
'taxonomy' => 'taxonomy',
) ) );
$wp_customize->add_control( new Dropdown_Terms_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'dropdown-posts':
$control_args = array_merge(
$control_args,
$this->extract_args_to_array( $args, array(
'post_type' => 'post_type',
) ) );
$wp_customize->add_control( new Dropdown_Posts_Control( $wp_customize, $setting_id,
$control_args ) );
break;
/* WP Included classes below this line */
case 'color-picker':
$wp_customize->add_control( new \WP_Customize_Color_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'image-control':
$control_args = array_merge(
$control_args,
$this->extract_args_to_array( $args, array(
'mime_type' => 'mime_type',
) ) );
$wp_customize->add_control( new \WP_Customize_Image_Control( $wp_customize, $setting_id,
$control_args ) );
break;
case 'upload-control':
$control_args = array_merge(
$control_args,
$this->extract_args_to_array( $args, array(
'mime_type' => 'mime_type',
) ) );
$wp_customize->add_control( new \WP_Customize_Upload_Control( $wp_customize, $setting_id,
$control_args ) );
break;
default:
$wp_customize->add_control( $setting_id, $control_args );
}
}
}
/**
* Extracts the args listed in $extract from the data in the $args array
*
* @param array $args
* @param array $extract
*
* @return array
*/
protected function extract_args_to_array( $args, $extract ) {
$extracted_args = array();
if ( ! empty( $extract ) && ! empty( $args ) ) {
foreach ( (array) $extract as $key => $args_key ) {
if ( isset( $args[ $args_key ] ) ) {
$extracted_args[ $key ] = $args[ $args_key ];
}
}
}
return $extracted_args;
}
}
new Better_Theme_Customizer();