akismet
1 year ago
constant-contact
1 year ago
recaptcha
1 year ago
sendinblue
1 year ago
stripe
1 year ago
acceptance.php
1 year ago
checkbox.php
1 year ago
count.php
1 year ago
date.php
1 year ago
disallowed-list.php
1 year ago
doi-helper.php
4 years ago
file.php
1 year ago
flamingo.php
1 year ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
1 year ago
quiz.php
1 year ago
really-simple-captcha.php
1 year ago
reflection.php
3 years ago
response.php
6 years ago
select.php
1 year ago
submit.php
1 year ago
text.php
1 year ago
textarea.php
1 year ago
acceptance.php
339 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 | 'selectable-values' => true, |
| 16 | ) |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | function wpcf7_acceptance_form_tag_handler( $tag ) { |
| 21 | if ( empty( $tag->name ) ) { |
| 22 | return ''; |
| 23 | } |
| 24 | |
| 25 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 26 | |
| 27 | $class = wpcf7_form_controls_class( $tag->type ); |
| 28 | |
| 29 | if ( $validation_error ) { |
| 30 | $class .= ' wpcf7-not-valid'; |
| 31 | } |
| 32 | |
| 33 | if ( $tag->has_option( 'invert' ) ) { |
| 34 | $class .= ' invert'; |
| 35 | } |
| 36 | |
| 37 | if ( $tag->has_option( 'optional' ) ) { |
| 38 | $class .= ' optional'; |
| 39 | } |
| 40 | |
| 41 | $atts = array( |
| 42 | 'class' => trim( $class ), |
| 43 | ); |
| 44 | |
| 45 | $item_atts = array( |
| 46 | 'type' => 'checkbox', |
| 47 | 'name' => $tag->name, |
| 48 | 'value' => '1', |
| 49 | 'tabindex' => $tag->get_option( 'tabindex', 'signed_int', true ), |
| 50 | 'checked' => $tag->has_option( 'default:on' ), |
| 51 | 'class' => $tag->get_class_option() ? $tag->get_class_option() : null, |
| 52 | 'id' => $tag->get_id_option(), |
| 53 | ); |
| 54 | |
| 55 | if ( $validation_error ) { |
| 56 | $item_atts['aria-invalid'] = 'true'; |
| 57 | $item_atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 58 | $tag->name |
| 59 | ); |
| 60 | } else { |
| 61 | $item_atts['aria-invalid'] = 'false'; |
| 62 | } |
| 63 | |
| 64 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 65 | |
| 66 | $content = empty( $tag->content ) |
| 67 | ? (string) reset( $tag->values ) |
| 68 | : $tag->content; |
| 69 | |
| 70 | $content = trim( $content ); |
| 71 | |
| 72 | if ( $content ) { |
| 73 | if ( $tag->has_option( 'label_first' ) ) { |
| 74 | $html = sprintf( |
| 75 | '<span class="wpcf7-list-item-label">%2$s</span><input %1$s />', |
| 76 | $item_atts, |
| 77 | $content |
| 78 | ); |
| 79 | } else { |
| 80 | $html = sprintf( |
| 81 | '<input %1$s /><span class="wpcf7-list-item-label">%2$s</span>', |
| 82 | $item_atts, |
| 83 | $content |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | $html = sprintf( |
| 88 | '<span class="wpcf7-list-item"><label>%s</label></span>', |
| 89 | $html |
| 90 | ); |
| 91 | |
| 92 | } else { |
| 93 | $html = sprintf( |
| 94 | '<span class="wpcf7-list-item"><input %1$s /></span>', |
| 95 | $item_atts |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | $html = sprintf( |
| 100 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><span %2$s>%3$s</span>%4$s</span>', |
| 101 | esc_attr( $tag->name ), |
| 102 | wpcf7_format_atts( $atts ), |
| 103 | $html, |
| 104 | $validation_error |
| 105 | ); |
| 106 | |
| 107 | return $html; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* Validation filter */ |
| 112 | |
| 113 | add_filter( 'wpcf7_validate_acceptance', |
| 114 | 'wpcf7_acceptance_validation_filter', 10, 2 ); |
| 115 | |
| 116 | function wpcf7_acceptance_validation_filter( $result, $tag ) { |
| 117 | if ( ! wpcf7_acceptance_as_validation() ) { |
| 118 | return $result; |
| 119 | } |
| 120 | |
| 121 | if ( $tag->has_option( 'optional' ) ) { |
| 122 | return $result; |
| 123 | } |
| 124 | |
| 125 | $name = $tag->name; |
| 126 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 127 | |
| 128 | $invert = $tag->has_option( 'invert' ); |
| 129 | |
| 130 | if ( $invert and $value |
| 131 | or ! $invert and ! $value ) { |
| 132 | $result->invalidate( $tag, wpcf7_get_message( 'accept_terms' ) ); |
| 133 | } |
| 134 | |
| 135 | return $result; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* Acceptance filter */ |
| 140 | |
| 141 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter', 10, 2 ); |
| 142 | |
| 143 | function wpcf7_acceptance_filter( $accepted, $submission ) { |
| 144 | $tags = wpcf7_scan_form_tags( array( 'type' => 'acceptance' ) ); |
| 145 | |
| 146 | foreach ( $tags as $tag ) { |
| 147 | $name = $tag->name; |
| 148 | |
| 149 | if ( empty( $name ) ) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 154 | |
| 155 | $content = empty( $tag->content ) |
| 156 | ? (string) reset( $tag->values ) |
| 157 | : $tag->content; |
| 158 | |
| 159 | $content = trim( $content ); |
| 160 | |
| 161 | if ( $value and $content ) { |
| 162 | $submission->add_consent( $name, $content ); |
| 163 | } |
| 164 | |
| 165 | if ( $tag->has_option( 'optional' ) ) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | $invert = $tag->has_option( 'invert' ); |
| 170 | |
| 171 | if ( $invert and $value |
| 172 | or ! $invert and ! $value ) { |
| 173 | $accepted = false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return $accepted; |
| 178 | } |
| 179 | |
| 180 | add_filter( 'wpcf7_form_class_attr', |
| 181 | 'wpcf7_acceptance_form_class_attr', 10, 1 ); |
| 182 | |
| 183 | function wpcf7_acceptance_form_class_attr( $class_attr ) { |
| 184 | if ( wpcf7_acceptance_as_validation() ) { |
| 185 | return $class_attr . ' wpcf7-acceptance-as-validation'; |
| 186 | } |
| 187 | |
| 188 | return $class_attr; |
| 189 | } |
| 190 | |
| 191 | function wpcf7_acceptance_as_validation() { |
| 192 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return $contact_form->is_true( 'acceptance_as_validation' ); |
| 197 | } |
| 198 | |
| 199 | add_filter( 'wpcf7_mail_tag_replaced_acceptance', |
| 200 | 'wpcf7_acceptance_mail_tag', 10, 4 ); |
| 201 | |
| 202 | function wpcf7_acceptance_mail_tag( $replaced, $submitted, $html, $mail_tag ) { |
| 203 | $form_tag = $mail_tag->corresponding_form_tag(); |
| 204 | |
| 205 | if ( ! $form_tag ) { |
| 206 | return $replaced; |
| 207 | } |
| 208 | |
| 209 | if ( ! empty( $submitted ) ) { |
| 210 | $replaced = __( 'Consented', 'contact-form-7' ); |
| 211 | } else { |
| 212 | $replaced = __( 'Not consented', 'contact-form-7' ); |
| 213 | } |
| 214 | |
| 215 | $content = empty( $form_tag->content ) |
| 216 | ? (string) reset( $form_tag->values ) |
| 217 | : $form_tag->content; |
| 218 | |
| 219 | if ( ! $html ) { |
| 220 | $content = wp_strip_all_tags( $content ); |
| 221 | } |
| 222 | |
| 223 | $content = trim( $content ); |
| 224 | |
| 225 | if ( $content ) { |
| 226 | $replaced = sprintf( |
| 227 | /* translators: 1: 'Consented' or 'Not consented', 2: conditions */ |
| 228 | _x( '%1$s: %2$s', 'mail output for acceptance checkboxes', |
| 229 | 'contact-form-7' ), |
| 230 | $replaced, |
| 231 | $content |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | return $replaced; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* Tag generator */ |
| 240 | |
| 241 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_acceptance', 35, 0 ); |
| 242 | |
| 243 | function wpcf7_add_tag_generator_acceptance() { |
| 244 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 245 | |
| 246 | $tag_generator->add( 'acceptance', __( 'acceptance', 'contact-form-7' ), |
| 247 | 'wpcf7_tag_generator_acceptance', |
| 248 | array( 'version' => '2' ) |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | function wpcf7_tag_generator_acceptance( $contact_form, $options ) { |
| 253 | $field_types = array( |
| 254 | 'acceptance' => array( |
| 255 | 'display_name' => __( 'Acceptance checkbox', 'contact-form-7' ), |
| 256 | 'heading' => __( 'Acceptance checkbox form-tag generator', 'contact-form-7' ), |
| 257 | 'description' => __( 'Generates a form-tag for an <a href="https://contactform7.com/acceptance-checkbox/">acceptance checkbox</a>.', 'contact-form-7' ), |
| 258 | ), |
| 259 | ); |
| 260 | |
| 261 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 262 | |
| 263 | ?> |
| 264 | <header class="description-box"> |
| 265 | <h3><?php |
| 266 | echo esc_html( $field_types['acceptance']['heading'] ); |
| 267 | ?></h3> |
| 268 | |
| 269 | <p><?php |
| 270 | $description = wp_kses( |
| 271 | $field_types['acceptance']['description'], |
| 272 | array( |
| 273 | 'a' => array( 'href' => true ), |
| 274 | 'strong' => array(), |
| 275 | ), |
| 276 | array( 'http', 'https' ) |
| 277 | ); |
| 278 | |
| 279 | echo $description; |
| 280 | ?></p> |
| 281 | </header> |
| 282 | |
| 283 | <div class="control-box"> |
| 284 | <fieldset> |
| 285 | <legend id="<?php echo esc_attr( $tgg->ref( 'type-legend' ) ); ?>"><?php |
| 286 | echo esc_html( __( 'Field type', 'contact-form-7' ) ); |
| 287 | ?></legend> |
| 288 | |
| 289 | <select data-tag-part="basetype" aria-labelledby="<?php echo esc_attr( $tgg->ref( 'type-legend' ) ); ?>"><?php |
| 290 | echo sprintf( |
| 291 | '<option %1$s>%2$s</option>', |
| 292 | wpcf7_format_atts( array( |
| 293 | 'value' => 'acceptance', |
| 294 | ) ), |
| 295 | esc_html( $field_types['acceptance']['display_name'] ) |
| 296 | ); |
| 297 | ?></select> |
| 298 | <br /> |
| 299 | <label> |
| 300 | <input type="checkbox" data-tag-part="option" data-tag-option="optional" checked="checked" /> |
| 301 | <?php echo esc_html( __( "This checkbox is optional.", 'contact-form-7' ) ); ?> |
| 302 | </label> |
| 303 | </fieldset> |
| 304 | |
| 305 | <?php |
| 306 | $tgg->print( 'field_name' ); |
| 307 | |
| 308 | $tgg->print( 'class_attr' ); |
| 309 | ?> |
| 310 | |
| 311 | <fieldset> |
| 312 | <legend id="<?php echo esc_attr( $tgg->ref( 'value-legend' ) ); ?>"><?php |
| 313 | echo esc_html( __( 'Condition', 'contact-form-7' ) ); |
| 314 | ?></legend> |
| 315 | <?php |
| 316 | echo sprintf( |
| 317 | '<input %s />', |
| 318 | wpcf7_format_atts( array( |
| 319 | 'type' => 'text', |
| 320 | 'required' => true, |
| 321 | 'value' => __( 'Put the condition for consent here.', 'contact-form-7' ), |
| 322 | 'data-tag-part' => 'content', |
| 323 | 'aria-labelledby' => $tgg->ref( 'value-legend' ), |
| 324 | ) ) |
| 325 | ); |
| 326 | ?> |
| 327 | </fieldset> |
| 328 | </div> |
| 329 | |
| 330 | <footer class="insert-box"> |
| 331 | <?php |
| 332 | $tgg->print( 'insert_box_content' ); |
| 333 | |
| 334 | $tgg->print( 'mail_tag_tip' ); |
| 335 | ?> |
| 336 | </footer> |
| 337 | <?php |
| 338 | } |
| 339 |