admin-functions.php
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Renders the field select HTML for admin pages
*
* @access public
* @return mixed HTML field
*/
function wpf_render_tag_multiselect( $settings, $meta_name, $field_id = null, $field_sub_id = null, $disabled = false, $placeholder = false, $limit = null ) {
$available_tags = wp_fusion()->settings->get('available_tags');
// If no tags, set a blank array
if(!is_array($available_tags))
$available_tags = array();
if( is_array( reset($available_tags) ) ) {
// Handling for select with category groupings
$tag_categories = array();
foreach( $available_tags as $value ) {
$tag_categories[] = $value['category'];
}
$tag_categories = array_unique($tag_categories);
echo '<select' . ($disabled == true ? ' disabled' : '') . ' data-placeholder="' . (empty($placeholder) ? 'Select tags' : $placeholder) . '" multiple="multiple" data-limit="' . $limit .'" id="' . $field_id . (isset($field_sub_id) ? '-' . $field_sub_id : '') . '" class="select4-wpf-tags" name="' . $meta_name . (isset($field_id) ? '[' . $field_id . ']' : '') . (isset($field_sub_id) ? '[' . $field_sub_id . ']' : '') . '[]">';
foreach($tag_categories as $tag_category) {
echo '<optgroup label="' . $tag_category . '">';
foreach($available_tags as $id => $field_data) {
if($field_data['category'] == $tag_category) {
echo '<option value="'.esc_attr($id).'"'. (is_null($field_sub_id) ? selected(true, in_array($id, (array)$settings ), FALSE) : selected(true, in_array($id, (array)$settings [$field_sub_id]), FALSE)).'>'.$field_data['label'].'</option>';
}
}
echo '</optgroup>';
}
echo '</select>';
} else {
// Handling for single level select (no categories)
echo '<select ' . ($disabled == true ? ' disabled' : '') . ' data-placeholder="' . (empty($placeholder) ? 'Select tags' : $placeholder) . '" multiple="multiple" id="' . $field_id . (isset($field_sub_id) ? '-' . $field_sub_id : '') . '" data-limit="' . $limit .'" class="select4-wpf-tags" name="' . $meta_name . (isset($field_id) ? '[' . $field_id . ']' : '') . (isset($field_sub_id) ? '[' . $field_sub_id . ']' : '') . '[]">';
foreach( $available_tags as $id => $tag ) {
// Fix for bug with "0" appearing as an available tag
if($tag == "0")
continue;
echo '<option value="' . esc_attr($id) . '"'. (is_null($field_sub_id) ? selected(true, in_array($id, (array)$settings), FALSE) : selected(true, in_array($id, (array)$settings[$field_sub_id]), FALSE)).'>' . $tag . '</option>';
}
if(in_array('add_tags', wp_fusion()->crm->supports)) {
// Check to see if new custom tags have been added
foreach( (array)$settings as $i => $tag ) {
// For settings with sub-ids (like Woo variations)
if(is_array($tag)) {
foreach($tag as $sub_tag) {
if(!in_array($sub_tag, $available_tags) && $i == $field_sub_id ) {
echo '<option value="' . esc_attr($sub_tag) . '" selected="selected">' . $sub_tag . '</option>';
$available_tags[$sub_tag] = $sub_tag;
wp_fusion()->settings->set('available_tags', $available_tags);
}
}
} elseif(!in_array($tag, $available_tags)) {
echo '<option value="' . esc_attr($tag) . '" selected="selected">' . $tag . '</option>';
$available_tags[$tag] = $tag;
wp_fusion()->settings->set('available_tags', $available_tags);
}
}
}
echo '</select>';
}
}
/**
* Renders a dropdown with all custom fields for the current CRM
*
* @access public
* @return mixed HTML field
*/
function wpf_render_crm_field_select( $setting, $meta_name, $field_id, $field_sub_id = null ) {
echo '<select id="' . $field_id . (isset($field_sub_id) ? '-' . $field_sub_id : '') . '" class="select4-crm-field" name="' . $meta_name . '[' . $field_id . ']' . (isset($field_sub_id) ? '[' . $field_sub_id . ']' : '') . '[crm_field]" data-placeholder="Select a field">';
echo '<option></option>';
$crm_fields = wp_fusion()->settings->get('crm_fields');
if(!empty( $crm_fields )) {
foreach( $crm_fields as $group_header => $fields ) {
// For CRMs with separate custom and built in fields
if(is_array($fields)) {
echo '<optgroup label="' . $group_header . '">';
foreach($crm_fields[$group_header] as $field => $label) {
if(is_array($label))
$label = $label['label'];
echo '<option ' . selected(esc_attr( $setting ), $field ) . ' value="' . $field . '">' . $label . '</option>';
}
echo '</optgroup>';
} else {
$field = $group_header;
$label = $fields;
echo '<option ' . selected(esc_attr( $setting ), $field ) . ' value="' . $field . '">' . $label . '</option>';
}
}
}
// Save custom added fields to the DB
if(in_array('add_fields', wp_fusion()->crm->supports)) {
// Check to see if new custom fields have been added
if(!empty($setting) && !isset($crm_fields[$setting])) {
// Lowercase and remove spaces
$setting_safe = strtolower( str_replace(' ', '', $setting) );
echo '<option value="' . $setting_safe . '" selected="selected">' . $setting . '</option>';
$crm_fields[$setting_safe] = $setting;
wp_fusion()->settings->set('crm_fields', $crm_fields);
// Save safe crm field to DB
$contact_fields = wp_fusion()->settings->get('contact_fields');
$contact_fields[$field_sub_id]['crm_field'] = $setting_safe;
wp_fusion()->settings->set('contact_fields', $contact_fields);
}
}
echo '</select>';
}