cf7-conditional-fields
Last commit date
js
3 years ago
jsdoc-out
4 years ago
Wpcf7cfMailParser.php
4 years ago
admin-style.css
4 years ago
admin-style.css.map
6 years ago
admin-style.scss
4 years ago
admin.php
3 years ago
cf7cf.php
3 years ago
contact-form-7-conditional-fields.php
2 years ago
init.php
2 years ago
readme.txt
2 years ago
style.css
3 years ago
tg_pane_group.php
5 years ago
wpcf7cf-options.php
4 years ago
admin.php
252 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 ( isset($_GET['page']) && ( $_GET['page'] == 'wpcf7' && isset($_GET['post']) || $_GET['page'] == 'wpcf7-new' ) ) { |
| 11 | //only load styles and scripts if this is a CF7 detail page. |
| 12 | wpcf7cf_admin_enqueue_form_edit_scripts($hook_suffix); |
| 13 | } |
| 14 | |
| 15 | } |
| 16 | |
| 17 | /* fix for std post editor used in Smart Grid */ |
| 18 | add_action('cf7sg_enqueue_admin_editor_scripts', 'wpcf7cf_admin_enqueue_form_edit_scripts'); |
| 19 | |
| 20 | function wpcf7cf_admin_enqueue_form_edit_scripts($hook_suffix){ |
| 21 | wp_enqueue_script('cf7cf-scripts-admin', wpcf7cf_plugin_url( 'js/scripts_admin.js' ),array('jquery-ui-autocomplete', 'jquery-ui-sortable'), WPCF7CF_VERSION,true); |
| 22 | wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_options_0', wpcf7cf_get_settings()); |
| 23 | //wp_localize_script('cf7cf-scripts-admin', 'wpcf7cf_newEntryHTML', ); |
| 24 | } |
| 25 | |
| 26 | add_filter('wpcf7_editor_panels', 'add_conditional_panel'); |
| 27 | |
| 28 | function add_conditional_panel($panels) { |
| 29 | if ( current_user_can( 'wpcf7_edit_contact_forms' ) ) { |
| 30 | $panels['wpcf7cf-conditional-panel'] = array( |
| 31 | 'title' => __( 'Conditional fields', 'cf7-conditional-fields' ), |
| 32 | 'callback' => 'wpcf7cf_editor_panel_conditional' |
| 33 | ); |
| 34 | } |
| 35 | return $panels; |
| 36 | } |
| 37 | |
| 38 | function wpcf7cf_all_field_options($post, $selected = '-1') { |
| 39 | $all_fields = $post->scan_form_tags(); |
| 40 | ?> |
| 41 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select field --', 'cf7-conditional-fields' ); ?></option> |
| 42 | <?php |
| 43 | foreach ($all_fields as $tag) { |
| 44 | if ($tag['type'] == 'group' || $tag['name'] == '') continue; |
| 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 | function wpcf7cf_all_group_options($post, $selected = '-1') { |
| 52 | $all_groups = $post->scan_form_tags(array('type'=>'group')); |
| 53 | |
| 54 | ?> |
| 55 | <option value="-1" <?php echo $selected == '-1'?'selected':'' ?>><?php _e( '-- Select group --', 'cf7-conditional-fields' ); ?></option> |
| 56 | <?php |
| 57 | foreach ($all_groups as $tag) { |
| 58 | ?> |
| 59 | <option value="<?php echo $tag['name']; ?>" <?php echo $selected == $tag['name']?'selected':'' ?>><?php echo $tag['name']; ?></option> |
| 60 | <?php |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!function_exists('all_operator_options')) { |
| 65 | function all_operator_options($selected = 'equals') { |
| 66 | $all_options = array('equals', 'not equals'); |
| 67 | $all_options = apply_filters('wpcf7cf_get_operators', $all_options); |
| 68 | foreach($all_options as $option) { |
| 69 | // backwards compat |
| 70 | $selected = $selected == '≤' ? 'less than or equals' : $selected; |
| 71 | $selected = $selected == '≥' ? 'greater than or equals' : $selected; |
| 72 | $selected = $selected == '>' ? 'greater than' : $selected; |
| 73 | $selected = $selected == '<' ? 'less than' : $selected; |
| 74 | |
| 75 | ?> |
| 76 | <option value="<?php echo htmlentities($option) ?>" <?php echo $selected == $option?'selected':'' ?>><?php echo htmlentities($option) ?></option> |
| 77 | <?php |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function wpcf7cf_editor_panel_conditional($form) { |
| 83 | |
| 84 | $settings = wpcf7cf_get_settings(); |
| 85 | $is_text_only = $settings['conditions_ui'] === 'text_only'; |
| 86 | |
| 87 | // print_r($settings); |
| 88 | |
| 89 | $form_id = isset($_GET['post']) ? $_GET['post'] : false; |
| 90 | |
| 91 | if ($form_id !== false) { |
| 92 | $wpcf7cf_entries = CF7CF::getConditions($form_id); |
| 93 | $wpcf7cf_entries = array_values($wpcf7cf_entries); |
| 94 | } else { |
| 95 | $wpcf7cf_entries = []; |
| 96 | } |
| 97 | |
| 98 | ?> |
| 99 | <div class="wpcf7cf-inner-container"> |
| 100 | |
| 101 | <label class="wpcf7cf-switch" id="wpcf7cf-text-only-switch"> |
| 102 | <span class="label"><?php _e( 'Text mode', 'cf7-conditional-fields' ); ?></span> |
| 103 | <span class="switch"> |
| 104 | <input type="checkbox" id="wpcf7cf-text-only-checkbox" name="wpcf7cf-text-only-checkbox" value="text_only" <?php echo $is_text_only ? 'checked':''; ?>> |
| 105 | <span class="slider round"></span> |
| 106 | </span> |
| 107 | </label> |
| 108 | |
| 109 | <h2><?php _e( 'Conditional fields', 'cf7-conditional-fields' ); ?></h2> |
| 110 | |
| 111 | <div id="wpcf7cf-entries-ui" style="display:none"> |
| 112 | <div id="wpcf7cf-entries"> |
| 113 | </div> |
| 114 | |
| 115 | <span id="wpcf7cf-add-button" title="<?php _e( 'add new rule', 'cf7-conditional-fields' ); ?>"><?php _e( '+ add new conditional rule', 'cf7-conditional-fields'); ?></span> |
| 116 | |
| 117 | <div id="wpcf7cf-a-lot-of-conditions" class="wpcf7cf-notice notice-warning" style="display:none;"> |
| 118 | <p> |
| 119 | <strong><?php _e( 'Wow, That\'s a lot of conditions!', 'cf7-conditional-fields' ); ?></strong><br> |
| 120 | <?php |
| 121 | // translators: 1. max recommended conditions |
| 122 | echo sprintf( __( 'You can only add up to %d conditions using this interface.', 'cf7-conditional-fields' ), WPCF7CF_MAX_RECOMMENDED_CONDITIONS ) . ' '; |
| 123 | // translators: 1,2: strong tags, 3. max recommended conditions |
| 124 | printf( __( 'Please switch to %1$sText mode%2$s if you want to add more than %3$d conditions.', 'cf7-conditional-fields' ), '<a href="#" class="wpcf7cf-switch-to-txt-link">', '</a>', WPCF7CF_MAX_RECOMMENDED_CONDITIONS ); ?> |
| 125 | </p> |
| 126 | </div> |
| 127 | |
| 128 | </div> |
| 129 | |
| 130 | <div id="wpcf7cf-text-entries"> |
| 131 | <div id="wpcf7cf-settings-text-wrap"> |
| 132 | <textarea id="wpcf7cf-settings-text" name="wpcf7cf-settings-text"><?php echo CF7CF::serializeConditions($wpcf7cf_entries) ?></textarea> |
| 133 | <br> |
| 134 | </div> |
| 135 | </div> |
| 136 | </div> |
| 137 | <?php |
| 138 | } |
| 139 | |
| 140 | // Save conditions and summary field |
| 141 | add_action( 'wpcf7_after_save', function($contact_form) { |
| 142 | |
| 143 | if ( ! isset( $_POST ) || empty( $_POST ) || ! isset( $_POST['wpcf7cf-settings-text'] ) ) { |
| 144 | return; |
| 145 | } |
| 146 | $post_id = $contact_form->id(); |
| 147 | if ( ! $post_id ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // we intentionally don't use sanitize_textarea_field here, |
| 152 | // because basically any character is a valid character. |
| 153 | // To arm agains SQL injections and other funky junky, the CF7CF::parse_conditions function is used. |
| 154 | $conditions_string = stripslashes($_POST['wpcf7cf-settings-text']); |
| 155 | $conditions = CF7CF::parse_conditions($conditions_string); |
| 156 | |
| 157 | CF7CF::setConditions($post_id, $conditions); |
| 158 | |
| 159 | if (isset($_POST['wpcf7cf-summary-template'])) { |
| 160 | WPCF7CF_Summary::saveSummaryTemplate($_POST['wpcf7cf-summary-template'],$post_id); |
| 161 | } |
| 162 | |
| 163 | }, 8, 1 ); |
| 164 | |
| 165 | // duplicate conditions on duplicate form part 1. |
| 166 | add_filter('wpcf7_copy','wpcf7cf_copy', 10, 2); |
| 167 | function wpcf7cf_copy($new_form,$current_form) { |
| 168 | |
| 169 | $id = $current_form->id(); |
| 170 | $props = $new_form->get_properties(); |
| 171 | $props['messages']['wpcf7cf_copied'] = $id; |
| 172 | $new_form->set_properties($props); |
| 173 | |
| 174 | return $new_form; |
| 175 | } |
| 176 | |
| 177 | // duplicate conditions on duplicate form part 2. |
| 178 | add_action('wpcf7_after_save','wpcf7cf_after_save',10,1); |
| 179 | function wpcf7cf_after_save($contact_form) { |
| 180 | $props = $contact_form->get_properties(); |
| 181 | $original_id = isset($props['messages']['wpcf7cf_copied']) ? $props['messages']['wpcf7cf_copied'] : 0; |
| 182 | if ($original_id !== 0) { |
| 183 | $post_id = $contact_form->id(); |
| 184 | unset($props['messages']['wpcf7cf_copied']); |
| 185 | $contact_form->set_properties($props); |
| 186 | CF7CF::setConditions($post_id, CF7CF::getConditions($original_id)); |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | function wpcf7cf_sanitize_options($options) { |
| 192 | //$options = array_values($options); |
| 193 | $sanitized_options = []; |
| 194 | foreach ($options as $option_entry) { |
| 195 | $sanitized_option = []; |
| 196 | $sanitized_option['then_field'] = sanitize_text_field($option_entry['then_field']); |
| 197 | foreach ($option_entry['and_rules'] as $and_rule) { |
| 198 | $sanitized_option['and_rules'][] = [ |
| 199 | 'if_field' => sanitize_text_field($and_rule['if_field']), |
| 200 | 'operator' => $and_rule['operator'], |
| 201 | 'if_value' => sanitize_text_field($and_rule['if_value']), |
| 202 | ]; |
| 203 | } |
| 204 | |
| 205 | $sanitized_options[] = $sanitized_option; |
| 206 | } |
| 207 | return $sanitized_options; |
| 208 | } |
| 209 | |
| 210 | add_action('admin_notices', function () { |
| 211 | |
| 212 | $settings = wpcf7cf_get_settings(); |
| 213 | |
| 214 | $nid = 'install-cf7'; |
| 215 | if (!defined('WPCF7_VERSION') && empty($settings['notice_dismissed_'.$nid])) { |
| 216 | ?> |
| 217 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 218 | <p> |
| 219 | <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>. |
| 220 | </p> |
| 221 | </div> |
| 222 | <?php |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | $nid = 'rollback-cf7-'.WPCF7CF_CF7_MAX_VERSION; |
| 227 | if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '<' ) && empty($settings['notice_dismissed_'.$nid]) && current_user_can('update_plugins') ) { |
| 228 | ?> |
| 229 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 230 | <p> |
| 231 | <strong>Conditional Fields for Contact Form 7</strong> is not yet tested with your current version of Contact Form 7. |
| 232 | <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>. |
| 233 | <br>For a quick and safe rollback, we recommend <a href="https://wordpress.org/plugins/wp-rollback/" target="_blank">WP Rollback</a>. |
| 234 | </p> |
| 235 | </div> |
| 236 | <?php |
| 237 | } |
| 238 | |
| 239 | $nid = 'update-cf7-'.WPCF7CF_CF7_MAX_VERSION; |
| 240 | if ( version_compare( WPCF7CF_CF7_MAX_VERSION, WPCF7_VERSION, '>' ) && empty($settings['notice_dismissed_'.$nid]) && current_user_can('update_plugins') ) { |
| 241 | ?> |
| 242 | <div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="<?php echo $nid ?>"> |
| 243 | <p> |
| 244 | <strong>Conditional Fields for Contact Form 7</strong> is fully compatible and tested with Contact Form 7 version <?php echo WPCF7CF_CF7_MAX_VERSION ?>. |
| 245 | <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> |
| 246 | </p> |
| 247 | </div> |
| 248 | <?php |
| 249 | } |
| 250 | |
| 251 | }); |
| 252 |