forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-option-radio-palette.php
166 lines (145 loc) · 4.36 KB
/
class-option-radio-palette.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionRadioPalette extends TitanFrameworkOption {
public $defaultSecondarySettings = array(
'options' => array(),
);
function __construct( $settings, $owner ) {
parent::__construct( $settings, $owner );
add_action( 'tf_livepreview_pre_' . $this->getOptionNamespace(), array( $this, 'preLivePreview' ), 10, 3 );
}
/*
* Display for options and meta
*/
public function display() {
if ( empty( $this->settings['options'] ) ) {
return;
}
if ( $this->settings['options'] == array() ) {
return;
}
$this->echoOptionHeader();
// Get the correct value, since we are accepting indices in the default setting
$value = $this->getValue();
if ( $value == '' ) {
$value = 0;
}
if ( ! is_array( $value ) ) {
$value = $this->settings['options'][$value];
}
// print the palettes
foreach ( $this->settings['options'] as $key => $colorSet ) {
printf( '<label id="%s"><input id="%s" type="radio" name="%s" value="%s" %s/> <span>',
$this->getID() . $key,
$this->getID() . $key,
$this->getID(),
esc_attr( $key ),
$value == $colorSet ? 'checked="checked"' : '' // can't use checked with arrays
);
if ( ! is_array( $colorSet ) ) {
continue;
}
foreach ( $colorSet as $color ) {
echo "<span style='background: {$color}'></span>";
}
echo "</span></label>";
}
$this->echoOptionFooter();
}
// Save the index of the selected palette
public function cleanValueForSaving( $value ) {
if ( ! is_array( $this->settings['options'] ) ) {
return $value;
}
// if the key above is zero, we will get a blank value
if ( $value === '' ) {
$value = 0;
}
return $value;
}
// The value we should return is an array of the selected colors
public function cleanValueForGetting( $value ) {
if ( is_array( $value ) ) {
return $value;
}
$value = stripslashes( $value );
if ( is_serialized( $value ) ) {
return unserialize( $value );
}
if ( empty( $value ) ) {
$value = 0;
}
if ( array_key_exists( $value, $this->settings['options'] ) ) {
return $this->settings['options'][$value];
}
return $value;
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionRadioPaletteControl( $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,
) ) );
}
// For live previews, we need to give our options to javascript and then get the proper value from it
public function preLivePreview( $optionID, $optionType, $option ) {
if ( $optionID != $this->settings['id'] || empty( $this->settings['options'] ) ) {
return;
}
if ( $this->settings['options'] == array() ) {
return;
}
?>
var options = JSON.parse('<?php echo json_encode( $this->settings['options'] ) ?>'),
key = value;
value = options[value];
<?php
}
}
/*
* We create a new control for the theme customizer
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionRadioPaletteControl', 1 );
function registerTitanFrameworkOptionRadioPaletteControl() {
class TitanFrameworkOptionRadioPaletteControl extends WP_Customize_Control {
public $description;
public function render_content() {
// Get the correct value, we might get a blank if index / value is 0
$value = $this->value();
if ( $value == '' ) {
$value = 0;
}
?><span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span><?php
if ( ! empty( $this->description ) ) {
echo "<p class='description'>" . $this->description . "</p>";
}
// print the palettes
foreach ( $this->choices as $key => $colorSet ) {
if ( ! is_array( $colorSet ) ) {
continue;
}
?>
<span class='tf-radio-palette'>
<label>
<input type="radio" name="<?php echo $this->id ?>" value="<?php echo esc_attr( $key ) ?>" <?php $this->link(); checked( $value, $key ); ?>/>
<span>
<?php
foreach ( $colorSet as $color ) {
echo "<span style='background: {$color}'></span>";
}
?>
</span>
</label>
</span>
<?php
}
}
}
}