akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
9 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
9 months ago
disallowed-list.php
9 months ago
doi-helper.php
4 years ago
file.php
11 months ago
flamingo.php
9 months ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
9 months ago
quiz.php
11 months ago
really-simple-captcha.php
9 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
9 months ago
submit.php
11 months ago
text.php
9 months ago
textarea.php
11 months ago
acceptance.php
347 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( |
| 114 | 'wpcf7_validate_acceptance', |
| 115 | 'wpcf7_acceptance_validation_filter', |
| 116 | 10, 2 |
| 117 | ); |
| 118 | |
| 119 | function wpcf7_acceptance_validation_filter( $result, $tag ) { |
| 120 | if ( ! wpcf7_acceptance_as_validation() ) { |
| 121 | return $result; |
| 122 | } |
| 123 | |
| 124 | if ( $tag->has_option( 'optional' ) ) { |
| 125 | return $result; |
| 126 | } |
| 127 | |
| 128 | $value = wpcf7_superglobal_post( $tag->name ) ? 1 : 0; |
| 129 | |
| 130 | $invert = $tag->has_option( 'invert' ); |
| 131 | |
| 132 | if ( |
| 133 | $invert and $value or |
| 134 | ! $invert and ! $value |
| 135 | ) { |
| 136 | $result->invalidate( $tag, wpcf7_get_message( 'accept_terms' ) ); |
| 137 | } |
| 138 | |
| 139 | return $result; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* Acceptance filter */ |
| 144 | |
| 145 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter', 10, 2 ); |
| 146 | |
| 147 | function wpcf7_acceptance_filter( $accepted, $submission ) { |
| 148 | $tags = wpcf7_scan_form_tags( array( 'type' => 'acceptance' ) ); |
| 149 | |
| 150 | foreach ( $tags as $tag ) { |
| 151 | if ( empty( $tag->name ) ) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | $value = wpcf7_superglobal_post( $tag->name ) ? 1 : 0; |
| 156 | |
| 157 | $content = empty( $tag->content ) |
| 158 | ? (string) reset( $tag->values ) |
| 159 | : $tag->content; |
| 160 | |
| 161 | $content = trim( $content ); |
| 162 | |
| 163 | if ( $value and $content ) { |
| 164 | $submission->add_consent( $tag->name, $content ); |
| 165 | } |
| 166 | |
| 167 | if ( $tag->has_option( 'optional' ) ) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | $invert = $tag->has_option( 'invert' ); |
| 172 | |
| 173 | if ( |
| 174 | $invert and $value or |
| 175 | ! $invert and ! $value |
| 176 | ) { |
| 177 | $accepted = false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $accepted; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | add_filter( |
| 186 | 'wpcf7_form_class_attr', |
| 187 | 'wpcf7_acceptance_form_class_attr', |
| 188 | 10, 1 |
| 189 | ); |
| 190 | |
| 191 | function wpcf7_acceptance_form_class_attr( $class_attr ) { |
| 192 | if ( wpcf7_acceptance_as_validation() ) { |
| 193 | return $class_attr . ' wpcf7-acceptance-as-validation'; |
| 194 | } |
| 195 | |
| 196 | return $class_attr; |
| 197 | } |
| 198 | |
| 199 | function wpcf7_acceptance_as_validation() { |
| 200 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return $contact_form->is_true( 'acceptance_as_validation' ); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | add_filter( |
| 209 | 'wpcf7_mail_tag_replaced_acceptance', |
| 210 | 'wpcf7_acceptance_mail_tag', |
| 211 | 10, 4 |
| 212 | ); |
| 213 | |
| 214 | function wpcf7_acceptance_mail_tag( $replaced, $submitted, $html, $mail_tag ) { |
| 215 | $form_tag = $mail_tag->corresponding_form_tag(); |
| 216 | |
| 217 | if ( ! $form_tag ) { |
| 218 | return $replaced; |
| 219 | } |
| 220 | |
| 221 | if ( ! empty( $submitted ) ) { |
| 222 | $replaced = __( 'Consented', 'contact-form-7' ); |
| 223 | } else { |
| 224 | $replaced = __( 'Not consented', 'contact-form-7' ); |
| 225 | } |
| 226 | |
| 227 | $content = empty( $form_tag->content ) |
| 228 | ? (string) reset( $form_tag->values ) |
| 229 | : $form_tag->content; |
| 230 | |
| 231 | if ( ! $html ) { |
| 232 | $content = wp_strip_all_tags( $content ); |
| 233 | } |
| 234 | |
| 235 | $content = trim( $content ); |
| 236 | |
| 237 | if ( $content ) { |
| 238 | $replaced = sprintf( |
| 239 | /* translators: 1: 'Consented' or 'Not consented', 2: conditions */ |
| 240 | _x( '%1$s: %2$s', 'mail output for acceptance checkboxes', 'contact-form-7' ), |
| 241 | $replaced, |
| 242 | $content |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | return $replaced; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /* Tag generator */ |
| 251 | |
| 252 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_acceptance', 35, 0 ); |
| 253 | |
| 254 | function wpcf7_add_tag_generator_acceptance() { |
| 255 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 256 | |
| 257 | $tag_generator->add( 'acceptance', __( 'acceptance', 'contact-form-7' ), |
| 258 | 'wpcf7_tag_generator_acceptance', |
| 259 | array( 'version' => '2' ) |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | function wpcf7_tag_generator_acceptance( $contact_form, $options ) { |
| 264 | $field_types = array( |
| 265 | 'acceptance' => array( |
| 266 | 'display_name' => __( 'Acceptance checkbox', 'contact-form-7' ), |
| 267 | 'heading' => __( 'Acceptance checkbox form-tag generator', 'contact-form-7' ), |
| 268 | 'description' => __( 'Generates a form-tag for an <a href="https://contactform7.com/acceptance-checkbox/">acceptance checkbox</a>.', 'contact-form-7' ), |
| 269 | ), |
| 270 | ); |
| 271 | |
| 272 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 273 | |
| 274 | $formatter = new WPCF7_HTMLFormatter(); |
| 275 | |
| 276 | $formatter->append_start_tag( 'header', array( |
| 277 | 'class' => 'description-box', |
| 278 | ) ); |
| 279 | |
| 280 | $formatter->append_start_tag( 'h3' ); |
| 281 | |
| 282 | $formatter->append_preformatted( |
| 283 | esc_html( $field_types['acceptance']['heading'] ) |
| 284 | ); |
| 285 | |
| 286 | $formatter->end_tag( 'h3' ); |
| 287 | |
| 288 | $formatter->append_start_tag( 'p' ); |
| 289 | |
| 290 | $formatter->append_preformatted( |
| 291 | wp_kses_data( $field_types['acceptance']['description'] ) |
| 292 | ); |
| 293 | |
| 294 | $formatter->end_tag( 'header' ); |
| 295 | |
| 296 | $formatter->append_start_tag( 'div', array( |
| 297 | 'class' => 'control-box', |
| 298 | ) ); |
| 299 | |
| 300 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 301 | $tgg->print( 'field_type', array( |
| 302 | 'with_optional' => true, |
| 303 | 'select_options' => array( |
| 304 | 'acceptance' => $field_types['acceptance']['display_name'], |
| 305 | ), |
| 306 | ) ); |
| 307 | |
| 308 | $tgg->print( 'field_name' ); |
| 309 | |
| 310 | $tgg->print( 'class_attr' ); |
| 311 | } ); |
| 312 | |
| 313 | $formatter->append_start_tag( 'fieldset' ); |
| 314 | |
| 315 | $formatter->append_start_tag( 'legend', array( |
| 316 | 'id' => $tgg->ref( 'value-legend' ), |
| 317 | ) ); |
| 318 | |
| 319 | $formatter->append_preformatted( |
| 320 | esc_html( __( 'Condition', 'contact-form-7' ) ) |
| 321 | ); |
| 322 | |
| 323 | $formatter->end_tag( 'legend' ); |
| 324 | |
| 325 | $formatter->append_start_tag( 'input', array( |
| 326 | 'type' => 'text', |
| 327 | 'required' => true, |
| 328 | 'value' => __( 'Put the condition for consent here.', 'contact-form-7' ), |
| 329 | 'data-tag-part' => 'content', |
| 330 | 'aria-labelledby' => $tgg->ref( 'value-legend' ), |
| 331 | ) ); |
| 332 | |
| 333 | $formatter->end_tag( 'div' ); |
| 334 | |
| 335 | $formatter->append_start_tag( 'footer', array( |
| 336 | 'class' => 'insert-box', |
| 337 | ) ); |
| 338 | |
| 339 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 340 | $tgg->print( 'insert_box_content' ); |
| 341 | |
| 342 | $tgg->print( 'mail_tag_tip' ); |
| 343 | } ); |
| 344 | |
| 345 | $formatter->print(); |
| 346 | } |
| 347 |