forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-option-select.php
113 lines (97 loc) · 2.89 KB
/
class-option-select.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionSelect extends TitanFrameworkOption {
public $defaultSecondarySettings = array(
'options' => array(),
);
/*
* Display for options and meta
*/
public function display() {
$this->echoOptionHeader();
?><select name="<?php echo $this->getID(); ?>"><?php
foreach ( $this->settings['options'] as $value => $label ) {
// this is if we have option groupings
if ( is_array( $label ) ) {
?><optgroup label="<?php echo $value ?>"><?php
foreach ( $label as $subValue => $subLabel ) {
printf("<option value=\"%s\" %s>%s</option>",
$subValue,
selected( $this->getValue(), $subValue, false ),
$subLabel
);
}
?></optgroup><?php
// this is for normal list of options
} else {
printf("<option value=\"%s\" %s>%s</option>",
$value,
selected( $this->getValue(), $value, false ),
$label
);
}
}
?></select><?php
$this->echoOptionFooter();
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$isAssociativeArray = false;
if ( count( $this->settings['options'] ) ) {
foreach ( $this->settings['options'] as $value => $label ) {
$isAssociativeArray = is_array( $label );
break;
}
}
// Not associative array, do normal control
if ( ! $isAssociativeArray ) {
$class = "TitanFrameworkCustomizeControl";
// Associative array, custom make the control
} else {
$class = "TitanFrameworkOptionSelectControl";
}
$wp_customize->add_control( new $class( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->settings['id'],
'type' => 'select',
'choices' => $this->settings['options'],
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'priority' => $priority,
) ) );
}
}
/*
* We create a new control for the theme customizer (for the grouped options only)
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionSelectControl', 1 );
function registerTitanFrameworkOptionSelectControl() {
class TitanFrameworkOptionSelectControl extends WP_Customize_Control {
public $description;
public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
foreach ( $this->choices as $value => $label ):
?><optgroup label="<?php echo $value ?>"><?php
foreach ( $label as $subValue => $subLabel ) {
printf("<option value=\"%s\" %s>%s</option>",
esc_attr( $subValue ),
selected( $this->value(), $subValue, false ),
$subLabel
);
}
?></optgroup><?php
endforeach;
?>
</select>
</label>
<?php
echo "<p class='description'>{$this->description}</p>";
}
}
}