cf7-conditional-fields
Last commit date
js
5 years ago
jsdoc-out
5 years ago
Wpcf7cfMailParser.php
5 years ago
admin-style.css
6 years ago
admin-style.css.map
6 years ago
admin-style.scss
6 years ago
admin.php
5 years ago
cf7cf.php
5 years ago
contact-form-7-conditional-fields.php
5 years ago
init.php
5 years ago
readme.txt
5 years ago
style.css
6 years ago
tg_pane_group.php
5 years ago
wpcf7cf-options.php
5 years ago
admin.php
329 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 | |
| 7 | wp_enqueue_script('cf7cf-scripts-admin-all-pages', wpcf7cf_plugin_url( 'js/scripts_admin_all_pages.js' ),array( 'jquery' ), WPCF7CF_VERSION,true); |
| 8 | |
| 9 | |
| 10 | if ( false === strpos( $hook_suffix, 'wpcf7' ) ) { |
| 11 | return; //don't load styles and scripts if this isn't a CF7 page. |
| 12 | } |
| 13 | |
| 14 | wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array('jquery-ui-autocomplete', 'jquery-ui-sortable'), WPCF7CF_VERSION,true); |
| 15 | wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_options_0', wpcf7cf_get_settings()); |
| 16 | |
| 17 | } |
| 18 | |
| 19 | add_filter('wpcf7_editor_panels', 'add_conditional_panel'); |
| 20 | |
| 21 | function add_conditional_panel($panels) { |
| 22 | if ( current_user_can( 'wpcf7_edit_contact_form' ) ) { |
| 23 | $panels['wpcf7cf-conditional-panel'] = array( |
| 24 | 'title' => __( 'Conditional fields', 'cf7-conditional-fields' ), |
| 25 | 'callback' => 'wpcf7cf_editor_panel_conditional' |
| 26 | ); |
| 27 | } |
| 28 | return $panels; |
| 29 | } |
| 30 | |
| 31 | function wpcf7cf_all_field_options($post, $selected = '-1') { |
| 32 | $all_fields = $post->scan_form_tags(); |
| 33 | ?> |
| 34 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select field --', 'cf7-conditional-fields' ); ?></option> |
| 35 | <?php |
| 36 | foreach ($all_fields as $tag) { |
| 37 | if ($tag['type'] == 'group' || $tag['name'] == '') continue; |
| 38 | ?> |
| 39 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 40 | <?php |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function wpcf7cf_all_group_options($post, $selected = '-1') { |
| 45 | $all_groups = $post->scan_form_tags(array('type'=>'group')); |
| 46 | |
| 47 | ?> |
| 48 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select group --', 'cf7-conditional-fields' ); ?></option> |
| 49 | <?php |
| 50 | foreach ($all_groups as $tag) { |
| 51 | ?> |
| 52 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 53 | <?php |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (!function_exists('all_operator_options')) { |
| 58 | function all_operator_options($selected = 'equals') { |
| 59 | $all_options = array('equals', 'not equals'); |
| 60 | $all_options = apply_filters('wpcf7cf_get_operators', $all_options); |
| 61 | foreach($all_options as $option) { |
| 62 | // backwards compat |
| 63 | $selected = $selected == '≤' ? 'less than or equals' : $selected; |
| 64 | $selected = $selected == '≥' ? 'greater than or equals' : $selected; |
| 65 | $selected = $selected == '>' ? 'greater than' : $selected; |
| 66 | $selected = $selected == '<' ? 'less than' : $selected; |
| 67 | |
| 68 | ?> |
| 69 | <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option> |
| 70 | <?php |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function wpcf7cf_editor_panel_conditional($form) { |
| 76 | |
| 77 | $settings = wpcf7cf_get_settings(); |
| 78 | $is_text_only = $settings['conditions_ui'] === 'text_only'; |
| 79 | |
| 80 | // print_r($settings); |
| 81 | |
| 82 | $form_id = isset($_GET['post']) ? $_GET['post'] : false; |
| 83 | |
| 84 | if ($form_id === false) { |
| 85 | ?> |
| 86 | <div class="wpcf7cf-inner-container"> |
| 87 | <h2><?php _e( 'Conditional fields', 'cf7-conditional-fields' ); ?></h2> |
| 88 | <p><?php _e( 'You need to save your form, before you can start adding conditions.', 'cf7-conditional-fields' ); ?></p> |
| 89 | </div> |
| 90 | <?php |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $wpcf7cf_entries = CF7CF::getConditions($form_id); |
| 95 | |
| 96 | if (!is_array($wpcf7cf_entries)) $wpcf7cf_entries = array(); |
| 97 | |
| 98 | $wpcf7cf_entries = array_values($wpcf7cf_entries); |
| 99 | |
| 100 | ?> |
| 101 | <div class="wpcf7cf-inner-container"> |
| 102 | |
| 103 | <label class="wpcf7cf-switch" id="wpcf7cf-text-only-switch"> |
| 104 | <span class="label"><?php _e( 'Text mode', 'cf7-conditional-fields' ); ?></span> |
| 105 | <span class="switch"> |
| 106 | <input type="checkbox" id="wpcf7cf-text-only-checkbox" name="wpcf7cf-text-only-checkbox" value="text_only" <?php echo $is_text_only ? 'checked':''; ?>> |
| 107 | <span class="slider round"></span> |
| 108 | </span> |
| 109 | </label> |
| 110 | |
| 111 | <h2><?php _e( 'Conditional fields', 'cf7-conditional-fields' ); ?></h2> |
| 112 | |
| 113 | <div id="wpcf7cf-entries-ui" style="display:none"> |
| 114 | <?php |
| 115 | print_entries_html($form); |
| 116 | ?> |
| 117 | <div id="wpcf7cf-entries"> |
| 118 | <?php |
| 119 | //print_entries_html($form, $wpcf7cf_entries); |
| 120 | ?> |
| 121 | </div> |
| 122 | |
| 123 | <span id="wpcf7cf-add-button" title="<?php _e( 'add new rule', 'cf7-conditional-fields' ); ?>"><?php _e( '+ add new conditional rule', 'cf7-conditional-fields'); ?></span> |
| 124 | |
| 125 | <div id="wpcf7cf-a-lot-of-conditions" class="wpcf7cf-notice notice-warning" style="display:none;"> |
| 126 | <p> |
| 127 | <strong><?php _e( 'Wow, That\'s a lot of conditions!', 'cf7-conditional-fields' ); ?></strong><br> |
| 128 | <?php |
| 129 | // translators: 1. max recommended conditions |
| 130 | echo sprintf( __( 'You can only add up to %d conditions using this interface.', 'cf7-conditional-fields' ), WPCF7CF_MAX_RECOMMENDED_CONDITIONS ) . ' '; |
| 131 | // translators: 1,2: strong tags, 3. max recommended conditions |
| 132 | printf( __( 'Please switch to %1$sText mode%2$s if you want to add more than %3$d conditions.', 'cf7-conditional-fields' ), '<strong>', '</strong>', WPCF7CF_MAX_RECOMMENDED_CONDITIONS ); ?> |
| 133 | </p> |
| 134 | </div> |
| 135 | |
| 136 | </div> |
| 137 | |
| 138 | <div id="wpcf7cf-text-entries"> |
| 139 | <div id="wpcf7cf-settings-text-wrap"> |
| 140 | |
| 141 | <textarea id="wpcf7cf-settings-text" name="wpcf7cf-settings-text"><?php echo CF7CF::serializeConditions($wpcf7cf_entries) ?></textarea> |
| 142 | <br> |
| 143 | </div> |
| 144 | </div> |
| 145 | </div> |
| 146 | <?php |
| 147 | } |
| 148 | |
| 149 | // duplicate conditions on duplicate form part 1. |
| 150 | add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2); |
| 151 | function wpcf7cf_copy($new_form,$current_form) { |
| 152 | |
| 153 | $id = $current_form->id(); |
| 154 | $props = $new_form->get_properties(); |
| 155 | $props['messages']['wpcf7cf_copied'] = $id; |
| 156 | $new_form->set_properties($props); |
| 157 | |
| 158 | return $new_form; |
| 159 | } |
| 160 | |
| 161 | // duplicate conditions on duplicate form part 2. |
| 162 | add_action('wpcf7_after_save','wpcf7cf_after_save',10,1); |
| 163 | function wpcf7cf_after_save($contact_form) { |
| 164 | $props = $contact_form->get_properties(); |
| 165 | $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0; |
| 166 | if ($original_id !== 0) { |
| 167 | $post_id = $contact_form->id(); |
| 168 | unset($props['messages']['wpcf7cf_copied']); |
| 169 | $contact_form->set_properties($props); |
| 170 | CF7CF::setConditions($post_id, CF7CF::getConditions($original_id)); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // wpcf7_save_contact_form callback |
| 176 | add_action( 'wpcf7_save_contact_form', 'wpcf7cf_save_contact_form', 10, 1 ); |
| 177 | function wpcf7cf_save_contact_form( $contact_form ) |
| 178 | { |
| 179 | |
| 180 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf-settings-text'] ) ) { |
| 181 | return; |
| 182 | } |
| 183 | $post_id = $contact_form->id(); |
| 184 | if ( ! $post_id ) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | // we intentionally don't use sanitize_textarea_field here, |
| 190 | // because basically any character is a valid character. |
| 191 | // To arm agains SQL injections and other funky junky, the CF7CF::parse_conditions function is used. |
| 192 | $conditions_string = stripslashes($_POST['wpcf7cf-settings-text']); |
| 193 | $conditions = CF7CF::parse_conditions($conditions_string); |
| 194 | |
| 195 | CF7CF::setConditions($post_id, $conditions); |
| 196 | |
| 197 | if (isset($_POST['wpcf7cf-summary-template'])) { |
| 198 | WPCF7CF_Summary::saveSummaryTemplate($_POST['wpcf7cf-summary-template'],$post_id); |
| 199 | } |
| 200 | |
| 201 | return; |
| 202 | |
| 203 | }; |
| 204 | |
| 205 | function wpcf7cf_sanitize_options($options) { |
| 206 | //$options = array_values($options); |
| 207 | $sanitized_options = []; |
| 208 | foreach ($options as $option_entry) { |
| 209 | $sanitized_option = []; |
| 210 | $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']); |
| 211 | foreach ($option_entry['and_rules'] as $and_rule) { |
| 212 | $sanitized_option['and_rules'][] = [ |
| 213 | 'if_field' => sanitize_text_field($and_rule['if_field']), |
| 214 | 'operator' => $and_rule['operator'], |
| 215 | 'if_value' => sanitize_text_field($and_rule['if_value']), |
| 216 | ]; |
| 217 | } |
| 218 | |
| 219 | $sanitized_options[] = $sanitized_option; |
| 220 | } |
| 221 | return $sanitized_options; |
| 222 | } |
| 223 | |
| 224 | function print_entries_html($form, $wpcf7cf_entries = false) { |
| 225 | |
| 226 | $is_dummy = !$wpcf7cf_entries; |
| 227 | |
| 228 | if ($is_dummy) { |
| 229 | $wpcf7cf_entries = array( |
| 230 | '{id}' => array( |
| 231 | 'then_field' => '-1', |
| 232 | 'and_rules' => array( |
| 233 | 0 => array( |
| 234 | 'if_field' => '-1', |
| 235 | 'operator' => 'equals', |
| 236 | 'if_value' => '' |
| 237 | ) |
| 238 | ) |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | foreach($wpcf7cf_entries as $i => $entry) { |
| 244 | |
| 245 | // check for backwards compatibility ( < 2.0 ) |
| 246 | if (!key_exists('and_rules', $wpcf7cf_entries[$i]) || !is_array($wpcf7cf_entries[$i]['and_rules'])) { |
| 247 | $wpcf7cf_entries[$i]['and_rules'][0] = $wpcf7cf_entries[$i]; |
| 248 | } |
| 249 | |
| 250 | $and_entries = array_values($wpcf7cf_entries[$i]['and_rules']); |
| 251 | |
| 252 | if ($is_dummy) { |
| 253 | echo '<div id="wpcf7cf-new-entry">'; |
| 254 | } else { |
| 255 | echo '<div class="entry">'; |
| 256 | } |
| 257 | ?> |
| 258 | <div class="wpcf7cf-if"> |
| 259 | <span class="label"><?php _e( 'Show', 'cf7-conditional-fields' ); ?></span> |
| 260 | <select class="then-field-select"><?php wpcf7cf_all_group_options($form, $entry['then_field']); ?></select> |
| 261 | </div> |
| 262 | <div class="wpcf7cf-and-rules" data-next-index="<?php echo count($and_entries) ?>"> |
| 263 | <?php |
| 264 | |
| 265 | |
| 266 | |
| 267 | foreach($and_entries as $and_i => $and_entry) { |
| 268 | ?> |
| 269 | <div class="wpcf7cf-and-rule"> |
| 270 | <span class="rule-part if-txt label"><?php _e( 'if', 'cf7-conditional-fields' ); ?></span> |
| 271 | <select class="rule-part if-field-select"><?php wpcf7cf_all_field_options( $form, $and_entry['if_field'] ); ?></select> |
| 272 | <select class="rule-part operator"><?php all_operator_options( $and_entry['operator'] ) ?></select> |
| 273 | <input class="rule-part if-value" type="text" placeholder="<?php _e( 'value', 'cf7-conditional-fields' ); ?>" value="<?php echo $and_entry['if_value'] ?>"> |
| 274 | <span class="and-button"><?php _e( 'And', 'cf7-conditional-fields' ); ?></span> |
| 275 | <span title="<?php _e( 'delete rule', 'cf7-conditional-fields' ); ?>" class="rule-part delete-button"><?php _e( 'remove', 'cf7-conditional-fields' ); ?></span> |
| 276 | </div> |
| 277 | <?php |
| 278 | } |
| 279 | ?> |
| 280 | </div> |
| 281 | <?php |
| 282 | echo '</div>'; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | add_action('admin_notices', function () { |
| 287 | |
| 288 | $settings = wpcf7cf_get_settings(); |
| 289 | |
| 290 | $nid = 'install-cf7'; |
| 291 | |
| 292 | if (!defined('WPCF7_VERSION') && empty($settings['notice_dismissed_'.$nid])) { |
| 293 | ?> |
| 294 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 295 | <p> |
| 296 | <strong>Conditional Fields for Contact Form 7</strong> depends on Contact Form 7. Please install <a target="_blank" href="https://downloads.wordpress.org/plugin/contact-form-7.<?php echo WPCF7CF_CF7_MAX_VERSION ?>.zip">Contact Form 7</a>. |
| 297 | </p> |
| 298 | </div> |
| 299 | <?php |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | $nid = 'rollback-cf7-'.WPCF7CF_CF7_MAX_VERSION; |
| 304 | if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '<' ) && empty($settings['notice_dismissed_'.$nid]) ) { |
| 305 | ?> |
| 306 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 307 | <p> |
| 308 | <strong>Conditional Fields for Contact Form 7</strong> is not yet compatible with your current version of Contact Form 7. |
| 309 | <br>If you notice any problems with your forms, please roll back to Contact Form 7 <strong>version <?php echo WPCF7CF_CF7_MAX_VERSION ?></strong>. |
| 310 | <br>For a quick and safe rollback, we recommend <a href="https://wordpress.org/plugins/wp-rollback/" target="_blank">WP Rollback</a>. |
| 311 | </p> |
| 312 | </div> |
| 313 | <?php |
| 314 | } |
| 315 | |
| 316 | $nid = 'update-cf7-'.WPCF7CF_CF7_MAX_VERSION; |
| 317 | if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '>' ) && empty($settings['notice_dismissed_'.$nid]) ) { |
| 318 | ?> |
| 319 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 320 | <p> |
| 321 | <strong>Conditional Fields for Contact Form 7</strong> is fully compatible and tested with Contact Form 7 version <?php echo WPCF7CF_CF7_MAX_VERSION ?>. |
| 322 | <br>Compatibility with other versions of CF7 is not guaranteed, so please install <a target="_blank" href="https://downloads.wordpress.org/plugin/contact-form-7.<?php echo WPCF7CF_CF7_MAX_VERSION ?>.zip">CF7 version <?php echo WPCF7CF_CF7_MAX_VERSION ?></a> |
| 323 | </p> |
| 324 | </div> |
| 325 | <?php |
| 326 | } |
| 327 | |
| 328 | }); |
| 329 |