1 | <?php
|
---|
2 | /**
|
---|
3 | ** A base module for [acceptance]
|
---|
4 | **/
|
---|
5 |
|
---|
6 | /* Shortcode handler */
|
---|
7 |
|
---|
8 | function wpcf7_acceptance_shortcode_handler( $tag ) {
|
---|
9 | global $wpcf7_contact_form;
|
---|
10 |
|
---|
11 | if ( ! is_array( $tag ) )
|
---|
12 | return '';
|
---|
13 |
|
---|
14 | $type = $tag['type'];
|
---|
15 | $name = $tag['name'];
|
---|
16 | $options = (array) $tag['options'];
|
---|
17 | $values = (array) $tag['values'];
|
---|
18 |
|
---|
19 | if ( empty( $name ) )
|
---|
20 | return '';
|
---|
21 |
|
---|
22 | $atts = '';
|
---|
23 | $id_att = '';
|
---|
24 | $class_att = '';
|
---|
25 |
|
---|
26 | $class_att .= ' wpcf7-acceptance';
|
---|
27 |
|
---|
28 | foreach ( $options as $option ) {
|
---|
29 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
30 | $id_att = $matches[1];
|
---|
31 |
|
---|
32 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
33 | $class_att .= ' ' . $matches[1];
|
---|
34 |
|
---|
35 | } elseif ( 'invert' == $option ) {
|
---|
36 | $class_att .= ' wpcf7-invert';
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | if ( $id_att )
|
---|
41 | $atts .= ' id="' . trim( $id_att ) . '"';
|
---|
42 |
|
---|
43 | if ( $class_att )
|
---|
44 | $atts .= ' class="' . trim( $class_att ) . '"';
|
---|
45 |
|
---|
46 | $default_on = (bool) preg_grep( '/^default:on$/i', $options );
|
---|
47 |
|
---|
48 | if ( WPCF7_LOAD_JS )
|
---|
49 | $onclick = ' onclick="wpcf7ToggleSubmit(this.form);"';
|
---|
50 |
|
---|
51 | $checked = $default_on ? ' checked="checked"' : '';
|
---|
52 |
|
---|
53 | $html = '<input type="checkbox" name="' . $name . '" value="1"' . $atts . $onclick . $checked . ' />';
|
---|
54 |
|
---|
55 | return $html;
|
---|
56 | }
|
---|
57 |
|
---|
58 | wpcf7_add_shortcode( 'acceptance', 'wpcf7_acceptance_shortcode_handler', true );
|
---|
59 |
|
---|
60 |
|
---|
61 | /* Acceptance filter */
|
---|
62 |
|
---|
63 | function wpcf7_acceptance_filter( $accepted ) {
|
---|
64 | global $wpcf7_contact_form;
|
---|
65 |
|
---|
66 | $fes = $wpcf7_contact_form->form_scan_shortcode( array( 'type' => 'acceptance' ) );
|
---|
67 |
|
---|
68 | foreach ( $fes as $fe ) {
|
---|
69 | $name = $fe['name'];
|
---|
70 | $options = (array) $fe['options'];
|
---|
71 |
|
---|
72 | if ( empty( $name ) )
|
---|
73 | continue;
|
---|
74 |
|
---|
75 | $value = $_POST[$name] ? 1 : 0;
|
---|
76 |
|
---|
77 | $invert = (bool) preg_grep( '%^invert$%', $options );
|
---|
78 |
|
---|
79 | if ( $invert && $value || ! $invert && ! $value )
|
---|
80 | $accepted = false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | return $accepted;
|
---|
84 | }
|
---|
85 |
|
---|
86 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter' );
|
---|
87 |
|
---|
88 | ?>
|
---|