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
245 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 | if ( current_user_can( 'wpcf7_edit_contact_form' ) ) { |
| 19 | $panels['wpcf7cf-conditional-panel'] = array( |
| 20 | 'title' => __( 'Conditional fields', 'wpcf7cf' ), |
| 21 | 'callback' => 'wpcf7cf_editor_panel_conditional' |
| 22 | ); |
| 23 | } |
| 24 | return $panels; |
| 25 | } |
| 26 | |
| 27 | function wpcf7cf_all_field_options($post, $selected = '-1') { |
| 28 | $all_fields = $post->scan_form_tags(); |
| 29 | ?> |
| 30 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select field --</option> |
| 31 | <?php |
| 32 | foreach ($all_fields as $tag) { |
| 33 | if ($tag['type'] == 'group' || $tag['name'] == '') continue; |
| 34 | ?> |
| 35 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 36 | <?php |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function wpcf7cf_all_group_options($post, $selected = '-1') { |
| 41 | $all_groups = $post->scan_form_tags(array('type'=>'group')); |
| 42 | |
| 43 | ?> |
| 44 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>>-- Select group --</option> |
| 45 | <?php |
| 46 | foreach ($all_groups as $tag) { |
| 47 | ?> |
| 48 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 49 | <?php |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (!function_exists('all_operator_options')) { |
| 54 | function all_operator_options($selected = 'equals') { |
| 55 | $all_options = array('equals', 'not equals'); |
| 56 | $all_options = apply_filters('wpcf7cf_get_operators', $all_options); |
| 57 | foreach($all_options as $option) { |
| 58 | ?> |
| 59 | <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option> |
| 60 | <?php |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function wpcf7cf_editor_panel_conditional($form) { |
| 66 | |
| 67 | $form_id = $_GET['post']; |
| 68 | $wpcf7cf_entries = get_post_meta($form_id,'wpcf7cf_options',true); |
| 69 | |
| 70 | if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array(); |
| 71 | |
| 72 | $wpcf7cf_entries = array_values($wpcf7cf_entries); |
| 73 | |
| 74 | ?> |
| 75 | <div class="wpcf7cf-inner-container"> |
| 76 | <h3><?php echo esc_html( __( 'Conditional fields', 'wpcf7cf' ) ); ?></h3> |
| 77 | |
| 78 | <?php |
| 79 | print_entries_html($form); |
| 80 | ?> |
| 81 | |
| 82 | <div id="wpcf7cf-entries"> |
| 83 | <!-- <pre>--><?php //print_r($wpcf7cf_entries) ?><!--</pre>--> |
| 84 | <?php |
| 85 | print_entries_html($form, $wpcf7cf_entries); |
| 86 | ?> |
| 87 | </div> |
| 88 | |
| 89 | <span id="wpcf7cf-add-button" title="add new rule">+ add new conditional rule</span> |
| 90 | |
| 91 | <div id="wpcf7cf-text-entries"> |
| 92 | <p><a href="#" id="wpcf7cf-settings-to-text">import/export</a></p> |
| 93 | <div id="wpcf7cf-settings-text-wrap"> |
| 94 | <textarea id="wpcf7cf-settings-text"></textarea> |
| 95 | <br> |
| 96 | Import actions (Beta feature!): |
| 97 | <input type="button" value="Add conditions" id="add-fields" > |
| 98 | <input type="button" value="Overwrite conditions" id="overwrite-fields" > |
| 99 | <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> |
| 100 | |
| 101 | <p><a href="#" id="wpcf7cf-settings-text-clear">Clear</a></p> |
| 102 | |
| 103 | </div> |
| 104 | </div> |
| 105 | </div> |
| 106 | <?php |
| 107 | } |
| 108 | |
| 109 | // duplicate conditions on duplicate form part 1. |
| 110 | add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2); |
| 111 | function wpcf7cf_copy($new_form,$current_form) { |
| 112 | |
| 113 | $id = $current_form->id(); |
| 114 | $props = $new_form->get_properties(); |
| 115 | $props['messages']['wpcf7cf_copied'] = $id; |
| 116 | $new_form->set_properties($props); |
| 117 | |
| 118 | return $new_form; |
| 119 | } |
| 120 | |
| 121 | // duplicate conditions on duplicate form part 2. |
| 122 | add_action('wpcf7_after_save','wpcf7cf_after_save',10,1); |
| 123 | function wpcf7cf_after_save($contact_form) { |
| 124 | $props = $contact_form->get_properties(); |
| 125 | $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0; |
| 126 | if ($original_id !== 0) { |
| 127 | $post_id = $contact_form->id(); |
| 128 | unset($props['messages']['wpcf7cf_copied']); |
| 129 | $contact_form->set_properties($props); |
| 130 | update_post_meta( $post_id, 'wpcf7cf_options', get_post_meta($original_id, 'wpcf7cf_options', true)); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // wpcf7_save_contact_form callback |
| 136 | add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 ); |
| 137 | function wpcf7cf_save_contact_form( $contact_form ) |
| 138 | { |
| 139 | |
| 140 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf_options'] ) || ! is_array( $_POST['wpcf7cf_options'] ) ) { |
| 141 | return; |
| 142 | } |
| 143 | $post_id = $contact_form->id(); |
| 144 | if ( ! $post_id ) |
| 145 | return; |
| 146 | |
| 147 | unset($_POST['wpcf7cf_options']['{id}']); // remove the dummy entry |
| 148 | |
| 149 | $options = wpcf7cf_sanitize_options($_POST['wpcf7cf_options']); |
| 150 | |
| 151 | |
| 152 | |
| 153 | update_post_meta( $post_id, 'wpcf7cf_options', $options ); |
| 154 | |
| 155 | return; |
| 156 | |
| 157 | }; |
| 158 | |
| 159 | function wpcf7cf_sanitize_options($options) { |
| 160 | //$options = array_values($options); |
| 161 | $sanitized_options = []; |
| 162 | foreach ($options as $option_entry) { |
| 163 | $sanitized_option = []; |
| 164 | $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']); |
| 165 | foreach ($option_entry['and_rules'] as $and_rule) { |
| 166 | $sanitized_option['and_rules'][] = [ |
| 167 | 'if_field' => sanitize_text_field($and_rule['if_field']), |
| 168 | 'operator' => $and_rule['operator'], |
| 169 | 'if_value' => sanitize_text_field($and_rule['if_value']), |
| 170 | ]; |
| 171 | } |
| 172 | |
| 173 | $sanitized_options[] = $sanitized_option; |
| 174 | } |
| 175 | return $sanitized_options; |
| 176 | } |
| 177 | |
| 178 | function print_entries_html($form, $wpcf7cf_entries = false) { |
| 179 | |
| 180 | $is_dummy = !$wpcf7cf_entries; |
| 181 | |
| 182 | if ($is_dummy) { |
| 183 | $wpcf7cf_entries = array( |
| 184 | '{id}' => array( |
| 185 | 'then_field' => '-1', |
| 186 | 'and_rules' => array( |
| 187 | 0 => array( |
| 188 | 'if_field' => '-1', |
| 189 | 'operator' => 'equals', |
| 190 | 'if_value' => '' |
| 191 | ) |
| 192 | ) |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | foreach($wpcf7cf_entries as $i => $entry) { |
| 198 | |
| 199 | // check for backwards compatibility ( < 2.0 ) |
| 200 | if (!key_exists('and_rules', $wpcf7cf_entries[$i]) || !is_array($wpcf7cf_entries[$i]['and_rules'])) { |
| 201 | $wpcf7cf_entries[$i]['and_rules'][0] = $wpcf7cf_entries[$i]; |
| 202 | } |
| 203 | |
| 204 | $and_entries = array_values($wpcf7cf_entries[$i]['and_rules']); |
| 205 | |
| 206 | if ($is_dummy) { |
| 207 | ?> |
| 208 | <div id="wpcf7cf-new-entry"> |
| 209 | <?php |
| 210 | } else { |
| 211 | ?> |
| 212 | <div class="entry" id="entry-<?php echo $i ?>"> |
| 213 | <?php |
| 214 | } |
| 215 | ?> |
| 216 | <div class="wpcf7cf-if"> |
| 217 | <span class="label">Show</span> |
| 218 | <select name="wpcf7cf_options[<?php echo $i ?>][then_field]" class="then-field-select"><?php wpcf7cf_all_group_options($form, $entry['then_field']); ?></select> |
| 219 | </div> |
| 220 | <div class="wpcf7cf-and-rules" data-next-index="<?php echo count($and_entries) ?>"> |
| 221 | <?php |
| 222 | |
| 223 | |
| 224 | |
| 225 | foreach($and_entries as $and_i => $and_entry) { |
| 226 | ?> |
| 227 | <div class="wpcf7cf-and-rule"> |
| 228 | <span class="rule-part if-txt label">if</span> |
| 229 | <select name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][if_field]" |
| 230 | class="rule-part if-field-select"><?php wpcf7cf_all_field_options( $form, $and_entry['if_field'] ); ?></select> |
| 231 | <select name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][operator]" |
| 232 | class="rule-part operator"><?php all_operator_options( $and_entry['operator'] ) ?></select> |
| 233 | <input name="wpcf7cf_options[<?php echo $i ?>][and_rules][<?php echo $and_i ?>][if_value]" class="rule-part if-value" type="text" |
| 234 | placeholder="value" value="<?php echo $and_entry['if_value'] ?>"> |
| 235 | <span class="and-button">And</span> |
| 236 | <span title="delete rule" class="rule-part delete-button">remove</span> |
| 237 | </div> |
| 238 | <?php |
| 239 | } |
| 240 | ?> |
| 241 | </div> |
| 242 | </div> |
| 243 | <?php |
| 244 | } |
| 245 | } |