PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/trenkwill/thema-design
PHP | 232 lines | 164 code | 62 blank | 6 comment | 25 complexity | e6f24db78e92bf060067c7f5758efb82 MD5 | raw file
  1. <?php
  2. /**
  3. ** A base module for [select] and [select*]
  4. **/
  5. /* Shortcode handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_shortcode_select' );
  7. function wpcf7_add_shortcode_select() {
  8. wpcf7_add_shortcode( array( 'select', 'select*' ),
  9. 'wpcf7_select_shortcode_handler', true );
  10. }
  11. function wpcf7_select_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. $atts = array();
  20. $atts['class'] = $tag->get_class_option( $class );
  21. $atts['id'] = $tag->get_id_option();
  22. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
  23. if ( $tag->is_required() )
  24. $atts['aria-required'] = 'true';
  25. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  26. $multiple = $tag->has_option( 'multiple' );
  27. $include_blank = $tag->has_option( 'include_blank' );
  28. $first_as_label = $tag->has_option( 'first_as_label' );
  29. $values = $tag->values;
  30. $labels = $tag->labels;
  31. if ( $data = (array) $tag->get_data_option() ) {
  32. $values = array_merge( $values, array_values( $data ) );
  33. $labels = array_merge( $labels, array_values( $data ) );
  34. }
  35. $defaults = array();
  36. $default_choice = $tag->get_default_option( null, 'multiple=1' );
  37. foreach ( $default_choice as $value ) {
  38. $key = array_search( $value, $values, true );
  39. if ( false !== $key ) {
  40. $defaults[] = (int) $key + 1;
  41. }
  42. }
  43. if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) {
  44. $defaults = array_merge( $defaults, explode( '_', $matches[1] ) );
  45. }
  46. $defaults = array_unique( $defaults );
  47. $shifted = false;
  48. if ( $include_blank || empty( $values ) ) {
  49. array_unshift( $labels, '---' );
  50. array_unshift( $values, '' );
  51. $shifted = true;
  52. } elseif ( $first_as_label ) {
  53. $values[0] = '';
  54. }
  55. $html = '';
  56. $hangover = wpcf7_get_hangover( $tag->name );
  57. foreach ( $values as $key => $value ) {
  58. $selected = false;
  59. if ( $hangover ) {
  60. if ( $multiple ) {
  61. $selected = in_array( esc_sql( $value ), (array) $hangover );
  62. } else {
  63. $selected = ( $hangover == esc_sql( $value ) );
  64. }
  65. } else {
  66. if ( ! $shifted && in_array( (int) $key + 1, (array) $defaults ) ) {
  67. $selected = true;
  68. } elseif ( $shifted && in_array( (int) $key, (array) $defaults ) ) {
  69. $selected = true;
  70. }
  71. }
  72. $item_atts = array(
  73. 'value' => $value,
  74. 'selected' => $selected ? 'selected' : '' );
  75. $item_atts = wpcf7_format_atts( $item_atts );
  76. $label = isset( $labels[$key] ) ? $labels[$key] : $value;
  77. $html .= sprintf( '<option %1$s>%2$s</option>',
  78. $item_atts, esc_html( $label ) );
  79. }
  80. if ( $multiple )
  81. $atts['multiple'] = 'multiple';
  82. $atts['name'] = $tag->name . ( $multiple ? '[]' : '' );
  83. $atts = wpcf7_format_atts( $atts );
  84. $html = sprintf(
  85. '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
  86. sanitize_html_class( $tag->name ), $atts, $html, $validation_error );
  87. return $html;
  88. }
  89. /* Validation filter */
  90. add_filter( 'wpcf7_validate_select', 'wpcf7_select_validation_filter', 10, 2 );
  91. add_filter( 'wpcf7_validate_select*', 'wpcf7_select_validation_filter', 10, 2 );
  92. function wpcf7_select_validation_filter( $result, $tag ) {
  93. $tag = new WPCF7_Shortcode( $tag );
  94. $name = $tag->name;
  95. if ( isset( $_POST[$name] ) && is_array( $_POST[$name] ) ) {
  96. foreach ( $_POST[$name] as $key => $value ) {
  97. if ( '' === $value )
  98. unset( $_POST[$name][$key] );
  99. }
  100. }
  101. $empty = ! isset( $_POST[$name] ) || empty( $_POST[$name] ) && '0' !== $_POST[$name];
  102. if ( $tag->is_required() && $empty ) {
  103. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  104. }
  105. return $result;
  106. }
  107. /* Tag generator */
  108. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_menu', 25 );
  109. function wpcf7_add_tag_generator_menu() {
  110. $tag_generator = WPCF7_TagGenerator::get_instance();
  111. $tag_generator->add( 'menu', __( 'drop-down menu', 'contact-form-7' ),
  112. 'wpcf7_tag_generator_menu' );
  113. }
  114. function wpcf7_tag_generator_menu( $contact_form, $args = '' ) {
  115. $args = wp_parse_args( $args, array() );
  116. $description = __( "Generate a form-tag for a drop-down menu. For more details, see %s.", 'contact-form-7' );
  117. $desc_link = wpcf7_link( __( 'http://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, Radio Buttons and Menus', 'contact-form-7' ) );
  118. ?>
  119. <div class="control-box">
  120. <fieldset>
  121. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  122. <table class="form-table">
  123. <tbody>
  124. <tr>
  125. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  126. <td>
  127. <fieldset>
  128. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  129. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  130. </fieldset>
  131. </td>
  132. </tr>
  133. <tr>
  134. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  135. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  136. </tr>
  137. <tr>
  138. <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
  139. <td>
  140. <fieldset>
  141. <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
  142. <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea>
  143. <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 />
  144. <label><input type="checkbox" name="multiple" class="option" /> <?php echo esc_html( __( 'Allow multiple selections', 'contact-form-7' ) ); ?></label><br />
  145. <label><input type="checkbox" name="include_blank" class="option" /> <?php echo esc_html( __( 'Insert a blank item as the first option', 'contact-form-7' ) ); ?></label>
  146. </fieldset>
  147. </td>
  148. </tr>
  149. <tr>
  150. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  151. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  152. </tr>
  153. <tr>
  154. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  155. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  156. </tr>
  157. </tbody>
  158. </table>
  159. </fieldset>
  160. </div>
  161. <div class="insert-box">
  162. <input type="text" name="select" class="tag code" readonly="readonly" onfocus="this.select()" />
  163. <div class="submitbox">
  164. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  165. </div>
  166. <br class="clear" />
  167. <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>
  168. </div>
  169. <?php
  170. }