acceptance.php
15 years ago
captcha.php
15 years ago
checkbox.php
15 years ago
file.php
15 years ago
icl.php
16 years ago
quiz.php
15 years ago
response.php
15 years ago
select.php
15 years ago
special-mail-tags.php
16 years ago
submit.php
15 years ago
text.php
15 years ago
textarea.php
15 years ago
acceptance.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [acceptance] |
| 4 | **/ |
| 5 | |
| 6 | /* Shortcode handler */ |
| 7 | |
| 8 | wpcf7_add_shortcode( 'acceptance', 'wpcf7_acceptance_shortcode_handler', true ); |
| 9 | |
| 10 | function wpcf7_acceptance_shortcode_handler( $tag ) { |
| 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 | $tabindex_att = ''; |
| 26 | |
| 27 | $class_att .= ' wpcf7-acceptance'; |
| 28 | |
| 29 | foreach ( $options as $option ) { |
| 30 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 31 | $id_att = $matches[1]; |
| 32 | |
| 33 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 34 | $class_att .= ' ' . $matches[1]; |
| 35 | |
| 36 | } elseif ( 'invert' == $option ) { |
| 37 | $class_att .= ' wpcf7-invert'; |
| 38 | |
| 39 | } elseif ( preg_match( '%^tabindex:(\d+)$%', $option, $matches ) ) { |
| 40 | $tabindex_att = (int) $matches[1]; |
| 41 | |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if ( $id_att ) |
| 46 | $atts .= ' id="' . trim( $id_att ) . '"'; |
| 47 | |
| 48 | if ( $class_att ) |
| 49 | $atts .= ' class="' . trim( $class_att ) . '"'; |
| 50 | |
| 51 | if ( '' !== $tabindex_att ) |
| 52 | $atts .= sprintf( ' tabindex="%d"', $tabindex_att ); |
| 53 | |
| 54 | $default_on = (bool) preg_grep( '/^default:on$/i', $options ); |
| 55 | |
| 56 | $checked = $default_on ? ' checked="checked"' : ''; |
| 57 | |
| 58 | $html = '<input type="checkbox" name="' . $name . '" value="1"' . $atts . $checked . ' />'; |
| 59 | |
| 60 | $validation_error = wpcf7_get_validation_error( $name ); |
| 61 | |
| 62 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>'; |
| 63 | |
| 64 | return $html; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* Validation filter */ |
| 69 | |
| 70 | add_filter( 'wpcf7_validate_acceptance', 'wpcf7_acceptance_validation_filter', 10, 2 ); |
| 71 | |
| 72 | function wpcf7_acceptance_validation_filter( $result, $tag ) { |
| 73 | if ( ! wpcf7_acceptance_as_validation() ) |
| 74 | return $result; |
| 75 | |
| 76 | $name = $tag['name']; |
| 77 | |
| 78 | if ( empty( $name ) ) |
| 79 | return $result; |
| 80 | |
| 81 | $options = (array) $tag['options']; |
| 82 | |
| 83 | $value = $_POST[$name] ? 1 : 0; |
| 84 | |
| 85 | $invert = (bool) preg_grep( '%^invert$%', $options ); |
| 86 | |
| 87 | if ( $invert && $value || ! $invert && ! $value ) { |
| 88 | $result['valid'] = false; |
| 89 | $result['reason'][$name] = wpcf7_get_message( 'accept_terms' ); |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* Acceptance filter */ |
| 97 | |
| 98 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter' ); |
| 99 | |
| 100 | function wpcf7_acceptance_filter( $accepted ) { |
| 101 | $fes = wpcf7_scan_shortcode( array( 'type' => 'acceptance' ) ); |
| 102 | |
| 103 | foreach ( $fes as $fe ) { |
| 104 | $name = $fe['name']; |
| 105 | $options = (array) $fe['options']; |
| 106 | |
| 107 | if ( empty( $name ) ) |
| 108 | continue; |
| 109 | |
| 110 | $value = $_POST[$name] ? 1 : 0; |
| 111 | |
| 112 | $invert = (bool) preg_grep( '%^invert$%', $options ); |
| 113 | |
| 114 | if ( $invert && $value || ! $invert && ! $value ) |
| 115 | $accepted = false; |
| 116 | } |
| 117 | |
| 118 | return $accepted; |
| 119 | } |
| 120 | |
| 121 | add_filter( 'wpcf7_form_class_attr', 'wpcf7_acceptance_form_class_attr' ); |
| 122 | |
| 123 | function wpcf7_acceptance_form_class_attr( $class ) { |
| 124 | if ( wpcf7_acceptance_as_validation() ) |
| 125 | return $class . ' wpcf7-acceptance-as-validation'; |
| 126 | |
| 127 | return $class; |
| 128 | } |
| 129 | |
| 130 | function wpcf7_acceptance_as_validation() { |
| 131 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 132 | return false; |
| 133 | |
| 134 | $settings = $contact_form->additional_setting( 'acceptance_as_validation', false ); |
| 135 | |
| 136 | foreach ( $settings as $setting ) { |
| 137 | if ( in_array( $setting, array( 'on', 'true', '1' ) ) ) |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /* Tag generator */ |
| 146 | |
| 147 | add_action( 'admin_init', 'wpcf7_add_tag_generator_acceptance', 35 ); |
| 148 | |
| 149 | function wpcf7_add_tag_generator_acceptance() { |
| 150 | wpcf7_add_tag_generator( 'acceptance', __( 'Acceptance', 'wpcf7' ), |
| 151 | 'wpcf7-tg-pane-acceptance', 'wpcf7_tg_pane_acceptance' ); |
| 152 | } |
| 153 | |
| 154 | function wpcf7_tg_pane_acceptance( &$contact_form ) { |
| 155 | ?> |
| 156 | <div id="wpcf7-tg-pane-acceptance" class="hidden"> |
| 157 | <form action=""> |
| 158 | <table> |
| 159 | <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr> |
| 160 | </table> |
| 161 | |
| 162 | <table> |
| 163 | <tr> |
| 164 | <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 165 | <input type="text" name="id" class="idvalue oneline option" /></td> |
| 166 | |
| 167 | <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 168 | <input type="text" name="class" class="classvalue oneline option" /></td> |
| 169 | </tr> |
| 170 | |
| 171 | <tr> |
| 172 | <td colspan="2"> |
| 173 | <br /><input type="checkbox" name="default:on" class="option" /> <?php echo esc_html( __( "Make this checkbox checked by default?", 'wpcf7' ) ); ?> |
| 174 | <br /><input type="checkbox" name="invert" class="option" /> <?php echo esc_html( __( "Make this checkbox work inversely?", 'wpcf7' ) ); ?> |
| 175 | <br /><span style="font-size: smaller;"><?php echo esc_html( __( "* That means visitor who accepts the term unchecks it.", 'wpcf7' ) ); ?></span> |
| 176 | </td> |
| 177 | </tr> |
| 178 | </table> |
| 179 | |
| 180 | <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="acceptance" class="tag" readonly="readonly" onfocus="this.select()" /></div> |
| 181 | </form> |
| 182 | </div> |
| 183 | <?php |
| 184 | } |
| 185 | |
| 186 | ?> |