<?php
function bsa_pro_head_menu()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php require_once 'dashboard.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_spaces()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php require_once 'items.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_add_new_space()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php bsaAddNewSpace(); ?>
		<?php require_once 'add-space.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_html_creator()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php require_once 'html-creator.php'; ?>
	</div>
<?php
}

function bsaAddNewSpace()
{
	$format = ((get_option('bsa_pro_plugin_currency_format')) ? explode('|', get_option('bsa_pro_plugin_currency_format')) : array(2, '.', ''));
	$clear1 = (isset($format[2]) && $format[2] != '' ? $format[2] : null);
	$clear2 = (isset($format[1]) && $format[1] != '.' ? $format[1] : null);
	$space_id = (isset($_GET['space_id']) && $_GET['space_id'] > 0 ? $_GET['space_id'] : 0);

	$cpc_price = (isset($_POST['cpc_price']) && $_POST['cpc_price'] > 0 ? $_POST['cpc_price'] : 0);
	$cpm_price = (isset($_POST['cpm_price']) && $_POST['cpm_price'] > 0 ? $_POST['cpm_price'] : 0);
	$cpd_price = (isset($_POST['cpd_price']) && $_POST['cpd_price'] > 0 ? $_POST['cpd_price'] : 0);
	if ( $clear1 != null ){
		$cpc_price = str_replace($clear1,'', $cpc_price);
		$cpm_price = str_replace($clear1,'', $cpm_price);
		$cpd_price = str_replace($clear1,'', $cpd_price);
	}
	if ( $clear2 != null ){
		$cpc_price = str_replace($clear2,'.', $cpc_price);
		$cpm_price = str_replace($clear2,'.', $cpm_price);
		$cpd_price = str_replace($clear2,'.', $cpd_price);
	}

	if ( $_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateSpace' ) {

		if ( $_POST["name"] != '' ) {

			$model = new BSA_PRO_Model();
			$cpc_status = $model->billingValidator('cpc');
			$cpm_status = $model->billingValidator('cpm');
			$cpd_status = $model->billingValidator('cpd');

			if ( $_POST['max_items'] < $model->countAds($space_id) || $cpc_status == 'incorrect' || $cpm_status == 'incorrect' || $cpd_status == 'incorrect' ) {

				if ( $_POST['max_items'] < $model->countAds($space_id) ) { // if ad limit smaller than number of active ads

					echo '
					<div class="updated settings-error">
						<p><strong>Error!</strong> You have more active ads than ad limit.<br><br>
						<strong>Note!</strong><br>
						Increase number of maximum ads.</p>
					</div>';

				} else {

					echo '
					<div class="updated settings-error">
						<p><strong>Error, incorrect contract values!</strong> Enter the price and value for a minimum of 1st contract or more (add them one by one with different values).<br><br>
						<strong>Note!</strong><br>
						Enter <strong>0</strong> in the price field if you do not want to use some of billing model.</p>
					</div>';
				}

			} else {

				$advanced_opt = array(
					'hide_for_id' => (isset($_POST['hide_for_id']) && $_POST['hide_for_id'] != '' ? implode(",", $_POST['hide_for_id']) : null), // hide for specific posts / pages
					'show_customs' => (isset($_POST['show_customs']) && $_POST['show_customs'] != '' ? $_POST['show_customs'] : null), // show for specific customs
					'hide_customs' => (isset($_POST['hide_customs']) && $_POST['hide_customs'] != '' ? $_POST['hide_customs'] : null), // hide for specific customs
				);

				$model->updateSpace(
					$space_id,
					stripslashes($_POST['name']),
					stripslashes($_POST['title']),
					stripslashes($_POST['add_new']),
					$cpc_price,
					$cpm_price,
					$cpd_price,
                    ($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_1'] > 0 ? $_POST['cpc_contract_1'] : 0) : 0),
					($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_2'] > 0 ? $_POST['cpc_contract_2'] : 0) : 0),
					($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_3'] > 0 ? $_POST['cpc_contract_3'] : 0) : 0),
					($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_1'] > 0 ? $_POST['cpm_contract_1'] : 0) : 0),
					($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_2'] > 0 ? $_POST['cpm_contract_2'] : 0) : 0),
					($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_3'] > 0 ? $_POST['cpm_contract_3'] : 0) : 0),
					($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_1'] > 0 ? $_POST['cpd_contract_1'] : 0) : 0),
					($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_2'] > 0 ? $_POST['cpd_contract_2'] : 0) : 0),
					($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_3'] > 0 ? $_POST['cpd_contract_3'] : 0) : 0),
					($_POST['discount_2'] > 0 && $_POST['discount_2'] <= 100 ? $_POST['discount_2'] : 0),
					($_POST['discount_3'] > 0 && $_POST['discount_3'] <= 100 ? $_POST['discount_3'] : 0),
					( isset($_POST['grid_system']) && $_POST['grid_system'] != '' ? $_POST['grid_system'] : ''),
					( isset($_POST['template']) && $_POST['template'] != '' ? $_POST['template'] : ''),
					( isset($_POST['display_type']) && $_POST['display_type'] != '' ? $_POST['display_type'] : ''),
					( isset($_POST['random']) && $_POST['random'] != '' ? $_POST['random'] : ''),
					( isset($_POST['max_items']) && $_POST['max_items'] != '' ? $_POST['max_items'] : ''),
					( isset($_POST['col_per_row']) && $_POST['col_per_row'] != '' ? $_POST['col_per_row'] : ''),
					( isset($_POST['font']) && $_POST['font'] != '' ? $_POST['font'] : ''),
					( isset($_POST['font_url']) && $_POST['font_url'] != '' ? $_POST['font_url'] : ''),
					( isset($_POST['header_bg']) && $_POST['header_bg'] != '' ? $_POST['header_bg'] : ''),
					( isset($_POST['header_color']) && $_POST['header_color'] != '' ? $_POST['header_color'] : ''),
					( isset($_POST['link_color']) && $_POST['link_color'] != '' ? $_POST['link_color'] : ''),
					( isset($_POST['ads_bg']) && $_POST['ads_bg'] != '' ? $_POST['ads_bg'] : ''),
					( isset($_POST['ad_bg']) && $_POST['ad_bg'] != '' ? $_POST['ad_bg'] : ''),
					( isset($_POST['ad_title_color']) && $_POST['ad_title_color'] != '' ? $_POST['ad_title_color'] : ''),
					( isset($_POST['ad_desc_color']) && $_POST['ad_desc_color'] != '' ? $_POST['ad_desc_color'] : ''),
					( isset($_POST['ad_url_color']) && $_POST['ad_url_color'] != '' ? $_POST['ad_url_color'] : ''),
					( isset($_POST['ad_extra_color_1']) && $_POST['ad_extra_color_1'] != '' ? $_POST['ad_extra_color_1'] : ''),
					( isset($_POST['ad_extra_color_2']) && $_POST['ad_extra_color_2'] != '' ? $_POST['ad_extra_color_2'] : ''),
					( isset($_POST['animation']) && $_POST['animation'] != '' ? $_POST['animation'] : ''),
					( isset($_POST['space_categories']) && $_POST['space_categories'] != '' ? implode(",", $_POST['space_categories']) : null ),
					( isset($_POST['space_tags']) && $_POST['space_tags'] != '' ? implode(",", $_POST['space_tags']) : null ),
					( isset($_POST['show_in_country']) && $_POST['show_in_country'] != '' ? implode(",", $_POST['show_in_country']) : null ),
					( isset($_POST['hide_in_country']) && $_POST['hide_in_country'] != '' ? implode(",", $_POST['hide_in_country']) : null ),
					( isset($_POST['show_in_advanced']) && $_POST['show_in_advanced'] != '' ? strtolower($_POST['show_in_advanced']).',' : null ),
					( isset($_POST['hide_in_advanced']) && $_POST['hide_in_advanced'] != '' ? strtolower($_POST['hide_in_advanced']).',' : null ),
					( isset($_POST['position']) && $_POST['position'] != '' ? $_POST['position'] : ''),
                    ( isset($_POST['devices']) && $_POST['devices'] != '' ? implode(",", $_POST['devices']) : null ),
					( isset($_POST['unavailable_dates']) && $_POST['unavailable_dates'] != '' ? $_POST['unavailable_dates'] : null ),
					( isset($_POST['show_ads']) && isset($_POST['show_close_btn']) && isset($_POST['close_ads']) ? ($_POST['show_ads'] > 0 ? $_POST['show_ads'] : '0').','.($_POST['show_close_btn'] > 0 ? $_POST['show_close_btn'] : '0').','.($_POST['close_ads'] > 0 ? $_POST['close_ads'] : '0') : '0,0,0' ),
					json_encode($advanced_opt),
					(($_POST['status'] == 'active') ? 'active' : 'inactive')
				);
				do_action( 'bsa-pro-updateSpace', $_POST, $space_id );

				delete_site_transient(bsa_get_opt('prefix').'bsa_space_'.$space_id); // reset cache

				echo '
					<div class="updated settings-error">
						<p><strong>Space updated.</strong></p>
					</div>';
			}

		} else {

			echo '
			<div class="updated settings-error">
				<p><strong>Space has not been saved.</strong> The name field is required!</p>
			</div>';
		}

	} elseif ( $_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'addNewSpace' ) {

		if ( $_POST["name"] != '' ) {

			$model = new BSA_PRO_Model();
			$cpc_status = $model->billingValidator('cpc');
			$cpm_status = $model->billingValidator('cpm');
			$cpd_status = $model->billingValidator('cpd');

			if ( $cpc_status == 'incorrect' || $cpm_status == 'incorrect' || $cpd_status == 'incorrect' ) {

				echo '
					<div class="updated settings-error">
						<p><strong>Error, incorrect contract values!</strong> Enter the price and value for a minimum of 1st contract or more (add them one by one with different values).<br><br>
						<strong>Note!</strong><br>
						Enter <strong>0</strong> in the price field if you do not want to use some of billing model.</p>
					</div>';

			} else {

				$advanced_opt = array(
					'hide_for_id' => (isset($_POST['hide_for_id']) && $_POST['hide_for_id'] != '' ? implode(",", $_POST['hide_for_id']) : null), // hide for specific posts / pages
					'show_customs' => (isset($_POST['show_customs']) && $_POST['show_customs'] != '' ? $_POST['show_customs'] : null), // show for specific customs
					'hide_customs' => (isset($_POST['hide_customs']) && $_POST['hide_customs'] != '' ? $_POST['hide_customs'] : null), // hide for specific customs
				);

				$spaces = (($model->getSpaces('active')) ? $model->getSpaces('active') : NULL);

				$dsadasd = '';
				if ( isset($spaces) && count($spaces) > 0 && get_option('bsa_pro_'.$dsadasd.'u'.'pd'.'at'.$dsadasd.'e'.'_s'.'ta'.'tus') == 'i'.$dsadasd.'n'.'v'.$dsadasd.'a'.'l'.$dsadasd.'i'.$dsadasd.'d' ) {
					echo '
							<div class="updated settings-error">
								<p><strong>Space has not been saved.</strong><br>
								You can add only one Space in the Trial Version. <a href="https://1.envato.market/buy-regular-ads-pro-6" target="_blank">Buy now</a> the latest version of Ads Pro.</p>
							</div>';
				} else {
					$model->addNewSpace(
						NULL,
						stripslashes($_POST['name']),
						stripslashes($_POST['title']),
						stripslashes($_POST['add_new']),
						$cpc_price,
						$cpm_price,
						$cpd_price,
						($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_1'] > 0 ? $_POST['cpc_contract_1'] : 0) : 0),
						($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_2'] > 0 ? $_POST['cpc_contract_2'] : 0) : 0),
						($_POST['cpc_price'] > 0 ? ($_POST['cpc_contract_3'] > 0 ? $_POST['cpc_contract_3'] : 0) : 0),
						($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_1'] > 0 ? $_POST['cpm_contract_1'] : 0) : 0),
						($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_2'] > 0 ? $_POST['cpm_contract_2'] : 0) : 0),
						($_POST['cpm_price'] > 0 ? ($_POST['cpm_contract_3'] > 0 ? $_POST['cpm_contract_3'] : 0) : 0),
						($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_1'] > 0 ? $_POST['cpd_contract_1'] : 0) : 0),
						($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_2'] > 0 ? $_POST['cpd_contract_2'] : 0) : 0),
						($_POST['cpd_price'] > 0 ? ($_POST['cpd_contract_3'] > 0 ? $_POST['cpd_contract_3'] : 0) : 0),
						($_POST['discount_2'] > 0 && $_POST['discount_2'] <= 100 ? $_POST['discount_2'] : 0),
						($_POST['discount_3'] > 0 && $_POST['discount_3'] <= 100 ? $_POST['discount_3'] : 0),
						( isset($_POST['grid_system']) && $_POST['grid_system'] != '' ? $_POST['grid_system'] : ''),
						( isset($_POST['template']) && $_POST['template'] != '' ? $_POST['template'] : ''),
						( isset($_POST['display_type']) && $_POST['display_type'] != '' ? $_POST['display_type'] : ''),
						( isset($_POST['random']) && $_POST['random'] != '' ? $_POST['random'] : ''),
						( isset($_POST['max_items']) && $_POST['max_items'] != '' ? $_POST['max_items'] : ''),
						( isset($_POST['col_per_row']) && $_POST['col_per_row'] != '' ? $_POST['col_per_row'] : ''),
						( isset($_POST['font']) && $_POST['font'] != '' ? $_POST['font'] : ''),
						( isset($_POST['font_url']) && $_POST['font_url'] != '' ? $_POST['font_url'] : ''),
						( isset($_POST['header_bg']) && $_POST['header_bg'] != '' ? $_POST['header_bg'] : ''),
						( isset($_POST['header_color']) && $_POST['header_color'] != '' ? $_POST['header_color'] : ''),
						( isset($_POST['link_color']) && $_POST['link_color'] != '' ? $_POST['link_color'] : ''),
						( isset($_POST['ads_bg']) && $_POST['ads_bg'] != '' ? $_POST['ads_bg'] : ''),
						( isset($_POST['ad_bg']) && $_POST['ad_bg'] != '' ? $_POST['ad_bg'] : ''),
						( isset($_POST['ad_title_color']) && $_POST['ad_title_color'] != '' ? $_POST['ad_title_color'] : ''),
						( isset($_POST['ad_desc_color']) && $_POST['ad_desc_color'] != '' ? $_POST['ad_desc_color'] : ''),
						( isset($_POST['ad_url_color']) && $_POST['ad_url_color'] != '' ? $_POST['ad_url_color'] : ''),
						( isset($_POST['ad_extra_color_1']) && $_POST['ad_extra_color_1'] != '' ? $_POST['ad_extra_color_1'] : ''),
						( isset($_POST['ad_extra_color_2']) && $_POST['ad_extra_color_2'] != '' ? $_POST['ad_extra_color_2'] : ''),
						( isset($_POST['animation']) && $_POST['animation'] != '' ? $_POST['animation'] : ''),
						( isset($_POST['space_categories']) && $_POST['space_categories'] != '' ? implode(",", $_POST['space_categories']) : null ),
						( isset($_POST['space_tags']) && $_POST['space_tags'] != '' ? implode(",", $_POST['space_tags']) : null ),
						( isset($_POST['show_in_country']) && $_POST['show_in_country'] != '' ? implode(",", $_POST['show_in_country']) : null ),
						( isset($_POST['hide_in_country']) && $_POST['hide_in_country'] != '' ? implode(",", $_POST['hide_in_country']) : null ),
						( isset($_POST['show_in_advanced']) && $_POST['show_in_advanced'] != '' ? strtolower($_POST['show_in_advanced']) : null ),
						( isset($_POST['hide_in_advanced']) && $_POST['hide_in_advanced'] != '' ? strtolower($_POST['hide_in_advanced']) : null ),
						( isset($_POST['position']) && $_POST['position'] != '' ? $_POST['position'] : ''),
                        ( isset($_POST['devices']) && $_POST['devices'] != '' ? implode(",", $_POST['devices']) : null ),
						( isset($_POST['unavailable_dates']) && $_POST['unavailable_dates'] != '' ? $_POST['unavailable_dates'] : null ),
						( isset($_POST['show_ads']) && isset($_POST['show_close_btn']) && isset($_POST['close_ads']) ? ($_POST['show_ads'] > 0 ? $_POST['show_ads'] : '0').','.($_POST['show_close_btn'] > 0 ? $_POST['show_close_btn'] : '0').','.($_POST['close_ads'] > 0 ? $_POST['close_ads'] : '0') : '0,0,0' ),
						json_encode($advanced_opt),
						(($_POST['status'] == 'active') ? 'active' : 'inactive')
					);

					do_action( 'bsa-pro-addNewSpace', $_POST, $model->getTableName('spaces') );
					$_SESSION['bsa_space_status'] = 'space_added';

					echo '
						<div class="updated settings-error">
							<p><strong>Space saved.</strong> Click <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-spaces">here</a> to show all spaces.</p>
						</div>';
				}
			}

		} else {

			echo '
			<div class="updated settings-error">
				<p><strong>Space has not been saved.</strong> The name field is required!</p>
			</div>';
		}
	}
}

function bsa_pro_sub_menu_add_new_ad()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php bsaAddNewAd(); ?>
		<?php require_once 'add-ad.php'; ?>
	</div>
<?php
}

function bsaAddNewAd()
{
	$plugin_id = 'bsa_pro_plugin_';

	if ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateAd') {

		// validate form
		if ( isset($_GET['ad_id']) && bsa_space(bsa_ad($_GET['ad_id'], 'space_id'), 'template') != 'html' ) {
			foreach (explode(',', str_replace('desc', 'description', $_POST['inputs_required'])) as $input) {
				$error = FALSE;
				if ($input == 'img') {
					if ($_FILES['img']["name"] == '') {
						$error = FALSE; // img not required for updateAd Action
					}
				} else {
					if ( !isset($_POST[$input]) || isset($_POST[$input]) && $_POST[$input] == '' ) {
						$error = TRUE;
					}
				}
				if ($error == TRUE) {
					echo '
				<div class="updated settings-error">
					<p><strong>Ad has not been saved.</strong> The ' . str_replace(',', ', ', str_replace('desc', 'description', $_POST['inputs_required'])) . ' fields are required!</p>
				</div>';
					return;
				}
			}
		}

		if ( $_POST["buyer_email"] != '' ) {

			// if isset img
			$uploadName = strtolower($_FILES["img"]["name"]);
			if ( $uploadName ) {
				$allowedExts = array("gif", "jpeg", "jpg", "png", "webp");
				$temp = explode(".", $uploadName);
				$extension = end($temp);
				$fileName = NULL;

				if ((($_FILES["img"]["type"] == "image/gif")
						|| ($_FILES["img"]["type"] == "image/jpeg")
						|| ($_FILES["img"]["type"] == "image/jpg")
						|| ($_FILES["img"]["type"] == "image/pjpeg")
						|| ($_FILES["img"]["type"] == "image/x-png")
						|| ($_FILES["img"]["type"] == "image/png")
				     || ($_FILES["img"]["type"] == "image/webp"))
					&& $_FILES["img"]["error"] == 0
					&& in_array($extension, $allowedExts)) {

					$fileName = time().'-'.$uploadName;
					$path     = bsa_upload_url('basedir', $fileName);
					$thumbLoc = $_FILES["img"]["tmp_name"];

					list($width, $height) = getimagesize($thumbLoc);
					$maxSize = get_option($plugin_id.'thumb_size');
					$maxWidth = get_option($plugin_id.'thumb_w');
					$maxHeight = get_option($plugin_id.'thumb_h');

					if (($_FILES["img"]["size"] > $maxSize * 1024) OR $width > $maxWidth OR $height > $maxHeight) {
						echo '
						<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Images was too high.</p>
						</div>';
						return;
					} else {
						// save img
						move_uploaded_file($thumbLoc, $path);
					}
				} else {
					echo '
					<div class="updated settings-error">
						<p><strong>Ad has not been saved.</strong> Type of image invalid.</p>
					</div>
					';
					return;
				}
			} else {
				$fileName = (isset($_POST["img_url"]) && $_POST["img_url"] != '' ? $_POST["img_url"] : null);
			}

			// validate start & end dates
			$starts = bsaGetPost('start_date').' '.bsaGetPost('start_time');
			$ends = bsaGetPost('end_date').' '.bsaGetPost('end_time');
			if ( $starts != ' ' && bsaValidateDatetime($starts) == false || $starts != ' ' && strtotime($starts) < time() ) {
				echo '	<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Invalid Start Date (format date: 2022-01-30, time: 15:30).</p>
						</div>
						';
				return;
			}
			if ( $ends != ' ' && bsaValidateDatetime($ends) == false || $ends != ' ' && strtotime($ends) < time() || $ends != ' ' && $starts != ' ' && strtotime($ends) < strtotime($starts) ) {
				echo '	<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Invalid End Date (format date: 2022-01-30, time: 15:30).</p>
						</div>
						';
				return;
			}

			$limit = bsa_ad($_GET['ad_id'], 'ad_limit');
			if ( isset($_POST["increase_limit"]) && $_POST["increase_limit"] != '' ) {
				if ( $_POST["increase_limit"] > 0 || $_POST["increase_limit"] < 0 ) { // increase / decrease limit
					if ( bsa_ad($_GET['ad_id'], 'ad_model') == 'cpd' ) {
						$time = time();
						$increase = $_POST["increase_limit"] * 24 * 60 * 60;
						$diff = $limit - $time;
						$increase_limit = ($diff <= 0) ? $time + $increase : $limit + $increase;
					} else {
						$increase_limit = $limit + $_POST["increase_limit"];
					}
				} else {
					$increase_limit = bsa_ad($_GET['ad_id'], 'ad_limit');
				}
			} else {
				$increase_limit = null;
			}

			if ( (bsa_role() == 'user') ) {
				$status = ((get_option('bsa_pro_plugin_auto_accept') == 'no') ? 'pending' : null);
				$increase_limit = null;
			} else {
				$status = (in_array($_POST["ad_status"], array('draft', 'active', 'pending', 'blocked')) ? $_POST["ad_status"] : null);
			}

			$capping = ( isset($_POST["capping"]) && $_POST["capping"] > 0 ? number_format($_POST["capping"], 0, '', '') : 0);
			$ad_name = ( isset($_POST["ad_name"]) && $_POST["ad_name"] != '' ) ? $_POST["ad_name"] : null;
			$optional_field = ( isset($_POST["optional_field"]) && $_POST["optional_field"] != '' ) ? $_POST["optional_field"] : null;

			$space_id = bsa_ad($_GET['ad_id'], 'space_id');
			if ( $capping > 0 ) {
				$_SESSION['capped_ad_'.$_GET['ad_id']] = $capping;
			} else {
				unset($_SESSION['capped_ad_'.$_GET['ad_id']]);
			}
			if ( isset($_SESSION['bsa_capped_ads_' . $space_id]) ) {
				$_SESSION['bsa_capped_ads_' . $space_id] = str_replace(',' . $_GET['ad_id'], '', $_SESSION['bsa_capped_ads_' . $space_id]);
			}

			$model = new BSA_PRO_Model();
			$model->updateAd(
					$_GET['ad_id'],
					$ad_name,
					$_POST["buyer_email"],
					$_POST["title"],
					$_POST["description"],
					$_POST["button"],
					$_POST["url"],
					$fileName,
					stripslashes( $_POST["html"] ),
					$capping,
					$optional_field,
					$increase_limit,
					( $starts != ' ' && strtotime($starts) > time() ? strtotime($starts) : null ),
					( $ends != ' ' && strtotime($ends) > time() ? strtotime($ends) : null ),
					( isset($_POST['show_in_country']) && $_POST['show_in_country'] != '' ? implode(",", $_POST['show_in_country']) : null ),
					( isset($_POST['show_in_advanced']) && $_POST['show_in_advanced'] != '' ? strtolower($_POST['show_in_advanced']).',' : null ),
					$status,
				    ( isset($_POST['space_ids']) && $_POST['space_ids'] != '' ? implode(",", $_POST['space_ids']) : null ));

			echo '
						<div class="updated settings-error">
							<p><strong>Success!</strong> Ad saved.</p>
						</div>';

		} else {

			echo '
			<div class="updated settings-error">
				<p><strong>Ad has not been saved.</strong> The buyer email field is required!</p>
			</div>';

		}

	} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'addNewAd') {

		// validate form
			if ( isset($_POST["space_id"]) && bsa_space($_POST["space_id"], 'template') != 'html' ) {
			foreach ( explode(',', str_replace('desc', 'description', $_POST['inputs_required'])) as $input ) {
				$error = FALSE;
				if ( $input == 'img' ) {
					if ( $_FILES['img']["name"] != '' || isset($_POST["img_url"]) && $_POST["img_url"] != '' ) {
						$error = FALSE;
					} else {
						$error = TRUE;
					}
				} else {
					if ( $_POST[$input] == '' ) {
						$error = TRUE;
					}
				}
				if ( $error == TRUE ) {
					echo '
					<div class="updated settings-error">
						<p><strong>Ad has not been saved.</strong> The '.str_replace(',', ', ', str_replace('desc', 'description', $_POST['inputs_required'])).' fields are required!</p>
					</div>';
					return;
				}
			}
		}

		if ( isset($_POST["buyer_email"]) && $_POST["buyer_email"] != '' && isset($_POST["space_id"]) && $_POST["space_id"] != '' && isset($_POST["ad_model"]) && $_POST["ad_model"] != '' && isset($_POST["ad_limit_" . $_POST["ad_model"]]) && $_POST["ad_limit_" . $_POST["ad_model"]] != '' ) {

			// if isset img
			if ( $_FILES['img']["name"] ) {
				$allowedExts = array("gif", "jpeg", "jpg", "png", "webp", "GIF", "JPEG", "JPG", "PNG", "WEBP");
				$temp = explode(".", $_FILES["img"]["name"]);
				$extension = end($temp);
				$fileName = NULL;

				if ((($_FILES["img"]["type"] == "image/gif")
						|| ($_FILES["img"]["type"] == "image/jpeg")
						|| ($_FILES["img"]["type"] == "image/jpg")
						|| ($_FILES["img"]["type"] == "image/pjpeg")
						|| ($_FILES["img"]["type"] == "image/x-png")
						|| ($_FILES["img"]["type"] == "image/png")
				     || ($_FILES["img"]["type"] == "image/webp"))
					&& $_FILES["img"]["error"] == 0
					&& in_array($extension, $allowedExts)) {

					$fileName = time().'-'.$_FILES["img"]["name"];
					$path     = bsa_upload_url('basedir', $fileName);
					$thumbLoc = $_FILES["img"]["tmp_name"];

					list($width, $height) = getimagesize($thumbLoc);
					$maxSize = get_option($plugin_id.'thumb_size');
					$maxWidth = get_option($plugin_id.'thumb_w');
					$maxHeight = get_option($plugin_id.'thumb_h');

					if (($_FILES["img"]["size"] > $maxSize * 1024) OR $width > $maxWidth OR $height > $maxHeight) {
						echo '
						<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Images was too high.</p>
						</div>';
						return;
					} else {
						// save img
						move_uploaded_file($thumbLoc, $path);
					}
				} else {
					echo '
					<div class="updated settings-error">
						<p><strong>Ad has not been saved.</strong> Type of image invalid.</p>
					</div>
					';
					return;
				}
			} else {
				$fileName = (isset($_POST["img_url"]) && $_POST["img_url"] != '' ? $_POST["img_url"] : '');
			}

			// validate start & end dates
			$starts = bsaGetPost('start_date').' '.bsaGetPost('start_time');
			$ends = bsaGetPost('end_date').' '.bsaGetPost('end_time');
			if ( $starts != ' ' && bsaValidateDatetime($starts) == false || $starts != ' ' && strtotime($starts) < time() ) {
				echo '	<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Invalid Start Date (format date: 2022-01-30, time: 15:30).</p>
						</div>
						';
				return;
			}
			if ( $ends != ' ' && bsaValidateDatetime($ends) == false || $ends != ' ' && strtotime($ends) < time() || $ends != ' ' && $starts != ' ' && strtotime($ends) < strtotime($starts) ) {
				echo '	<div class="updated settings-error">
							<p><strong>Ad has not been saved.</strong> Invalid End Date (format date: 2022-01-30, time: 15:30).</p>
						</div>
						';
				return;
			}

			// set limit for cpd - change days to timestamp
			if ( $_POST["ad_model"] == 'cpd' ) {
				$ad_limit = time() + ($_POST["ad_limit_" . $_POST["ad_model"]] * 24 * 60 * 60);
			} else {
				$ad_limit = $_POST["ad_limit_" . $_POST["ad_model"]];
			}

			$model = new BSA_PRO_Model();

			if ( (bsa_role() == 'user') ) {
				$status = ((get_option('bsa_pro_plugin_auto_accept') == 'no') ? 'pending' : 'active');
			} else {
				$status = $_POST["ad_status"];
			}

			$capping = ( $_POST["capping"] > 0 ? number_format($_POST["capping"], 0, '', '') : 0);
			$ad_name = ( isset($_POST["ad_name"]) && $_POST["ad_name"] != '' ) ? $_POST["ad_name"] : null;
			$optional_field = ( isset($_POST["optional_field"]) && $_POST["optional_field"] != '' ) ? $_POST["optional_field"] : null;

			$model->addNewAd(
			NULL,
				$_POST["space_id"],
				$ad_name,
				$_POST["buyer_email"],
				$_POST["title"],
				$_POST["description"],
				$_POST["button"],
				$_POST["url"],
				$fileName,
				stripslashes( $_POST["html"] ),
				$optional_field,
				$_POST["ad_model"],
				$ad_limit,
				$capping,
				0.00,
				2,
				( $starts != ' ' && strtotime($starts) > time() ? strtotime($starts) : null ),
				( $ends != ' ' && strtotime($ends) > time() ? strtotime($ends) : null ),
				( isset($_POST['show_in_country']) && $_POST['show_in_country'] != '' ? implode(",", $_POST['show_in_country']) : null ),
				( isset($_POST['show_in_advanced']) && $_POST['show_in_advanced'] != '' ? strtolower($_POST['show_in_advanced']) : null ),
				$status); // paid 2 - Added via Admin Panel

			$_SESSION['bsa_ad_status'] = 'ad_added';

			echo '
			<div class="updated settings-error">
				<p><strong>Success!</strong> Ad saved.</p>
			</div>';

		} else {

			echo '
			<div class="updated settings-error">
				<p><strong>Ad has not been saved.</strong> The buyer email, space id, billing model fields are required!</p>
			</div>';

		}
	}
}

function bsa_pro_sub_menu_creator()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		} ?>
		<?php bsaCreateAdTemplate(); ?>
		<?php require_once 'creator.php'; ?>
	</div>
<?php
}

function bsaCreateAdTemplate($width = null, $height = null) {
	// Determine if this is a form submission from the admin Creator page
	$is_form_submit = (
		$_SERVER['REQUEST_METHOD'] == 'POST' &&
		isset( $_POST['bsaProAction'] ) &&
		$_POST['bsaProAction'] == 'adCreator'
	);

	// If called from the form, enforce capability and nonce to prevent CSRF/code injection
	if ( $is_form_submit ) {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( __( 'You are not allowed to create ad templates.', 'bsa-pro' ) );
		}

		if (
			! isset( $_POST['bsa_create_template_nonce'] ) ||
			! wp_verify_nonce( $_POST['bsa_create_template_nonce'], 'bsa_create_template' )
		) {
			wp_die( __( 'Security check failed.', 'bsa-pro' ) );
		}
	}

	if ( $is_form_submit || ( $width !== null && $height !== null ) ) {
		// Validate ad dimensions
		if (
			( isset( $_POST['ad_width'], $_POST['ad_height'] ) &&
			  intval( $_POST['ad_width'] ) > 0 &&
			  intval( $_POST['ad_height'] ) > 0 ) ||
			( $width !== null && $height !== null )
		) {

			$ad_width  = ( $width === null ) ? intval( $_POST['ad_width'] ) : intval( $width );
			$ad_height = ( $height === null ) ? intval( $_POST['ad_height'] ) : intval( $height );

			// Prepare paths
			$upload_dir = wp_upload_dir();
			$base_dir   = trailingslashit( $upload_dir['basedir'] ) . 'bsa-templates/';

			// Ensure directory exists
			if ( ! file_exists( $base_dir ) ) {
				wp_mkdir_p( $base_dir );
			}

			// Template name
			$s_width  = $ad_width;
			$s_height = $ad_height;
			$template_slug = 'block-' . $s_width . '--' . $s_height;

			$css_styles = '
/* -- START -- Reset */
#bsa-block-'.$ad_width.'--'.$ad_height.' h3,
#bsa-block-'.$ad_width.'--'.$ad_height.' a,
#bsa-block-'.$ad_width.'--'.$ad_height.' img,
#bsa-block-'.$ad_width.'--'.$ad_height.' span,
#bsa-block-'.$ad_width.'--'.$ad_height.' p {
	margin: 0;
	padding: 0;
	border: 0;
	border-radius: 0;
	-webkit-box-shadow: none;
	-moz-box-shadow: none;
	box-shadow: none;
	text-decoration: none;
	line-height: 1.25;
}
/* -- END -- Reset */


/* -- START -- TEMPLATE */
#bsa-block-'.$ad_width.'--'.$ad_height.'.apPluginContainer .bsaProItem,
#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProItemInner__thumb,
#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProAnimateThumb {
	max-width: '.$ad_width.'px;
	max-height: '.$ad_height.'px;
	aspect-ratio: '.$ad_width.'/'.$ad_height.';
}

#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProAnimateThumb {
	position: relative;
	width: 100%;
	height: '.$ad_height.'px;
}

#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProAnimateThumb:before{
	content: "";
	display: block;
}

#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProItemInner__img {
	height: 100%;
}

#bsa-block-'.$ad_width.'--'.$ad_height.' .bsaProItemInner__img {
	position:  absolute;
	width: 100%;
	max-width: '.$ad_width.'px;
	height: 100%;
	max-height: '.$ad_height.'px;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	background-size: 100%;
	background-repeat: no-repeat;
}
/* -- END -- TEMPLATE */';

			$css_path      = plugin_dir_path(__DIR__) . 'frontend/css/block-' . $s_width . '--' . $s_height . '.css';
			$template_path = plugin_dir_path(__DIR__) . 'frontend/template/block-' . $s_width . '--' . $s_height . '.php';

			if (is_writable(dirname($css_path)) && is_writable(dirname($template_path))) {
				file_put_contents($css_path, $css_styles, LOCK_EX);

				$template_code = file_get_contents(plugin_dir_path(__DIR__) . 'frontend/template/block-300--250.php');
				if ($template_code !== false) {
					$template_overwrite = str_replace(['300', '250'], ['11111', '22222'], $template_code);
					$template_php       = str_replace(['11111', '22222'], [$s_width, $s_height], $template_overwrite);
					file_put_contents($template_path, $template_php, LOCK_EX);
				}

				$custom_templates = get_option('bsa_pro_plugin_custom_templates', '');
				$templates_array  = array_filter(array_map('trim', explode(',', $custom_templates)));
				if (!in_array($s_width . '--' . $s_height, $templates_array, true)) {
					$templates_array[] = $s_width . '--' . $s_height;
					update_option('bsa_pro_plugin_custom_templates', implode(',', $templates_array));
				}

				// Generate CSS if needed
				bsa_pro_generate_css( get_option( 'bsa_pro_plugin_rtl_support' ) === 'yes' );

				if ( $width === null && $height === null ) {
					printf(
						'<div class="updated settings-error"><p><strong>%s</strong></p></div>',
						esc_html( sprintf( __( 'Ad Template (%s) has been saved.', 'bsa-pro' ), $template_slug ) )
					);
				}
			} else {
				if ( $width === null && $height === null ) {
					echo '<div class="error settings-error"><p><strong>Error!</strong> Template could not be created. Please check folder permissions.</p></div>';
				}
			}
		} else {
			if ( $width === null && $height === null ) {
				echo '<div class="error settings-error"><p><strong>Error!</strong> Both fields are required!</p></div>';
			}
		}
	}
}


function bsa_pro_sub_menu_settings()
{
	?>
	<div class="wrap">
		<?php
		if ( is_multisite() && is_main_site() ) {
			if (get_site_option('bsa_pro_plugin_main_basedir') == null ||
				get_site_option('bsa_pro_plugin_main_baseurl') == null ||
				get_site_option('bsa_pro_plugin_order_form_url') == null ||
				get_site_option('bsa_pro_plugin_order_form_url') != get_option('bsa_pro_plugin_ordering_form_url') ||
				get_site_option('bsa_pro_plugin_agency_order_form_url') == null ||
				get_site_option('bsa_pro_plugin_agency_order_form_url') != get_option('bsa_pro_plugin_agency_ordering_form_url')) {
				$upload_dir = wp_upload_dir();
				update_site_option('bsa_pro_plugin_main_basedir', $upload_dir['basedir']);
				update_site_option('bsa_pro_plugin_main_baseurl', $upload_dir['baseurl']);
				update_site_option('bsa_pro_plugin_order_form_url', get_option('bsa_pro_plugin_ordering_form_url'));
				update_site_option('bsa_pro_plugin_agency_order_form_url', get_option('bsa_pro_plugin_agency_ordering_form_url'));
			}
		} ?>
		<?php bsaUpdateSettings(); ?>
		<?php require_once 'settings.php'; ?>

	</div>
<?php
}

function bsaUpdateSettings()
{
	$opt = 'bsa_pro_plugin_';

	if ( $_SERVER["REQUEST_METHOD"] == "POST" &&
		$_POST["bsaProAction"] == 'updateSettings' &&
		$_POST["ordering_form_url"] != '' )
	{
		// Check Update
		update_option('bsa_pro_update_version', 100);
		// Counters
		if ( isset( $_POST['clicks_counter'] ) && $_POST['clicks_counter'] > 0 ) {
			update_option('bsa_pro_plugin_dashboard_clicks', $_POST['clicks_counter']);
		}
		if ( isset( $_POST['views_counter'] ) && $_POST['views_counter'] > 0 ) {
			update_option('bsa_pro_plugin_dashboard_views', $_POST['views_counter']);
		}
		// Settings
		update_option($opt.'settings', array(
			// woo
			'woo_item' 				=> $_POST['woo_item'],
			// form
			'form_restrictions' 	=> $_POST['form_restrictions'],
			// notifications
			'up_expires_notice' 	=> ( isset($_POST['up_expires_notice']) && $_POST['up_expires_notice'] == 'yes' ? $_POST['up_expires_notice'] : 'no' ),
			'up_expired_notice' 	=> ( isset($_POST['up_expired_notice']) && $_POST['up_expired_notice'] == 'yes' ? $_POST['up_expired_notice'] : 'no' ),
			'up_cpc_notice' 		=> ( $_POST['up_cpc_notice'] > 0 ? $_POST['up_cpc_notice'] : 10 ),
			'up_cpm_notice' 		=> ( $_POST['up_cpm_notice'] > 0 ? $_POST['up_cpm_notice'] : 1000 ),
			'up_cpd_notice' 		=> ( $_POST['up_cpd_notice'] > 0 ? $_POST['up_cpd_notice'] : 5 ),
		));
		// order form
		update_option($opt.'order_form', array(
			'optional_field' 		=> $_POST['optional_field'],
		));
		// plugin settings
		update_option($opt.'purchase_code', str_replace(' ', '', $_POST['purchase_code']));
		update_option($opt.'paypal', $_POST['paypal']);
		update_option($opt.'secret_key', $_POST['secret_key']);
		update_option($opt.'publishable_key', $_POST['publishable_key']);
		update_option($opt.'trans_payment_bank_transfer_content', $_POST['trans_payment_bank_transfer_content']);
		update_option($opt.'ordering_form_url', $_POST['ordering_form_url']);
		update_option($opt.'currency_code', $_POST['currency_code']);
		update_option($opt.'stripe_code', $_POST['stripe_code']);
		update_option($opt.'currency_symbol', $_POST['currency_symbol']);
		update_option($opt.'symbol_position', $_POST['symbol_position']);
		update_option($opt.'auto_accept', $_POST['auto_accept']);
		update_option($opt.'calendar', $_POST['calendar']);
		update_option($opt.'advanced', array(
			'order_by' 			=> (isset($_POST['order_by']) ? $_POST['order_by'] : 'id'),
			'filters' 			=> 'disabled',
			'geotarget' 		=> 0, // increase % of the price
			'terms' 			=> 0, // increase % of the price
		));
		// installation settings
		update_option($opt.'installation', $_POST['installation']);
		// hooks settings
		update_site_option($opt.'before_hook', str_replace('\"', '', $_POST['before_hook']));
		update_site_option($opt.'after_1_paragraph', str_replace('\"', '', $_POST['after_1_paragraph']));
		update_site_option($opt.'after_2_paragraph', str_replace('\"', '', $_POST['after_2_paragraph']));
		update_site_option($opt.'after_3_paragraph', str_replace('\"', '', $_POST['after_3_paragraph']));
		update_site_option($opt.'after_4_paragraph', str_replace('\"', '', $_POST['after_4_paragraph']));
		update_site_option($opt.'after_5_paragraph', str_replace('\"', '', $_POST['after_5_paragraph']));
		update_site_option($opt.'after_6_paragraph', str_replace('\"', '', $_POST['after_6_paragraph']));
		update_site_option($opt.'after_7_paragraph', str_replace('\"', '', $_POST['after_7_paragraph']));
		update_site_option($opt.'after_8_paragraph', str_replace('\"', '', $_POST['after_8_paragraph']));
		update_site_option($opt.'after_9_paragraph', str_replace('\"', '', $_POST['after_9_paragraph']));
		update_site_option($opt.'after_10_paragraph', str_replace('\"', '', $_POST['after_10_paragraph']));
		update_site_option($opt.'after_hook', str_replace('\"', '', $_POST['after_hook']));
		// BuddyPress hooks
		update_option($opt.'bp_stream_hook', array(
			1 	=> str_replace('\"', '', $_POST['after_1_activity']),
			2 	=> str_replace('\"', '', $_POST['after_2_activity']),
			3 	=> str_replace('\"', '', $_POST['after_3_activity']),
			4 	=> str_replace('\"', '', $_POST['after_4_activity']),
			5 	=> str_replace('\"', '', $_POST['after_5_activity']),
			6 	=> str_replace('\"', '', $_POST['after_6_activity']),
			7 	=> str_replace('\"', '', $_POST['after_7_activity']),
			8 	=> str_replace('\"', '', $_POST['after_8_activity']),
			9 	=> str_replace('\"', '', $_POST['after_9_activity']),
			10 	=> str_replace('\"', '', $_POST['after_10_activity']),
			11 	=> str_replace('\"', '', $_POST['after_11_activity']),
			12 	=> str_replace('\"', '', $_POST['after_12_activity']),
			13 	=> str_replace('\"', '', $_POST['after_13_activity']),
			14 	=> str_replace('\"', '', $_POST['after_14_activity']),
			15 	=> str_replace('\"', '', $_POST['after_15_activity']),
			16 	=> str_replace('\"', '', $_POST['after_16_activity']),
			17 	=> str_replace('\"', '', $_POST['after_17_activity']),
			18 	=> str_replace('\"', '', $_POST['after_18_activity']),
			19 	=> str_replace('\"', '', $_POST['after_19_activity']),
			20 	=> str_replace('\"', '', $_POST['after_20_activity']),
		));
		// bbPress hooks
		$imp_forum = array();
		for ( $i = 1; $i <= get_option( '_bbp_topics_per_page', '15' ); $i++ ) {
			$imp_forum[$i] = str_replace('\"', '', $_POST['after_'.$i.'_topic']);
		}
		update_option($opt.'bbp_forum_hook', $imp_forum);
		$imp_topic = array();
		for ( $i = 1; $i <= get_option( '_bbp_replies_per_page', '15' ); $i++ ) {
			$imp_topic[$i] = str_replace('\"', '', $_POST['after_'.$i.'_reply']);
		}
		update_option($opt.'bbp_topic_hook', $imp_topic);
		// admin panel settings
		update_option($opt.'username', $_POST['username']);
		update_option($opt.'free_ads_limit', ($_POST['free_ads_limit'] > 0 ? $_POST['free_ads_limit'] : 0));
		update_option($opt.'editable', $_POST['editable']);
		update_option($opt.'rtl_support', $_POST['rtl_support']);
		update_option($opt.'html_preview', $_POST['html_preview']);
		update_option($opt.'hide_if_logged', $_POST['hide_if_logged']);
		update_option($opt.'link_bar', $_POST['link_bar']);
		update_option($opt.'upload_dir', preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '', $_POST['upload_dir'])));
		update_option($opt.'prefix', $_POST['prefix']);
		update_option($opt.'admin_settings', array(
			'selection' 	=> $_POST['selection'],
			'nofollow' 		=> $_POST['nofollow'],
			'privileges' 	=> $_POST['privileges'],
			'ad_name' 		=> $_POST['ad_name'],
		));
		// affiliate program
		update_option($opt.'ap_cookie_lifetime', ($_POST['ap_cookie_lifetime'] >= 10) ? $_POST['ap_cookie_lifetime'] : 30);
		update_option($opt.'ap_commission', ($_POST['ap_commission'] >= 0 && $_POST['ap_commission'] <= 100) ? number_format(($_POST['ap_commission'] > 0 ? $_POST['ap_commission'] : 0), 0, '', '') : 10);
		update_option($opt.'ap_minimum_withdrawal', ($_POST['ap_minimum_withdrawal'] >= 0) ? $_POST['ap_minimum_withdrawal'] : 50);
		// marketing agency
		update_option($opt.'private_ma', $_POST['private_ma']);
		update_option($opt.'agency_api_url', $_POST['agency_api_url']);
		update_option($opt.'agency_ordering_form_url', $_POST['agency_ordering_form_url']);
		update_option($opt.'agency_commission', $_POST['agency_commission']);
		update_option($opt.'agency_other_sites', $_POST['agency_other_sites']);
		update_option($opt.'agency_auto_accept', $_POST['agency_auto_accept']);
		update_option($opt.'agency_minimum_withdrawal', $_POST['agency_minimum_withdrawal']);
		// thumbnail settings
		update_option($opt.'carousel_script', (isset($_POST['carousel_script']) && $_POST['carousel_script'] == 'bx' ? 'bx' : 'owl'));
		update_site_option($opt.'example_ad', $_POST['example_ad']);
		update_option($opt.'thumb_size', $_POST['thumb_size']);
		update_option($opt.'thumb_w', $_POST['thumb_w']);
		update_option($opt.'thumb_h', $_POST['thumb_h']);
		// length of inputs
		update_option($opt.'max_title', 40);
		update_option($opt.'max_desc', 80);
		// form customization
		update_option($opt.'form_bg', $_POST['form_bg']);
		update_option($opt.'form_c', $_POST['form_c']);
		update_option($opt.'form_input_bg', $_POST['form_input_bg']);
		update_option($opt.'form_input_c', $_POST['form_input_c']);
		update_option($opt.'form_price_c', $_POST['form_price_c']);
		update_option($opt.'form_discount_bg', $_POST['form_discount_bg']);
		update_option($opt.'form_discount_c', $_POST['form_discount_c']);
		update_option($opt.'form_button_bg', $_POST['form_button_bg']);
		update_option($opt.'form_button_c', $_POST['form_button_c']);
		update_option($opt.'form_alert_c', $_POST['form_alert_c']);
		update_option($opt.'form_alert_success_bg', $_POST['form_alert_success_bg']);
		update_option($opt.'form_alert_failed_bg', $_POST['form_alert_failed_bg']);
		update_option($opt.'stats_views_line', $_POST['stats_views_line']);
		update_option($opt.'stats_clicks_line', $_POST['stats_clicks_line']);
		update_option($opt.'custom_css', stripslashes_deep($_POST['custom_css']));
		update_option($opt.'custom_js', stripslashes_deep($_POST['custom_js']));
		if ( isset($_POST['advanced_calendar']) ) {
			update_option($opt.'advanced_calendar', stripslashes_deep($_POST['advanced_calendar']));
		}
		// other settings
		update_option($opt.'other', array(
			'optimization' 		=> $_POST['optimization'],
			'countdown' 		=> $_POST['countdown'],
			'crop_tool' 		=> $_POST['crop_tool'],
		));
		// affiliate program customization
		update_option($opt.'ap_custom', array(
			'general_bg' 		=> $_POST['general_bg'],
			'general_color' 	=> $_POST['general_color'],
			'commission_bg' 	=> $_POST['commission_bg'],
			'commission_color' 	=> $_POST['commission_color'],
			'balance_bg' 		=> $_POST['balance_bg'],
			'balance_color' 	=> $_POST['balance_color'],
			'link_color' 		=> $_POST['link_color'],
			'ref_bg' 			=> $_POST['ref_bg'],
			'ref_color' 		=> $_POST['ref_color'],
			'table_bg' 			=> $_POST['table_bg'],
			'table_color' 		=> $_POST['table_color']
		));
		// user panel customization
		update_option($opt.'user_panel', array(
			'head_bg' 			=> $_POST['head_bg'],
			'head_color' 		=> $_POST['head_color'],
			'body_bg' 			=> $_POST['body_bg'],
			'body_color' 		=> $_POST['body_color'],
			'separator' 		=> $_POST['separator'],
			'link_color' 		=> $_POST['link_color'],
			'pending_bg' 		=> $_POST['pending_bg'],
			'pending_color' 	=> $_POST['pending_color'],
			'active_bg' 		=> $_POST['active_bg'],
			'active_color' 		=> $_POST['active_color'],
			'expired_bg' 		=> $_POST['expired_bg'],
			'expired_color' 	=> $_POST['expired_color'],
			'button_bg' 		=> $_POST['button_bg'],
			'button_color' 		=> $_POST['button_color']
		));
		if ( isset($_POST['custom_js']) && $_POST['custom_js'] != '' ) {
			file_put_contents(plugin_dir_path( __FILE__ ) . '/../frontend/js/custom.js', stripslashes_deep($_POST['custom_js']));
		}

		// re-generate css
		bsa_pro_generate_css( (get_option('bsa_pro_plugin_rtl_support') == 'yes' ? true : null) );

		echo '
		<div class="updated settings-error">
			<p><strong>Settings saved.</strong></p>
		</div>';

	} else {

		if( $_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateSettings' && $_POST["ordering_form_url"] == '' ||
			$_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateSettings' && $_POST["ordering_form_url"] == '#' ) {
			echo '
			<div class="updated settings-error">
				<p><strong>Note!</strong> URL to ordering form field is required because is used to display statistics!</p>
			</div>';
		}
	}
}

function bsa_pro_sub_menu_translations()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php bsaUpdateTranslations(); ?>
		<?php require_once 'translations.php'; ?>
	</div>
<?php
}

function bsaUpdateTranslations()
{
	$opt = 'bsa_pro_plugin_';
	$opt_trans = 'bsa_pro_plugin_trans_';

	if ( $_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateTranslations')
	{
		// Translations
		// agency ordering form
		update_option($opt_trans.'agency_title_form', stripslashes($_POST['agency_title_form']));
		update_option($opt_trans.'agency_back_button', stripslashes($_POST['agency_back_button']));
		update_option($opt_trans.'agency_visit_site', stripslashes($_POST['agency_visit_site']));
		update_option($opt_trans.'agency_buy_ad', stripslashes($_POST['agency_buy_ad']));
		// form left
		update_option($opt_trans.'form_left_header', stripslashes($_POST['form_left_header']));
		update_option($opt_trans.'edit_left_header', stripslashes($_POST['edit_left_header']));
		update_option($opt_trans.'form_left_select_space', stripslashes($_POST['form_left_select_space']));
		update_option($opt_trans.'form_left_email', stripslashes($_POST['form_left_email']));
		update_option($opt_trans.'form_left_eg_email', stripslashes($_POST['form_left_eg_email']));
		update_option($opt_trans.'form_left_title', stripslashes($_POST['form_left_title']));
		update_option($opt_trans.'form_left_eg_title', stripslashes($_POST['form_left_eg_title']));
		update_option($opt_trans.'form_left_desc', stripslashes($_POST['form_left_desc']));
		update_option($opt_trans.'form_left_eg_desc', stripslashes($_POST['form_left_eg_desc']));
		update_option($opt_trans.'form_left_url', stripslashes($_POST['form_left_url']));
		update_option($opt_trans.'form_left_eg_url', stripslashes($_POST['form_left_eg_url']));
		update_option($opt_trans.'form_left_thumb', stripslashes($_POST['form_left_thumb']));
		update_option($opt_trans.'form_left_calendar', stripslashes($_POST['form_left_calendar']));
		update_option($opt_trans.'form_left_eg_calendar', stripslashes($_POST['form_left_eg_calendar']));
		// form right
		update_option($opt_trans.'form_right_header', stripslashes($_POST['form_right_header']));
		update_option($opt_trans.'form_right_cpc_name', stripslashes($_POST['form_right_cpc_name']));
		update_option($opt_trans.'form_right_cpm_name', stripslashes($_POST['form_right_cpm_name']));
		update_option($opt_trans.'form_right_cpd_name', stripslashes($_POST['form_right_cpd_name']));
		update_option($opt_trans.'form_right_clicks', stripslashes($_POST['form_right_clicks']));
		update_option($opt_trans.'form_right_views', stripslashes($_POST['form_right_views']));
		update_option($opt_trans.'form_right_days', stripslashes($_POST['form_right_days']));
		update_option($opt_trans.'form_live_preview', stripslashes($_POST['form_live_preview']));
		update_option($opt_trans.'form_right_button_pay', stripslashes($_POST['form_right_button_pay']));
		update_option($opt_trans.'edit_right_button_pay', stripslashes($_POST['edit_right_button_pay']));
		// payments
		update_option($opt_trans.'payment_paid', stripslashes($_POST['payment_paid']));
		update_option($opt_trans.'payment_select', stripslashes($_POST['payment_select']));
		update_option($opt_trans.'payment_return', stripslashes($_POST['payment_return']));
		update_option($opt_trans.'payment_stripe_title', stripslashes($_POST['payment_stripe_title']));
		update_option($opt_trans.'payment_paypal_title', stripslashes($_POST['payment_paypal_title']));
		update_option($opt_trans.'payment_bank_transfer_title', stripslashes($_POST['payment_bank_transfer_title']));
		// alerts
		// success
		update_option($opt_trans.'alert_success', stripslashes($_POST['alert_success']));
		update_option($opt_trans.'form_success', stripslashes($_POST['form_success']));
		update_option($opt_trans.'payment_success', stripslashes($_POST['payment_success']));
		// failed
		update_option($opt_trans.'alert_failed', stripslashes($_POST['alert_failed']));
		update_option($opt_trans.'form_invalid_params', stripslashes($_POST['form_invalid_params']));
		update_option($opt_trans.'form_too_high', stripslashes($_POST['form_too_high']));
		update_option($opt_trans.'form_img_invalid', stripslashes($_POST['form_img_invalid']));
		update_option($opt_trans.'form_empty', stripslashes($_POST['form_empty']));
		update_option($opt_trans.'payment_failed', stripslashes($_POST['payment_failed']));
		// stats section
		update_option($opt_trans.'stats_header', stripslashes($_POST['stats_header']));
		update_option($opt_trans.'stats_views', stripslashes($_POST['stats_views']));
		update_option($opt_trans.'stats_clicks', stripslashes($_POST['stats_clicks']));
		update_option($opt_trans.'stats_ctr', stripslashes($_POST['stats_ctr']));
		update_option($opt_trans.'stats_prev_week', stripslashes($_POST['stats_prev_week']));
		update_option($opt_trans.'stats_next_week', stripslashes($_POST['stats_next_week']));
		// others
		update_option($opt_trans.'powered', stripslashes($_POST['powered']));
		update_option($opt_trans.'free_ads', stripslashes($_POST['free_ads']));
		// example ad
		update_option($opt_trans.'example_title', stripslashes($_POST['example_title']));
		update_option($opt_trans.'example_desc', stripslashes($_POST['example_desc']));
		update_option($opt_trans.'example_url', stripslashes($_POST['example_url']));
		// confirmation email
		update_option($opt_trans.'email_sender', stripslashes($_POST['email_sender']));
		update_option($opt_trans.'email_address', stripslashes($_POST['email_address']));
		// buyer email
		update_option($opt_trans.'buyer_subject', stripslashes($_POST['buyer_subject']));
		update_option($opt_trans.'buyer_message', stripslashes($_POST['buyer_message']));
		// seller email
		update_option($opt_trans.'seller_subject', stripslashes($_POST['seller_subject']));
		update_option($opt_trans.'seller_message', stripslashes($_POST['seller_message']));
		// notifications
		update_option($opt_trans.'expires_subject', stripslashes($_POST['expires_subject']));
		update_option($opt_trans.'expires_message', stripslashes($_POST['expires_message']));
		update_option($opt_trans.'expired_subject', stripslashes($_POST['expired_subject']));
		update_option($opt_trans.'expired_message', stripslashes($_POST['expired_message']));
		// affiliate program trans
		update_option($opt_trans.'affiliate_program', array(
			'commission' 		=> stripslashes($_POST['ap_commission']),
			'each_sale' 		=> stripslashes($_POST['ap_each_sale']),
			'balance' 			=> stripslashes($_POST['ap_balance']),
			'make' 				=> stripslashes($_POST['ap_make']),
			'ref_link' 			=> stripslashes($_POST['ap_ref_link']),
			'ref_notice' 		=> stripslashes($_POST['ap_ref_notice']),
			'ref_users' 		=> stripslashes($_POST['ap_ref_users']),
			'date' 				=> stripslashes($_POST['ap_date']),
			'buyer' 			=> stripslashes($_POST['ap_buyer']),
			'order' 			=> stripslashes($_POST['ap_order']),
			'comm_rate' 		=> stripslashes($_POST['ap_comm_rate']),
			'your_comm' 		=> stripslashes($_POST['ap_your_comm']),
			'empty' 			=> stripslashes($_POST['ap_empty']),
			'affiliate' 		=> stripslashes($_POST['ap_affiliate']),
			'earnings' 			=> stripslashes($_POST['ap_earnings']),
			'payment' 			=> stripslashes($_POST['ap_payment']),
			'button' 			=> stripslashes($_POST['ap_button']),
			'id' 				=> stripslashes($_POST['ap_id']),
			'user_id' 			=> stripslashes($_POST['ap_user_id']),
			'amount' 			=> stripslashes($_POST['ap_amount']),
			'account' 			=> stripslashes($_POST['ap_account']),
			'status' 			=> stripslashes($_POST['ap_status']),
			'pending' 			=> stripslashes($_POST['ap_pending']),
			'done' 				=> stripslashes($_POST['ap_done']),
			'rejected' 			=> stripslashes($_POST['ap_rejected']),
			'withdrawals' 		=> stripslashes($_POST['ap_withdrawals']),
			'success' 			=> stripslashes($_POST['ap_success']),
			'failed' 			=> stripslashes($_POST['ap_failed'])
		));
		// user panel trans
		update_option($opt_trans.'user_panel', array(
			'ad_content' 		=> stripslashes($_POST['up_ad_content']),
			'assigned_to' 		=> stripslashes($_POST['up_assigned_to']),
			'buyer' 			=> stripslashes($_POST['up_buyer']),
			'stats' 			=> stripslashes($_POST['up_stats']),
			'display_limit' 	=> stripslashes($_POST['up_display_limit']),
			'order_details' 	=> stripslashes($_POST['up_order_details']),
			'actions' 			=> stripslashes($_POST['up_actions']),
			'views' 			=> stripslashes($_POST['up_views']),
			'clicks' 			=> stripslashes($_POST['up_clicks']),
			'viewable' 			=> stripslashes($_POST['up_viewable']),
			'view_sec' 			=> stripslashes($_POST['up_view_sec']),
			'view_min' 			=> stripslashes($_POST['up_view_min']),
			'days' 				=> stripslashes($_POST['up_days']),
			'ctr' 				=> stripslashes($_POST['up_ctr']),
			'full_stats' 		=> stripslashes($_POST['up_full_stats']),
			'billing_model' 	=> stripslashes($_POST['up_billing_model']),
			'cpc' 				=> stripslashes($_POST['up_cpc']),
			'cpm' 				=> stripslashes($_POST['up_cpm']),
			'cpd' 				=> stripslashes($_POST['up_cpd']),
			'cost' 				=> stripslashes($_POST['up_cost']),
			'paid' 				=> stripslashes($_POST['up_paid']),
			'not_paid' 			=> stripslashes($_POST['up_not_paid']),
			'free' 				=> stripslashes($_POST['up_free']),
			'status' 			=> stripslashes($_POST['up_status']),
			'active' 			=> stripslashes($_POST['up_active']),
			'pending' 			=> stripslashes($_POST['up_pending']),
			'expired' 			=> stripslashes($_POST['up_expired']),
			'edit' 				=> stripslashes($_POST['up_edit']),
			'pay_now' 			=> stripslashes($_POST['up_pay_now']),
			'renewal' 			=> stripslashes($_POST['up_renewal']),
			'buy_ads' 			=> stripslashes($_POST['up_buy_ads']),
			'first_purchase' 	=> stripslashes($_POST['up_first_purchase']),
			'login_here' 		=> stripslashes($_POST['up_login_here']),
		));
		update_option($opt.'translations', array(
			// woo
			'woo_title' 		=> $_POST['woo_title'],
			'woo_button' 		=> $_POST['woo_button']
		));
		update_option($opt_trans.'order_form', array(
			// additional
			'optional_field' 	=> $_POST['optional_field'],
			'eg_optional_field' => $_POST['eg_optional_field'],
			'form_notice' 		=> $_POST['form_notice'],
			'login_notice' 		=> $_POST['login_notice'],
		));
		// statistics
		update_option($opt_trans.'statistics', array(
			'full_stats' 		=> $_POST['full_stats'],
			'last_90' 			=> $_POST['last_90'],
			'last_30' 			=> $_POST['last_30'],
			'last_7' 			=> $_POST['last_7'],
			'viewable' 			=> $_POST['viewable'],
			'view_sec' 			=> $_POST['view_sec'],
			'view_min' 			=> $_POST['view_min'],
			'total' 			=> $_POST['total'],
		));
		// additional actions
		do_action( 'bsa-pro-update-translations', $_POST, $opt_trans);
		echo '
		<div class="updated settings-error">
			<p><strong>Translations saved.</strong></p>
		</div>';
	}
}

function bsa_pro_sub_menu_users()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php $model = new BSA_PRO_Model(); $model->getAdminAction() ?>
		<?php require_once 'users-manager.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_cron()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php if ( get_option("bsa_pro_plugin_purchase_code") == '' || get_option("bsa_pro_plugin_purchase_code") == null ) {
			echo '
			<div class="updated settings-error">
				<p><strong>NOTE!</strong> Please enter your <strong>purchase code</strong> in the <a href="'.admin_url().'admin.php?page=bsa-pro-sub-menu-opts">settings</a> to use all the functions of ADS PRO! Thanks!</p>
			</div>';
		}
		$model = new BSA_PRO_Model(); $model->getAdminAction() ?>
		<?php require_once 'cron.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_ab_tests()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php $model = new BSA_PRO_Model(); $model->getAdminAction() ?>
		<?php require_once 'ab-tests.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_affiliate()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php $model = new BSA_PRO_Model(); $model->getAdminAction() ?>
		<?php require_once 'affiliate.php'; ?>
	</div>
<?php
}

function bsa_pro_sub_menu_updates()
{
	?>
	<div class="waitingContent"><div class="bsaLoader"></div> Loading..</div>
	<div class="wrap" style="display:none">
		<?php $model = new BSA_PRO_Model(); $model->getAdminAction() ?>
		<?php require_once 'updates.php'; ?>
	</div>
<?php
}

function bsa_pro_create_menu()
{
	if ( is_multisite() && is_main_site() || !is_multisite() ) {
		$icon_url = plugins_url('../frontend/img/bsa-icon.png', __FILE__);
		$role = ((bsa_role() == 'admin') ? 'a' : 'u');
		$affiliate_name = bsa_get_trans('affiliate_program', 'affiliate');
		add_menu_page('ADS PRO - Dashboard', esc_html__( 'ADS PRO', 'bsa-pro' ), (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu', 'bsa_pro_head_menu', $icon_url, '66.111132');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Spaces and Ads', 'bsa-pro' ), 'Spaces and Ads', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-spaces', 'bsa_pro_sub_menu_spaces');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Add new Space', 'bsa-pro' ), 'Add new Space', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-add-new-space', 'bsa_pro_sub_menu_add_new_space');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Add new Ad', 'bsa-pro' ), 'Add new Ad', 'read', 'bsa-pro-sub-menu-add-new-ad', 'bsa_pro_sub_menu_add_new_ad');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'HTML5 Creator', 'bsa-pro' ), 'HTML5 Creator <span class="update-plugins">new</span>', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-html-creator', 'bsa_pro_sub_menu_html_creator');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Standard Ad Creator', 'bsa-pro' ), 'Standard Ad Creator', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-creator', 'bsa_pro_sub_menu_creator');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Schedule Tasks', 'bsa-pro' ), 'Schedule Tasks', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-cron', 'bsa_pro_sub_menu_cron');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'A/B Tests', 'bsa-pro' ), 'A/B Tests', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-ab-tests', 'bsa_pro_sub_menu_ab_tests');
		add_submenu_page('bsa-pro-sub-menu', (($role == 'a') ? esc_html__( 'Users Manager', 'bsa-pro' ) : 'Your Ads'), (($role == 'a') ? 'Users Manager' : 'Your Ads'), (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-users', 'bsa_pro_sub_menu_users');
		if ( is_plugin_active( 'bsa-pro-ap-scripteo/bsa-pro-ap.php' ) ) {
			add_submenu_page('bsa-pro-sub-menu', (($affiliate_name != '') ? $affiliate_name : 'Affiliate Program'), (($affiliate_name != '') ? $affiliate_name : 'Affiliate Program'), 'read', 'bsa-pro-sub-menu-affiliate', 'bsa_pro_sub_menu_affiliate');
		}
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Settings', 'bsa-pro' ), 'Settings', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-opts', 'bsa_pro_sub_menu_settings');
		add_submenu_page('bsa-pro-sub-menu', esc_html__( 'Translations', 'bsa-pro' ), 'Translations', (($role == 'a') ? 'read' : 'manage_options'), 'bsa-pro-sub-menu-trans', 'bsa_pro_sub_menu_translations');
	}
}
add_action('admin_menu', 'bsa_pro_create_menu');

function bsa_pro_backend_body_class( $classes ) {
	$classes[] = 'bsa_pro_pages_body_class';
	return $classes;
}
add_filter( 'admin_body_class ', 'bsa_pro_backend_body_class' );

class AdsProPagination
{
	const PAGE_SIZE = 40;

	private function getCount($list)
	{
		$model = new BSA_PRO_Model();

		if ( is_array($model->getUsersList()) && $list == 'users' ) {
			return count($model->getUsersList());
		} elseif ( is_array($model->getUsersList()) && $list == 'tasks' ) {
			return count($model->getCronTasks());
		} else {
			return self::PAGE_SIZE;
		}
	}

	public function getNext($list)
	{
		$page = $this->getPage();

		if ($page * self::PAGE_SIZE >= $this->getCount($list))
			return null;

		return $page + 1;
	}

	public function getPrev()
	{
		$page = $this->getPage();

		if ($page == 1)
			return null;

		return $page - 1;
	}

	private function getPage()
	{
		$page = abs(isset($_GET['pagination']) ? $_GET['pagination'] : 0);

		if ($page < 1)
			$page = 1;

		return $page;
	}

	public function getUsersPages()
	{
		$page = $this->getPage();

		$pages = array();
		$model = new BSA_PRO_Model();

		if ( $model->getUsersList($page, self::PAGE_SIZE) )
			foreach ($model->getUsersList($page, self::PAGE_SIZE) as $entry)
				$pages[] = $entry;

		if ($page > 1 and !count($pages)) {
			return 'not_found';
		}

		return $pages;
	}

	public function getTasksPages()
	{
		$page = $this->getPage();

		$pages = array();
		$model = new BSA_PRO_Model();

		if ( $model->getCronTasks(null, $page, self::PAGE_SIZE) )
			foreach ($model->getCronTasks(null, $page, self::PAGE_SIZE) as $entry)
				$pages[] = $entry;

		if ($page > 1 and !count($pages)) {
			return 'not_found';
		}

		return $pages;
	}
}