forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-option-sortable.php
330 lines (282 loc) · 7.72 KB
/
class-option-sortable.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Sortable Option Class
*
* @author Benjamin Intal
* @package Titan Framework Core
* @since 1.4
**/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Code Option Class
*
* @since 1.4
**/
class TitanFrameworkOptionSortable extends TitanFrameworkOption {
// Default settings specific to this option
public $defaultSecondarySettings = array(
'options' => array(),
'visible_button' => true,
);
private static $firstLoad = true;
/**
* Constructor
*
* @since 1.4
*/
function __construct( $settings, $owner ) {
parent::__construct( $settings, $owner );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueSortable' ) );
add_action( 'admin_head', array( __CLASS__, 'createSortableScript' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueueSortable' ) );
}
/**
* Enqueues the jQuery UI scripts
*
* @return void
* @since 1.4
*/
public function enqueueSortable() {
wp_enqueue_script( 'jquery-ui-core' );
wp_enqueue_script( 'jquery-ui-sortable' );
}
/**
* Creates the javascript needed for sortable to run
*
* @return void
* @since 1.4
*/
public static function createSortableScript() {
if ( ! self::$firstLoad ) {
return;
}
self::$firstLoad = false;
?>
<script>
jQuery(document).ready(function($) {
"use strict";
// initialize
$('.tf-sortable > ul ~ input').each(function() {
var value = $(this).val();
try {
value = unserialize( value );
} catch (err) {
return;
}
var ul = $(this).siblings('ul:eq(0)');
ul.find('li').addClass('invisible').find('i.visibility').toggleClass('dashicons-visibility-faint');
$.each(value, function(i, val) {
ul.find('li[data-value=' + val + ']').removeClass('invisible').find('i.visibility').toggleClass('dashicons-visibility-faint');
});
});
$('.tf-sortable > ul').each(function() {
$(this).sortable()
.disableSelection()
.on( "sortstop", function( event, ui ) {
tfUpdateSortable(ui.item.parent());
})
.find('li').each(function() {
$(this).find('i.visibility').click(function() {
$(this).toggleClass('dashicons-visibility-faint').parents('li:eq(0)').toggleClass('invisible');
});
})
.click(function() {
tfUpdateSortable( $(this).parents('ul:eq(0)') );
})
});
});
function tfUpdateSortable(ul) {
"use strict";
var $ = jQuery;
var values = [];
ul.find('li').each(function() {
if ( ! $(this).is('.invisible') ) {
values.push( $(this).attr('data-value') );
}
});
ul.siblings('input').eq(0).val( serialize( values ) ).trigger('change');
}
</script>
<?php
}
/**
* Displays the option in admin panels and meta boxes
*
* @return void
* @since 1.4
*/
public function display() {
if ( ! is_array( $this->settings['options'] ) ) {
return;
}
if ( ! count( $this->settings['options'] ) ) {
return;
}
$this->echoOptionHeader( true );
$values = $this->getValue();
if ( $values == '' ) {
$values = array_keys( $this->settings['options'] );
}
if ( is_serialized( $values ) ) {
$values = unserialize( $values );
}
if ( count( $values ) != count( $this->settings['options'] ) ) {
$this->settings['visible_button'] = true;
}
$visibleButton = '';
if ( $this->settings['visible_button'] === true ) {
$visibleButton = "<i class='dashicons dashicons-visibility visibility'></i>";
}
?>
<ul>
<?php
foreach ( $values as $dummy => $value ) {
printf( "<li data-value='%s'><i class='dashicons dashicons-menu'></i>%s%s</li>",
esc_attr( $value ),
$visibleButton,
$this->settings['options'][ $value ]
);
}
$invisibleKeys = array_diff( array_keys( $this->settings['options'] ), $values );
foreach ( $invisibleKeys as $dummy => $value ) {
printf( "<li data-value='%s'><i class='dashicons dashicons-menu'></i>%s%s</li>",
esc_attr( $value ),
$visibleButton,
$this->settings['options'][ $value ]
);
}
?>
</ul>
<div class='clear: both'></div>
<?php
if ( ! is_serialized( $values ) ) {
$values = serialize( $values );
}
printf( "<input type='hidden' name=\"%s\" id=\"%s\" value=\"%s\" />",
$this->getID(),
$this->getID(),
esc_attr( $values )
);
$this->echoOptionFooter( false );
}
/**
* 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 ) {
return stripslashes( $value );
}
/**
* Cleans the raw value for getting
*
* @param string $value The raw value
* @return string The cleaned value
* @since 1.4
*/
public function cleanValueForGetting( $value ) {
if ( is_array( $value ) ) {
return $value;
}
if ( is_serialized( stripslashes( $value ) ) ) {
return unserialize( $value );
}
return $value;
}
/**
* 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.4
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionSortableControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->settings['id'],
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'priority' => $priority,
'options' => $this->settings['options'],
'visible_button' => $this->settings['visible_button'],
) ) );
}
}
/*
* We create a new control for the theme customizer
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionSortableControl', 1 );
/**
* Creates the option for the theme customizer
*
* @return void
* @since 1.4
*/
function registerTitanFrameworkOptionSortableControl() {
class TitanFrameworkOptionSortableControl extends WP_Customize_Control {
public $description;
public $options;
public $visible_button;
public function render_content() {
TitanFrameworkOptionSortable::createSortableScript();
if ( ! is_array( $this->options ) ) {
return;
}
if ( ! count( $this->options ) ) {
return;
}
?>
<label class='tf-sortable'>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<?php
$values = $this->value();
if ( $values == '' ) {
$values = array_keys( $this->options );
}
if ( is_serialized( $values ) ) {
$values = unserialize( $values );
}
if ( count( $values ) != count( $this->options ) ) {
$this->visible_button = true;
}
$visibleButton = '';
if ( $this->visible_button === true ) {
$visibleButton = "<i class='dashicons dashicons-visibility visibility'></i>";
}
?>
<ul>
<?php
foreach ( $values as $dummy => $value ) {
printf( "<li data-value='%s'><i class='dashicons dashicons-menu'></i>%s%s</li>",
esc_attr( $value ),
$visibleButton,
$this->options[ $value ]
);
}
$invisibleKeys = array_diff( array_keys( $this->options ), $values );
foreach ( $invisibleKeys as $dummy => $value ) {
printf( "<li data-value='%s'><i class='dashicons dashicons-menu'></i>%s%s</li>",
esc_attr( $value ),
$visibleButton,
$this->options[ $value ]
);
}
?>
</ul>
<div class='clear: both'></div>
<?php
if ( ! is_serialized( $values ) ) {
$values = serialize( $values );
}
?>
<input type='hidden' <?php $this->link(); ?> value='<?php echo esc_attr( $values ) ?>'/>
</label>
<?php
echo "<p class='description'>{$this->description}</p>";
}
}
}