<?php
/***************************************** for input fields like text, numbers, email and website **************************************/

if ( in_array( $field->type, $input_fields ) ) {
	
	$input_classes_types = array(  'ginput_container_text', 'ginput_container_phone', 'ginput_container_email', 'ginput_container_website', 'ginput_container_post_title', 'ginput_container_number', 'ginput_container_name', 'ginput_container_product_price','ginput_container_post_tags' );
	$is_complex_field = false;
	$contains_error = false;
	//$contains_placeholder = false;
	$input_id = '';
	$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 not a complex field then do the modifications
	if ( ! $is_complex_field ) {
		//get the first div
		foreach ( $all_divs as $div ) {
			$div_classes = $div->getAttribute( 'class' );

			$div_classes_array = explode( ' ', $div_classes );

			//var_dump($div_classes_array);

			$is_valid_input_field = false;

			foreach ( $div_classes_array as $div_class_value ) {
				if ( in_array( $div_class_value, $input_classes_types ) ) {
					$is_valid_input_field = true;
					//if is valid input and contains error
				}
			}
			// var_dump($is_valid_input_field);
			if ( $is_valid_input_field ) {


				//add class to input
				$inputs = $dom->getElementsByTagName( 'input' );
				foreach ( $inputs as $input ) {
					$id = $input->getAttribute( 'id' );

					//$input_type = $input->getAttribute( 'type' );
					if ( preg_match( '/^input_/', $id ) ) {
						$input_id = $id;
						$input_classes = $input->getAttribute( 'class' );
						if( $contains_error ){
							$input_classes = $input_classes.' is-invalid';
						}

						$input->setAttribute( 'class', $input_classes . ' form-control' );
					}
				}


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

	  /***************************************** for postimage meta **************************************/

	  $post_meta_input_types = array( 'ginput_post_image_title', 'ginput_post_image_caption', 'ginput_post_image_description' );
	  $is_valid_post_meta = false;
	  $spans = $dom->getElementsByTagName('span');
	  foreach ($spans as $span) {
		  $span_classes = $span->getAttribute('class');
  
		  $span_classes_array = explode(' ', $span_classes);
  
		  //var_dump($div_classes_array);
  
		  $is_valid_input_field = false;
  
		  foreach ($span_classes_array as $span_class_value) {
			  if (in_array($span_class_value, $post_meta_input_types)) {
				  $is_valid_post_meta = true;
			  }
		  }
		  
		  if( $is_valid_post_meta ){
  
			   //add classes to div
			   $add_span_classes = ' form-group';
			   $span->setAttribute('class', $span->getAttribute('class') . $add_span_classes);

  
			   $inputs = $span->getElementsByTagName('input');
			   foreach( $inputs as $input){
				  $input_classes = $input->getAttribute('class');
				  if( $contains_error ){
					$input_classes = $input_classes.' is-invalid';
				}
				  $input->setAttribute('class', $input_classes . ' form-control');
				 
			   }

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




