acceptance.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. ** A base module for [acceptance]
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_acceptance', 10, 0 );
  7. function wpcf7_add_form_tag_acceptance() {
  8. wpcf7_add_form_tag( 'acceptance',
  9. 'wpcf7_acceptance_form_tag_handler',
  10. array(
  11. 'name-attr' => true,
  12. )
  13. );
  14. }
  15. function wpcf7_acceptance_form_tag_handler( $tag ) {
  16. if ( empty( $tag->name ) ) {
  17. return '';
  18. }
  19. $validation_error = wpcf7_get_validation_error( $tag->name );
  20. $class = wpcf7_form_controls_class( $tag->type );
  21. if ( $validation_error ) {
  22. $class .= ' wpcf7-not-valid';
  23. }
  24. if ( $tag->has_option( 'invert' ) ) {
  25. $class .= ' invert';
  26. }
  27. if ( $tag->has_option( 'optional' ) ) {
  28. $class .= ' optional';
  29. }
  30. $atts = array(
  31. 'class' => trim( $class ),
  32. );
  33. $item_atts = array();
  34. $item_atts['type'] = 'checkbox';
  35. $item_atts['name'] = $tag->name;
  36. $item_atts['value'] = '1';
  37. $item_atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  38. $item_atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  39. if ( $tag->has_option( 'default:on' ) ) {
  40. $item_atts['checked'] = 'checked';
  41. }
  42. $item_atts['class'] = $tag->get_class_option();
  43. $item_atts['id'] = $tag->get_id_option();
  44. $item_atts = wpcf7_format_atts( $item_atts );
  45. $content = empty( $tag->content )
  46. ? (string) reset( $tag->values )
  47. : $tag->content;
  48. $content = trim( $content );
  49. if ( $content ) {
  50. if ( $tag->has_option( 'label_first' ) ) {
  51. $html = sprintf(
  52. '<span class="wpcf7-list-item-label">%2$s</span><input %1$s />',
  53. $item_atts, $content );
  54. } else {
  55. $html = sprintf(
  56. '<input %1$s /><span class="wpcf7-list-item-label">%2$s</span>',
  57. $item_atts, $content );
  58. }
  59. $html = sprintf(
  60. '<span class="wpcf7-list-item"><label>%s</label></span>',
  61. $html
  62. );
  63. } else {
  64. $html = sprintf(
  65. '<span class="wpcf7-list-item"><input %1$s /></span>',
  66. $item_atts );
  67. }
  68. $atts = wpcf7_format_atts( $atts );
  69. $html = sprintf(
  70. '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>',
  71. sanitize_html_class( $tag->name ), $atts, $html, $validation_error );
  72. return $html;
  73. }
  74. /* Validation filter */
  75. add_filter( 'wpcf7_validate_acceptance',
  76. 'wpcf7_acceptance_validation_filter', 10, 2 );
  77. function wpcf7_acceptance_validation_filter( $result, $tag ) {
  78. if ( ! wpcf7_acceptance_as_validation() ) {
  79. return $result;
  80. }
  81. if ( $tag->has_option( 'optional' ) ) {
  82. return $result;
  83. }
  84. $name = $tag->name;
  85. $value = ( ! empty( $_POST[$name] ) ? 1 : 0 );
  86. $invert = $tag->has_option( 'invert' );
  87. if ( $invert and $value
  88. or ! $invert and ! $value ) {
  89. $result->invalidate( $tag, wpcf7_get_message( 'accept_terms' ) );
  90. }
  91. return $result;
  92. }
  93. /* Acceptance filter */
  94. add_filter( 'wpcf7_acceptance', 'wpcf7_acceptance_filter', 10, 2 );
  95. function wpcf7_acceptance_filter( $accepted, $submission ) {
  96. $tags = wpcf7_scan_form_tags( array( 'type' => 'acceptance' ) );
  97. foreach ( $tags as $tag ) {
  98. $name = $tag->name;
  99. if ( empty( $name ) ) {
  100. continue;
  101. }
  102. $value = ( ! empty( $_POST[$name] ) ? 1 : 0 );
  103. $content = empty( $tag->content )
  104. ? (string) reset( $tag->values )
  105. : $tag->content;
  106. $content = trim( $content );
  107. if ( $value and $content ) {
  108. $submission->add_consent( $name, $content );
  109. }
  110. if ( $tag->has_option( 'optional' ) ) {
  111. continue;
  112. }
  113. $invert = $tag->has_option( 'invert' );
  114. if ( $invert and $value
  115. or ! $invert and ! $value ) {
  116. $accepted = false;
  117. }
  118. }
  119. return $accepted;
  120. }
  121. add_filter( 'wpcf7_form_class_attr',
  122. 'wpcf7_acceptance_form_class_attr', 10, 1 );
  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. function wpcf7_acceptance_as_validation() {
  130. if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  131. return false;
  132. }
  133. return $contact_form->is_true( 'acceptance_as_validation' );
  134. }
  135. add_filter( 'wpcf7_mail_tag_replaced_acceptance',
  136. 'wpcf7_acceptance_mail_tag', 10, 4 );
  137. function wpcf7_acceptance_mail_tag( $replaced, $submitted, $html, $mail_tag ) {
  138. $form_tag = $mail_tag->corresponding_form_tag();
  139. if ( ! $form_tag ) {
  140. return $replaced;
  141. }
  142. if ( ! empty( $submitted ) ) {
  143. $replaced = __( 'Consented', 'contact-form-7' );
  144. } else {
  145. $replaced = __( 'Not consented', 'contact-form-7' );
  146. }
  147. $content = empty( $form_tag->content )
  148. ? (string) reset( $form_tag->values )
  149. : $form_tag->content;
  150. if ( ! $html ) {
  151. $content = wp_strip_all_tags( $content );
  152. }
  153. $content = trim( $content );
  154. if ( $content ) {
  155. $replaced = sprintf(
  156. /* translators: 1: 'Consented' or 'Not consented', 2: conditions */
  157. _x( '%1$s: %2$s', 'mail output for acceptance checkboxes',
  158. 'contact-form-7' ),
  159. $replaced,
  160. $content
  161. );
  162. }
  163. return $replaced;
  164. }
  165. /* Tag generator */
  166. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_acceptance', 35, 0 );
  167. function wpcf7_add_tag_generator_acceptance() {
  168. $tag_generator = WPCF7_TagGenerator::get_instance();
  169. $tag_generator->add( 'acceptance', __( 'acceptance', 'contact-form-7' ),
  170. 'wpcf7_tag_generator_acceptance' );
  171. }
  172. function wpcf7_tag_generator_acceptance( $contact_form, $args = '' ) {
  173. $args = wp_parse_args( $args, array() );
  174. $type = 'acceptance';
  175. $description = __( "Generate a form-tag for an acceptance checkbox. For more details, see %s.", 'contact-form-7' );
  176. $desc_link = wpcf7_link( __( 'https://contactform7.com/acceptance-checkbox/', 'contact-form-7' ), __( 'Acceptance Checkbox', 'contact-form-7' ) );
  177. ?>
  178. <div class="control-box">
  179. <fieldset>
  180. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  181. <table class="form-table">
  182. <tbody>
  183. <tr>
  184. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  185. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  186. </tr>
  187. <tr>
  188. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-content' ); ?>"><?php echo esc_html( __( 'Condition', 'contact-form-7' ) ); ?></label></th>
  189. <td><input type="text" name="content" class="oneline large-text" id="<?php echo esc_attr( $args['content'] . '-content' ); ?>" /></td>
  190. </tr>
  191. <tr>
  192. <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
  193. <td>
  194. <fieldset>
  195. <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
  196. <label><input type="checkbox" name="optional" class="option" checked="checked" /> <?php echo esc_html( __( 'Make this checkbox optional', 'contact-form-7' ) ); ?></label>
  197. </fieldset>
  198. </td>
  199. </tr>
  200. <tr>
  201. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  202. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  203. </tr>
  204. <tr>
  205. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  206. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  207. </tr>
  208. </tbody>
  209. </table>
  210. </fieldset>
  211. </div>
  212. <div class="insert-box">
  213. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  214. <div class="submitbox">
  215. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  216. </div>
  217. </div>
  218. <?php
  219. }