cf7-conditional-fields
Last commit date
js
9 years ago
admin-style.css
9 years ago
admin.php
9 years ago
cf7cf.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
142 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 | if (!function_exists('all_operator_options')) { |
| 50 | function all_operator_options($selected = 'equals') { |
| 51 | $all_options = array('equals', 'not equals'); |
| 52 | $all_options = apply_filters('wpcf7cf_get_operators', $all_options); |
| 53 | foreach($all_options as $option) { |
| 54 | ?> |
| 55 | <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option> |
| 56 | <?php |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | function wpcf7cf_editor_panel_conditional($form) { |
| 62 | |
| 63 | $form_id = $_GET['post']; |
| 64 | $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true); |
| 65 | |
| 66 | if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array(); |
| 67 | |
| 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]" class="operator"><?php all_operator_options(); ?></select> |
| 77 | <input name="wpcf7cf_options[{id}][if_value]" class="if-value" type="text" placeholder="value"> |
| 78 | then show |
| 79 | <select name="wpcf7cf_options[{id}][then_field]" class="then-field-select"><?php all_group_options($form); ?></select> |
| 80 | </div> |
| 81 | <a id="wpcf7cf-delete-button" class="delete-button" title="delete rule" href="#"><span class="dashicons dashicons-dismiss"></span> Remove rule</a> |
| 82 | <a id="wpcf7cf-add-button" title="add new rule" href="#"><span class="dashicons dashicons-plus-alt"></span> add new conditional rule</a> |
| 83 | |
| 84 | <div id="wpcf7cf-entries"> |
| 85 | <?php |
| 86 | $i = 0; |
| 87 | foreach($wpcf7cf_entries as $id => $entry) { |
| 88 | ?> |
| 89 | <div class="entry" id="entry-<?php echo $i ?>"> |
| 90 | if |
| 91 | <select name="wpcf7cf_options[<?php echo $i ?>][if_field]" class="if-field-select"><?php all_field_options($form, $entry['if_field']); ?></select> |
| 92 | <select name="wpcf7cf_options[<?php echo $i ?>][operator]" class="operator"><?php all_operator_options($entry['operator']) ?></select> |
| 93 | <input name="wpcf7cf_options[<?php echo $i ?>][if_value]" class="if-value" type="text" placeholder="value" value="<?php echo $entry['if_value'] ?>"> |
| 94 | then show |
| 95 | <select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php all_group_options($form, $entry['then_field']); ?></select> |
| 96 | <a style="display: inline-block;" href="#" title="delete rule" class="delete-button"><span class="dashicons dashicons-dismiss"></span> Remove rule</a> |
| 97 | </div> |
| 98 | <?php |
| 99 | $i++; |
| 100 | } |
| 101 | ?> |
| 102 | </div> |
| 103 | |
| 104 | |
| 105 | <div id="wpcf7cf-text-entries"> |
| 106 | <p><a href="#" id="wpcf7cf-settings-to-text">import/export</a></p> |
| 107 | <div id="wpcf7cf-settings-text-wrap"> |
| 108 | <textarea id="wpcf7cf-settings-text"></textarea> |
| 109 | <br> |
| 110 | Import actions (Beta feature!): |
| 111 | <input type="button" value="Add conditions" id="add-fields" > |
| 112 | <input type="button" value="Overwrite conditions" id="overwrite-fields" > |
| 113 | <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> |
| 114 | |
| 115 | <p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p> |
| 116 | |
| 117 | </div> |
| 118 | </div> |
| 119 | <?php |
| 120 | } |
| 121 | |
| 122 | // define the wpcf7_save_contact_form callback |
| 123 | function wpcf7cf_save_contact_form( $contact_form ) |
| 124 | { |
| 125 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) ) |
| 126 | return; |
| 127 | $post_id = $contact_form->id(); |
| 128 | if ( ! $post_id ) |
| 129 | return; |
| 130 | |
| 131 | unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry |
| 132 | |
| 133 | $options = array_values($_POST['wpcf7cf_options']); |
| 134 | |
| 135 | update_post_meta( $post_id, 'wpcf7cf_options', $options ); |
| 136 | |
| 137 | return; |
| 138 | |
| 139 | }; |
| 140 | |
| 141 | // add the action |
| 142 | add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 ); |