cf7-conditional-fields
Last commit date
js
7 years ago
admin-style.css
7 years ago
admin-style.css.map
7 years ago
admin-style.scss
7 years ago
admin.php
7 years ago
cf7cf.php
7 years ago
contact-form-7-conditional-fields.php
7 years ago
init.php
7 years ago
readme.txt
7 years ago
style.css
7 years ago
tg_pane_group.php
7 years ago
wpcf7cf-options.php
7 years ago
admin.php
196 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 | |
| 10 | wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array('jquery-ui-autocomplete', 'jquery-ui-sortable'), WPCF7CF_VERSION,true); |
| 11 | wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_options_0', get_option(WPCF7CF_OPTIONS)); |
| 12 | |
| 13 | } |
| 14 | |
| 15 | add_filter('wpcf7_editor_panels', 'add_conditional_panel'); |
| 16 | |
| 17 | function add_conditional_panel($panels) { |
| 18 | $panels['wpcf7cf-conditional-panel'] = array( |
| 19 | 'title' => __( 'Conditional fields', 'wpcf7cf' ), |
| 20 | 'callback' => 'wpcf7cf_editor_panel_conditional' |
| 21 | ); |
| 22 | return $panels; |
| 23 | } |
| 24 | |
| 25 | function wpcf7cf_all_field_options($post, $selected = '-1') { |
| 26 | $all_fields = $post->scan_form_tags(); |
| 27 | ?> |
| 28 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option> |
| 29 | <?php |
| 30 | foreach ($all_fields as $tag) { |
| 31 | if ($tag['type'] == 'group' || $tag['name'] == '') continue; |
| 32 | ?> |
| 33 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 34 | <?php |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function wpcf7cf_all_group_options($post, $selected = '-1') { |
| 39 | $all_groups = $post->scan_form_tags(array('type'=>'group')); |
| 40 | |
| 41 | ?> |
| 42 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option> |
| 43 | <?php |
| 44 | foreach ($all_groups as $tag) { |
| 45 | ?> |
| 46 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 47 | <?php |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!function_exists('all_operator_options')) { |
| 52 | function all_operator_options($selected = 'equals') { |
| 53 | $all_options = array('equals', 'not equals'); |
| 54 | $all_options = apply_filters('wpcf7cf_get_operators', $all_options); |
| 55 | foreach($all_options as $option) { |
| 56 | ?> |
| 57 | <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option> |
| 58 | <?php |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function wpcf7cf_editor_panel_conditional($form) { |
| 64 | |
| 65 | $form_id = $_GET['post']; |
| 66 | $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true); |
| 67 | |
| 68 | if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array(); |
| 69 | |
| 70 | $wpcf7cf_entries = array_values($wpcf7cf_entries); |
| 71 | |
| 72 | ?> |
| 73 | <div class="wpcf7cf-inner-container"> |
| 74 | <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3> |
| 75 | |
| 76 | <?php |
| 77 | print_entries_html($form); |
| 78 | ?> |
| 79 | |
| 80 | <div id="wpcf7cf-entries"> |
| 81 | <!-- <pre>--><?php //print_r($wpcf7cf_entries) ?><!--</pre>--> |
| 82 | <?php |
| 83 | print_entries_html($form, $wpcf7cf_entries); |
| 84 | ?> |
| 85 | </div> |
| 86 | |
| 87 | <span id="wpcf7cf-add-button" title="add new rule">+ add new conditional rule</span> |
| 88 | |
| 89 | <div id="wpcf7cf-text-entries"> |
| 90 | <p><a href="#" id="wpcf7cf-settings-to-text">import/export</a></p> |
| 91 | <div id="wpcf7cf-settings-text-wrap"> |
| 92 | <textarea id="wpcf7cf-settings-text"></textarea> |
| 93 | <br> |
| 94 | Import actions (Beta feature!): |
| 95 | <input type="button" value="Add conditions" id="add-fields" > |
| 96 | <input type="button" value="Overwrite conditions" id="overwrite-fields" > |
| 97 | <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> |
| 98 | |
| 99 | <p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p> |
| 100 | |
| 101 | </div> |
| 102 | </div> |
| 103 | </div> |
| 104 | <?php |
| 105 | } |
| 106 | |
| 107 | // define the wpcf7_save_contact_form callback |
| 108 | function wpcf7cf_save_contact_form( $contact_form ) |
| 109 | { |
| 110 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) ) |
| 111 | return; |
| 112 | $post_id = $contact_form->id(); |
| 113 | if ( ! $post_id ) |
| 114 | return; |
| 115 | |
| 116 | unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry |
| 117 | |
| 118 | $options = array_values($_POST['wpcf7cf_options']); |
| 119 | |
| 120 | update_post_meta( $post_id, 'wpcf7cf_options', $options ); |
| 121 | |
| 122 | return; |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | // add the action |
| 127 | add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 ); |
| 128 | |
| 129 | function print_entries_html($form, $wpcf7cf_entries = false) { |
| 130 | |
| 131 | $is_dummy = !$wpcf7cf_entries; |
| 132 | |
| 133 | if ($is_dummy) { |
| 134 | $wpcf7cf_entries = array( |
| 135 | '{id}' => array( |
| 136 | 'then_field' => '-1', |
| 137 | 'and_rules' => array( |
| 138 | 0 => array( |
| 139 | 'if_field' => '-1', |
| 140 | 'operator' => 'equals', |
| 141 | 'if_value' => '' |
| 142 | ) |
| 143 | ) |
| 144 | ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | foreach($wpcf7cf_entries as $i => $entry) { |
| 149 | |
| 150 | // check for backwards compatibility ( < 2.0 ) |
| 151 | if (!key_exists('and_rules', $wpcf7cf_entries[$i]) || !is_array($wpcf7cf_entries[$i]['and_rules'])) { |
| 152 | $wpcf7cf_entries[$i]['and_rules'][0] = $wpcf7cf_entries[$i]; |
| 153 | } |
| 154 | |
| 155 | $and_entries = array_values($wpcf7cf_entries[$i]['and_rules']); |
| 156 | |
| 157 | if ($is_dummy) { |
| 158 | ?> |
| 159 | <div id="wpcf7cf-new-entry"> |
| 160 | <?php |
| 161 | } else { |
| 162 | ?> |
| 163 | <div class="entry" id="entry-<?php echo $i ?>"> |
| 164 | <?php |
| 165 | } |
| 166 | ?> |
| 167 | <div class="wpcf7cf-if"> |
| 168 | <span class="label">Show</span> |
| 169 | <select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php wpcf7cf_all_group_options($form, $entry['then_field']); ?></select> |
| 170 | </div> |
| 171 | <div class="wpcf7cf-and-rules" data-next-index="<?php echo count($and_entries) ?>"> |
| 172 | <?php |
| 173 | |
| 174 | |
| 175 | |
| 176 | foreach($and_entries as $and_i => $and_entry) { |
| 177 | ?> |
| 178 | <div class="wpcf7cf-and-rule"> |
| 179 | <span class="rule-part if-txt label">if</span> |
| 180 | <select name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][if_field]" |
| 181 | class="rule-part if-field-select"><?php wpcf7cf_all_field_options( $form, $and_entry['if_field'] ); ?></select> |
| 182 | <select name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][operator]" |
| 183 | class="rule-part operator"><?php all_operator_options( $and_entry['operator'] ) ?></select> |
| 184 | <input name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][if_value]" class="rule-part if-value" type="text" |
| 185 | placeholder="value" value="<?php echo $and_entry['if_value'] ?>"> |
| 186 | <span class="and-button">And</span> |
| 187 | <span title="delete rule" class="rule-part delete-button">remove</span> |
| 188 | </div> |
| 189 | <?php |
| 190 | } |
| 191 | ?> |
| 192 | </div> |
| 193 | </div> |
| 194 | <?php |
| 195 | } |
| 196 | } |