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