<?php
/*
 * Developed By Devozon
 *
 * Check Out at https://devozon.com/
 * Email: support@devozon.com
 * Message: https://www.facebook.com/devozon
 *
 * Please feel free to contact me at any time if you need a custom script or any development work.
 *
 * Devozon Team:
 * Coded by Sanad Nabil
 * Theme by Ahmed Reda
 * Tested By Smith Carter
 *
 * Devozon All rights reserved
 * Copyright (c) 2023-2024.
 * Last Update 16 \ 1 \ 2024
 */

/**
 **  Theme Created , Developed , Designed By Devozon
 **  Check Out at https://devozon.com/
 **  Coded By Sanad Nabil
 **  Phone number 01028842795
 **  Message https://www.facebook.com/devozon
 **  Devozon All rights reserved
 **
 **  Submitted By Ahmed Reda
 **  ===================
 **
 **  ===================
 **/

if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class FENSE_BPVT_ADM
{
	
	public function add_option( $name, $id, $type, $subtype, $data = '', $key = 'tab_1', $comment = '' )
	{
		
		$z = 'FENSE_BPVT_DEVOZON_' . $id;
		
		$args = [
			'title' => $name,
			'type' => $type,
			'subtype' => $subtype,
			'id' => $z,
			'name' => $z,
			'required' => 'true',
			'get_options_list' => '',
			'value_type' => 'normal',
			'wp_data' => 'option',
			'tg' => $data,
			'comment' => $comment,
		];
		
		add_settings_field(
			$z,
			$name,
			[ 'FENSE_BPVT_Admin', 'FENSE_BPVT_render_settings_field' ],
			'FENSE_BPVT_' . $key,
			'FENSE_BPVT_general_section',
			$args
		);
		
		register_setting( 'FENSE_BPVT_' . $key, $z );
		
		unset( $args );
	}
	
	public function FORM_select( $args )
	{
		
		if ( $args['wp_data'] == 'option' ) {
			$db_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$db_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$html = '';
		
		$html .= '<div class="form-floating"><select class="form-select" id="' . esc_attr( $args['id'] ) . '" name="' . esc_attr( $args['name'] ) . '">';
		
		foreach ( $args['tg'] as $name => $value ) {
			$selected = ( $db_value == $value ) ? 'selected' : '';
			$html .= '<option value="' . esc_textarea( $value ) . '" ' . esc_html( $selected ) . '>' . esc_html( $name ) . '</option>';
		}
		
		$html .= '</select><label for="' . esc_attr( $args['id'] ) . '">' . esc_attr( $args['title'] ) . '</label></div>';
		
		$html .= '<div class="mt-2">' . $args['comment'] . '</div>';
		
		echo self::tg_esc_html( $html );
	}
	
	public function tg_esc_html( $str )
	{
		
		$arr = [
			'br' => [],
			'p' => [],
			'a' => [
				'id' => [],
				'class' => [],
				'href' => [],
			],
			'strong' => [],
			'span' => [
				'id' => [],
				'class' => [],
			],
			'i' => [
				'id' => [],
				'class' => [],
			],
			'label' => [
				'id' => [],
				'class' => [],
				'for' => [],
			],
			'input' => [
				'id' => [],
				'class' => [],
				'name' => [],
				'type' => [],
				'step' => [],
				'min' => [],
				'max' => [],
				'disabled' => [],
				'value' => [],
				'size' => [],
				'checked' => [],
				'data-role' => [],
			],
			'textarea' => [
				'id' => [],
				'class' => [],
				'name' => [],
				'rows' => [],
				'cols' => [],
			],
			'select' => [
				'id' => [],
				'class' => [],
				'type' => [],
				'name' => [],
				'aria-label' => [],
				'multiselect-search' => [],
				'multiselect-select-all' => [],
				'multiselect-max-items' => [],
				'multiple' => [],
			],
			'option' => [
				'value' => [],
				'selected' => [],
			],
			'div' => [
				'class' => [],
				'id' => [],
			],
		];
		
		return wp_kses( $str, $arr );
	}
	
	public function FORM_multi_select( $args )
	{
		
		if ( $args['wp_data'] == 'option' ) {
			$db_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$db_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$html = '';
		
		$html .= '<select class="select form-select" type="checkbox" name="' . esc_attr( $args['name'] ) . '[]" id="field2"  multiselect-search="true" multiselect-select-all="true" multiselect-max-items="3" multiple="multiple">';
		
		foreach ( $args['tg'] as $value => $name ) {
			if ( !empty( $db_value ) ) {
				$selected = ( in_array( $value, $db_value ) ) ? 'selected' : '';
			} else {
				$selected = '';
			}
			$html .= '<option value="' . esc_textarea( $value ) . '" ' . esc_html( $selected ) . '>' . esc_html( $name ) . '</option>';
		}
		
		$html .= '</select>';
		
		$html .= '<div class="mt-2">' . $args['comment'] . '</div>';
		
		echo self::tg_esc_html( $html );
	}
	
	public function FORM_text_area( $args )
	{
		if ( $args['wp_data'] == 'option' ) {
			$db_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$db_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$html = '';
		
		$html .= '<textarea class="form-field" name="' . esc_html( $args['name'] ) . '" rows="4" cols="50">';
		$html .= $db_value . '</textarea>';
		
		$html .= '<div class="mt-2">' . $args['comment'] . '</div>';
		
		echo self::tg_esc_html( $html );
	}
	
	public function FORM_ad_code( $args )
	{
		
		if ( $args['wp_data'] == 'option' ) {
			$db_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$db_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$html = '';
		
		$html .= '<textarea class="form-field" name="' . esc_html( $args['name'] ) . '" rows="4" cols="50">';
		$html .= esc_textarea( $db_value ) . '</textarea>';
		
		$html .= '<div class="mt-2">' . $args['comment'] . '</div>';
		
		echo $html;
	}
	
	public function FORM_multi_input( $args )
	{
		
		if ( $args['wp_data'] == 'option' ) {
			$db_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$db_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$html = '';
		
		$html .= '<input class="form-field" data-role="tagsinput" name="' . esc_html( $args['name'] ) . '" value="' . esc_html( $db_value ) . '" >';
		$html .= '</input>';
		
		$html .= '<div class="mt-2">' . $args['comment'] . '</div>';
		
		echo $html;
	}
	
	
	public function FORM_ip_ranges( $args ) {
		$opt = $args['name'];
		$id  = str_replace(['[',']'], ['-',''], $opt);
		
		// load existing
		if ( $args['wp_data']=='option' ) {
			$db = get_option( $opt, [] );
		} else {
			$db = get_post_meta( $args['post_id'], $opt, true ) ?: [];
		}
  
		// container
		echo "<div id='ip-range-container-{$id}'>";
        
        if (!empty($db)) {
			foreach ( (array)$db as $i => $r ) {
				printf(
					"<div class='d-flex mb-4 ip-range-row'>
                <div class='col-md-5'>
                    <label class='form-label'>IP From</label>
                    <div class='input-group'>
                        <span class='input-group-text'><i class='ri-arrow-left-right-line'></i></span>
                        <input type='text' class='form-control' name='%1\$s[%2\$d][from]' value='%3\$s' required pattern='^((25[0-5]|2[0-4]\\d|[01]?\\d?\\d)(\\.|$)){4}$' placeholder='192.168.0.1'>
                    </div>
                </div>
                <div class='col-md-5 ms-3'>
                    <label class='form-label'>IP To</label>
                    <div class='input-group'>
                        <span class='input-group-text'><i class='ri-arrow-left-right-line'></i></span>
                        <input type='text' class='form-control' name='%1\$s[%2\$d][to]'   value='%4\$s' required pattern='^((25[0-5]|2[0-4]\\d|[01]?\\d?\\d)(\\.|$)){4}$' placeholder='192.168.0.255'>
                    </div>
                </div>
                <button type='button' class='btn btn-danger remove-range'>×</button>
            </div>",
					esc_attr( $opt ), $i,
					esc_attr( $r['from'] ?? '' ), esc_attr( $r['to'] ?? '' )
				);
			}
		}
		echo "</div>";
		
		// add button
		echo "<button type='button' id='add-ip-range-{$id}' class='btn btn-primary mb-3'>
            <i class='ri-add-fill'></i> Add Range
          </button>";
		
		echo '<div class="mt-2">' . $args['comment'] . '</div>';
		
		// JS
		?>
		<script>
            document.addEventListener("DOMContentLoaded", () => {
                
                const optName = "<?php echo esc_js($opt) ?>";
                const container = document.getElementById("ip-range-container-<?php echo $id?>");
                const addBtn    = document.getElementById("add-ip-range-<?php echo $id?>");
                let index = container.querySelectorAll('.ip-range-row').length;

                addBtn.addEventListener('click', () => {
                    const row = document.createElement('div');
                    row.className = 'd-flex mb-4 ip-range-row';
                    row.innerHTML = `
          <div class="col-md-5">
            <label class="form-label">IP From</label>
            <div class="input-group">
              <span class="input-group-text"><i class="ri-arrow-left-right-line"></i></span>
              <input type="text" class="form-control" name="${optName}[${index}][from]" required pattern="^((25[0-5]|2[0-4]\\d|[01]?\\d?\\d)(\\.|$)){4}$" placeholder="192.168.0.1">
            </div>
          </div>
          <div class="col-md-5 ms-3">
            <label class="form-label">IP To</label>
            <div class="input-group">
              <span class="input-group-text"><i class="ri-arrow-left-right-line"></i></span>
              <input type="text" class="form-control" name="${optName}[${index}][to]" required pattern="^((25[0-5]|2[0-4]\\d|[01]?\\d?\\d)(\\.|$)){4}$" placeholder="192.168.0.255">
            </div>
          </div>
          <button type="button" class="btn btn-danger remove-range">×</button>
        `;
                    container.appendChild(row);
                    index++;
                });

                container.addEventListener('click', e => {
                    if ( e.target.classList.contains('remove-range') ) {
                        e.target.closest('.ip-range-row').remove();
                    }
                });
                
                // attach to every “from”/“to” input
                document.querySelectorAll('#ip-range-container-<?php echo esc_js($id) ?> input[type="text"]').forEach(input => {
                    // on blur
                    input.addEventListener('blur', (e) => {
                        if (!isValidIP(input.value)) {
                            e.stopPropagation()
                            input.setCustomValidity('Enter a valid IPv4 address (e.g. 192.168.0.1)');
                        } else {
                            input.setCustomValidity('');
                        }
                        input.reportValidity();
                    });
                });
            });
		</script>
		<?php
	}

	
	
	public function FORM_input( $args )
	{
		
		if ( $args['wp_data'] == 'option' ) {
			$wp_data_value = get_option( $args['name'] );
		} elseif ( $args['wp_data'] == 'post_meta' ) {
			$wp_data_value = get_post_meta( $args['post_id'], $args['name'], true );
		}
		
		$value = ( $args['value_type'] == 'serialized' ) ? serialize( $wp_data_value ) : $wp_data_value;
		if ( $args['subtype'] != 'checkbox' ) {
			$prependStart = ( isset( $args['prepend_value'] ) ) ? '<div class="input-prepend"> <span class="add-on">' . esc_html( $args['prepend_value'] ) . '</span>' : '';
			$prependEnd = ( isset( $args['prepend_value'] ) ) ? '</div>' : '';
			$step = ( isset( $args['step'] ) ) ? 'step="' . esc_attr( $args['step'] ) . '"' : '';
			$min = ( isset( $args['min'] ) ) ? 'min="' . esc_attr( $args['min'] ) . '"' : '';
			$max = ( isset( $args['max'] ) ) ? 'max="' . esc_attr( $args['max'] ) . '"' : '';
			if ( isset( $args['disabled'] ) ) {
				
				$tg_out = $prependStart . '<div class="form__group field"><input class="form__field" type="' . esc_attr( $args['subtype'] ) . '" id="' . esc_attr( $args['id'] ) . '_disabled" ' . $step . ' ' . $max . ' ' . $min . ' name="' . esc_attr( $args['name'] ) . '_disabled" size="40" disabled value="' . esc_attr( $value ) . '" /><input type="hidden" id="' . esc_attr( $args['id'] ) . '" ' . $step . ' ' . $max . ' ' . $min . ' name="' . esc_attr( $args['name'] ) . '" size="40" value="' . esc_attr( $value ) . '" /><label for="name" class="form__label">Name</label></div>' . $prependEnd;
				
				echo self::tg_esc_html( $tg_out );
				
			} else {
				$tg_out = $prependStart . '<div class="form-floating"><input type="text" class="form-control" type="' . esc_attr( $args['subtype'] ) . '" id="' . esc_attr( $args['id'] ) . '" "' . esc_attr( $args['required'] ) . '" ' . $step . ' ' . $max . ' ' . $min . ' name="' . esc_attr( $args['name'] ) . '" size="40" value="' . esc_attr( $value ) . '"><label for="' . esc_attr( $args['id'] ) . '">' . esc_attr( $args['title'] ) . '</label></div><div class="mt-2">' . $args['comment'] . '</div>' . $prependEnd;
				
				echo self::tg_esc_html( $tg_out );
			}
			/*<input required="required" '.$disabled.' type="number" step="any" id="'.$this->plugin_name.'_cost2" name="'.$this->plugin_name.'_cost2" value="' . esc_attr( $cost ) . '" size="25" /><input type="hidden" id="'.$this->plugin_name.'_cost" step="any" name="'.$this->plugin_name.'_cost" value="' . esc_attr( $cost ) . '" />*/
			
		} else {
			$checked = ( $value ) ? 'checked' : '';
			$lpout = '<label class="switch"><input type="' . esc_attr( $args['subtype'] ) . '" id="' . esc_attr( $args['id'] ) . '" "' . esc_attr( $args['required'] ) . '" name="' . esc_attr( $args['name'] ) . '" size="40" value="1" ' . $checked . ' /><span class="slider round"></span></label>';
			
            $lpout .= (!empty($args['comment'])) ? '<div class="mt-2">' . $args['comment'] . '</div>' : '';
            
			echo self::tg_esc_html( $lpout );
			
		}
	}
	
	public function country_list()
	{
		
		$countries_list = [
			"AF" => "Afghanistan",
			"AX" => "Aland Islands",
			"AL" => "Albania",
			"DZ" => "Algeria",
			"AS" => "American Samoa",
			"AD" => "Andorra",
			"AO" => "Angola",
			"AI" => "Anguilla",
			"AQ" => "Antarctica",
			"AG" => "Antigua and Barbuda",
			"AR" => "Argentina",
			"AM" => "Armenia",
			"AW" => "Aruba",
			"AU" => "Australia",
			"AT" => "Austria",
			"AZ" => "Azerbaijan",
			"BS" => "Bahamas",
			"BH" => "Bahrain",
			"BD" => "Bangladesh",
			"BB" => "Barbados",
			"BY" => "Belarus",
			"BE" => "Belgium",
			"BZ" => "Belize",
			"BJ" => "Benin",
			"BM" => "Bermuda",
			"BT" => "Bhutan",
			"BO" => "Bolivia",
			"BQ" => "Bonaire, Sint Eustatius and Saba",
			"BA" => "Bosnia and Herzegovina",
			"BW" => "Botswana",
			"BV" => "Bouvet Island",
			"BR" => "Brazil",
			"IO" => "British Indian Ocean Territory",
			"BN" => "Brunei Darussalam",
			"BG" => "Bulgaria",
			"BF" => "Burkina Faso",
			"BI" => "Burundi",
			"KH" => "Cambodia",
			"CM" => "Cameroon",
			"CA" => "Canada",
			"CV" => "Cape Verde",
			"KY" => "Cayman Islands",
			"CF" => "Central African Republic",
			"TD" => "Chad",
			"CL" => "Chile",
			"CN" => "China",
			"CX" => "Christmas Island",
			"CC" => "Cocos (Keeling) Islands",
			"CO" => "Colombia",
			"KM" => "Comoros",
			"CG" => "Congo",
			"CD" => "Congo, Democratic Republic of the Congo",
			"CK" => "Cook Islands",
			"CR" => "Costa Rica",
			"CI" => "Cote D'Ivoire",
			"HR" => "Croatia",
			"CU" => "Cuba",
			"CW" => "Curacao",
			"CY" => "Cyprus",
			"CZ" => "Czech Republic",
			"DK" => "Denmark",
			"DJ" => "Djibouti",
			"DM" => "Dominica",
			"DO" => "Dominican Republic",
			"EC" => "Ecuador",
			"EG" => "Egypt",
			"SV" => "El Salvador",
			"GQ" => "Equatorial Guinea",
			"ER" => "Eritrea",
			"EE" => "Estonia",
			"ET" => "Ethiopia",
			"FK" => "Falkland Islands (Malvinas)",
			"FO" => "Faroe Islands",
			"FJ" => "Fiji",
			"FI" => "Finland",
			"FR" => "France",
			"GF" => "French Guiana",
			"PF" => "French Polynesia",
			"TF" => "French Southern Territories",
			"GA" => "Gabon",
			"GM" => "Gambia",
			"GE" => "Georgia",
			"DE" => "Germany",
			"GH" => "Ghana",
			"GI" => "Gibraltar",
			"GR" => "Greece",
			"GL" => "Greenland",
			"GD" => "Grenada",
			"GP" => "Guadeloupe",
			"GU" => "Guam",
			"GT" => "Guatemala",
			"GG" => "Guernsey",
			"GN" => "Guinea",
			"GW" => "Guinea-Bissau",
			"GY" => "Guyana",
			"HT" => "Haiti",
			"HM" => "Heard Island and Mcdonald Islands",
			"VA" => "Holy See (Vatican City State)",
			"HN" => "Honduras",
			"HK" => "Hong Kong",
			"HU" => "Hungary",
			"IS" => "Iceland",
			"IN" => "India",
			"ID" => "Indonesia",
			"IR" => "Iran, Islamic Republic of",
			"IQ" => "Iraq",
			"IE" => "Ireland",
			"IM" => "Isle of Man",
			"IL" => "Israel",
			"IT" => "Italy",
			"JM" => "Jamaica",
			"JP" => "Japan",
			"JE" => "Jersey",
			"JO" => "Jordan",
			"KZ" => "Kazakhstan",
			"KE" => "Kenya",
			"KI" => "Kiribati",
			"KP" => "Korea, Democratic People's Republic of",
			"KR" => "Korea, Republic of",
			"XK" => "Kosovo",
			"KW" => "Kuwait",
			"KG" => "Kyrgyzstan",
			"LA" => "Lao People's Democratic Republic",
			"LV" => "Latvia",
			"LB" => "Lebanon",
			"LS" => "Lesotho",
			"LR" => "Liberia",
			"LY" => "Libyan Arab Jamahiriya",
			"LI" => "Liechtenstein",
			"LT" => "Lithuania",
			"LU" => "Luxembourg",
			"MO" => "Macao",
			"MK" => "Macedonia, the Former Yugoslav Republic of",
			"MG" => "Madagascar",
			"MW" => "Malawi",
			"MY" => "Malaysia",
			"MV" => "Maldives",
			"ML" => "Mali",
			"MT" => "Malta",
			"MH" => "Marshall Islands",
			"MQ" => "Martinique",
			"MR" => "Mauritania",
			"MU" => "Mauritius",
			"YT" => "Mayotte",
			"MX" => "Mexico",
			"FM" => "Micronesia, Federated States of",
			"MD" => "Moldova, Republic of",
			"MC" => "Monaco",
			"MN" => "Mongolia",
			"ME" => "Montenegro",
			"MS" => "Montserrat",
			"MA" => "Morocco",
			"MZ" => "Mozambique",
			"MM" => "Myanmar",
			"NA" => "Namibia",
			"NR" => "Nauru",
			"NP" => "Nepal",
			"NL" => "Netherlands",
			"AN" => "Netherlands Antilles",
			"NC" => "New Caledonia",
			"NZ" => "New Zealand",
			"NI" => "Nicaragua",
			"NE" => "Niger",
			"NG" => "Nigeria",
			"NU" => "Niue",
			"NF" => "Norfolk Island",
			"MP" => "Northern Mariana Islands",
			"NO" => "Norway",
			"OM" => "Oman",
			"PK" => "Pakistan",
			"PW" => "Palau",
			"PS" => "Palestinian Territory, Occupied",
			"PA" => "Panama",
			"PG" => "Papua New Guinea",
			"PY" => "Paraguay",
			"PE" => "Peru",
			"PH" => "Philippines",
			"PN" => "Pitcairn",
			"PL" => "Poland",
			"PT" => "Portugal",
			"PR" => "Puerto Rico",
			"QA" => "Qatar",
			"RE" => "Reunion",
			"RO" => "Romania",
			"RU" => "Russian Federation",
			"RW" => "Rwanda",
			"BL" => "Saint Barthelemy",
			"SH" => "Saint Helena",
			"KN" => "Saint Kitts and Nevis",
			"LC" => "Saint Lucia",
			"MF" => "Saint Martin",
			"PM" => "Saint Pierre and Miquelon",
			"VC" => "Saint Vincent and the Grenadines",
			"WS" => "Samoa",
			"SM" => "San Marino",
			"ST" => "Sao Tome and Principe",
			"SA" => "Saudi Arabia",
			"SN" => "Senegal",
			"RS" => "Serbia",
			"CS" => "Serbia and Montenegro",
			"SC" => "Seychelles",
			"SL" => "Sierra Leone",
			"SG" => "Singapore",
			"SX" => "Sint Maarten",
			"SK" => "Slovakia",
			"SI" => "Slovenia",
			"SB" => "Solomon Islands",
			"SO" => "Somalia",
			"ZA" => "South Africa",
			"GS" => "South Georgia and the South Sandwich Islands",
			"SS" => "South Sudan",
			"ES" => "Spain",
			"LK" => "Sri Lanka",
			"SD" => "Sudan",
			"SR" => "Suriname",
			"SJ" => "Svalbard and Jan Mayen",
			"SZ" => "Swaziland",
			"SE" => "Sweden",
			"CH" => "Switzerland",
			"SY" => "Syrian Arab Republic",
			"TW" => "Taiwan, Province of China",
			"TJ" => "Tajikistan",
			"TZ" => "Tanzania, United Republic of",
			"TH" => "Thailand",
			"TL" => "Timor-Leste",
			"TG" => "Togo",
			"TK" => "Tokelau",
			"TO" => "Tonga",
			"TT" => "Trinidad and Tobago",
			"TN" => "Tunisia",
			"TR" => "Turkey",
			"TM" => "Turkmenistan",
			"TC" => "Turks and Caicos Islands",
			"TV" => "Tuvalu",
			"UG" => "Uganda",
			"UA" => "Ukraine",
			"AE" => "United Arab Emirates",
			"GB" => "United Kingdom",
			"US" => "United States",
			"UM" => "United States Minor Outlying Islands",
			"UY" => "Uruguay",
			"UZ" => "Uzbekistan",
			"VU" => "Vanuatu",
			"VE" => "Venezuela",
			"VN" => "Viet Nam",
			"VG" => "Virgin Islands, British",
			"VI" => "Virgin Islands, U.s.",
			"WF" => "Wallis and Futuna",
			"EH" => "Western Sahara",
			"YE" => "Yemen",
			"ZM" => "Zambia",
			"ZW" => "Zimbabwe",
		];
		
		return $countries_list;
	}
}
