PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/modules/checkbox.php

https://gitlab.com/webkod3r/tripolis
PHP | 356 lines | 259 code | 90 blank | 7 comment | 50 complexity | a84bb6a56e59a7833de49967d34705fd MD5 | raw file
  1. <?php
  2. /**
  3. ** A base module for [checkbox], [checkbox*], and [radio]
  4. **/
  5. /* Shortcode handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_shortcode_checkbox' );
  7. function wpcf7_add_shortcode_checkbox() {
  8. wpcf7_add_shortcode( array( 'checkbox', 'checkbox*', 'radio' ),
  9. 'wpcf7_checkbox_shortcode_handler', true );
  10. }
  11. function wpcf7_checkbox_shortcode_handler( $tag ) {
  12. $tag = new WPCF7_Shortcode( $tag );
  13. if ( empty( $tag->name ) )
  14. return '';
  15. $validation_error = wpcf7_get_validation_error( $tag->name );
  16. $class = wpcf7_form_controls_class( $tag->type );
  17. if ( $validation_error )
  18. $class .= ' wpcf7-not-valid';
  19. $label_first = $tag->has_option( 'label_first' );
  20. $use_label_element = $tag->has_option( 'use_label_element' );
  21. $exclusive = $tag->has_option( 'exclusive' );
  22. $free_text = $tag->has_option( 'free_text' );
  23. $multiple = false;
  24. if ( 'checkbox' == $tag->basetype )
  25. $multiple = ! $exclusive;
  26. else // radio
  27. $exclusive = false;
  28. if ( $exclusive )
  29. $class .= ' wpcf7-exclusive-checkbox';
  30. $atts = array();
  31. $atts['class'] = $tag->get_class_option( $class );
  32. $atts['id'] = $tag->get_id_option();
  33. $tabindex = $tag->get_option( 'tabindex', 'int', true );
  34. if ( false !== $tabindex )
  35. $tabindex = absint( $tabindex );
  36. $html = '';
  37. $count = 0;
  38. $values = (array) $tag->values;
  39. $labels = (array) $tag->labels;
  40. if ( $data = (array) $tag->get_data_option() ) {
  41. if ( $free_text ) {
  42. $values = array_merge(
  43. array_slice( $values, 0, -1 ),
  44. array_values( $data ),
  45. array_slice( $values, -1 ) );
  46. $labels = array_merge(
  47. array_slice( $labels, 0, -1 ),
  48. array_values( $data ),
  49. array_slice( $labels, -1 ) );
  50. } else {
  51. $values = array_merge( $values, array_values( $data ) );
  52. $labels = array_merge( $labels, array_values( $data ) );
  53. }
  54. }
  55. $defaults = array();
  56. $default_choice = $tag->get_default_option( null, 'multiple=1' );
  57. foreach ( $default_choice as $value ) {
  58. $key = array_search( $value, $values, true );
  59. if ( false !== $key ) {
  60. $defaults[] = (int) $key + 1;
  61. }
  62. }
  63. if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) {
  64. $defaults = array_merge( $defaults, explode( '_', $matches[1] ) );
  65. }
  66. $defaults = array_unique( $defaults );
  67. $hangover = wpcf7_get_hangover( $tag->name, $multiple ? array() : '' );
  68. foreach ( $values as $key => $value ) {
  69. $class = 'wpcf7-list-item';
  70. $checked = false;
  71. if ( $hangover ) {
  72. if ( $multiple ) {
  73. $checked = in_array( esc_sql( $value ), (array) $hangover );
  74. } else {
  75. $checked = ( $hangover == esc_sql( $value ) );
  76. }
  77. } else {
  78. $checked = in_array( $key + 1, (array) $defaults );
  79. }
  80. if ( isset( $labels[$key] ) )
  81. $label = $labels[$key];
  82. else
  83. $label = $value;
  84. $item_atts = array(
  85. 'type' => $tag->basetype,
  86. 'name' => $tag->name . ( $multiple ? '[]' : '' ),
  87. 'value' => $value,
  88. 'checked' => $checked ? 'checked' : '',
  89. 'tabindex' => $tabindex ? $tabindex : '' );
  90. $item_atts = wpcf7_format_atts( $item_atts );
  91. if ( $label_first ) { // put label first, input last
  92. $item = sprintf(
  93. '<span class="wpcf7-list-item-label">%1$s</span>&nbsp;<input %2$s />',
  94. esc_html( $label ), $item_atts );
  95. } else {
  96. $item = sprintf(
  97. '<input %2$s />&nbsp;<span class="wpcf7-list-item-label">%1$s</span>',
  98. esc_html( $label ), $item_atts );
  99. }
  100. if ( $use_label_element )
  101. $item = '<label>' . $item . '</label>';
  102. if ( false !== $tabindex )
  103. $tabindex += 1;
  104. $count += 1;
  105. if ( 1 == $count ) {
  106. $class .= ' first';
  107. }
  108. if ( count( $values ) == $count ) { // last round
  109. $class .= ' last';
  110. if ( $free_text ) {
  111. $free_text_name = sprintf(
  112. '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name );
  113. $free_text_atts = array(
  114. 'name' => $free_text_name,
  115. 'class' => 'wpcf7-free-text',
  116. 'tabindex' => $tabindex ? $tabindex : '' );
  117. if ( wpcf7_is_posted() && isset( $_POST[$free_text_name] ) ) {
  118. $free_text_atts['value'] = wp_unslash(
  119. $_POST[$free_text_name] );
  120. }
  121. $free_text_atts = wpcf7_format_atts( $free_text_atts );
  122. $item .= sprintf( ' <input type="text" %s />', $free_text_atts );
  123. $class .= ' has-free-text';
  124. }
  125. }
  126. $item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>';
  127. $html .= $item;
  128. }
  129. $atts = wpcf7_format_atts( $atts );
  130. $html = sprintf(
  131. '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>',
  132. sanitize_html_class( $tag->name ), $atts, $html, $validation_error );
  133. return $html;
  134. }
  135. /* Validation filter */
  136. add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 );
  137. add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 );
  138. add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 );
  139. function wpcf7_checkbox_validation_filter( $result, $tag ) {
  140. $tag = new WPCF7_Shortcode( $tag );
  141. $type = $tag->type;
  142. $name = $tag->name;
  143. $value = isset( $_POST[$name] ) ? (array) $_POST[$name] : array();
  144. if ( $tag->is_required() && empty( $value ) ) {
  145. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  146. }
  147. return $result;
  148. }
  149. /* Adding free text field */
  150. add_filter( 'wpcf7_posted_data', 'wpcf7_checkbox_posted_data' );
  151. function wpcf7_checkbox_posted_data( $posted_data ) {
  152. $tags = wpcf7_scan_shortcode(
  153. array( 'type' => array( 'checkbox', 'checkbox*', 'radio' ) ) );
  154. if ( empty( $tags ) ) {
  155. return $posted_data;
  156. }
  157. foreach ( $tags as $tag ) {
  158. $tag = new WPCF7_Shortcode( $tag );
  159. if ( ! isset( $posted_data[$tag->name] ) ) {
  160. continue;
  161. }
  162. $posted_items = (array) $posted_data[$tag->name];
  163. if ( $tag->has_option( 'free_text' ) ) {
  164. if ( WPCF7_USE_PIPE ) {
  165. $values = $tag->pipes->collect_afters();
  166. } else {
  167. $values = $tag->values;
  168. }
  169. $last = array_pop( $values );
  170. $last = html_entity_decode( $last, ENT_QUOTES, 'UTF-8' );
  171. if ( in_array( $last, $posted_items ) ) {
  172. $posted_items = array_diff( $posted_items, array( $last ) );
  173. $free_text_name = sprintf(
  174. '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name );
  175. $free_text = $posted_data[$free_text_name];
  176. if ( ! empty( $free_text ) ) {
  177. $posted_items[] = trim( $last . ' ' . $free_text );
  178. } else {
  179. $posted_items[] = $last;
  180. }
  181. }
  182. }
  183. $posted_data[$tag->name] = $posted_items;
  184. }
  185. return $posted_data;
  186. }
  187. /* Tag generator */
  188. add_action( 'wpcf7_admin_init',
  189. 'wpcf7_add_tag_generator_checkbox_and_radio', 30 );
  190. function wpcf7_add_tag_generator_checkbox_and_radio() {
  191. $tag_generator = WPCF7_TagGenerator::get_instance();
  192. $tag_generator->add( 'checkbox', __( 'checkboxes', 'contact-form-7' ),
  193. 'wpcf7_tag_generator_checkbox' );
  194. $tag_generator->add( 'radio', __( 'radio buttons', 'contact-form-7' ),
  195. 'wpcf7_tag_generator_checkbox' );
  196. }
  197. function wpcf7_tag_generator_checkbox( $contact_form, $args = '' ) {
  198. $args = wp_parse_args( $args, array() );
  199. $type = $args['id'];
  200. if ( 'radio' != $type ) {
  201. $type = 'checkbox';
  202. }
  203. if ( 'checkbox' == $type ) {
  204. $description = __( "Generate a form-tag for a group of checkboxes. For more details, see %s.", 'contact-form-7' );
  205. } elseif ( 'radio' == $type ) {
  206. $description = __( "Generate a form-tag for a group of radio buttons. For more details, see %s.", 'contact-form-7' );
  207. }
  208. $desc_link = wpcf7_link( __( 'http://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, Radio Buttons and Menus', 'contact-form-7' ) );
  209. ?>
  210. <div class="control-box">
  211. <fieldset>
  212. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  213. <table class="form-table">
  214. <tbody>
  215. <?php if ( 'checkbox' == $type ) : ?>
  216. <tr>
  217. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  218. <td>
  219. <fieldset>
  220. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  221. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  222. </fieldset>
  223. </td>
  224. </tr>
  225. <?php endif; ?>
  226. <tr>
  227. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  228. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  229. </tr>
  230. <tr>
  231. <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
  232. <td>
  233. <fieldset>
  234. <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
  235. <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea>
  236. <label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><span class="description"><?php echo esc_html( __( "One option per line.", 'contact-form-7' ) ); ?></span></label><br />
  237. <label><input type="checkbox" name="label_first" class="option" /> <?php echo esc_html( __( 'Put a label first, a checkbox last', 'contact-form-7' ) ); ?></label><br />
  238. <label><input type="checkbox" name="use_label_element" class="option" /> <?php echo esc_html( __( 'Wrap each item with label element', 'contact-form-7' ) ); ?></label>
  239. <?php if ( 'checkbox' == $type ) : ?>
  240. <br /><label><input type="checkbox" name="exclusive" class="option" /> <?php echo esc_html( __( 'Make checkboxes exclusive', 'contact-form-7' ) ); ?></label>
  241. <?php endif; ?>
  242. </fieldset>
  243. </td>
  244. </tr>
  245. <tr>
  246. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  247. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  248. </tr>
  249. <tr>
  250. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  251. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  252. </tr>
  253. </tbody>
  254. </table>
  255. </fieldset>
  256. </div>
  257. <div class="insert-box">
  258. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  259. <div class="submitbox">
  260. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  261. </div>
  262. <br class="clear" />
  263. <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To use the value input through this field in a mail field, you need to insert the corresponding mail-tag (%s) into the field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p>
  264. </div>
  265. <?php
  266. }