Repository navigation
Expand file tree
/
Copy pathclass-ezcustomizer-settings.php
More file actions
112 lines (98 loc) · 3.84 KB
/
Copy pathclass-ezcustomizer-settings.php
File metadata and controls
112 lines (98 loc) · 3.84 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
<?php
/**
* Main EZ_Customizer_Settings class by Rob Ruiz
*
* @since 1.0.0
*/
class EZ_Customizer_Settings {
public $args;
private $wp_customize, $i18n, $settings_id;
/**
* Sets up our class.
*
* @since 1.0.0
*
* @param array $args Associative array the auto-constructs all desired settings in the customizer. See ReadME.
*/
public function __construct($args)
{
$this->args = $args;
$this->settings_id = $args['settings_id'];
$this->i18n = $args['settings_id'];
if(isset($args['i18n'])){
$this->i18n = $args['i18n'];
}
$this->hooks();
}
private function hooks(){
add_action( 'customize_register', array($this, 'ez_customizer_settings_register') );
}
public function ez_customizer_settings_register($wp_customize){
$this->wp_customize = $wp_customize;
$this->register_sections();
$this->add_settings();
}
private function register_sections(){
foreach($this->args['sections'] as $section){
$section_id = $this->settings_id.'_'.$section['id'].'_section';
$section_args = $section;
$section_args['title'] = __( $section['title'], $this->i18n );
$this->wp_customize->add_section( $section_id, $section_args);
}
}
private function add_settings(){
foreach($this->args['settings'] as $setting){
$setting_args = $this->get_settings_args($setting);
$setting = $this->clean_setting_array($setting);
$this->wp_customize->add_setting( $setting['id'] , $setting_args );
if(!isset($setting['type'])){
$control = array(
'id'=> $this->args['theme_name'].'_theme_'.$setting['id'],
'label' => $setting['label'],
'section' => $args['section'] = $this->settings_id.'_'.$setting['section'].'_section'
);
$this->wp_customize->add_control($setting['id'], $control);
} else {
$this->wp_customize->add_control($this->get_type_control($setting));
}
}
}
private function get_settings_args($setting){
$setting_args = array();
if(isset($setting['refresh']) && !$setting['refresh']){
$setting_args['transport'] = 'postMessage';
unset($setting['refresh']);
}
if(isset($setting['default'])){
$setting_args['default'] = $setting['default'];
unset($setting['default']);
}
return $setting_args;
}
private function clean_setting_array($setting){
unset($setting['refresh']);
unset($setting['default']);
return $setting;
}
private function get_type_control($setting){
$control_id = $this->args['theme_name'].'_theme_'.$setting['id'];
/**
* Here we are basing our control args on the initial settings array
* Refer to https://developer.wordpress.org/reference/classes/wp_customize_control/ for acceptable args
*/
$args = $setting;
/* Altering values that we simplified for class instantiation where needed */
$args['label'] = __( $setting['label'], $this->i18n );
$args['section'] = $this->settings_id.'_'.$setting['section'].'_section';
$args['settings'] = $setting['id'];
switch($setting['type']){
case 'color':
return new WP_Customize_Color_Control( $this->wp_customize, $control_id, $args);
case 'date':
return new WP_Customize_Date_Time_Control($this->wp_customize, $control_id, $args);
case 'media':
return new WP_Customize_Media_Control($this->wp_customize,$control_id, $args);
}
return new WP_Customize_control($this->wp_customize,$control_id, $args);
}
}