-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin.php
134 lines (129 loc) · 5.05 KB
/
admin.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
<?php
/**
* Admin
*/
class WC_Category_Locker_Admin
{
/**
* Constructor
*/
public function __construct()
{
add_action('product_cat_add_form_fields', [$this, 'add_category_fields'], 25);
add_action('product_cat_edit_form_fields', [$this, 'edit_category_fields'], 25);
add_action('created_term', [$this, 'save_category_fields'], 10, 3);
add_action('edit_term', [$this, 'save_category_fields'], 10, 3);
}
/**
* Add category fields
*
* @since 1.0
* @return void
*/
public function add_category_fields()
{
?>
<div class="form-field">
<label id="wcl_cat_password_protected">
<input type="checkbox" name="wcl_cat_password_protected" value="1" />
<?php _e('Password Protected', WCL_PLUGIN_DOMAIN); ?>
</label>
<div id="wcl_cat_password" style="display:none; float: left;">
<label>
<?php _e('Password:', WCL_PLUGIN_DOMAIN); ?>
<input type="text" name="wcl_cat_password" value="" required="required" />
</label>
</div>
<script>
jQuery('#wcl_cat_password_protected').on('click', function() {
var $checked = jQuery('input[name="wcl_cat_password_protected"]:checkbox:checked').length > 0;
if($checked) {
jQuery('#wcl_cat_password').find('input').prop('disabled', false);
jQuery('#wcl_cat_password').slideDown();
} else {
jQuery('#wcl_cat_password').find('input').prop('disabled', true);
jQuery('#wcl_cat_password').slideUp();
}
});
</script>
<div class="clear"></div>
</div>
<?php
}
/**
* Edit category fields
*
* @param object $term
* @since 1.0
* @return void
*/
public function edit_category_fields($term)
{
$wcl_cat_password_protected = absint(get_woocommerce_term_meta($term->term_id, 'wcl_cat_password_protected', true));
$wcl_cat_password = get_woocommerce_term_meta($term->term_id, 'wcl_cat_password', true); ?>
<tr class="form-field">
<th scope="row" valign="top">
Password Protection
</th>
<td>
<label id="wcl_cat_password_protected">
<input type="checkbox" name="wcl_cat_password_protected" value="1"
<?php
if ($wcl_cat_password_protected) {
echo 'checked="checked"';
} ?> />
<?php _e('Password Protected', WCL_PLUGIN_DOMAIN); ?>
</label>
<div class="clear"></div>
<div id="wcl_cat_password" style="<?php
if (!$wcl_cat_password_protected) {
echo 'display:none;';
} ?> float: left;">
<label>
<?php _e('Password:', WCL_PLUGIN_DOMAIN); ?>
<input type="text" name="wcl_cat_password" value="<?php echo $wcl_cat_password; ?>" <?php
if (!$wcl_cat_password_protected) {
echo 'disabled="disabled"';
} ?> required="required" />
</label>
</div>
<script>
jQuery('#wcl_cat_password_protected').on('click', function() {
var $checked = jQuery('input[name="wcl_cat_password_protected"]:checkbox:checked').length > 0;
if($checked) {
jQuery('#wcl_cat_password').find('input').prop('disabled', false);
jQuery('#wcl_cat_password').slideDown();
} else {
jQuery('#wcl_cat_password').find('input').prop('disabled', true);
jQuery('#wcl_cat_password').slideUp();
}
});
</script>
<div class="clear"></div>
</td>
</tr>
<?php
}
/**
* Save category fields
*
* @param integer $term_id
* @param string $tt_id
* @param string $taxonomy
* @since 1.0
* @return void
*/
public function save_category_fields($term_id, $tt_id = '', $taxonomy = '')
{
if (isset($_POST['wcl_cat_password_protected']) && 'product_cat' === $taxonomy) {
update_woocommerce_term_meta($term_id, 'wcl_cat_password_protected', absint($_POST['wcl_cat_password_protected']));
} elseif ('product_cat' === $taxonomy) {
update_woocommerce_term_meta($term_id, 'wcl_cat_password_protected', 0);
}
if (isset($_POST['wcl_cat_password']) && 'product_cat' === $taxonomy) {
update_woocommerce_term_meta($term_id, 'wcl_cat_password', esc_attr($_POST['wcl_cat_password']));
}
}
}
// init
$WC_Category_Locker_Admin = new WC_Category_Locker_Admin();