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
9 years ago
admin.php
240 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->scan_form_tags(); |
| 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->scan_form_tags(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 wpcf7cf_editor_panel_conditional($form) { |
| 59 | |
| 60 | $form_id = $_GET['post']; |
| 61 | $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true); |
| 62 | |
| 63 | if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array(); |
| 64 | |
| 65 | |
| 66 | ?> |
| 67 | <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3> |
| 68 | |
| 69 | |
| 70 | <div id="wpcf7cf-new-entry"> |
| 71 | if |
| 72 | <select name="wpcf7cf_options[{id}][if_field]" class="if-field-select"><?php all_field_options($form); ?></select> |
| 73 | <select name="wpcf7cf_options[{id}][operator]" class="operator"><?php all_operator_options(); ?></select> |
| 74 | <input name="wpcf7cf_options[{id}][if_value]" class="if-value" type="text" placeholder="value"> |
| 75 | then show |
| 76 | <select name="wpcf7cf_options[{id}][then_field]" class="then-field-select"><?php all_group_options($form); ?></select> |
| 77 | </div> |
| 78 | <a id="wpcf7cf-delete-button" class="delete-button" title="delete rule" href="#"><span class="dashicons dashicons-dismiss"></span> Remove rule</a> |
| 79 | <a id="wpcf7cf-add-button" title="add new rule" href="#"><span class="dashicons dashicons-plus-alt"></span> add new conditional rule</a> |
| 80 | |
| 81 | <div id="wpcf7cf-entries"> |
| 82 | <?php |
| 83 | $i = 0; |
| 84 | foreach($wpcf7cf_entries as $id => $entry) { |
| 85 | ?> |
| 86 | <div class="entry" id="entry-<?php echo $i ?>"> |
| 87 | if |
| 88 | <select name="wpcf7cf_options[<?php echo $i ?>][if_field]" class="if-field-select"><?php all_field_options($form, $entry['if_field']); ?></select> |
| 89 | <select name="wpcf7cf_options[<?php echo $i ?>][operator]" class="operator"><?php all_operator_options($entry['operator']) ?></select> |
| 90 | <input name="wpcf7cf_options[<?php echo $i ?>][if_value]" class="if-value" type="text" placeholder="value" value="<?php echo $entry['if_value'] ?>"> |
| 91 | then show |
| 92 | <select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php all_group_options($form, $entry['then_field']); ?></select> |
| 93 | <a style="display: inline-block;" href="#" title="delete rule" class="delete-button"><span class="dashicons dashicons-dismiss"></span> Remove rule</a> |
| 94 | </div> |
| 95 | <?php |
| 96 | $i++; |
| 97 | } |
| 98 | ?> |
| 99 | </div> |
| 100 | |
| 101 | |
| 102 | <div id="wpcf7cf-text-entries"> |
| 103 | <p><a href="#" id="wpcf7cf-settings-to-text">import/export</a></p> |
| 104 | <div id="wpcf7cf-settings-text-wrap"> |
| 105 | <textarea id="wpcf7cf-settings-text"></textarea> |
| 106 | <br> |
| 107 | Import actions (Beta feature!): |
| 108 | <input type="button" value="Add conditions" id="add-fields" > |
| 109 | <input type="button" value="Overwrite conditions" id="overwrite-fields" > |
| 110 | <span style="color:red"><b>WARNING</b>: If you screw something up, just reload the page without saving. If you click <em>save</em> after screwing up, you're screwed.</span> |
| 111 | |
| 112 | <p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p> |
| 113 | |
| 114 | </div> |
| 115 | </div> |
| 116 | |
| 117 | |
| 118 | <script> |
| 119 | (function($) { |
| 120 | |
| 121 | var index = $('#wpcf7cf-entries .entry').length; |
| 122 | |
| 123 | $('.delete-button').click(function(){ |
| 124 | |
| 125 | //if (confirm('You sure?')===false) return false; |
| 126 | $(this).parent().remove(); |
| 127 | return false; |
| 128 | |
| 129 | }); |
| 130 | |
| 131 | $('#wpcf7cf-add-button').click(function(){ |
| 132 | |
| 133 | var id = add_condition_fields(); |
| 134 | |
| 135 | return false; |
| 136 | |
| 137 | }); |
| 138 | |
| 139 | function clear_all_condition_fields() { |
| 140 | $('.entry').remove(); |
| 141 | } |
| 142 | |
| 143 | function add_condition_fields() { |
| 144 | var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id'); |
| 145 | $('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button); |
| 146 | $delete_button.click(function(){ |
| 147 | $(this).parent().remove(); |
| 148 | return false; |
| 149 | }); |
| 150 | index++; |
| 151 | |
| 152 | return (index-1); |
| 153 | } |
| 154 | |
| 155 | function import_condition_fields() { |
| 156 | var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/); |
| 157 | console.log(lines); |
| 158 | for (var i = lines.length+1; i>-1; i--) { |
| 159 | |
| 160 | var str = lines[i]; |
| 161 | |
| 162 | var match = regex.exec(str); |
| 163 | |
| 164 | if (match == null) continue; |
| 165 | |
| 166 | console.log(match[1]+' '+match[2]+' '+match[3]+' '+match[4]); |
| 167 | |
| 168 | var id = add_condition_fields(); |
| 169 | |
| 170 | $('#entry-'+id+' .if-field-select').val(match[1]); |
| 171 | $('#entry-'+id+' .operator').val(match[2]); |
| 172 | $('#entry-'+id+' .if-value').val(match[3]); |
| 173 | $('#entry-'+id+' .then-field-select').val(match[4]); |
| 174 | |
| 175 | regex.lastIndex = 0; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // export/import settings |
| 180 | |
| 181 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 182 | |
| 183 | $('#wpcf7cf-settings-to-text').click(function() { |
| 184 | $('#wpcf7cf-settings-text-wrap').show(); |
| 185 | |
| 186 | $('#wpcf7cf-settings-text').val(''); |
| 187 | $('#wpcf7cf-entries .entry').each(function() { |
| 188 | var $entry = $(this); |
| 189 | var line = 'if [' + $entry.find('.if-field-select').val() + ']' |
| 190 | + ' ' + $entry.find('.operator').val() |
| 191 | + ' "' + $entry.find('.if-value').val() + '" then show' |
| 192 | + ' [' + $entry.find('.then-field-select').val() + ']'; |
| 193 | $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select(); |
| 194 | }); |
| 195 | return false; |
| 196 | }); |
| 197 | |
| 198 | var regex = /if \[(.*)] (.*equals) "(.*)" then show \[(.*)]/g; |
| 199 | |
| 200 | $('#add-fields').click(function() { |
| 201 | import_condition_fields(); |
| 202 | }); |
| 203 | |
| 204 | $('#overwrite-fields').click(function() { |
| 205 | clear_all_condition_fields(); |
| 206 | import_condition_fields(); |
| 207 | }); |
| 208 | |
| 209 | $('#wpcf7cf-settings-text-clear').click(function() { |
| 210 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 211 | $('#wpcf7cf-settings-text').val(''); |
| 212 | return false; |
| 213 | }); |
| 214 | |
| 215 | })( jQuery ); |
| 216 | </script> |
| 217 | <?php |
| 218 | } |
| 219 | |
| 220 | // define the wpcf7_save_contact_form callback |
| 221 | function wpcf7cf_save_contact_form( $contact_form ) |
| 222 | { |
| 223 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) ) |
| 224 | return; |
| 225 | $post_id = $contact_form->id(); |
| 226 | if ( ! $post_id ) |
| 227 | return; |
| 228 | |
| 229 | unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry |
| 230 | |
| 231 | $options = array_values($_POST['wpcf7cf_options']); |
| 232 | |
| 233 | update_post_meta( $post_id, 'wpcf7cf_options', $options ); |
| 234 | |
| 235 | return; |
| 236 | |
| 237 | }; |
| 238 | |
| 239 | // add the action |
| 240 | add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 ); |