<?php
if ( ! defined( 'ABSPATH' ) ) exit;      
 
function wp_gamedva() {
ini_set('display_errors', ERRORS);
@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');   
@ini_set('user_agent', 'My-Application/2.5'); 
echo do_shortcode( '[css]' );
$types	= 'post';
$sources	= 'gamedva.com';
$Go_Curl 	= new Go_Curl();

$demos	= 'https://gamedva.com/roblox-mod-apk-2'; 
$demosx	= 'https://'.parse_url($demos)['host'];  

$checkx	= $Go_Curl->http_responsex($demosx); 

$keyz	= $opt_themes['api_key_scrape'];
//if($keyz){
//$rand_apikey 	= array_rand($keyz);
//$apikey 		= $keyz[$rand_apikey]; 
//}

$protocol	= 'https://'.$sources;
$status	= $Go_Curl->http_responsex($demos); 
 
?>
	
   
    <div class="play_menu" style="text-transform:uppercase">
        <b>Add sources from <?php echo parse_url($protocol)['host']; ?></b>
    </div> 
	 
	
	<?php		
	
	$status_code = $Go_Curl->http_responsex($demos);
 
	$allowed_statuses = [200, 201]; // or whatever indicates success

	if (in_array($status_code, [301, 302, 303, 307, 308, 403])) {
	?> 
	<div class="wrap">
	<div class="containerX">
	<h2></h2>
	<h3></h3>	 
	<!--<h5 style="text-align: center;">because its blocked by cloudflare</h5>-->
	<?php		
	if($keyz) {
	?>
	<div style="position:fixed; top:70px; right:20px; background:#333; color:#fff; padding:12px 20px; border-radius:8px; box-shadow:0 4px 12px rgba(0,0,0,0.2); z-index:9999; animation:toast 30s forwards;">
	<p style="text-align: center;">because <?php echo parse_url($protocol)['host']; ?>  blocked by cloudflare</p>
	<p style="text-align: center;">Your using by Api Key</p>
	<input onclick="this.select()" readonly type="text" id="api_key_scrape" name="api_key_scrape" value="<?php echo api_rand(); ?>" class="regular-text" style="text-align: center;display: block;margin-right: auto;margin-left: auto;"/>
	</div>   
	<?php } else { ?>	
	<div style="position:fixed; top:70px; right:20px; background:#333; color:#fff; padding:12px 20px; border-radius:8px; box-shadow:0 4px 12px rgba(0,0,0,0.2); z-index:9999; animation:toast 30s forwards;">
	<p style="text-align: center;">Sorry.... scraping from <?php echo parse_url($demos)['host']; ?> blocked by cloudflare</p>
	<p style="text-align: center;">Please add your API key</p>
	<p style="text-align: center;"><a href="<?php echo apikey_page; ?>" target="_blank">Click HERE to add API key </a></p>
	</div>
 
	<?php } 
	
	$errorcode = $Go_Curl->http_responsex($demos); 
 
	if($errorcode === 403 && !$keyz) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Forbidden (blocked by cloudflare)</h5> ';

	} elseif($errorcode === 400) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Bad Request </h5> ';
		
	} elseif($errorcode === 404) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Not Found </h5> ';
		
	} elseif($errorcode === 500) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Internal Server Error </h5> ';
		
	} elseif($errorcode === 502) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Bad Gateway </h5> ';
		
	} elseif($errorcode === 503) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' Service Unavailable </h5> ';
		
	} elseif($errorcode === 200) {
	echo '<h5 style="text-align: center;">Status : '.$errorcode.' OK </h5> ';
		
	}
	?>
	
	</div>
	</div>
	<?php 
	} elseif (in_array($status_code, $allowed_statuses)) {
	?> 
	 
	<?php 
	} else {
	?> 
	 
	<?php 
	}
	?>
 
	
	
	
    <?php 
		if(isset($_POST['wp_sb'])) {
		ini_set('display_errors', ERRORS);
		if(isset($_POST['wp_url'])) {
		$url	= trim(strip_tags($_POST['wp_url']));
		if(stristr($url, $sources)) {
			//require_once 'gamedva.class.php';
			require_once get_template_directory().'/libs/addons/includes/class/gamedva.class.php';
			########## 
		
			global $opt_themes;
			$getslinks		= new scrapes;
			$gets_data		= $getslinks->scrape_web_info($url, false);
			require_once get_template_directory().'/libs/addons/includes/inc/top.php';
			
			if(count($gets_data) AND !isset($gets_data['error'])) {
			require_once get_template_directory().'/libs/addons/includes/inc/post_content.php';	
			require_once get_template_directory().'/libs/addons/includes/inc/img_gallery.php';	
 
			/*
			start from playstore
			*/
			require_once get_template_directory().'/libs/addons/includes/inc/playstore.php';	
			
			
			/*
			start from sources
			*/ 
			require_once get_template_directory().'/libs/addons/includes/inc/gma.php';	
			
			/*
			for apkmody metabox	
			*/
			require_once get_template_directory().'/libs/addons/includes/inc/dlpro.php';	
			
					
			$download_info_link = array();
			$count	= count($gets_data['name_downloadlinks_gma']);
			for ($i = 0; $i < $count; $i++) { 
			if ( !empty($gets_data['sizes_GP']) ) {
			$size_dw = $gets_data['sizes_GP'];
			} else {
			$size_dw = $gets_data['size_sc'];
			}
			if ( !empty($gets_data['downloadlink_gma_'[$i]]) ) { 
			$download_info_link_data = [
			'name' 	=> $gets_data['name_downloadlinks_gma'][$i],
			'url' 	=> $gets_data['downloadlink_gma_'[$i]],
			'sizes'	=> $gets_data['size_downloadlinks_gma'][$i],
			'sizes1' 	=> $gets_data['size_downloadlinks_gma'][$i],
			'title' 	=> $gets_data['name_downloadlinks_gma'][$i],
			'links' 	=> $gets_data['downloadlink_gma_'[$i]],
			'size' 	=> $gets_data['size_downloadlinks_gma'][$i],
			'tipes' 	=> $gets_data['types_downloadlinks_gma'][$i],
			'notes' 	=> $gets_data['notes_downloadlinks_gma'][$i],
			]; 
			 
			array_push($download_info_link, $download_info_link_data);
			update_post_meta($post_id, "repeatable_fields", $download_info_link);
			update_post_meta($post_id, "group_downloads_links", $download_info_link);
			}
		 
			}
			
			if ( !empty($gets_data['sizes_GP']) ) {
				$size_dwz = $gets_data['sizes_GP'];
			} else {
				$size_dwz = $gets_data['size_sc'];
			}
			
			if ( !empty($gets_data['title_GP']) ) { 
				$title_dwz = $gets_data['title_GP'].' Apk'; 
			} else {
				$title_dwz = $gets_data['app_name'].' Apk'; 
			}
			
			/* 
			if ($opt_themes['ouo']) {
				$linkapkz	= ouo_io_exhemes('https://exthem.es/apk.php?pkg='.$gets_data['GP_ID']);
			} else {
				$linkapkz	= ouo_io_exhemes('https://exthem.es/apk.php?pkg='.$gets_data['GP_ID']);
			}

			if ($opt_themes['shrinkearn']) {
				$linkapkz	= shrinkearn_exhemes('https://exthem.es/apk.php?pkg='.$gets_data['GP_ID']);
			} else {
				$linkapkz	= shrinkearn_exhemes('https://exthem.es/apk.php?pkg='.$gets_data['GP_ID']);
			} 
			*/
			
			$linkapkz	= ouo_io_exhemes('https://exthem.es/apk.php?pkg='.$gets_data['GP_ID']);
			 
			$download_info_link_data_gps = [
				'name' 		=> $title_dwz,
				'url' 		=> $linkapkz,
				'sizes1' 	=> $size_dwz,
				'sizes' 	=> $size_dwz,
				'title' 	=> $title_dwz,
				'links' 	=> $linkapkz,
				'size' 		=> $size_dwz,
				'tipes' 	=> '',
				'notes' 	=> '',
			]; 
			 
			array_push($download_info_link, $download_info_link_data_gps);
			update_post_meta($post_id, "repeatable_fields", $download_info_link);
			update_post_meta($post_id, "group_downloads_links", $download_info_link);			
			
			require_once get_template_directory().'/libs/addons/includes/inc/dw_apt.php';	
			require_once get_template_directory().'/libs/addons/includes/inc/dw_apkgk.php';	
			 
			wp_set_object_terms( $post_id, $gets_data['title_GP'], 'post_tag', true ); 
			
			if($post_id)
			$urlX	= get_permalink( $post_id );
			require_once get_template_directory().'/libs/addons/includes/result.php'; 
			}
			} else {
			echo '<div class="play_menu" style="text-transform:uppercase!important;color:#00a0d2"><h3 style="color:#00a0d2">Please check your link.. your link is <b style="color:red">'.$url.'</b></h3></div>';
			}
				}
			}
    ?>
 

    <div style="clear:both"></div>
	
	
    <div class="wrap">
	<div class="play_fixedx play_menu" id="play_fixed" style="margin-bottom: 10px;">       
	<ul id="menunya" class="play_menus" style="text-transform:capitalize">
	<li data-tab="tab1"><a><i class="fa fa-home"></i> Home</a></li> 
	<li <?php if(isset($_POST['submit'])) { ?> class="active" <?php } ?> data-tab="tab2"><a><i class="fa fa-search"></i> Search</a></li>
	<li <?php if (!isset($_POST["submit"]) && !isset($_POST["wp_sb"])) { ?>class="active" <?php } ?>data-tab="tab3"><a><i class="fa fa-rss"></i> Latest Games</a></li>
	<li data-tab="tab4"><a><i class="fa fa-rss"></i> Latest Apps</a></li>
	<li data-tab="tab5"><a><i class="fa fa-pencil-square-o"></i> add manual</a></li>
	<?php if(isset($_POST['wp_sb'])) { ?>
	<li <?php if(isset($_POST['wp_sb'])) { ?>class="active" <?php } ?>data-tab="tab6"><a><i class="fa fa-bullhorn"></i> Debugs</a></li>
	<?php } ?>	 
	 
	</ul>
	<div style="clear:both"></div>
	</div>
    </div>
	 
    <div class="containerX">
	<div class="tab-content">
	
	<div id="tab1" class="tab-pane fade">  
	<?php global $opt_themes; ?> 	
	<ul class="play_menu" >
	<li>Your Status Post is <?php if($opt_themes['apk_status'] != 'draft') { ?> <blink><strong class="blink blink-one" style="color:green"><?php echo $opt_themes['apk_status']; ?></strong></blink> You ready to Make auto posting now <?php } else { ?><blink><strong class="blink blink-one" style="color:red"><?php echo $opt_themes['apk_status']; ?></strong></blink>. Please Change to <blink><strong class="blink" style="color:green">publish</strong></blink> to make Auto Posting,  you can go to Setting Page <?php } ?> </li>
	</ul>
	</div>     
	
	<div id="tab2" class="tab-pane fade <?php if(isset($_POST['submit'])) { ?>in active <?php } ?>">
	<div class="play_menu" style="text-transform:uppercase"><b>Search</b></div>     

	<center> 

	<?php 
	if (isset($_POST['submit']))  { 
	if ((!isset($_POST['search_query'])) ) { 
	$error = "*" . "Please fill all the required fields"; 
	} else { 
	$search_query = $_POST['search_query'];  
	} 
	} 
	?>
	<form id="form1" method="post" action=""> 
	<?php 
	if (isset($_POST['submit'])) { if (isset($error)) { 
	echo "<p style='color:red;'>" 
	. $error . "</p>"; 
	} } 
	?>  

	
	<ul style=" ">
	<li>Just add your <b>keyword</b> on form and click <b>Search</b></li> 
	</ul>	
	<input style="width: 90% !important;" class="apkextractor" type="text" name="search_query" placeholder="example : whatsapp"/>  
	<input class="button-primary" type="submit" name="submit" value="Search"/> 
	</form> 
	</center> 
	

	<?php
	if(isset($_POST['submit'])) { 
	if(!isset($error)) { 
	ini_set('display_errors', 'on'); 
	//require_once 'ssl.php'; 
	@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');  
	ini_set('user_agent', 'My-Application/2.5');	 
	$search_query	= strtolower($_POST['search_query']); 
	$base_url	= 'https://gamedva.com/?s=';
	$search_url	= $base_url.html_entity_decode( $search_query, ENT_COMPAT, 'UTF-8' );
	$link_cari	= $search_url;  
	//$html	= get_url($link_cari, false, stream_context_create($ssl));   
	//$html	= get_url($link_cari);   

	$errorcode = http_response($link_cari); 
	 
	if($errorcode === 403) {
	$html 	= scrapedo($link_cari);
	//$html	= zenrows($sc_url);
	} elseif($errorcode === 200) {
	$html		= file_get_html($link_cari, false, stream_context_create($ssl));
	}
	
	
	$judul	= $Go_Curl->match_all('/<p class="entry-title">(.*?)<\/p>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	
	$links	= $Go_Curl->match_all('/<a.*?href="(.*?)\".*?>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	 
	$img	= $Go_Curl->match_all('/<img.*?src="(.*?)".*?>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	 	
	$mods	= $Go_Curl->match_all('/<div class="entry-excerpt">(.*?)<\/div>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	  
	?>

	<div class="play_menu" style="text-transform:uppercase"><b><?php echo count($judul); ?> Result for "<?php echo $search_query; ?>"</b></div>  
	<section>
	<div class="row"> 
	<?php
	$count	= count($judul);
	for ($i = 0; $i < $count; $i++) {   
	?>	
	 
	<div class="col-12 col-md-6 mb-4">
	<span class="position-relative archive-post--remove app-container">
	<?php 	 
	$path	= $img[$i];
	$type 	= pathinfo($path, PATHINFO_EXTENSION);
	$data 	= file_get_contents($path);
	$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);  
	?>
	<div class="flex-shrink-0">
	<img src="<?php echo $base64; ?>" class="app-logo " width="64" height="64"> 
	</div>
	
	<div class="app-info">
	<h3 class="h5 font-weight-semibold w-100 app-title"><?php echo $judul[$i]; ?></h3>	
	 
	<div class="text-truncate text-muted app-desc">
	<span class="clamp-1 w-100">
	<?php echo $mods[$i]; ?>
	</span>	
	</div>
	</div>
	 
	<div class="app-get">
	<form method="POST" target="_blank">
	<input style="display:none" type="text" name="wp_url" value="<?php echo $links[$i]; ?>">
	<input type="submit" id="submit" name="wp_sb" class="get-button" value="<?php echo postnow; ?>" />
	</form>
	</div>
	 
	</span>
	</div>	
    	 
	<?php } ?> 

	</div>
	</section>	
	<?php }} ?>	
	</div> 
	 
	<div id="tab3" class="tab-pane fade <?php if (!isset($_POST["submit"]) && !isset($_POST["wp_sb"])) { ?>in active <?php } ?>">
	
	<div class="play_menu" style="text-transform:uppercase"><b>Latest Games </b></div>	


	<?php  	
	// Updated scraper code for gamedva.com/game/ as of January 2026
	// The original regex patterns are still largely functional based on current site behavior,
	// but we've added improvements: better container matching, lazy-loading support (data-src),
	// cleaner title extraction, and safer fallback handling.

	$paged = isset($_GET['games']) ? (int)$_GET['games'] : 1;
	$paged = max(1, $paged); // Prevent invalid pages

	$base_url = ($paged > 1) ? "https://gamedva.com/game/page/{$paged}/" : "https://gamedva.com/game/";

	$is_ajax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
		
	$status_code = $Go_Curl->http_responsex($base_url);
	$allowed_statuses = [200, 201];

	if (in_array($status_code, [301, 302, 303, 307, 308, 403])) {
		// Likely blocked, redirected, or protected — use bypass method (e.g., with headers/proxies/JS rendering)
		$html = $Go_Curl->scrapedox($base_url);
	} elseif (in_array($status_code, $allowed_statuses)) {
		$html = $Go_Curl->scrape_sitex($base_url);
	} else {
		// Fallback for other cases (404, 500, etc.)
		$html = $Go_Curl->scrape_sitex($base_url);
	}

	if (empty($html)) {
		die("Failed to retrieve page content.");
	}

	// Improved container extraction — more flexible (allows attributes in div)
	// Original class "ap-loop-wrap" appears to still be in use as of early 2026
	$list_block = $Go_Curl->match('/<div\s+class="ap-loop-wrap"[^>]*>(.*?)<nav\s+id="ap-pagination"/msi', $html, 1);

	if (empty($list_block)) {
		// Fallback if structure slightly changed
		$list_block = $Go_Curl->match('/<div\s+class="ap-loop-wrap"[^>]*>(.*?)<\/div>\s*<nav\s+id="ap-pagination"/msi', $html, 1);
	}

	if (empty($list_block)) {
		die("Could not find game list container. Site structure may have changed.");
	}

	// Extract titles from <p class="entry-title"> or fallback to <h3>/<a> title
	$judul = $Go_Curl->match_all('/<p\s+class="entry-title"[^>]*>(.*?)<\/p>/msi', $list_block, 1);
	if (empty($judul)) {
		$judul = $Go_Curl->match_all('/<h[2-4][^>]*class="[^"]*title[^"]*"[^>]*><a[^>]*>(.*?)<\/a>/msi', $list_block, 1);
	}

	// Extract links (main game page URLs)
	
	$links	= $Go_Curl->match_all('/<a.*?href="(.*?)\".*?>/ms', $list_block, 1);
	
	/* $links3 = $Go_Curl->match_all('/<a[^>]+href=["\']([^"\']+)["\'][^>]*>\s*<img/msi', $list_block, 1); // Links near images
	if (empty($links)) {
		$links3 = $Go_Curl->match_all('/<p\s+class="entry-title"[^>]*>\s*<a[^>]+href=["\']([^"\']+)["\']/msi', $list_block, 1);
	} */
	 
	
	// Extract images — support both src and data-src (lazy loading common)
	$img = $Go_Curl->match_all('/<img[^>]+src=["\']([^"\']+)["\']/msi', $list_block, 1);
	if (empty($img) || count($img) < count($judul)) {
		$img = $Go_Curl->match_all('/<img[^>]+data-src=["\']([^"\']+)["\']/msi', $list_block, 1); // Lazy load fallback
	}

	// Extract mod features/description
	$mods = $Go_Curl->match_all('/<div\s+class="entry-excerpt"[^>]*>(.*?)<\/div>/msi', $list_block, 1);

	// Optional: Clean up extracted data (strip tags if needed)
	foreach ($judul as &$title) {
		$title = strip_tags($title);
	}
	foreach ($mods as &$desc) {
		$desc = strip_tags($desc);
	}
	unset($title, $desc);

	?>
	<section>
	<!-- Games List -->
	<div class="row" id="games-list">
	<?php
	$count	= count($judul);
	preg_match_all('#href=["\']https?://gamedva\.com/game/page/(\d+)["\']#i', $html, $page_matches);
	$page_numbers = array_unique($page_matches[1] ?? []);
	$total_pages = !empty($page_numbers) ? max($page_numbers) : $total_pages;
	$total_pages = max($total_pages, $paged);
	
	$counter = 0;
	for ($i = 0; $i < $count; $i++) {   
	$counter++;		
	$title  	= htmlspecialchars(trim($judul[$i]));			
	$appId = $Go_Curl->get_playstore_id_by_title_google($title);
	?>	
	 
	<div class="col-12 col-md-6 mb-4">
	<span class="position-relative archive-post--remove app-container">
	<?php 	 
	$path	= $img[$i];
	$type 	= pathinfo($path, PATHINFO_EXTENSION);
	$data 	= file_get_contents($path);
	$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);  
	?>
	<div class="flex-shrink-0" style="position:relative;">
	<img src="<?php echo $base64; ?>" class="app-logo " width="64" height="64"> 
	<span style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,0.8);color:white;padding:6px 10px;border-radius:6px;font-weight:bold;border:2px solid white;">
	<?php echo $counter; ?>
	</span>		
	</div>
	
	<div class="app-info">
	<h3 class="h5 font-weight-semibold w-100 app-title"><?php echo $judul[$i]; ?> (<?= $appId ?>)</h3>	
	 
	<div class="text-truncate text-muted app-desc">
	<span class="clamp-1 w-100">
	<?php echo $mods[$i]; ?>
	</span>	
	</div>
	</div>
	 
	<div class="app-get">
	<form method="POST" target="_blank">
	<input style="display:none" type="text" name="wp_url" value="<?php echo $links[$i]; ?>">
	<input type="submit" id="submit" name="wp_sb" class="get-button" value="<?php echo postnow; ?>" />
	</form>
	</div>
	 
	</span>
	</div>	
    	 
	<?php } ?> 
	
	</div>
	</section>

	 
	
		<!-- AJAX Pagination Navigation (always shown, even on initial load) -->
			<nav aria-label="Games pagination" class="mt-4" style="text-align: center !important;">
				<ul class="pagination justify-content-center">
					<!-- Previous Button -->
					<li class="page-item <?= ($paged <= 1) ? 'disabled' : '' ?>">
						<a class="page-link ajax-page" href="#" data-page="<?= $paged - 1 ?>" tabindex="-1">Previous</a>
					</li>

					<?php
					// Show limited page numbers: current ± 4, plus first and last
					$start = max(1, $paged - 4);
					$end = min($total_pages, $paged + 4);

					// First page
					if ($start > 1) {
						echo '<li class="page-item"><a class="page-link ajax-page" href="#" data-page="1">1</a></li>';
						if ($start > 2) echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
					}

					// Middle pages
					for ($pg = $start; $pg <= $end; $pg++) {
						$active = ($pg == $paged) ? 'active' : '';
						echo '<li class="page-item ' . $active . '"><a class="page-link ajax-page" href="#" data-page="' . $pg . '">' . $pg . '</a></li>';
					}

					// Last page
					if ($end < $total_pages) {
						if ($end < $total_pages - 1) echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
						echo '<li class="page-item"><a class="page-link ajax-page" href="#" data-page="' . $total_pages . '">' . $total_pages . '</a></li>';
					}
					?>

					<!-- Next Button -->
					<li class="page-item <?= ($paged >= $total_pages) ? 'disabled' : '' ?>">
						<a class="page-link ajax-page" href="#" data-page="<?= $paged + 1 ?>">Next</a>
					</li>
				</ul>
			</nav>

		<!-- Loading Spinner -->
		<div id="loading-spinner" class="text-center my-5" style="display:none;">
			<div class="spinner-border text-primary" role="status">
				<span class="visually-hidden">Loading...</span>
			</div>
			<p class="mt-2">Loading page...</p>
		</div>
			
			<?php if ($is_ajax): ?>
				<?php 
				// For AJAX requests, only return the games content + pagination
				$output = ob_get_clean();
				echo $output;
				exit;
				?>
			<?php endif; ?>
			
		<script>
			document.addEventListener('DOMContentLoaded', function() {
				function attachAjaxListeners() {
					document.querySelectorAll('.ajax-page').forEach(function(link) {
						link.addEventListener('click', function(e) {
							e.preventDefault();
							
							const page = this.getAttribute('data-page');
							if (!page || this.parentElement.classList.contains('disabled')) return;

							const url = page == 1 ? '?page=sc-gamedva' : '?page=sc-gamedva&games=' + page;

							// Show spinner, hide list temporarily
							document.getElementById('games-list').style.opacity = '0.5';
							document.getElementById('loading-spinner').style.display = 'block';

							fetch(url, {
								headers: { 'X-Requested-With': 'XMLHttpRequest' }
							})
							.then(response => response.text())
							.then(html => {
								const parser = new DOMParser();
								const doc = parser.parseFromString(html, 'text/html');

								const newList = doc.querySelector('#games-list').innerHTML;
								const newPagination = doc.querySelector('nav[aria-label="Games pagination"]').parentNode.innerHTML; // Capture full nav + spinner

								document.querySelector('#games-list').innerHTML = newList;
								document.querySelector('nav[aria-label="Games pagination"]').parentNode.innerHTML = newPagination;

								// Re-attach listeners
								attachAjaxListeners();

								// Update URL
								history.pushState({page: page}, '', url);

								// Hide spinner
								document.getElementById('games-list').style.opacity = '1';
							})
							.catch(err => {
								console.error('Load error:', err);
								alert('Failed to load page. Please try refreshing.');
								document.getElementById('loading-spinner').style.display = 'none';
								document.getElementById('games-list').style.opacity = '1';
							});
						});
					});
				}

				attachAjaxListeners();

				// Basic back/forward support
				window.addEventListener('popstate', function(e) {
					location.reload();
				});
			});
			</script>
			

	</div> 

	<div id="tab4" class="tab-pane fade">
	<div class="play_menu" style="text-transform:uppercase"><b>Latest Apps</b></div>
	
	
	<section>
	<div class="row">
	<?php   
	$paged	= isset($_GET['apps'])?(int)$_GET['apps'] : 1; 
	 
	$base_url = ($paged > 1) ? "https://gamedva.com/apps/page/{$paged}/" : "https://gamedva.com/apps/";
	 
	
	
	$status_code = $Go_Curl->http_responsex($base_url);
	 
	$allowed_statuses = [200, 201]; // or whatever indicates success

	if (in_array($status_code, [301, 302, 303, 307, 308, 403])) {
		// Likely blocked or redirected — use scraper bypass
		$html = $Go_Curl->scrapedox($base_url);
	} elseif (in_array($status_code, $allowed_statuses)) {
		$html = $Go_Curl->scrape_sitex($base_url);
	} else {
		// Optional: handle other errors (e.g. 404, 500, etc.)
		$html = $Go_Curl->scrape_sitex($base_url);
	}
	
	$judul	= $Go_Curl->match_all('/<p class="entry-title">(.*?)<\/p>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	
	$links	= $Go_Curl->match_all('/<a.*?href="(.*?)\".*?>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	 
	$img	= $Go_Curl->match_all('/<img.*?src="(.*?)".*?>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	 	
	$mods	= $Go_Curl->match_all('/<div class="entry-excerpt">(.*?)<\/div>/ms', $Go_Curl->match('/<div class="ap-loop-wrap">(.*?)<nav id="ap-pagination">/ms', $html, 1), 1);
	
	
	preg_match_all('#href=["\']https?://gamedva\.com/apps/page/(\d+)["\']#i', $html, $page_matches);
	$page_numbers = array_unique($page_matches[1] ?? []);
	$total_pages = !empty($page_numbers) ? max($page_numbers) : $total_pages;
	$total_pages = max($total_pages, $paged);
	
	$count	= count($judul);
	$counter = 0;
	for ($i = 0; $i < $count; $i++) {   
	$counter++;		
	$title  	= htmlspecialchars(trim($judul[$i]));			
	$appId = $Go_Curl->get_playstore_id_by_title_google($title);
	?>	
	 
	<div class="col-12 col-md-6 mb-4">
	<span class="position-relative archive-post--remove app-container">
	<?php 	 
	$path	=$img[$i];
	$type 	= pathinfo($path, PATHINFO_EXTENSION);
	$data 	= file_get_contents($path);
	$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);  
	?>
	<div class="flex-shrink-0" style="position:relative;">
	<img src="<?php echo $base64; ?>" class="app-logo " width="64" height="64"> 
	<span style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,0.8);color:white;padding:6px 10px;border-radius:6px;font-weight:bold;border:2px solid white;">
	<?php echo $counter; ?>
	</span>		
	</div>
	
	<div class="app-info">
	<h3 class="h5 font-weight-semibold w-100 app-title"><?php echo $judul[$i]; ?> (<?= $appId ?>)</h3>	
	 
	<div class="text-truncate text-muted app-desc">
	<span class="clamp-1 w-100">
	<?php echo $mods[$i]; ?>
	</span>	
	</div>
	</div>
	 
	<div class="app-get">
	<form method="POST" target="_blank">
	<input style="display:none" type="text" name="wp_url" value="<?php echo $links[$i]; ?>">
	<input type="submit" id="submit" name="wp_sb" class="get-button" value="<?php echo postnow; ?>" />
	</form>
	</div>
	 
	</span>
	</div>	
    	 
	<?php } ?> 
	 
	 
	
	</div>
	</section>
	 	 
	
			<!-- PAGINATION -->
			<nav class="paginate mt-4">
				<ul class="pagination justify-content-center">
					<?php if ($paged > 1): ?>
						<li class="page-item"><a class="page-link" href="?page=sc-gamedva&apps=1">First</a></li>
						<li class="page-item"><a class="page-link" href="?page=sc-gamedva&apps=<?= $paged - 1 ?>">Previous</a></li>
					<?php endif; ?>

					<?php
					$show = 4;
					$start = max(1, $paged - $show);
					$end = min($total_pages, $paged + $show);
 
					for ($i = $start; $i <= $end; $i++):
						$is_active = ($i == $paged);
						$li_class2 = $is_active ? 'page-item active current-page' : 'page-item';
						$li_class = $is_active ? 'page-link active current-page' : 'page-link';
						$link_class = $is_active ? 'page-link navy' : 'page-link';
					?>
						<li class="<?= $li_class ?>">
							<a class="<?= $link_class ?>" href="?page=sc-gamedva&apps=<?= $i ?>"><?= $i ?></a>
						</li>
					<?php endfor; ?>

					
					<?php if ($end < $total_pages) { ?>
					<?php if ($end < $total_pages - 1)  ?>
					<li class="page-item disabled"><span class="page-link">...</span></li>
					<li class="page-item"><a class="page-link ajax-page" href="?page=sc-gamedva&apps=<?= $total_pages ?>" data-page="<?= $total_pages ?>"><?= $total_pages ?></a></li>
					<?php } ?>
					
					<?php if ($paged < $total_pages): ?>
						<li class="page-item"><a class="page-link" href="?page=sc-gamedva&apps=<?= $paged + 1 ?>">Next</a></li> 
					<?php endif; ?>
					
				</ul>
			</nav>
	

	</div> 

	<div id="tab5" class="tab-pane fade">
	<div class="play_menu"  style="text-transform:uppercase" ><i class="fa fa-pencil-square-o" aria-hidden="true"></i>&nbsp;<b>Paste your link post here</b></div>

	<div class="play_menu" >	 
	<form name="myForm" id="myForm" method="POST">
	<input class="apkextractor" type="text" name="wp_url" placeholder="example : <?php echo $demos; ?>"  >
	<input type="submit" id="Submit" name="wp_sb" class="button-primary" value="<?php echo postnow; ?>" />
	</form>
	</div> 
	
	<div class="play_menu" style="text-transform:uppercase"><b>Add manual</b></div>

	<div class="play_menu" style="text-transform:uppercase">
	<ol style="padding-right: 20px !important;">
	<li>Open website <a style="color:var(--color_link_h)!important;font-weight: 700;" href="https://<?php echo parse_url($demos)['host']; ?>" target="_blank"><?php echo parse_url($demos)['host']; ?></a></li>
	<li>Copy link post and paste to form</li>
	<li>use the url into this format:  <strong style="color:var(--color_link_h)!important;font-weight: 700;text-transform:lowercase!important"><?php echo $demos; ?></strong> </li>
	</ol>	
	</div>
	
	</div>
	 
	<div id="tab6" class="tab-pane fade <?php if(isset($_POST['wp_sb'])) { ?> in active<?php } ?>"> 	
			<?php require_once get_template_directory().'/libs/addons/includes/debug.php'; ?>
	</div> 
      
	</div>
	</div>
 
	 
    <div style="clear:both"></div>
  

<?php  
//form_add_post();	 
echo do_shortcode( '[ft_menu]' );
echo do_shortcode( '[ft_js]' ); 
}