constant-contact
4 years ago
recaptcha
4 years ago
sendinblue
4 years ago
stripe
4 years ago
acceptance.php
5 years ago
akismet.php
4 years ago
checkbox.php
4 years ago
count.php
7 years ago
date.php
5 years ago
disallowed-list.php
4 years ago
doi-helper.php
4 years ago
file.php
5 years ago
flamingo.php
4 years ago
hidden.php
7 years ago
listo.php
9 years ago
number.php
5 years ago
quiz.php
4 years ago
really-simple-captcha.php
4 years ago
response.php
6 years ago
select.php
4 years ago
submit.php
4 years ago
text.php
4 years ago
textarea.php
4 years ago
acceptance.php
304 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [acceptance] |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_acceptance', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_acceptance() { |
| 11 | wpcf7_add_form_tag( 'acceptance', |
| 12 | 'wpcf7_acceptance_form_tag_handler', |
| 13 | array( |
| 14 | 'name-attr' => true, |
| 15 | ) |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | function wpcf7_acceptance_form_tag_handler( $tag ) { |
| 20 | if ( empty( $tag->name ) ) { |
| 21 | return ''; |
| 22 | } |
| 23 | |
| 24 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 25 | |
| 26 | $class = wpcf7_form_controls_class( $tag->type ); |
| 27 | |
| 28 | if ( $validation_error ) { |
| 29 | $class .= ' wpcf7-not-valid'; |
| 30 | } |
| 31 | |
| 32 | if ( $tag->has_option( 'invert' ) ) { |
| 33 | $class .= ' invert'; |
| 34 | } |
| 35 | |
| 36 | if ( $tag->has_option( 'optional' ) ) { |
| 37 | $class .= ' optional'; |
| 38 | } |
| 39 | |
| 40 | $atts = array( |
| 41 | 'class' => trim( $class ), |
| 42 | ); |
| 43 | |
| 44 | $item_atts = array(); |
| 45 | |
| 46 | $item_atts['type'] = 'checkbox'; |
| 47 | $item_atts['name'] = $tag->name; |
| 48 | $item_atts['value'] = '1'; |
| 49 | $item_atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 50 | |
| 51 | if ( $validation_error ) { |
| 52 | $item_atts['aria-invalid'] = 'true'; |
| 53 | $item_atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 54 | $tag->name |
| 55 | ); |
| 56 | } else { |
| 57 | $item_atts['aria-invalid'] = 'false'; |
| 58 | } |
| 59 | |
| 60 | if ( $tag->has_option( 'default:on' ) ) { |
| 61 | $item_atts['checked'] = 'checked'; |
| 62 | } |
| 63 | |
| 64 | $item_atts['class'] = $tag->get_class_option(); |
| 65 | $item_atts['id'] = $tag->get_id_option(); |
| 66 | |
| 67 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 68 | |
| 69 | $content = empty( $tag->content ) |
| 70 | ? (string) reset( $tag->values ) |
| 71 | : $tag->content; |
| 72 | |
| 73 | $content = trim( $content ); |
| 74 | |
| 75 | if ( $content ) { |
| 76 | if ( $tag->has_option( 'label_first' ) ) { |
| 77 | $html = sprintf( |
| 78 | '<span class="wpcf7-list-item-label">%2$s</span><input %1$s />', |
| 79 | $item_atts, $content ); |
| 80 | } else { |
| 81 | $html = sprintf( |
| 82 | '<input %1$s /><span class="wpcf7-list-item-label">%2$s</span>', |
| 83 | $item_atts, $content ); |
| 84 | } |
| 85 | |
| 86 | $html = sprintf( |
| 87 | '<span class="wpcf7-list-item"><label>%s</label></span>', |
| 88 | $html |
| 89 | ); |
| 90 | |
| 91 | } else { |
| 92 | $html = sprintf( |
| 93 | '<span class="wpcf7-list-item"><input %1$s /></span>', |
| 94 | $item_atts ); |
| 95 | } |
| 96 | |
| 97 | $atts = wpcf7_format_atts( $atts ); |
| 98 | |
| 99 | $html = sprintf( |
| 100 | '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>', |
| 101 | sanitize_html_class( $tag->name ), $atts, $html, $validation_error ); |
| 102 | |
| 103 | return $html; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Validation filter */ |
| 108 | |
| 109 | add_filter( 'wpcf7_validate_acceptance', |
| 110 | 'wpcf7_acceptance_validation_filter', 10, 2 ); |
| 111 | |
| 112 | function wpcf7_acceptance_validation_filter( $result, $tag ) { |
| 113 | if ( ! wpcf7_acceptance_as_validation() ) { |
| 114 | return $result; |
| 115 | } |
| 116 | |
| 117 | if ( $tag->has_option( 'optional' ) ) { |
| 118 | return $result; |
| 119 | } |
| 120 | |
| 121 | $name = $tag->name; |
| 122 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 123 | |
| 124 | $invert = $tag->has_option( 'invert' ); |
| 125 | |
| 126 | if ( $invert and $value |
| 127 | or ! $invert and ! $value ) { |
| 128 | $result->invalidate( $tag, wpcf7_get_message( 'accept_terms' ) ); |
| 129 | } |
| 130 | |
| 131 | return $result; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* Acceptance filter */ |
| 136 | |
| 137 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter', 10, 2 ); |
| 138 | |
| 139 | function wpcf7_acceptance_filter( $accepted, $submission ) { |
| 140 | $tags = wpcf7_scan_form_tags( array( 'type' => 'acceptance' ) ); |
| 141 | |
| 142 | foreach ( $tags as $tag ) { |
| 143 | $name = $tag->name; |
| 144 | |
| 145 | if ( empty( $name ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 150 | |
| 151 | $content = empty( $tag->content ) |
| 152 | ? (string) reset( $tag->values ) |
| 153 | : $tag->content; |
| 154 | |
| 155 | $content = trim( $content ); |
| 156 | |
| 157 | if ( $value and $content ) { |
| 158 | $submission->add_consent( $name, $content ); |
| 159 | } |
| 160 | |
| 161 | if ( $tag->has_option( 'optional' ) ) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | $invert = $tag->has_option( 'invert' ); |
| 166 | |
| 167 | if ( $invert and $value |
| 168 | or ! $invert and ! $value ) { |
| 169 | $accepted = false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $accepted; |
| 174 | } |
| 175 | |
| 176 | add_filter( 'wpcf7_form_class_attr', |
| 177 | 'wpcf7_acceptance_form_class_attr', 10, 1 ); |
| 178 | |
| 179 | function wpcf7_acceptance_form_class_attr( $class ) { |
| 180 | if ( wpcf7_acceptance_as_validation() ) { |
| 181 | return $class . ' wpcf7-acceptance-as-validation'; |
| 182 | } |
| 183 | |
| 184 | return $class; |
| 185 | } |
| 186 | |
| 187 | function wpcf7_acceptance_as_validation() { |
| 188 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | return $contact_form->is_true( 'acceptance_as_validation' ); |
| 193 | } |
| 194 | |
| 195 | add_filter( 'wpcf7_mail_tag_replaced_acceptance', |
| 196 | 'wpcf7_acceptance_mail_tag', 10, 4 ); |
| 197 | |
| 198 | function wpcf7_acceptance_mail_tag( $replaced, $submitted, $html, $mail_tag ) { |
| 199 | $form_tag = $mail_tag->corresponding_form_tag(); |
| 200 | |
| 201 | if ( ! $form_tag ) { |
| 202 | return $replaced; |
| 203 | } |
| 204 | |
| 205 | if ( ! empty( $submitted ) ) { |
| 206 | $replaced = __( 'Consented', 'contact-form-7' ); |
| 207 | } else { |
| 208 | $replaced = __( 'Not consented', 'contact-form-7' ); |
| 209 | } |
| 210 | |
| 211 | $content = empty( $form_tag->content ) |
| 212 | ? (string) reset( $form_tag->values ) |
| 213 | : $form_tag->content; |
| 214 | |
| 215 | if ( ! $html ) { |
| 216 | $content = wp_strip_all_tags( $content ); |
| 217 | } |
| 218 | |
| 219 | $content = trim( $content ); |
| 220 | |
| 221 | if ( $content ) { |
| 222 | $replaced = sprintf( |
| 223 | /* translators: 1: 'Consented' or 'Not consented', 2: conditions */ |
| 224 | _x( '%1$s: %2$s', 'mail output for acceptance checkboxes', |
| 225 | 'contact-form-7' ), |
| 226 | $replaced, |
| 227 | $content |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | return $replaced; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Tag generator */ |
| 236 | |
| 237 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_acceptance', 35, 0 ); |
| 238 | |
| 239 | function wpcf7_add_tag_generator_acceptance() { |
| 240 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 241 | $tag_generator->add( 'acceptance', __( 'acceptance', 'contact-form-7' ), |
| 242 | 'wpcf7_tag_generator_acceptance' ); |
| 243 | } |
| 244 | |
| 245 | function wpcf7_tag_generator_acceptance( $contact_form, $args = '' ) { |
| 246 | $args = wp_parse_args( $args, array() ); |
| 247 | $type = 'acceptance'; |
| 248 | |
| 249 | $description = __( "Generate a form-tag for an acceptance checkbox. For more details, see %s.", 'contact-form-7' ); |
| 250 | |
| 251 | $desc_link = wpcf7_link( __( 'https://contactform7.com/acceptance-checkbox/', 'contact-form-7' ), __( 'Acceptance checkbox', 'contact-form-7' ) ); |
| 252 | |
| 253 | ?> |
| 254 | <div class="control-box"> |
| 255 | <fieldset> |
| 256 | <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> |
| 257 | |
| 258 | <table class="form-table"> |
| 259 | <tbody> |
| 260 | <tr> |
| 261 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> |
| 262 | <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> |
| 263 | </tr> |
| 264 | |
| 265 | <tr> |
| 266 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-content' ); ?>"><?php echo esc_html( __( 'Condition', 'contact-form-7' ) ); ?></label></th> |
| 267 | <td><input type="text" name="content" class="oneline large-text" id="<?php echo esc_attr( $args['content'] . '-content' ); ?>" /></td> |
| 268 | </tr> |
| 269 | |
| 270 | <tr> |
| 271 | <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th> |
| 272 | <td> |
| 273 | <fieldset> |
| 274 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend> |
| 275 | <label><input type="checkbox" name="optional" class="option" checked="checked" /> <?php echo esc_html( __( 'Make this checkbox optional', 'contact-form-7' ) ); ?></label> |
| 276 | </fieldset> |
| 277 | </td> |
| 278 | </tr> |
| 279 | |
| 280 | <tr> |
| 281 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 282 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> |
| 283 | </tr> |
| 284 | |
| 285 | <tr> |
| 286 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 287 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> |
| 288 | </tr> |
| 289 | |
| 290 | </tbody> |
| 291 | </table> |
| 292 | </fieldset> |
| 293 | </div> |
| 294 | |
| 295 | <div class="insert-box"> |
| 296 | <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> |
| 297 | |
| 298 | <div class="submitbox"> |
| 299 | <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> |
| 300 | </div> |
| 301 | </div> |
| 302 | <?php |
| 303 | } |
| 304 |