acceptance.php
11 years ago
akismet.php
11 years ago
captcha.php
11 years ago
checkbox.php
11 years ago
date.php
11 years ago
file.php
11 years ago
flamingo.php
11 years ago
jetpack.php
12 years ago
listo.php
12 years ago
number.php
11 years ago
quiz.php
11 years ago
response.php
11 years ago
select.php
11 years ago
submit.php
11 years ago
text.php
11 years ago
textarea.php
11 years ago
acceptance.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [acceptance] |
| 4 | **/ |
| 5 | |
| 6 | /* Shortcode handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_shortcode_acceptance' ); |
| 9 | |
| 10 | function wpcf7_add_shortcode_acceptance() { |
| 11 | wpcf7_add_shortcode( 'acceptance', |
| 12 | 'wpcf7_acceptance_shortcode_handler', true ); |
| 13 | } |
| 14 | |
| 15 | function wpcf7_acceptance_shortcode_handler( $tag ) { |
| 16 | $tag = new WPCF7_Shortcode( $tag ); |
| 17 | |
| 18 | if ( empty( $tag->name ) ) |
| 19 | return ''; |
| 20 | |
| 21 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 22 | |
| 23 | $class = wpcf7_form_controls_class( $tag->type ); |
| 24 | |
| 25 | if ( $validation_error ) |
| 26 | $class .= ' wpcf7-not-valid'; |
| 27 | |
| 28 | if ( $tag->has_option( 'invert' ) ) |
| 29 | $class .= ' wpcf7-invert'; |
| 30 | |
| 31 | $atts = array(); |
| 32 | |
| 33 | $atts['class'] = $tag->get_class_option( $class ); |
| 34 | $atts['id'] = $tag->get_id_option(); |
| 35 | $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true ); |
| 36 | |
| 37 | if ( $tag->has_option( 'default:on' ) ) |
| 38 | $atts['checked'] = 'checked'; |
| 39 | |
| 40 | $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; |
| 41 | |
| 42 | $atts['type'] = 'checkbox'; |
| 43 | $atts['name'] = $tag->name; |
| 44 | $atts['value'] = '1'; |
| 45 | |
| 46 | $atts = wpcf7_format_atts( $atts ); |
| 47 | |
| 48 | $html = sprintf( |
| 49 | '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', |
| 50 | sanitize_html_class( $tag->name ), $atts, $validation_error ); |
| 51 | |
| 52 | return $html; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Validation filter */ |
| 57 | |
| 58 | add_filter( 'wpcf7_validate_acceptance', 'wpcf7_acceptance_validation_filter', 10, 2 ); |
| 59 | |
| 60 | function wpcf7_acceptance_validation_filter( $result, $tag ) { |
| 61 | if ( ! wpcf7_acceptance_as_validation() ) |
| 62 | return $result; |
| 63 | |
| 64 | $tag = new WPCF7_Shortcode( $tag ); |
| 65 | |
| 66 | $name = $tag->name; |
| 67 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 68 | |
| 69 | $invert = $tag->has_option( 'invert' ); |
| 70 | |
| 71 | if ( $invert && $value || ! $invert && ! $value ) { |
| 72 | $result['valid'] = false; |
| 73 | $result['reason'][$name] = wpcf7_get_message( 'accept_terms' ); |
| 74 | } |
| 75 | |
| 76 | if ( isset( $result['reason'][$name] ) && $id = $tag->get_id_option() ) { |
| 77 | $result['idref'][$name] = $id; |
| 78 | } |
| 79 | |
| 80 | return $result; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* Acceptance filter */ |
| 85 | |
| 86 | add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter' ); |
| 87 | |
| 88 | function wpcf7_acceptance_filter( $accepted ) { |
| 89 | if ( ! $accepted ) |
| 90 | return $accepted; |
| 91 | |
| 92 | $fes = wpcf7_scan_shortcode( array( 'type' => 'acceptance' ) ); |
| 93 | |
| 94 | foreach ( $fes as $fe ) { |
| 95 | $name = $fe['name']; |
| 96 | $options = (array) $fe['options']; |
| 97 | |
| 98 | if ( empty( $name ) ) |
| 99 | continue; |
| 100 | |
| 101 | $value = ( ! empty( $_POST[$name] ) ? 1 : 0 ); |
| 102 | |
| 103 | $invert = (bool) preg_grep( '%^invert$%', $options ); |
| 104 | |
| 105 | if ( $invert && $value || ! $invert && ! $value ) |
| 106 | $accepted = false; |
| 107 | } |
| 108 | |
| 109 | return $accepted; |
| 110 | } |
| 111 | |
| 112 | add_filter( 'wpcf7_form_class_attr', 'wpcf7_acceptance_form_class_attr' ); |
| 113 | |
| 114 | function wpcf7_acceptance_form_class_attr( $class ) { |
| 115 | if ( wpcf7_acceptance_as_validation() ) |
| 116 | return $class . ' wpcf7-acceptance-as-validation'; |
| 117 | |
| 118 | return $class; |
| 119 | } |
| 120 | |
| 121 | function wpcf7_acceptance_as_validation() { |
| 122 | if ( ! $contact_form = wpcf7_get_current_contact_form() ) |
| 123 | return false; |
| 124 | |
| 125 | return $contact_form->is_true( 'acceptance_as_validation' ); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* Tag generator */ |
| 130 | |
| 131 | add_action( 'admin_init', 'wpcf7_add_tag_generator_acceptance', 35 ); |
| 132 | |
| 133 | function wpcf7_add_tag_generator_acceptance() { |
| 134 | if ( ! function_exists( 'wpcf7_add_tag_generator' ) ) |
| 135 | return; |
| 136 | |
| 137 | wpcf7_add_tag_generator( 'acceptance', __( 'Acceptance', 'contact-form-7' ), |
| 138 | 'wpcf7-tg-pane-acceptance', 'wpcf7_tg_pane_acceptance' ); |
| 139 | } |
| 140 | |
| 141 | function wpcf7_tg_pane_acceptance( $contact_form ) { |
| 142 | ?> |
| 143 | <div id="wpcf7-tg-pane-acceptance" class="hidden"> |
| 144 | <form action=""> |
| 145 | <table> |
| 146 | <tr><td><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr> |
| 147 | </table> |
| 148 | |
| 149 | <table> |
| 150 | <tr> |
| 151 | <td><code>id</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br /> |
| 152 | <input type="text" name="id" class="idvalue oneline option" /></td> |
| 153 | |
| 154 | <td><code>class</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br /> |
| 155 | <input type="text" name="class" class="classvalue oneline option" /></td> |
| 156 | </tr> |
| 157 | |
| 158 | <tr> |
| 159 | <td colspan="2"> |
| 160 | <br /><input type="checkbox" name="default:on" class="option" /> <?php echo esc_html( __( "Make this checkbox checked by default?", 'contact-form-7' ) ); ?> |
| 161 | <br /><input type="checkbox" name="invert" class="option" /> <?php echo esc_html( __( "Make this checkbox work inversely?", 'contact-form-7' ) ); ?> |
| 162 | <br /><span style="font-size: smaller;"><?php echo esc_html( __( "* That means visitor who accepts the term unchecks it.", 'contact-form-7' ) ); ?></span> |
| 163 | </td> |
| 164 | </tr> |
| 165 | </table> |
| 166 | |
| 167 | <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'contact-form-7' ) ); ?><br /><input type="text" name="acceptance" class="tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div> |
| 168 | </form> |
| 169 | </div> |
| 170 | <?php |
| 171 | } |
| 172 | |
| 173 | ?> |