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 0.2.2 0.2.3
cf7-conditional-fields / admin.php
cf7-conditional-fields Last commit date
js 9 years ago admin-style.css 9 years ago admin.php 9 years ago contact-form-7-conditional-fields.php 9 years ago readme.txt 9 years ago screenshot-1.png 10 years ago screenshot-2.png 10 years ago style.css 10 years ago
admin.php
206 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 wp_enqueue_style( 'contact-form-7-cf-admin', wpcf7cf_plugin_url( 'admin-style.css' ), array(), WPCF7CF_VERSION, 'all' );
10 wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array(), WPCF7CF_VERSION,true);
11 }
12
13 add_filter('wpcf7_editor_panels', 'add_conditional_panel');
14
15 function add_conditional_panel($panels) {
16 $panels['contitional-panel'] = array(
17 'title' => __( 'Conditional fields', 'wpcf7cf' ),
18 'callback' => 'wpcf7cf_editor_panel_conditional'
19 );
20 return $panels;
21 }
22
23 function all_field_options($post, $selected = '-1') {
24 $all_fields = $post->form_scan_shortcode();
25 ?>
26 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option>
27 <?php
28 foreach ($all_fields as $tag) {
29 if ($tag['type'] == 'group' || $tag['name'] == '') continue;
30 ?>
31 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
32 <?php
33 }
34 }
35
36 function all_group_options($post, $selected = '-1') {
37 $all_groups = $post->form_scan_shortcode(array('type'=>'group'));
38
39 ?>
40 <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option>
41 <?php
42 foreach ($all_groups as $tag) {
43 ?>
44 <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option>
45 <?php
46 }
47 }
48
49 function all_operator_options($selected = 'equals') {
50 $all_options = array('equals', 'not equals');
51 foreach($all_options as $option) {
52 ?>
53 <option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
54 <?php
55 }
56 }
57
58 function all_display_options($selected = 'show') {
59 $all_options = array('show', 'hide');
60 foreach($all_options as $option) {
61 ?>
62 <option value="<?php echo $option ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo $option ?></option>
63 <?php
64 }
65 }
66
67 function wpcf7cf_editor_panel_conditional($form) {
68
69 $form_id = $_GET['post'];
70 $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true);
71
72 if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array();
73
74
75 ?>
76 <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3>
77
78
79 <div id="wpcf7cf-new-entry">
80 if
81 <select name="wpcf7cf_options[{id}][if_field]" class="if-field-select"><?php all_field_options($form); ?></select>
82 <select name="wpcf7cf_options[{id}][operator]" class="operator"><?php all_operator_options(); ?></select>
83 <input name="wpcf7cf_options[{id}][if_value]" class="if-value" type="text" placeholder="value">
84 then
85 <select name="wpcf7cf_options[{id}][then_visibility]" class="then-visibility"><?php all_display_options() ?></select>
86 <select name="wpcf7cf_options[{id}][then_field]" class="then-field-select"><?php all_group_options($form); ?></select>
87 </div>
88 <a id="wpcf7cf-delete-button" class="delete-button" title="delete rule" href="#"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
89 <a id="wpcf7cf-add-button" title="add new rule" href="#"><span class="dashicons dashicons-plus-alt"></span> add new conditional rule</a>
90
91 <div id="wpcf7cf-entries">
92 <?php
93 $i = 0;
94 foreach($wpcf7cf_entries as $id => $entry) {
95 ?>
96 <div class="entry" id="entry-<?php echo $i ?>">
97 if
98 <select name="wpcf7cf_options[<?php echo $i ?>][if_field]" class="if-field-select"><?php all_field_options($form, $entry['if_field']); ?></select>
99 <select name="wpcf7cf_options[<?php echo $i ?>][operator]" class="operator"><?php all_operator_options($entry['operator']) ?></select>
100 <input name="wpcf7cf_options[<?php echo $i ?>][if_value]" class="if-value" type="text" placeholder="value" value="<?php echo $entry['if_value'] ?>">
101 then
102 <select name="wpcf7cf_options[<?php echo $i ?>][then_visibility]" class="then-visibility"><?php all_display_options($entry['then_visibility']) ?></select>
103 <select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php all_group_options($form, $entry['then_field']); ?></select>
104 <a style="display: inline-block;" href="#" title="delete rule" class="delete-button"><span class="dashicons dashicons-dismiss"></span> Remove rule</a>
105 </div>
106 <?php
107 $i++;
108 }
109 ?>
110 </div>
111
112
113 <div id="wpcf7cf-text-entries">
114 <p><a href="#" id="wpcf7cf-settings-to-text">Export settings</a></p>
115 <div id="wpcf7cf-settings-text-wrap">
116 <textarea id="wpcf7cf-settings-text"></textarea>
117 <p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p>
118 </div>
119 </div>
120
121 <script>
122 (function($) {
123 var index = $('#wpcf7cf-entries .entry').length;
124
125 $('.delete-button').click(function(){
126
127 //if (confirm('You sure?')===false) return false;
128 $(this).parent().remove();
129 return false;
130
131 });
132
133 $('#wpcf7cf-add-button').click(function(){
134
135 // if ($('#wpcf7cf-new-entry .if-field-select').val() == '-1' || $('#wpcf7cf-new-entry .then-field-select').val() == '-1') {
136 // alert('Please select 2 fields');
137 // $(this).parent().remove();
138 // return false;
139 // } else if($('#wpcf7cf-new-entry .if-field-select').val() == $('#wpcf7cf-new-entry .then-field-select').val()) {
140 // alert('The fields cannot be the same');
141 // $(this).parent().remove();
142 // return false;
143 // }
144
145 var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id');
146 $('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button);
147 $delete_button.click(function(){
148
149 //if (confirm('You sure?')===false) return false;
150 $(this).parent().remove();
151 return false;
152
153 });
154 index++;
155 return false;
156
157 });
158
159 // settings to
160
161 $('#wpcf7cf-settings-text-wrap').hide();
162
163 $('#wpcf7cf-settings-to-text').click(function() {
164 $('#wpcf7cf-settings-text-wrap').show();
165
166 $('#wpcf7cf-settings-text').val('');
167 $('#wpcf7cf-entries .entry').each(function() {
168 var $entry = $(this);
169 var line = 'if [' + $entry.find('.if-field-select').val() + ']'
170 + ' ' + $entry.find('.operator').val()
171 + ' "' + $entry.find('.if-value').val() + '" then '
172 + $entry.find('.then-visibility').val()
173 + ' [' + $entry.find('.then-field-select').val() + ']';
174 $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select();
175 });
176 return false;
177 });
178
179 $('#wpcf7cf-settings-text-clear').click(function() {
180 $('#wpcf7cf-settings-text-wrap').hide();
181 $('#wpcf7cf-settings-text').val('');
182 return false;
183 });
184
185 })( jQuery );
186 </script>
187 <?php
188 }
189
190 // define the wpcf7_save_contact_form callback
191 function wpcf7cf_save_contact_form( $contact_form )
192 {
193 if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) )
194 return;
195 $post_id = $contact_form->id();
196 if ( ! $post_id )
197 return;
198
199 unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry
200
201 update_post_meta( $post_id, 'wpcf7cf_options', $_POST['wpcf7cf_options'] );
202
203 };
204
205 // add the action
206 add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 );