forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-option-enable.php
135 lines (114 loc) · 4.12 KB
/
class-option-enable.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionEnable extends TitanFrameworkOption {
private static $firstLoad = true;
public $defaultSecondarySettings = array(
'enabled' => '',
'disabled' => '',
);
/*
* Display for options and meta
*/
public function display() {
$this->echoOptionHeader();
if ( empty( $this->settings['enabled'] ) ) {
$this->settings['enabled'] = __( 'Enabled', TF_I18NDOMAIN );
}
if ( empty( $this->settings['disabled'] ) ) {
$this->settings['disabled'] = __( 'Disabled', TF_I18NDOMAIN );
}
?>
<input name="<?php echo $this->getID() ?>" type="checkbox" id="<?php echo $this->getID() ?>" value="1" <?php checked( $this->getValue(), 1 ) ?>>
<span class="button button-<?php echo checked( $this->getValue(), 1, false ) ? 'primary' : 'secondary' ?>"><?php echo $this->settings['enabled'] ?></span><span class="button button-<?php echo checked( $this->getValue(), 1, false ) ? 'secondary' : 'primary' ?>"><?php echo $this->settings['disabled'] ?></span>
<?php
// load the javascript to init the colorpicker
if ( self::$firstLoad ):
?>
<script>
jQuery(document).ready(function($) {
"use strict";
$('body').on('click', '.tf-enable .button-secondary', function() {
$(this).parent().find('.button').toggleClass('button-primary button-secondary');
var checkBox = $(this).parents('.tf-enable').find('input');
if ( checkBox.is(':checked') ) {
checkBox.removeAttr('checked');
} else {
checkBox.attr('checked', 'checked');
}
checkBox.trigger('change');
});
});
</script>
<?php
endif;
$this->echoOptionFooter();
self::$firstLoad = false;
}
public function cleanValueForSaving( $value ) {
return $value != '1' ? '0' : '1';
}
public function cleanValueForGetting( $value ) {
return $value == '1' ? true : false;
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionEnableControl( $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,
) ) );
}
}
/*
* We create a new control for the theme customizer
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionEnableControl', 1 );
function registerTitanFrameworkOptionEnableControl() {
class TitanFrameworkOptionEnableControl extends WP_Customize_Control {
public $description;
public $options;
private static $firstLoad = true;
public function render_content() {
if ( empty( $this->options['enabled'] ) ) {
$this->options['enabled'] = __( 'Enabled', TF_I18NDOMAIN );
}
if ( empty( $this->options['disabled'] ) ) {
$this->options['disabled'] = __( 'Disabled', TF_I18NDOMAIN );
}
?>
<div class='tf-enable'>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<input type="checkbox" value="1" <?php $this->link(); ?>>
<span class="button button-<?php echo checked( $this->value(), 1, false ) ? 'primary' : 'secondary' ?>"><?php echo $this->options['enabled'] ?></span><span class="button button-<?php echo checked( $this->value(), 1, false ) ? 'secondary' : 'primary' ?>"><?php echo $this->options['disabled'] ?></span>
</div>
<?php
echo "<p class='description'>{$this->description}</p>";
// load the javascript to init the colorpicker
if ( self::$firstLoad ):
?>
<script>
jQuery(document).ready(function($) {
"use strict";
$('body').on('click', '.tf-enable .button-secondary', function() {
$(this).parent().find('.button').toggleClass('button-primary button-secondary');
var checkBox = $(this).parents('.tf-enable').find('input');
if ( checkBox.is(':checked') ) {
checkBox.removeAttr('checked');
} else {
checkBox.attr('checked', 'checked');
}
checkBox.trigger('change');
});
});
</script>
<?php
endif;
self::$firstLoad = false;
}
}
}