forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-option-multicheck.php
161 lines (138 loc) · 4.07 KB
/
class-option-multicheck.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionMulticheck extends TitanFrameworkOption {
public $defaultSecondarySettings = array(
'options' => array(),
);
/*
* Display for options and meta
*/
public function display() {
$this->echoOptionHeader( true );
echo "<fieldset>";
$savedValue = $this->cleanValueForGetting( $this->getValue() );
if ( empty( $savedValue ) ) {
$savedValue = array();
}
foreach ( $this->settings['options'] as $value => $label ) {
printf('<label for="%s"><input id="%s" type="checkbox" name="%s[]" value="%s" %s/> %s</label><br>',
$this->getID() . $value,
$this->getID() . $value,
$this->getID(),
esc_attr( $value ),
checked( in_array( $value, $savedValue ), true, false ),
$label
);
}
echo "</fieldset>";
$this->echoOptionFooter( false );
}
public function cleanValueForSaving( $value ) {
if ( empty( $value ) ) {
return array();
}
if ( is_serialized( $value ) ) {
return $value;
}
// CSV
if ( is_string( $value ) ) {
$value = explode( ',', $value );
}
return serialize( $value );
}
public function cleanValueForGetting( $value ) {
if ( empty( $value ) ) {
return array();
}
if ( is_array( $value ) ) {
return $value;
}
if ( is_serialized( $value ) ) {
return unserialize( $value );
}
if ( is_string( $value ) ) {
return explode( ',', $value );
}
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionMulticheckControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->settings['id'],
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'options' => $this->settings['options'],
'priority' => $priority,
) ) );
}
}
/*
* WP_Customize_Control with description
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionMulticheckControl', 1 );
function registerTitanFrameworkOptionMulticheckControl() {
class TitanFrameworkOptionMulticheckControl extends WP_Customize_Control {
public $description;
public $options;
private static $firstLoad = true;
// Since theme_mod cannot handle multichecks, we will do it with some JS
public function render_content() {
// the saved value is an array. convert it to csv
if ( is_array( $this->value() ) ) {
$savedValueCSV = implode( ',', $this->value() );
$values = $this->value();
} else {
$savedValueCSV = $this->value();
$values = explode( ',', $this->value() );
}
if ( self::$firstLoad ) {
self::$firstLoad = false;
?>
<script>
jQuery(document).ready(function($) {
"use strict";
$('input.tf-multicheck').change(function(event) {
event.preventDefault();
var csv = '';
$(this).parents('li:eq(0)').find('input[type=checkbox]').each(function() {
if ($(this).is(':checked')) {
csv = $(this).attr('value') ',';
}
});
csv = csv.replace(/, $/, "");
$(this).parents('li:eq(0)').find('input[type=hidden]').val(csv)
// we need to trigger the field afterwards to enable the save button
.trigger('change');
return true;
});
});
</script>
<?php
}
$description = '';
if ( ! empty( $this->description ) ) {
$description = "<p class='description'>" . $this->description . "</p>";
}
?>
<label class='tf-multicheck-container'>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<?php
echo $description;
foreach ( $this->options as $value => $label ) {
printf('<label for="%s"><input class="tf-multicheck" id="%s" type="checkbox" value="%s" %s/> %s</label><br>',
$this->id . $value,
$this->id . $value,
esc_attr( $value ),
checked( in_array( $value, $values ), true, false ),
$label
);
}
?>
<input type="hidden" value="<?php echo esc_attr( $savedValueCSV ); ?>" <?php $this->link(); ?> />
</label>
<?php
}
}
}