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.2 0.2.3
cf7-conditional-fields / admin.php
cf7-conditional-fields Last commit date
js 4 years ago jsdoc-out 4 years ago Wpcf7cfMailParser.php 5 years ago admin-style.css 6 years ago admin-style.css.map 6 years ago admin-style.scss 6 years ago admin.php 4 years ago cf7cf.php 4 years ago contact-form-7-conditional-fields.php 4 years ago init.php 4 years ago readme.txt 4 years ago style.css 6 years ago tg_pane_group.php 5 years ago wpcf7cf-options.php 4 years ago
admin.php
326 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
7 wp_enqueue_script('cf7cf-scripts-admin-all-pages', wpcf7cf_plugin_url( 'js/scripts_admin_all_pages.js' ),array( 'jquery' ), WPCF7CF_VERSION,true);
8
9
10 if ( false === strpos( $hook_suffix, 'wpcf7' ) ) {
11 return; //don't load styles and scripts if this isn't a CF7 page.
12 }
13
14 wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array('jquery-ui-autocomplete', 'jquery-ui-sortable'), WPCF7CF_VERSION,true);
15 wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_options_0', wpcf7cf_get_settings());
16
17 }
18
19 add_filter('wpcf7_editor_panels', 'add_conditional_panel');
20
21 function add_conditional_panel($panels) {
22 if ( current_user_can( 'wpcf7_edit_contact_form' ) ) {
23 $panels['wpcf7cf-conditional-panel'] = array(
24 'title' => __( 'Conditional fields', 'cf7-conditional-fields' ),
25 'callback' => 'wpcf7cf_editor_panel_conditional'
26 );
27 }
28 return $panels;
29 }
30
31 function wpcf7cf_all_field_options($post, $selected = '-1') {
32 $all_fields = $post->scan_form_tags();
33 ?>
34 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select field --', 'cf7-conditional-fields' ); ?></option>
35 <?php
36 foreach ($all_fields as $tag) {
37 if ($tag['type'] == 'group' || $tag['name'] == '') continue;
38 ?>
39 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
40 <?php
41 }
42 }
43
44 function wpcf7cf_all_group_options($post, $selected = '-1') {
45 $all_groups = $post->scan_form_tags(array('type'=>'group'));
46
47 ?>
48 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select group --', 'cf7-conditional-fields' ); ?></option>
49 <?php
50 foreach ($all_groups as $tag) {
51 ?>
52 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
53 <?php
54 }
55 }
56
57 if (!function_exists('all_operator_options')) {
58 function all_operator_options($selected = 'equals') {
59 $all_options = array('equals', 'not equals');
60 $all_options = apply_filters('wpcf7cf_get_operators', $all_options);
61 foreach($all_options as $option) {
62 // backwards compat
63 $selected = $selected == '' ? 'less than or equals' : $selected;
64 $selected = $selected == '' ? 'greater than or equals' : $selected;
65 $selected = $selected == '>' ? 'greater than' : $selected;
66 $selected = $selected == '<' ? 'less than' : $selected;
67
68 ?>
69 <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option>
70 <?php
71 }
72 }
73 }
74
75 function wpcf7cf_editor_panel_conditional($form) {
76
77 $settings = wpcf7cf_get_settings();
78 $is_text_only = $settings['conditions_ui'] === 'text_only';
79
80 // print_r($settings);
81
82 $form_id = isset($_GET['post']) ? $_GET['post'] : false;
83
84 if ($form_id === false) {
85 ?>
86 <div class="wpcf7cf-inner-container">
87 <h2><?php _e( 'Conditional fields', 'cf7-conditional-fields' ); ?></h2>
88 <p><?php _e( 'You need to save your form, before you can start adding conditions.', 'cf7-conditional-fields' ); ?></p>
89 </div>
90 <?php
91 return;
92 }
93
94 $wpcf7cf_entries = CF7CF::getConditions($form_id);
95
96 $wpcf7cf_entries = array_values($wpcf7cf_entries);
97
98 ?>
99 <div class="wpcf7cf-inner-container">
100
101 <label class="wpcf7cf-switch" id="wpcf7cf-text-only-switch">
102 <span class="label"><?php _e( 'Text mode', 'cf7-conditional-fields' ); ?></span>
103 <span class="switch">
104 <input type="checkbox" id="wpcf7cf-text-only-checkbox" name="wpcf7cf-text-only-checkbox" value="text_only" <?php echo $is_text_only ? 'checked':''; ?>>
105 <span class="slider round"></span>
106 </span>
107 </label>
108
109 <h2><?php _e( 'Conditional fields', 'cf7-conditional-fields' ); ?></h2>
110
111 <div id="wpcf7cf-entries-ui" style="display:none">
112 <?php
113 print_entries_html($form);
114 ?>
115 <div id="wpcf7cf-entries">
116 <?php
117 //print_entries_html($form, $wpcf7cf_entries);
118 ?>
119 </div>
120
121 <span id="wpcf7cf-add-button" title="<?php _e( 'add new rule', 'cf7-conditional-fields' ); ?>"><?php _e( '+ add new conditional rule', 'cf7-conditional-fields'); ?></span>
122
123 <div id="wpcf7cf-a-lot-of-conditions" class="wpcf7cf-notice notice-warning" style="display:none;">
124 <p>
125 <strong><?php _e( 'Wow, That\'s a lot of conditions!', 'cf7-conditional-fields' ); ?></strong><br>
126 <?php
127 // translators: 1. max recommended conditions
128 echo sprintf( __( 'You can only add up to %d conditions using this interface.', 'cf7-conditional-fields' ), WPCF7CF_MAX_RECOMMENDED_CONDITIONS ) . ' ';
129 // translators: 1,2: strong tags, 3. max recommended conditions
130 printf( __( 'Please switch to %1$sText mode%2$s if you want to add more than %3$d conditions.', 'cf7-conditional-fields' ), '<strong>', '</strong>', WPCF7CF_MAX_RECOMMENDED_CONDITIONS ); ?>
131 </p>
132 </div>
133
134 </div>
135
136 <div id="wpcf7cf-text-entries">
137 <div id="wpcf7cf-settings-text-wrap">
138
139 <textarea id="wpcf7cf-settings-text" name="wpcf7cf-settings-text"><?php echo CF7CF::serializeConditions($wpcf7cf_entries) ?></textarea>
140 <br>
141 </div>
142 </div>
143 </div>
144 <?php
145 }
146
147 // duplicate conditions on duplicate form part 1.
148 add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2);
149 function wpcf7cf_copy($new_form,$current_form) {
150
151 $id = $current_form->id();
152 $props = $new_form->get_properties();
153 $props['messages']['wpcf7cf_copied'] = $id;
154 $new_form->set_properties($props);
155
156 return $new_form;
157 }
158
159 // duplicate conditions on duplicate form part 2.
160 add_action('wpcf7_after_save','wpcf7cf_after_save',10,1);
161 function wpcf7cf_after_save($contact_form) {
162 $props = $contact_form->get_properties();
163 $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0;
164 if ($original_id !== 0) {
165 $post_id = $contact_form->id();
166 unset($props['messages']['wpcf7cf_copied']);
167 $contact_form->set_properties($props);
168 CF7CF::setConditions($post_id, CF7CF::getConditions($original_id));
169 return;
170 }
171 }
172
173 // wpcf7_save_contact_form callback
174 add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );
175 function wpcf7cf_save_contact_form( $contact_form )
176 {
177
178 if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf-settings-text'] ) ) {
179 return;
180 }
181 $post_id = $contact_form->id();
182 if ( ! $post_id ) {
183 return;
184 }
185
186
187 // we intentionally don't use sanitize_textarea_field here,
188 // because basically any character is a valid character.
189 // To arm agains SQL injections and other funky junky, the CF7CF::parse_conditions function is used.
190 $conditions_string = stripslashes($_POST['wpcf7cf-settings-text']);
191 $conditions = CF7CF::parse_conditions($conditions_string);
192
193 CF7CF::setConditions($post_id, $conditions);
194
195 if (isset($_POST['wpcf7cf-summary-template'])) {
196 WPCF7CF_Summary::saveSummaryTemplate($_POST['wpcf7cf-summary-template'],$post_id);
197 }
198
199 return;
200
201 };
202
203 function wpcf7cf_sanitize_options($options) {
204 //$options = array_values($options);
205 $sanitized_options = [];
206 foreach ($options as $option_entry) {
207 $sanitized_option = [];
208 $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']);
209 foreach ($option_entry['and_rules'] as $and_rule) {
210 $sanitized_option['and_rules'][] = [
211 'if_field' => sanitize_text_field($and_rule['if_field']),
212 'operator' => $and_rule['operator'],
213 'if_value' => sanitize_text_field($and_rule['if_value']),
214 ];
215 }
216
217 $sanitized_options[] = $sanitized_option;
218 }
219 return $sanitized_options;
220 }
221
222 function print_entries_html($form, $wpcf7cf_entries = false) {
223
224 $is_dummy = !$wpcf7cf_entries;
225
226 if ($is_dummy) {
227 $wpcf7cf_entries = array(
228 '{id}' => array(
229 'then_field' => '-1',
230 'and_rules' => array(
231 0 => array(
232 'if_field' => '-1',
233 'operator' => 'equals',
234 'if_value' => ''
235 )
236 )
237 )
238 );
239 }
240
241 foreach($wpcf7cf_entries as $i => $entry) {
242
243 // check for backwards compatibility ( < 2.0 )
244 if (!key_exists('and_rules', $wpcf7cf_entries[$i]) || !is_array($wpcf7cf_entries[$i]['and_rules'])) {
245 $wpcf7cf_entries[$i]['and_rules'][0] = $wpcf7cf_entries[$i];
246 }
247
248 $and_entries = array_values($wpcf7cf_entries[$i]['and_rules']);
249
250 if ($is_dummy) {
251 echo '<div id="wpcf7cf-new-entry">';
252 } else {
253 echo '<div class="entry">';
254 }
255 ?>
256 <div class="wpcf7cf-if">
257 <span class="label"><?php _e( 'Show', 'cf7-conditional-fields' ); ?></span>
258 <select class="then-field-select"><?php wpcf7cf_all_group_options($form, $entry['then_field']); ?></select>
259 </div>
260 <div class="wpcf7cf-and-rules" data-next-index="<?php echo count($and_entries) ?>">
261 <?php
262
263
264
265 foreach($and_entries as $and_i => $and_entry) {
266 ?>
267 <div class="wpcf7cf-and-rule">
268 <span class="rule-part if-txt label"><?php _e( 'if', 'cf7-conditional-fields' ); ?></span>
269 <select class="rule-part if-field-select"><?php wpcf7cf_all_field_options( $form, $and_entry['if_field'] ); ?></select>
270 <select class="rule-part operator"><?php all_operator_options( $and_entry['operator'] ) ?></select>
271 <input class="rule-part if-value" type="text" placeholder="<?php _e( 'value', 'cf7-conditional-fields' ); ?>" value="<?php echo $and_entry['if_value'] ?>">
272 <span class="and-button"><?php _e( 'And', 'cf7-conditional-fields' ); ?></span>
273 <span title="<?php _e( 'delete rule', 'cf7-conditional-fields' ); ?>" class="rule-part delete-button"><?php _e( 'remove', 'cf7-conditional-fields' ); ?></span>
274 </div>
275 <?php
276 }
277 ?>
278 </div>
279 <?php
280 echo '</div>';
281 }
282 }
283
284 add_action('admin_notices', function () {
285
286 $settings = wpcf7cf_get_settings();
287
288 $nid = 'install-cf7';
289 if (!defined('WPCF7_VERSION') && empty($settings['notice_dismissed_'.$nid])) {
290 ?>
291 <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>">
292 <p>
293 <strong>Conditional Fields for Contact Form 7</strong> depends on Contact Form 7. Please install <a target="_blank" href="https://downloads.wordpress.org/plugin/contact-form-7.<?php echo WPCF7CF_CF7_MAX_VERSION ?>.zip">Contact Form 7</a>.
294 </p>
295 </div>
296 <?php
297 return;
298 }
299
300 $nid = 'rollback-cf7-'.WPCF7CF_CF7_MAX_VERSION;
301 if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '<' ) && empty($settings['notice_dismissed_'.$nid]) && current_user_can('update_plugins') ) {
302 ?>
303 <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>">
304 <p>
305 <strong>Conditional Fields for Contact Form 7</strong> is not yet tested with your current version of Contact Form 7.
306 <br>If you notice any problems with your forms, please roll back to Contact Form 7 <strong>version <?php echo WPCF7CF_CF7_MAX_VERSION ?></strong>.
307 <br>For a quick and safe rollback, we recommend <a href="https://wordpress.org/plugins/wp-rollback/" target="_blank">WP Rollback</a>.
308 </p>
309 </div>
310 <?php
311 }
312
313 $nid = 'update-cf7-'.WPCF7CF_CF7_MAX_VERSION;
314 if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '>' ) && empty($settings['notice_dismissed_'.$nid]) && current_user_can('update_plugins') ) {
315 ?>
316 <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>">
317 <p>
318 <strong>Conditional Fields for Contact Form 7</strong> is fully compatible and tested with Contact Form 7 version <?php echo WPCF7CF_CF7_MAX_VERSION ?>.
319 <br>Compatibility with other versions of CF7 is not guaranteed, so please install <a target="_blank" href="https://downloads.wordpress.org/plugin/contact-form-7.<?php echo WPCF7CF_CF7_MAX_VERSION ?>.zip">CF7 version <?php echo WPCF7CF_CF7_MAX_VERSION ?></a>
320 </p>
321 </div>
322 <?php
323 }
324
325 });
326