<?php
if ( ! defined( 'ABSPATH' ) ) exit;  
 
function wp_googleplay() {
/**
 * ===============================
 * PHP & REQUEST CONFIG
 * Ambil option/theme global WordPress
 * Tampilkan error sesuai konstanta ERRORS
 * ===============================
*/ 
global $opt_themes;
@ini_set('display_errors', ERRORS);
 
/**
 * ===============================
 * LOAD CSS VIA SHORTCODE
 * ===============================
*/
echo do_shortcode('[css]');

/**
 * ===============================
 * INIT CLASS
 * Class untuk HTTP / CURL
 * Class utama struktur & scraper
 * ===============================
*/
$Go_Curl = new Go_Curl();
$sc = new StructurCode();

/**
 * ===============================
 * BASIC CONFIG
 * Post type yang akan digunakan
 * Source website target
 * Contoh URL demo (single post)
 * Ambil host saja dari URL demo
 * ===============================
*/ 
$types			= 'post';
$sources		= 'play.google.com';
$demos			= 'https://play.google.com/store/apps/details?id=com.roblox.client'; 
$demosx			= 'https://play.google.com/store/games'; 
$checkx			= $Go_Curl->http_responsex($demosx);  
$protocol 		= 'https://'.$sources;
?>

	<div class="wrap">
	<div class="play_menu" style="text-transform:uppercase">
        <b>Add sources from <?php echo parse_url($protocol)['host']; ?></b>
	</div> 
	</div> 


	<?php  
	if (isset($_POST['wp_sb']) && isset($_POST['wp_url'])) {
		@ini_set('display_errors', ERRORS);

		$url = trim(strip_tags($_POST['wp_url']));

		if (!stristr($url, $sources)) {
		echo '<div class="play_menu" style="text-transform:uppercase!important;color:#00a0d2">
				<h3 style="color:#00a0d2">Link not supported: <b style="color:red">' . esc_html($url) . '</b></h3>
			  </div>';
		return;
		}

			
		require_once get_template_directory().'/libs/addons/includes/class/googleplay.class.php'; 
		
		$getslinks = new scrapes;
		$data = $getslinks->scrape_web_info($url, false);

		if (empty($data) || isset($data['error'])) {
		echo '<div class="play_menu" style="text-transform:uppercase!important;color:#00a0d2">
				<h3 style="color:#00a0d2">Failed to retrieve data from the link</h3>
			  </div>';
		return;
		}	 
		
		$importer = new APK_Importer($data);

		// Jalankan proses import utama (create/update post, gambar, meta, taxonomy, dll)
		$result = $importer->import();
 
		$post_id = (int) $result;		
		
		$importer->add_direct_apk_download();	
		
		$thumb_id = get_post_thumbnail_id( $post_id );
		
		$img_id 	= $importer->get_featured_image_attachment_id();
		$image_url 	= wp_get_attachment_url( $img_id );

		/* 
		echo '<pre>';		
		print_r($img_id);
		echo '</pre>';	
	 
		echo '<pre>';		
		print_r($thumb_id);
		echo '</pre>';	 
		*/
	 
		// Jika semuanya berhasil → tampilkan hasil
		if ($post_id > 0) {
		$urlX = get_permalink($post_id);

		// File result.php biasanya menampilkan pesan sukses + link post
		echo $sc->render_result_post(
			$data,
			$post_id,
			$urlX,
			$image_url
		); 
		
		} else {
		echo '<div class="play_menu error" style="color:#dc3232">
				<h3>Post ID is invalid after import process</h3>
			  </div>';
		}
		 
	} 
?>

	  
<div class="wrap" id="play_fixed" >
	<div class=" play_menu" 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 data-tab="tab2"><a><i class="fa fa-search"></i> Search</a></li>  	
			<li data-tab="tab3"><a><i class="fa fa-rss"></i> Top Free Games </a></li>
			<li data-tab="tab4"><a><i class="fa fa-rss"></i> Top Free Apps </a></li>
			<li data-tab="tab5"><a><i class="fa fa-pencil-square-o" aria-hidden="true"></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'];  
			} 
		} 
		?>
		<ul style=" ">
		<li>Just add your <b>keyword</b> on form and click <b>Search</b></li> 
		</ul>	
			
		<form id="form1" method="post" action="" class="f"> 
		<?php 
		if (isset($_POST['submit']))  { 
			if (isset($error))  { 
				echo "<p style='color:red;'>". $error . "</p>"; 
			} 
		} 
		?>  
					
		<input type="text" name="search_query" placeholder="example : whatsapp"/>  
		<input type="submit" name="submit" value="Search"/> 
		</form> 
		</center> 
		

		<?php  
		if(isset($_POST['submit'])) { 
		if(!isset($error)) { 
		$search_query	= strtolower($_POST['search_query']); 
		$base_url		= 'https://play.google.com/store/search?c=apps&q=';
		$search_url	= $base_url . urlencode($search_query);
		?>
 
		<div class="play_menu" style="text-transform:uppercase"><b>Result</b></div>
		<section>
		<div class="row"> 
				  
		<?php  
		//$html	= file_get_contents($search_url);
		//$html	= scrape_site($search_url);	
		//$html	= file_get_html($search_url);	
		//$html	= get_linkz($search_url);	 
		//$html	= scrape_site($search_url);
		$html	= $Go_Curl->file_get_contents_curlx($search_url);
		$dom 		= new DOMDocument();
		@$dom->loadHTML($html);
		$xpath 		= new DomXPath($dom);
		$articles1 	= $xpath->query("//*[@class='Qfxief']"); 
		$articles2 	= $xpath->query("//*[@class='aoJE7e b0ZfVe ntZncd']"); 
								   
		foreach($articles1 as $article1) {
		$links1		= $xpath->query("//a[@class='Qfxief']/@href", $article1);
		$imgs1 		= $xpath->query("//div[@class='Jeh37']//img[@class='T75of KvQfUd']/@src", $article1);
		$names1 	= $xpath->query("//div[@class='vWM94c']", $article1);
		$devs1 		= $xpath->query("//div[@class='LbQbAe']", $article1);
		$rate1 		= $xpath->query("//div[@class='ClM7O']//div//div[@class='TT9eCd']", $article1);
		$total1 	= count($links1);  
		for($i=0;$i<$total1;$i++){
		?>

		<div class="col-12 col-md-6 mb-4">
		<span class="position-relative archive-post--remove app-container">
				
		<div class="flex-shrink-0">
		<img src="<?php echo $imgs1[$i]->nodeValue; ?>" class="app-logo " width="64" height="64"> 
		</div>
							
		<div class="app-info">
		<h3 class="h5 font-weight-semibold w-100 app-title"><?php echo $names1[$i]->nodeValue; ?></h3>	
		<div class="text-truncate text-muted app-desc">
		<span class="clamp-1 w-100">
		Devs :  <?php echo $devs1[$i]->nodeValue; ?>	
		</span>				
		</div>							 
		</div>
							 
		<div class="app-get">					
		<form method="POST" target="_blank">
		<input style="display:none" type="text" name="wp_url" value="https://play.google.com<?php echo $links1[$i]->nodeValue; ?>">
		<input type="submit" id="submit" name="wp_sb" class="get-button" value="<?php echo postnow; ?>" />
		</form>					
		<span class="app-version text-truncate">
		<svg class="mr-1" width="12" height="12" fill="orange" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"  xmlns="http://www.w3.org/2000/svg"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg><?php echo str_replace('star', '', $rate1[$i]->nodeValue); ?>
		</span>
		</div>
							 
		</span>
		</div>	 
				 
		<?php } } 
				
		foreach($articles2 as $article2) {
		$links2 	= $xpath->query("//a[@class='Si6A0c Gy4nib']/@href", $article2);
		$imgs2 		= $xpath->query("//div[@class='j2FCNc']//img[@class='T75of stzEZd']/@src", $article2);
		$names2 	= $xpath->query("//div[@class='ubGTjb']//span[@class='DdYX5']", $article2);
		$devs2 		= $xpath->query("//div[@class='ubGTjb']//span[@class='wMUdtb']", $article2);
		$rate2 		= $xpath->query("//div[@class='ubGTjb']//div//span[@class='w2kbF']", $article2);
		$total2 		= count($names2);  
		for($i=0;$i<$total2;$i++){
		if(!empty($names2)){
		?>

		<div class="col-12 col-md-6 mb-4">
		<span class="position-relative archive-post--remove app-container">
				
		<div class="flex-shrink-0">
		<img src="<?php echo $imgs2[$i]->nodeValue; ?>" class="app-logo " width="64" height="64"> 
		</div>
							
		<div class="app-info">
		<h3 class="h5 font-weight-semibold w-100 app-title"><?php echo $names2[$i]->nodeValue; ?></h3>	
		<div class="text-truncate text-muted app-desc">
		<span class="clamp-1 w-100">
		Devs :  <?php echo $devs2[$i]->nodeValue; ?>	
		</span>				
		</div>
		</div>
							 
		<div class="app-get">					
		<form method="POST" target="_blank">
		<input style="display:none" type="text" name="wp_url" value="https://play.google.com<?php echo $links2[$i]->nodeValue; ?>">
		<input type="submit" id="submit" name="wp_sb" class="get-button" value="<?php echo postnow; ?>" />
		</form>
								
		<span class="app-version text-truncate">
		<svg class="mr-1" width="12" height="12" fill="orange" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"  xmlns="http://www.w3.org/2000/svg"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg><?php echo str_replace('star', '', $rate2[$i]->nodeValue); ?>
		</span>
		</div>
				
		</span>
		</div>	
		<?php } }} ?> 

		</div>
		</section>	
		<?php } } ?>

	</div>

	<div id="tab3" class="tab-pane fade ">
		
		<div class="play_menu" style="text-transform:uppercase"><b>Top Free Games</b> </div>    
		 	
	
<?php
// ======================= OUTPUT HTML =======================
$topGames = $Go_Curl->getTopFreeGames(30);  
?>

			<section id="games-content">
				<div class="row">
				
				<?php foreach ($topGames as $game): ?>
				<div class="col-12 col-md-6 mb-4">
						<span class="position-relative archive-post--remove app-container d-flex align-items-center">
							<div class="flex-shrink-0" style="position:relative;">
								<img src="<?php echo $Go_Curl->getAppsPoster($game['package_id']); ?>" class="app-logo" width="64" height="64" loading="lazy">
								<span style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,0.85);color:white;padding:5px 9px;border-radius:6px;font-weight:bold;font-size:13px;border:2px solid white;">
									<?= $game['rank'] ?>
								</span>
							</div>
							<div class="app-info ms-3 flex-grow-1">
								<h3 class="h5 font-weight-semibold app-title mb-1"><?php echo $Go_Curl->getAppTitle($game['package_id']); ?> </h3>
								<div class="text-muted small"><?php echo $Go_Curl->getAppsCategory($game['package_id']); ?></div>
							</div>
							<div class="app-get">
								<form method="POST" target="_blank">
									<input type="hidden" name="wp_url" value="<?php echo $Go_Curl->getAppsLink($game['package_id']); ?>">
									<button type="submit" name="wp_sb" class="get-button btn btn-success btn-sm">Get Now</button>
								</form>
							</div>
						</span>
					</div>
					<?php endforeach; ?>					
				

				</div>
			</section>
		
		
	</div>
	
	<div id="tab4" class="tab-pane fade "> 
	
<?php
$topApps = $Go_Curl->scrapeGooglePlayApps('US', 'en_US', 30, false); // kosong = landing page popular apps
?>
		
				 
				
		<div class="play_menu" style="text-transform:uppercase"><b>Top Free Apps</b></div>      
			
			<section id="games-content">
				<div class="row">
				<?php foreach ($topApps as $app): ?>				
				
				<div class="col-12 col-md-6 mb-4">
						<span class="position-relative archive-post--remove app-container d-flex align-items-center">
							<div class="flex-shrink-0" style="position:relative;">
								<img src="<?php echo $Go_Curl->getAppsPoster($app['package_id']); ?>" class="app-logo" width="64" height="64" loading="lazy">
								<span style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,0.85);color:white;padding:5px 9px;border-radius:6px;font-weight:bold;font-size:13px;border:2px solid white;">
									<?= $app['rank'] ?>
								</span>
							</div>
							<div class="app-info ms-3 flex-grow-1">
								<h3 class="h5 font-weight-semibold app-title mb-1"><?php echo $Go_Curl->getAppTitle($app['package_id']); ?></h3>
								<div class="text-muted small"><?php echo $Go_Curl->getAppsCategory($app['package_id']); ?></div>
							</div>
							<div class="app-get">
								<form method="POST" target="_blank">
									<input type="hidden" name="wp_url" value="<?php echo $Go_Curl->getAppsLink($app['package_id']); ?>">
									<button type="submit" name="wp_sb" class="get-button btn btn-success btn-sm">Get Now</button>
								</form>
							</div>
						</span>
					</div>
					<?php endforeach; ?>					
				

				</div>
			</section>

	</div>
	
	<div id="tab5" class="tab-pane fade <?php if (!isset($_POST["submit"]) && !isset($_POST["wp_sb"])) { ?>in active<?php } ?>">
		<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" class='f'>
		<input type="text" name="wp_url" placeholder="example : <?php echo $demos; ?>"  >
		<input type="submit" id="Submit" name="wp_sb" 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 Google PlayStore <a style="color:var(--color_link_h)!important;font-weight: 700;" href="https://play.google.com/store/games?device=phone" target="_blank">games</a> or <a style="color:var(--color_link_h)!important;font-weight: 700;" href="https://play.google.com/store/apps?device=phone" target="_blank">apps</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-gp.php'; ?>
	</div>
      
	</div>
</div>

	 
<div style="clear:both"></div>

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