<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( ! class_exists( 'SFTCY_Wordform_FormValidation' ) ) {
	class SFTCY_Wordform_FormValidation {
		public static $wordform_id                   = null;
		public static $wordform_captcha_session_name = '';
		public static $sc_wordform_validation_data   = array();
		public static $wordform_uploaded_validated_data = [];

		public static $validation_text_message                    = '';
		public static $validation_number_message                  = '';
		public static $validation_textarea_message                = '';
		public static $validation_range_message                   = '';
		public static $validation_radio_message                   = '';
		public static $validation_checkbox_message                = '';
		public static $validation_select_message                  = '';
		public static $validation_email_message                   = '';
		public static $validation_captcha_empty_message           = '';
		public static $validation_captcha_wrong_message           = '';
		public static $validation_captcha_expire_message          = '';
		public static $validation_gcaptcha_error_message          = '';
		public static $validation_upload_required_message         = '';
		public static $validation_upload_allow_message   		  = '';
		public static $validation_upload_size_message   		  = '';	
		public static $validation_upload_allsize_message	      = '';
		public static $validation_upload_maxinput_message	      = '';
		public static $validation_form_submission_success_message = '';

		/**
		 * Fetch & set validation messages
		 */
		public static function sc_wordform_set_validation_messages() {
			// check if set validation messages
			if ( isset( self::$wordform_id ) && self::$wordform_id ) {
				$fallback_messages   = SFTCY_Wordform::wordform_default_validation_messages();
				$validation_messages = SFTCY_Wordform::sc_wordform_get_validation_messages_data_by_formid( self::$wordform_id );

				// Text Validation Message
				self::$validation_text_message = isset( $validation_messages['text-msg'] ) ? sanitize_text_field( $validation_messages['text-msg'] ) : sanitize_text_field( $fallback_messages['text-msg'] );
				// Number Validation Message
				self::$validation_number_message = isset( $validation_messages['number-msg'] ) ? sanitize_text_field( $validation_messages['number-msg'] ) : sanitize_text_field( $fallback_messages['number-msg'] );
				// Textarea Validation Message
				self::$validation_textarea_message = isset( $validation_messages['textarea-msg'] ) ? sanitize_text_field( $validation_messages['textarea-msg'] ) : sanitize_text_field( $fallback_messages['textarea-msg'] );
				// Range Validation Message
				self::$validation_range_message = isset( $validation_messages['range-msg'] ) ? sanitize_text_field( $validation_messages['range-msg'] ) : sanitize_text_field( $fallback_messages['range-msg'] );
				// Radio Validation Message
				self::$validation_radio_message = isset( $validation_messages['radio-msg'] ) ? sanitize_text_field( $validation_messages['radio-msg'] ) : sanitize_text_field( $fallback_messages['radio-msg'] );
				// Checkbox Validation Message
				self::$validation_checkbox_message = isset( $validation_messages['checkbox-msg'] ) ? sanitize_text_field( $validation_messages['checkbox-msg'] ) : sanitize_text_field( $fallback_messages['checkbox-msg'] );
				// Select Validation Message
				self::$validation_select_message = isset( $validation_messages['select-msg'] ) ? sanitize_text_field( $validation_messages['select-msg'] ) : sanitize_text_field( $fallback_messages['select-msg'] );
				// Email Validation Message
				self::$validation_email_message = isset( $validation_messages['email-msg'] ) ? sanitize_text_field( $validation_messages['email-msg'] ) : sanitize_text_field( $fallback_messages['email-msg'] );
				// Captcha-Empty Validation Message
				self::$validation_captcha_empty_message = isset( $validation_messages['captcha-empty-msg'] ) ? sanitize_text_field( $validation_messages['captcha-empty-msg'] ) : sanitize_text_field( $fallback_messages['captcha-empty-msg'] );
				// Captcha-Wrong Validation Message
				self::$validation_captcha_wrong_message = isset( $validation_messages['captcha-wrong-msg'] ) ? sanitize_text_field( $validation_messages['captcha-wrong-msg'] ) : sanitize_text_field( $fallback_messages['captcha-wrong-msg'] );
				// Captcha-Expire Validation Message
				self::$validation_captcha_expire_message = isset( $validation_messages['captcha-expire-msg'] ) ? sanitize_text_field( $validation_messages['captcha-expire-msg'] ) : sanitize_text_field( $fallback_messages['captcha-expire-msg'] );
				// gCaptcha Validation Error Message
				self::$validation_gcaptcha_error_message = isset( $validation_messages['gcaptcha-error-msg'] ) ? sanitize_text_field( $validation_messages['gcaptcha-error-msg'] ) : sanitize_text_field( $fallback_messages['gcaptcha-error-msg'] );				
				// Upload Validation Message
				self::$validation_upload_required_message = isset( $validation_messages['upload-required-msg'] ) ? sanitize_text_field( $validation_messages['upload-required-msg'] ) : sanitize_text_field( $fallback_messages['upload-required-msg'] );
				self::$validation_upload_allow_message = isset( $validation_messages['upload-allow-msg'] ) ? sanitize_text_field( $validation_messages['upload-allow-msg'] ) : sanitize_text_field( $fallback_messages['upload-allow-msg'] );
				self::$validation_upload_size_message = isset( $validation_messages['upload-size-msg'] ) ? sanitize_text_field( $validation_messages['upload-size-msg'] ) : sanitize_text_field( $fallback_messages['upload-size-msg'] );
				self::$validation_upload_allsize_message = isset( $validation_messages['upload-allsize-msg'] ) ? sanitize_text_field( $validation_messages['upload-allsize-msg'] ) : sanitize_text_field( $fallback_messages['upload-allsize-msg'] );
				self::$validation_upload_maxinput_message = isset( $validation_messages['upload-maxinput-msg'] ) ? sanitize_text_field( $validation_messages['upload-maxinput-msg'] ) : sanitize_text_field( $fallback_messages['upload-maxinput-msg'] );

				// Form Submission Validation Success Message
				self::$validation_form_submission_success_message = isset( $validation_messages['form-submission-success-msg'] ) ? sanitize_text_field( $validation_messages['form-submission-success-msg'] ) : sanitize_text_field( $fallback_messages['form-submission-success-msg'] );
			}
		}
		/**
		 * Text validation
		 */
		public static function text_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Text';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_text_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Text';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Text';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Number validation
		 */
		public static function number_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Number';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_number_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Number';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Number';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}		

		/**
		 * Textarea validation
		 */
		public static function textarea_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Textarea';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_textarea_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Textarea';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Textarea';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Range validation
		 */
		public static function range_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Range';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_number_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Number';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Number';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}


		/**
		 * Radio validation
		 */
		public static function radio_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Radio';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_radio_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Radio';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Radio';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Checkbox validation
		 */
		public static function checkbox_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Checkbox';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_checkbox_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Checkbox';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Checkbox';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Select validation
		 */
		public static function select_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Select';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_select_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Select';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Select';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Email validation
		 */
		public static function email_validation( $data = array() ) {
			if ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Email';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( $input_data['required'] == 'true' ) {
						$temp['required']         = true;
						$temp['validationMsg']    = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? self::$validation_email_message : '';
						$temp['validationStatus'] = ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ? 'error' : 'success';
					} else {
						$temp['inputType']        = 'Email';
						$temp['required']         = false;
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Email';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

				self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Captcha validation
		 */
		public static function captcha_validation( $data = array() ) {
			if ( array_filter( $data ) ) {				
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					$temp['inputType'] = 'Captcha';

					$temp['validationMsgClass'] = 'validation-msg-' . $key;
					$temp['label']              = $input_data['label'];
					if ( isset( $input_data['required'] ) && $input_data['required'] == 'true' ) {
						$temp['required'] = true;
					}

					// Retrieve server Captcha value
					$captcha_server_value = false;
					if ( isset( $input_data['captcha_session_name'] ) ) {
						$captcha_server_value                = get_transient( $input_data['captcha_session_name'] );
						self::$wordform_captcha_session_name = $input_data['captcha_session_name'];
					}

					// Captcha field empty
					if ( ! isset( $input_data['values'] ) || ! array_filter( $input_data['values'] ) ) {
						$temp['validationMsg']    = self::$validation_captcha_empty_message;
						$temp['validationStatus'] = 'error';
					}
					// Captcha expired or not set properly
					elseif ( ! $captcha_server_value ) {
						$temp['validationMsg']    = self::$validation_captcha_expire_message;
						$temp['validationStatus'] = 'error';
					}
					// Captcha entered wrong
					elseif ( isset( $input_data['values'][0] ) && str_replace( ' ', '', $input_data['values'][0] ) !== $captcha_server_value ) {
						$temp['validationMsg']    = self::$validation_captcha_wrong_message;
						$temp['validationStatus'] = 'error';
					}
					// Captcha entered correct
					elseif ( isset( $input_data['values'][0] ) && str_replace( ' ', '', $input_data['values'][0] ) == $captcha_server_value ) {
						$temp['validationMsg']    = '';
						$temp['validationStatus'] = 'success';
					}
					// something else happened
					else {
						$temp['validationMsg']    = __( 'Something went wrong with Captcha. Refresh Page.', 'wordform' );
						$temp['validationStatus'] = 'error';
					}

					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp['inputType']          = 'Captcha';
				$temp['validationMsgClass'] = 'validation-msg-' . $key;
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

					self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * Google reCaptcha Validation
		 */
		public static function gcaptcha_validation( $data = array() ) {

			// print_r( $data );
			// exit();

			$gcaptcha_data = SFTCY_Wordform::wordform_settings_page_grecaptcha_config();
			if ( empty( $gcaptcha_data['gcaptcha-site-key'] ) || empty( $gcaptcha_data['gcaptcha-secret-key'] ) || empty( $gcaptcha_data['gcaptcha-type'] ) || empty( $gcaptcha_data['gcaptcha-version'] )  ) {
				$temp = array();				
				$temp['inputType'] = 'gCaptcha';					
				$temp['validationStatus'] = 'error';
				// $temp['validationMsg'] = self::$validation_upload_allsize_message;
				$temp['validationMsg'] = '<span class="sc-wordform-error-msg">' . __('Google reCaptcha integrated but not configured properly.', 'wordform') . '</span>';

				self::$sc_wordform_validation_data[] = $temp;
		    }
			elseif ( array_filter( $data ) ) {
				foreach ( $data as $key => $input_data ) {
					$temp              = array();
					if ( isset( $input_data['token'] ) && ! empty( $input_data['token'] ) ) {
						$temp['inputType'] = 'gCaptcha';
						$temp['validationMsgClass'] = 'validation-msg-' . $key;
						$temp['label'] = $input_data['label'];
						$data = [ 'response' => $input_data['token'], 'secret' => $gcaptcha_data['gcaptcha-secret-key'] ];
						if ( SFTCY_Wordform_Captcha::is_gcaptcha_token_valid( $data ) == true ) {
							$temp['validationMsg']    = '';
							$temp['validationStatus'] = 'success';	
						}						
						else {
							$temp['validationMsg']    = self::$validation_gcaptcha_error_message;
							$temp['validationStatus'] = 'error';	
						}
					}
					// When token is empty! reCaptcha challenge not checked | not faced | not filled up
					else {
						$temp['inputType'] = 'gCaptcha';					
						$temp['validationStatus'] = 'error';
						// $temp['validationMsg'] = self::$validation_upload_allsize_message;
						$temp['validationMsg'] = '<span class="sc-wordform-error-msg"><br/>' . __('Google reCaptcha failed to verify.', 'wordform') . '</span>';		
					}
					self::$sc_wordform_validation_data[] = $temp;
				} // foreach
			} else {
				$temp = array();
				$temp['inputType']          = 'gCaptcha';				
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

				self::$sc_wordform_validation_data[] = $temp;
			}			
		}


		/**
		 * Upload validation
		 */
		public static function upload_validation( $data = array() ) {
			// echo '<pre>';
			// print_r( $data );
			// echo '</pre>';
			// exit();
			if ( array_filter( $data ) ) {		

				// Check all uploaded file size limit
				if ( self::is_all_uploaded_filesize_exceeded_limit( $data ) ) {
					$temp = array();
					$temp['inputType'] = 'Upload';					
					$temp['validationStatus'] = 'error';
					$temp['validationMsg'] = self::$validation_upload_allsize_message;

					self::$sc_wordform_validation_data[] = $temp;
				}
				else {
					// Check Each input file type element - if input file type multiple supported then each input file type may have more than one file uploaded
					foreach ( $data as $key => $input_data ) {
						$temp              = array();
						$temp['inputType'] = 'Upload';

						$temp['validationMsgClass'] = 'validation-msg-' . $key;
						$temp['label']              = isset( $input_data['label'] ) ? $input_data['label'] : '';

						// While file names found | Uploaded data available
						if ( isset( $input_data['name'] ) && array_filter( $input_data['name'] ) ) {
								//$temp['validationMsg']    = self::$validation_upload_required_message;	
								//$temp['validationStatus'] = 'error';
											
							// While uploaded file types not allowed
							if ( ! self::is_wordform_upload_file_types_allowed( $input_data ) ) {						
								$temp['validationMsg']    = self::$validation_upload_allow_message;
								$temp['validationStatus'] = 'error';
							}
							// While uploaded individual file size exceeded limit
							else if ( ! self::is_wordform_upload_file_size_allowed( $input_data ) ) {						
								$temp['validationMsg']    = self::$validation_upload_size_message;
								$temp['validationStatus'] = 'error';
							}
					}
					// While file names Not found | Uploaded data Not available
					else if ( ! isset( $input_data['name'] ) || ! array_filter( $input_data['name'] ) ) {
							// If required but not uploaded - error
							if ( isset( $input_data['required'] ) && $input_data['required'] == 'true' ) {
								$temp['required']         = true;
								$temp['validationStatus'] = 'error';
							}
							// Not required 
							else {							
								$temp['required']         = false;
								$temp['validationMsg']    = '';
								$temp['validationStatus'] = 'success';
							}
					}

					self::$sc_wordform_validation_data[] = $temp;
					} // foreach
				}
			} 
			else {
				$temp['inputType']          = 'Upload';				
				$temp['validationMsg']      = __( 'Input element data missing.', 'wordform' );
				$temp['validationStatus']   = 'error';

				self::$sc_wordform_validation_data[] = $temp;
			}
		}

		/**
		 * @param array $data - Individual file uploaded data
		 * Check file types allowed
		 * @return boolean 
		 */
 		public static function is_wordform_upload_file_types_allowed( $input_data ) {
			$error = 0;						
			$uploaded_file_types = [];
			$uploaded_file_extensions = [];
			// Process Allowed File Extensions
			$allowed_file_extensions = isset( $input_data['fileAllowed'] ) ? explode(',', $input_data['fileAllowed'] ) : [];
			// Remove dot (.) from extension
			$allowed_file_extensions = array_map( function( $ext ) { return strtolower( str_replace( '.', '', $ext ) ); }, $allowed_file_extensions );								
			if ( isset( $input_data['name'] ) && is_array( $input_data['name'] ) ) {		
				foreach ( $input_data['name'] as $filename ) {					
					$uploaded_file_types[] = wp_check_filetype( $filename );						
				}
			}
			// Process Uploaded File Extensions
			// $uploaded_file_extensions =  array_unique( array_column( $uploaded_file_types, 'ext' ) );
			$uploaded_file_extensions =  array_unique( array_column( $uploaded_file_types, 'type' ) );
			$uploaded_file_extensions = array_map( 'strtolower', $uploaded_file_extensions );
			// Remove dot (.) from extension
			$uploaded_file_extensions = array_map( function( $ext ) { return str_replace( '.', '', $ext );}, $uploaded_file_extensions );

			// echo '<pre>';
			// print_r( $uploaded_file_extensions );
			// print_r( $allowed_file_extensions );
			// echo '</pre>';

			// Check Image | Word | Pdf Files				
			// Check each uploaded file extension match with allowed file type extensions
			foreach ( $uploaded_file_extensions as $uploaded_ext ) {
				if ( ! in_array( $uploaded_ext, $allowed_file_extensions ) ) {
					$error++;
				}
			}
			return ( $error > 0 ) ? false : true;
		}
               
	/**
	 * Check uploaded individual file size exceeded limit - upload_max_filesize	 
	 * @return boolean
	 */ 
     public static function is_wordform_upload_file_size_allowed( $input_data ) {
		$error = 0;
		$upload_max_filesize = ini_get('upload_max_filesize');		
		$limitsize_mb = self::convert_filesize_into_mb( $upload_max_filesize );
		if ( isset( $input_data['size'] ) ) {
			foreach( $input_data['size'] as $filesize ) {
				$filesize_mb = number_format( $filesize / ( 1024*1024 ), 2 );									
				// $limitsize_mb = 0 means no limit
				if ( $limitsize_mb > 0 ) {		
					if ( $filesize_mb > $limitsize_mb ) { $error++; }
				}
			}
		}    
		return ( $error > 0 ) ? false : true;
	 }
    /**
	 * Check uploaded all files together total size exceeded all files size limit
	 * @return boolean
	 */
    public static function is_all_uploaded_filesize_exceeded_limit( $data ) {
		$upload_config_data = SFTCY_Wordform::wordform_settings_page_upload_config();
		//$post_max_size = ini_get('post_max_size');
		//$limitsize_mb = self::convert_filesize_into_mb( $post_max_size );
		$limitsize_mb = $upload_config_data['all_files_max_size_allowed'];
		$uploaded_all_file_size_mb = 0;
        foreach( $data as $key => $input_data ) {
             if ( isset( $input_data['size'] ) && $input_data['size'] > 0 ) {
				foreach ( $input_data['size'] as $filesize ) {
					//$filesize_mb = number_format( $filesize / ( 1024*1024 ), 2 );
					$filesize_mb = self::convert_bytes_into_mb( $filesize );
					$uploaded_all_file_size_mb += $filesize_mb;
				}
			 }
		}
		// echo 'Uploaded All Filesize in MB';
		$uploaded_all_file_size_mb = number_format( $uploaded_all_file_size_mb , 2 );
		// var_dump( $uploaded_all_file_size_mb );
		return ( $uploaded_all_file_size_mb > $limitsize_mb ) ? true : false;
	}

    /**
	 * Check number of total uploaded files exceeded max_file_uploads number
	 *
	 * @param array $data
	 * @return boolean
	 */
    public static function is_all_uploaded_number_of_files_exceeded_limit( $data ) {
		$max_file_uploads = ini_get( 'max_file_uploads');
		if ( $max_file_uploads <= 0 ) { return false; }
		$total_uploaded_files = 0; 
        foreach( $data as $key => $input_data ) {
			if ( isset( $input_data['name'] ) && is_array( $input_data['name'] ) ) {
				$total_uploaded_files += count( $input_data['name'] );
			}
	    }
        return ( $total_uploaded_files > $max_file_uploads ) ? true : false;
	}

	/**
	 * Check php ini configuration file size
	 * Convert them into MB
	 * @return integer - $limitsize_mb - MB format
	 */
	public static function convert_filesize_into_mb( $size ) {
		$limitsize_mb = 0;
		$filesize_unit = ( $size == 0 || $size == -1 ) ? '' : trim( preg_replace( '/[0-9]/', "", $size ) );
		if ( ! empty( $filesize_unit ) ) {
			// when 'upload_max_filesize' | 'post_max_size' set in MB | GB
             if ( strtolower( $filesize_unit ) == 'm' || strtolower( $filesize_unit ) == 'mb' ) {
                    $limitsize_mb = preg_replace( '/[^0-9]/', '', $size );
			 }
			 // when 'upload_max_filesize' | 'post_max_size' set in GB
             else if ( strtolower( $filesize_unit ) == 'g' || strtolower( $filesize_unit ) == 'gb' ) {
				$limitsize_mb =  preg_replace( '/[^0-9]/', '', $size ) * 1000;
		 	 }
			 // When 'upload_max_filesize' | 'post_max_size' without any size unit appended like 100 - consider value as MB
			 else {
				$limitsize_mb = preg_replace( '/[^0-9]/', '', $size );
			 }
		}
		return $limitsize_mb;		
	}

	/**
	 * Check php ini configuration file size
	 * Convert them ( upload_max_size | post_max_size ) into Bytes
	 * @return integer - Bytes format
	 */
	public static function convert_filesize_into_bytes( $size ) {
		$limitsize_bytes = 0;
		$filesize_unit = ( $size <= 0 ) ? '' : trim( preg_replace( '/[0-9]/', "", $size ) );
		if ( ! empty( $filesize_unit ) ) {
			// when 'upload_max_filesize' | 'post_max_size' set in MB | GB
             if ( strtolower( $filesize_unit ) == 'm' || strtolower( $filesize_unit ) == 'mb' ) {
                    $limitsize_bytes = preg_replace( '/[^0-9]/', '', $size ) * 1024 * 1024;
			 }
			 // when 'upload_max_filesize' | 'post_max_size' set in GB
             else if ( strtolower( $filesize_unit ) == 'g' || strtolower( $filesize_unit ) == 'gb' ) {
				$limitsize_bytes =  preg_replace( '/[^0-9]/', '', $size ) * 1024 * 1024 * 1024;
		 	 }
		}		
		return $limitsize_bytes;		
	}

	/**
	 * @param integer $bytes
	 * Convert file size bytes into MB
	 * @return float MB
	 */
	public static function convert_bytes_into_mb( $bytes = 0 ) {
		if ( $bytes <= 0 ) return 0;
		return  floatval( number_format( $bytes / ( 1024 * 1024 ), 2 ) );
	}


	} // End class
}
