<?php
/**
** A base module for [custom_country_list] and [custom_country_list*]
**/

/* form_tag handler */

add_action( 'wpcf7_init', 'wpcf7_add_form_tag_custom_country_list', 10, 0 );

function wpcf7_add_form_tag_custom_country_list() {
	wpcf7_add_form_tag( array( 'custom_country_list', 'custom_country_list*' ),
		'wpcf7_custom_country_list_form_tag_handler',
		array(
			'name-attr' => true,
			'selectable-values' => true,
		)
	);
}

function wpcf7_custom_country_list_form_tag_handler( $tag ) {
	if ( empty( $tag->name ) ) {
		return '';
	}

	$validation_error = wpcf7_get_validation_error( $tag->name );

	$class = wpcf7_form_controls_class( $tag->type );

	if ( $validation_error ) {
		$class .= ' wpcf7-not-valid';
	}

	$atts = array();

	$atts['class'] = $tag->get_class_option( $class );
	$atts['id'] = $tag->get_id_option();
	$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );

	if ( $tag->is_required() ) {
		$atts['aria-required'] = 'true';
	}

	if ( $validation_error ) {
		$atts['aria-invalid'] = 'true';
		$atts['aria-describedby'] = wpcf7_get_validation_error_reference(
			$tag->name
		);
	} else {
		$atts['aria-invalid'] = 'false';
	}

	$multiple = $tag->has_option( 'multiple' );


	if ( $data = (array) $tag->get_data_option() ) {
		$tag->values = array_merge( $tag->values, array_values( $data ) );
		$tag->labels = array_merge( $tag->labels, array_values( $data ) );
	}


	$atts['name'] = $tag->name . ( $multiple ? '[]' : '' );

	$atts = wpcf7_format_atts( $atts );
	
	$html = '';
	if ( $tag->has_option( 'placeholder' ) ) {
		$value = (string) reset( $tag->values );
		$html = '<option data-countryCode="" value="">'. $value .'</option>';
	}

	$html .= '
		
		<option data-countryCode="EG" value="20">Egypt - مصر</option>
		

		<optgroup label="Arab">
			<option data-countryCode="DZ" value="213">Algeria - الجزائر</option>
			<option data-countryCode="BH" value="973">Bahrain - البحرين</option>
			<option data-countryCode="IQ" value="964">Iraq - العراق</option>
			<option data-countryCode="JO" value="962">Jordan - الأردن</option>
			<option data-countryCode="KW" value="965">Kuwait - الكويت</option>
			<option data-countryCode="LY" value="218">Libya - ليبيا</option>
			<option data-countryCode="MA" value="212">Morocco - المغرب</option>
			<option data-countryCode="OM" value="968">Oman - عمان</option>
			<option data-countryCode="QA" value="974">Qatar - قطر</option>
			<option data-countryCode="TN" value="216">Tunisia - تونس</option>
			<option data-countryCode="SA" value="966">Saudi Arabia - السعودية</option>
			<option data-countryCode="SD" value="249">Sudan - السودان</option>
			<option data-countryCode="SI" value="963">Syria - سوريا</option>
			<option data-countryCode="AE" value="971">United Arab Emirates - الإمارات العربية</option>
			<option data-countryCode="YE" value="969">Yemen +969 - اليمن</option>
			<option data-countryCode="YE" value="967">Yemen +967 - اليمن</option>
			<option data-countryCode="LB" value="961">Lebanon - لبنان</option>
		</optgroup>
		<optgroup label="Countries">
			<option data-countryCode="DE" value="49">Germany</option>
			<option data-countryCode="GB" value="44">UK</option>
			<option data-countryCode="US" value="1">USA</option>
			<option data-countryCode="CA" value="1">Canada</option>
			<option data-countryCode="IT" value="39">Italy</option>
			<option data-countryCode="TR" value="90">Turkey</option>    
		</optgroup>
		<optgroup label="Other countries">
			<option data-countryCode="AR" value="54">Argentina</option>
			<option data-countryCode="AU" value="61">Australia</option>
			<option data-countryCode="AZ" value="994">Azerbaijan</option>
			<option data-countryCode="BS" value="1242">Bahamas</option>
			
			<option data-countryCode="BE" value="32">Belgium</option>
			<option data-countryCode="BR" value="55">Brazil</option>
			<option data-countryCode="BG" value="359">Bulgaria</option>
			<option data-countryCode="CA" value="1">Canada</option>
			<option data-countryCode="CN" value="86">China</option>
			<option data-countryCode="DK" value="45">Denmark</option>

			<option data-countryCode="FR" value="33">France</option>
			<option data-countryCode="GE" value="7880">Georgia</option>
			
			<option data-countryCode="GR" value="30">Greece</option>
			<option data-countryCode="HK" value="852">Hong Kong</option>
			<option data-countryCode="IS" value="354">Iceland</option>
			<option data-countryCode="IN" value="91">India</option>
			<option data-countryCode="ID" value="62">Indonesia</option>
			<option data-countryCode="IR" value="98">Iran</option>
			
			<option data-countryCode="IE" value="353">Ireland</option>

			<option data-countryCode="JP" value="81">Japan</option>
			<option data-countryCode="KR" value="82">Korea South</option>
			

			<option data-countryCode="MY" value="60">Malaysia</option>
			<option data-countryCode="MV" value="960">Maldives</option>
			<option data-countryCode="MR" value="222">Mauritania</option>
			<option data-countryCode="MX" value="52">Mexico</option>
			<option data-countryCode="NL" value="31">Netherlands</option>
			<option data-countryCode="NZ" value="64">New Zealand</option>
			<option data-countryCode="NI" value="505">Nicaragua</option>
			<option data-countryCode="PL" value="48">Poland</option>
			<option data-countryCode="PT" value="351">Portugal</option>
			
			<option data-countryCode="RO" value="40">Romania</option>
			<option data-countryCode="RU" value="7">Russia</option>

			<option data-countryCode="SG" value="65">Singapore</option>
			<option data-countryCode="SO" value="252">Somalia</option>
			<option data-countryCode="ZA" value="27">South Africa</option>
			<option data-countryCode="ES" value="34">Spain</option>
			<option data-countryCode="SZ" value="268">Swaziland</option>
			<option data-countryCode="SE" value="46">Sweden</option>
			<option data-countryCode="CH" value="41">Switzerland</option>
			
			<option data-countryCode="TJ" value="7">Tajikstan</option>
			<option data-countryCode="TH" value="66">Thailand</option>
			
			
			<option data-countryCode="TM" value="7">Turkmenistan</option>
			<option data-countryCode="TM" value="993">Turkmenistan</option>
			<option data-countryCode="TR" value="90">Turkey</option>    
			<option data-countryCode="TC" value="1649">Turks &amp; Caicos Islands</option>
			<option data-countryCode="GB" value="44">UK</option>
			<option data-countryCode="UA" value="380">Ukraine</option>
			
			<option data-countryCode="US" value="1">USA</option>
			

		</optgroup>
		<option data-countryCode="-" value="other">Other</option>';


	$html = sprintf(
		'<span class="wpcf7-form-control-wrap %1$s" data-name="%1$s"><select %2$s>%3$s</select>%4$s</span>',
		sanitize_html_class( $tag->name ), $atts, $html, $validation_error
	);

	return $html;
}


/* Validation filter */

add_filter( 'wpcf7_validate_custom_country_list', 'wpcf7_custom_country_list_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_custom_country_list*', 'wpcf7_custom_country_list_validation_filter', 10, 2 );

function wpcf7_custom_country_list_validation_filter( $result, $tag ) {
	$tag = new WPCF7_Shortcode( $tag );

	$name = $tag->name;

	$has_value = isset( $_POST[$name] ) && '' !== $_POST[$name];

	if ( $has_value and $tag->has_option( 'multiple' ) ) {
		$vals = array_filter( (array) $_POST[$name], function( $val ) {
			return '' !== $val;
		} );

		$has_value = ! empty( $vals );
	}

	if ( $tag->is_required() and ! $has_value ) {
		$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
	}
	return $result;
}