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 0.2.2 0.2.3
cf7-conditional-fields / admin.php
cf7-conditional-fields Last commit date
js 6 years ago Wpcf7cfMailParser.php 6 years ago admin-style.css 6 years ago admin-style.css.map 6 years ago admin-style.scss 6 years ago admin.php 6 years ago cf7cf.php 6 years ago contact-form-7-conditional-fields.php 6 years ago init.php 6 years ago readme.txt 6 years ago style.css 6 years ago tg_pane_group.php 6 years ago wpcf7cf-options.php 6 years ago
admin.php
245 lines
1 <?php
2
3 add_action( 'admin_enqueue_scripts', 'wpcf7cf_admin_enqueue_scripts', 11 ); // set priority so scripts and styles get loaded later.
4
5 function wpcf7cf_admin_enqueue_scripts( $hook_suffix ) {
6 if ( false === strpos( $hook_suffix, 'wpcf7' ) ) {
7 return; //don't load styles and scripts if this isn't a CF7 page.
8 }
9
10 wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array('jquery-ui-autocomplete', 'jquery-ui-sortable'), WPCF7CF_VERSION,true);
11 wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_options_0', get_option(WPCF7CF_OPTIONS));
12
13 }
14
15 add_filter('wpcf7_editor_panels', 'add_conditional_panel');
16
17 function add_conditional_panel($panels) {
18 if ( current_user_can( 'wpcf7_edit_contact_form' ) ) {
19 $panels['wpcf7cf-conditional-panel'] = array(
20 'title' => __( 'Conditional fields', 'wpcf7cf' ),
21 'callback' => 'wpcf7cf_editor_panel_conditional'
22 );
23 }
24 return $panels;
25 }
26
27 function wpcf7cf_all_field_options($post, $selected = '-1') {
28 $all_fields = $post->scan_form_tags();
29 ?>
30 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option>
31 <?php
32 foreach ($all_fields as $tag) {
33 if ($tag['type'] == 'group' || $tag['name'] == '') continue;
34 ?>
35 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
36 <?php
37 }
38 }
39
40 function wpcf7cf_all_group_options($post, $selected = '-1') {
41 $all_groups = $post->scan_form_tags(array('type'=>'group'));
42
43 ?>
44 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option>
45 <?php
46 foreach ($all_groups as $tag) {
47 ?>
48 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
49 <?php
50 }
51 }
52
53 if (!function_exists('all_operator_options')) {
54 function all_operator_options($selected = 'equals') {
55 $all_options = array('equals', 'not equals');
56 $all_options = apply_filters('wpcf7cf_get_operators', $all_options);
57 foreach($all_options as $option) {
58 // backwards compat
59 $selected = $selected == '' ? 'less than or equals' : $selected;
60 $selected = $selected == '' ? 'greater than or equals' : $selected;
61 $selected = $selected == '>' ? 'greater than' : $selected;
62 $selected = $selected == '<' ? 'less than' : $selected;
63
64 ?>
65 <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option>
66 <?php
67 }
68 }
69 }
70
71 function wpcf7cf_editor_panel_conditional($form) {
72
73 $form_id = $_GET['post'];
74
75 $wpcf7cf_entries = CF7CF::getConditions($form_id);
76
77 if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array();
78
79 $wpcf7cf_entries = array_values($wpcf7cf_entries);
80
81 ?>
82 <div class="wpcf7cf-inner-container">
83 <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3>
84
85 <?php
86 print_entries_html($form);
87 ?>
88
89 <div id="wpcf7cf-entries">
90 <?php
91 print_entries_html($form, $wpcf7cf_entries);
92 ?>
93 </div>
94
95 <span id="wpcf7cf-add-button" title="add new rule">+ add new conditional rule</span>
96
97 <div id="wpcf7cf-text-entries">
98 <p><a href="#" id="wpcf7cf-settings-to-text">Text view</a></p>
99 <div id="wpcf7cf-settings-text-wrap">
100 <textarea id="wpcf7cf-settings-text" name="wpcf7cf-settings-text"></textarea>
101 <br>
102 <input type="button" value="Update fields">
103 <p><a href="#" id="wpcf7cf-settings-text-clear">Hide</a></p>
104 </div>
105 </div>
106 </div>
107 <?php
108 }
109
110 // duplicate conditions on duplicate form part 1.
111 add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2);
112 function wpcf7cf_copy($new_form,$current_form) {
113
114 $id = $current_form->id();
115 $props = $new_form->get_properties();
116 $props['messages']['wpcf7cf_copied'] = $id;
117 $new_form->set_properties($props);
118
119 return $new_form;
120 }
121
122 // duplicate conditions on duplicate form part 2.
123 add_action('wpcf7_after_save','wpcf7cf_after_save',10,1);
124 function wpcf7cf_after_save($contact_form) {
125 $props = $contact_form->get_properties();
126 $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0;
127 if ($original_id !== 0) {
128 $post_id = $contact_form->id();
129 unset($props['messages']['wpcf7cf_copied']);
130 $contact_form->set_properties($props);
131 CF7CF::setConditions($post_id, CF7CF::getConditions($original_id));
132 return;
133 }
134 }
135
136 // wpcf7_save_contact_form callback
137 add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
138 function wpcf7cf_save_contact_form( $contact_form )
139 {
140
141 if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf-settings-text'] ) ) {
142 return;
143 }
144 $post_id = $contact_form->id();
145 if ( ! $post_id )
146 return;
147
148 // unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
149
150 // TODO: only save the one import/export field.
151 $conditions_string = stripslashes(sanitize_textarea_field($_POST['wpcf7cf-settings-text']));
152
153 $conditions = CF7CF::parse_conditions($conditions_string);
154
155 CF7CF::setConditions($post_id, $conditions);
156
157 return;
158
159 };
160
161 function wpcf7cf_sanitize_options($options) {
162 //$options = array_values($options);
163 $sanitized_options = [];
164 foreach ($options as $option_entry) {
165 $sanitized_option = [];
166 $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']);
167 foreach ($option_entry['and_rules'] as $and_rule) {
168 $sanitized_option['and_rules'][] = [
169 'if_field' => sanitize_text_field($and_rule['if_field']),
170 'operator' => $and_rule['operator'],
171 'if_value' => sanitize_text_field($and_rule['if_value']),
172 ];
173 }
174
175 $sanitized_options[] = $sanitized_option;
176 }
177 return $sanitized_options;
178 }
179
180 function print_entries_html($form, $wpcf7cf_entries = false) {
181
182 $is_dummy = !$wpcf7cf_entries;
183
184 if ($is_dummy) {
185 $wpcf7cf_entries = array(
186 '{id}' => array(
187 'then_field' => '-1',
188 'and_rules' => array(
189 0 => array(
190 'if_field' => '-1',
191 'operator' => 'equals',
192 'if_value' => ''
193 )
194 )
195 )
196 );
197 }
198
199 foreach($wpcf7cf_entries as $i => $entry) {
200
201 // check for backwards compatibility ( < 2.0 )
202 if (!key_exists('and_rules', $wpcf7cf_entries[$i]) || !is_array($wpcf7cf_entries[$i]['and_rules'])) {
203 $wpcf7cf_entries[$i]['and_rules'][0] = $wpcf7cf_entries[$i];
204 }
205
206 $and_entries = array_values($wpcf7cf_entries[$i]['and_rules']);
207
208 if ($is_dummy) {
209 ?>
210 <div id="wpcf7cf-new-entry">
211 <?php
212 } else {
213 ?>
214 <div class="entry" id="entry-<?php echo $i ?>">
215 <?php
216 }
217 ?>
218 <div class="wpcf7cf-if">
219 <span class="label">Show</span>
220 <select class="then-field-select"><?php wpcf7cf_all_group_options($form, $entry['then_field']); ?></select>
221 </div>
222 <div class="wpcf7cf-and-rules" data-next-index="<?php echo count($and_entries) ?>">
223 <?php
224
225
226
227 foreach($and_entries as $and_i => $and_entry) {
228 ?>
229 <div class="wpcf7cf-and-rule">
230 <span class="rule-part if-txt label">if</span>
231 <select class="rule-part if-field-select"><?php wpcf7cf_all_field_options( $form, $and_entry['if_field'] ); ?></select>
232 <select class="rule-part operator"><?php all_operator_options( $and_entry['operator'] ) ?></select>
233 <input class="rule-part if-value" type="text"
234 placeholder="value" value="<?php echo $and_entry['if_value'] ?>">
235 <span class="and-button">And</span>
236 <span title="delete rule" class="rule-part delete-button">remove</span>
237 </div>
238 <?php
239 }
240 ?>
241 </div>
242 </div>
243 <?php
244 }
245 }