
<?php
/***************************************** for complex fields like address and name *************************************/

if ( in_array( $field->type, $complex_fields ) ) {


	$is_complex_field = false;
	$contains_error = false;

	//check if error is present
	$all_divs = $dom->getElementsByTagName( 'div' );
	foreach ( $all_divs as $current_div ) {
		$current_div_classes = $current_div->getAttribute( 'class' );
		if ( strpos( $current_div_classes, 'validation_message' ) !== false ) {
			$contains_error = true;
			$current_div->setAttribute( 'class', $current_div->getAttribute( 'class' ) . ' invalid-feedback' );
		}

		if ( strpos( $current_div_classes, 'ginput_complex' ) !== false ) {
			$is_complex_field = true;
		}
	}
	// if its complex field only then process it.. ( added because of confirm email )
	if ( $is_complex_field  ) {
		$get_spans = $dom->getElementsByTagName( 'span' );

		foreach ( $get_spans as $span ) {
			$span_classes = $span->getAttribute( 'class' );
			//only modify fields if first, middle , last name or address field except for country selection
			if ( strpos( $span_classes, 'name_' ) !== false || strpos( $span_classes, 'address_' ) !== false  ) {
				if ( strpos( $span_classes, 'address_country' ) === false ) {
					$span->setAttribute('class', $span_classes.' form-group');
					$inputs = $span->getElementsByTagName( 'input' );
					foreach ($inputs as $input){
						$input->setAttribute( 'class', $input->getAttribute( 'class' ).' form-control' );
						if( $contains_error ){
							$input->setAttribute( 'class', $input->getAttribute( 'class' ).' is-invalid' );
						}
					}
				}

			}
		}
	}


	$content = utf8_decode($dom->saveHTML($dom->documentElement));
}
