forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-option-number.php
More file actions
221 lines (193 loc) · 5.45 KB
/
class-option-number.php
File metadata and controls
221 lines (193 loc) · 5.45 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
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
<?php
/**
* Number Option Class
*
* @author Benjamin Intal
* @package Titan Framework Core
**/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Number Option Class
*
* @since 1.0
**/
class TitanFrameworkOptionNumber extends TitanFrameworkOption {
// Default settings specific to this option
public $defaultSecondarySettings = array(
'size' => 'small', // or medium or large
'placeholder' => '', // show this when blank
'min' => 0,
'max' => 1000,
'step' => 1,
'default' => 0,
'unit' => ''
);
/**
* Constructor
*
* @since 1.4
*/
function __construct( $settings, $owner ) {
parent::__construct( $settings, $owner );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueSlider' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueueSlider' ) );
add_action( 'admin_head', array( __CLASS__, 'createSliderScript' ) );
}
/**
* Cleans up the serialized value before saving
*
* @param string $value The serialized value
* @return string The cleaned value
* @since 1.4
*/
public function cleanValueForSaving( $value ) {
if ( $value == '' ) {
return 0;
}
return $value;
}
/**
* Cleans the value for getOption
*
* @param string $value The raw value of the option
* @return mixes The cleaned value
* @since 1.4
*/
public function cleanValueForGetting( $value ) {
if ( $value == '' ) {
return 0;
}
return $value;
}
/**
* Enqueues the jQuery UI scripts
*
* @return void
* @since 1.4
*/
public function enqueueSlider() {
wp_enqueue_script( 'jquery-ui-core' );
wp_enqueue_script( 'jquery-ui-slider' );
}
/**
* Prints out the script the initializes the jQuery slider
*
* @return void
* @since 1.4
*/
public static function createSliderScript() {
?>
<script>
jQuery(document).ready(function($) {
"use strict";
$('.tf-number input[type=number]').each(function() {
if ( ! $(this).prev().is('.number-slider') ) {
return;
}
$(this).prev().slider({
max: Number( $(this).attr('max') ),
min: Number( $(this).attr('min') ),
step: Number( $(this).attr('step') ),
value: Number( $(this).val() ),
animate: "fast",
change: function( event, ui ) {
$(ui.handle).parent().next().val(ui.value).trigger('change');
},
slide: function( event, ui ) {
$(ui.handle).parent().next().val(ui.value).trigger('change');
}
}).disableSelection();
});
$('.tf-number input[type=number]').on('keyup', function() {
$(this).prev().slider('value', $(this).val());
});
});
</script>
<?php
}
/**
* Displays the option for admin pages and meta boxes
*
* @return void
* @since 1.0
*/
public function display() {
$this->echoOptionHeader();
echo "<div class='number-slider'></div>";
printf("<input class=\"%s-text\" name=\"%s\" placeholder=\"%s\" id=\"%s\" type=\"number\" value=\"%s\" min=\"%s\" max=\"%s\" step=\"%s\" /> %s <p class=\"description\">%s</p>",
$this->settings['size'],
$this->getID(),
$this->settings['placeholder'],
$this->getID(),
esc_attr( $this->getValue() ),
$this->settings['min'],
$this->settings['max'],
$this->settings['step'],
$this->settings['unit'],
$this->settings['desc']
);
$this->echoOptionFooter(false);
}
/**
* Registers the theme customizer control, for displaying the option
*
* @param WP_Customize $wp_enqueue_script The customize object
* @param TitanFrameworkCustomizerSection $section The section where this option will be placed
* @param int $priority The order of this control in the section
* @return void
* @since 1.0
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionNumberControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->getID(),
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'priority' => $priority,
'size' => $this->settings['size'],
'min' => $this->settings['min'],
'max' => $this->settings['max'],
'step' => $this->settings['step'],
'unit' => $this->settings['unit'],
) ) );
}
}
/*
* We create a new control for the theme customizer
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionNumberControl', 1 );
/**
* Creates the option for the theme customizer
*
* @return void
* @since 1.0
*/
function registerTitanFrameworkOptionNumberControl() {
class TitanFrameworkOptionNumberControl extends WP_Customize_Control {
public $description;
public $size;
public $min;
public $max;
public $step;
public $unit;
private static $firstLoad = true;
public function render_content() {
// Print out the jQuery slider initializer
if ( self::$firstLoad ) {
TitanFrameworkOptionNumber::createSliderScript();
}
self::$firstLoad = false;
?>
<label class='tf-number'>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<span class='number-slider'></span>
<input class="<?php echo esc_attr( $this->size ) ?>-text" min="<?php echo esc_attr( $this->min ) ?>" max="<?php echo esc_attr( $this->max ) ?>" step="<?php echo esc_attr( $this->step ) ?>" type="number" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
<?php echo esc_html( $this->unit ) ?>
</label>
<?php
if ( ! empty( $this->description ) ) {
echo "<p class='description'>{$this->description}</p>";
}
}
}
}