<?php
/*-----------------------------------------------*/
/*  EXTHEM.ES
/*  PREMIUM WORDRESS THEMES
/*
/*  STOP DON'T TRY EDIT
/*  IF YOU DON'T KNOW PHP 
/*
/*  As Errors In Your Themes
/*  Are Not The Responsibility
/*  Of The DEVELOPERS
/*  @EXTHEM.ES
/*  
/*  Follow Social Media Exthem.es
/*  Youtube : https://www.youtube.com/channel/UCpcZNXk6ySLtwRSBN6fVyLA
/*  Facebook : https://www.facebook.com/groups/exthem.es
/*  Twitter : https://x.com/bangreyblog
/*  Instagram : https://www.instagram.com/exthem.es/
/*  Telegram : https://t.me/exthemes_helps
/*  TikTok : https://www.tiktok.com/@exthemes
/*	More Premium Themes Visit Now On https://exthem.es/
/*-----------------------------------------------*/
if ( ! defined( 'ABSPATH' ) ) exit; 
// Silence is golden.

global $opt_themes;

$title_GP	= $gets_data['title_GP'] ? $gets_data['title_GP'] : $gets_data['app_name'];
$poster_GP	= $gets_data['poster_GP'] ? $gets_data['poster_GP'] : $gets_data['poster_sc'];

 
if ( !empty($poster_GP) ) { 
	$image_url		= $poster_GP;
	$image			= $poster_GP;
	//$title_image	= sanitize_title($gets_data['title_GP']);
	//$title_image	= sanitize_title_with_dashes(ex_themes_clean($gets_data['GP_ID']));
	//$title		= $title_image.'-'.$post_id;
	
	$title_image	= sanitize_title_with_dashes_and_no_special_chars($title_GP);
	//$title_image2	= sanitize_title_with_dashes($gets_data['title_GP']); 
	$title			= $post_id.'-'.$title_image;
			
	$uploaddir		= wp_upload_dir();
	$filename		= "{$title}.webp"; 
	$uploadfile		= $uploaddir['path'] . '/' . $filename;
	$contents		= file_get_contents($image);
	$savefile		= fopen($uploadfile, 'w');
	fwrite($savefile, $contents);
	fclose($savefile);
	$wp_filetype	= wp_check_filetype(basename($filename), null );
	$attachment		= array(
		'post_mime_type'	=> $wp_filetype['type'],
		'post_title'		=> $filename,
		'post_content'		=> '',
		'post_status'		=> 'inherit'
	);
	$attach_id		= wp_insert_attachment( $attachment, $uploadfile );
	require_once(ABSPATH . 'wp-admin/includes/image.php');
	$attach_data	= wp_generate_attachment_metadata( $attach_id, $uploadfile );
	wp_update_attachment_metadata( $attach_id, $attach_data );
	set_post_thumbnail( $post_id, $attach_id );
	$image_url = wp_get_attachment_url( $attach_id );
}
 
if ( !empty($gets_data['backgrounds_GP']) ) { 
	$image_bg		= $gets_data['backgrounds_GP'];
	//$title_image	= sanitize_title_with_dashes(ex_themes_clean($gets_data['GP_ID']));
	//$title			= $title_image.'-'.$post_id; 
	$title_bg		= sanitize_title_with_dashes_and_no_special_chars($gets_data['title_GP']); 
	$title_bg		= $post_id.'-'.$title_bg;
	$uploaddir		= wp_upload_dir();
	$filename_bg	= "{$title_bg}-bg.webp";
	$uploadfile_bg	= $uploaddir['path'] . '/' . $filename_bg;
	$contents_bg	= file_get_contents($image_bg);
	$savefile_bg	= fopen($uploadfile_bg, 'w');
	fwrite($savefile_bg, $contents_bg);
	fclose($savefile_bg);
	$wp_filetype_bg	= wp_check_filetype(basename($filename_bg), null );
	$attachment_bg	= array(
		'post_mime_type'	=> $wp_filetype_bg['type'],
		'post_title'		=> $filename_bg,
		'post_content'		=> '',
		'post_status'		=> 'inherit'
	);
	$attach_id_bg	= wp_insert_attachment( $attachment_bg, $uploadfile_bg );
	require_once(ABSPATH . 'wp-admin/includes/image.php');
	$attach_data_bg	= wp_generate_attachment_metadata( $attach_id_bg, $uploadfile_bg );
	wp_update_attachment_metadata( $attach_id_bg, $attach_data_bg ); 
	update_post_meta( $post_id, '_background_image_id', $attach_id_bg );
	update_post_meta( $post_id, 'background_images', $attach_id_bg );
	$image_bg_url = wp_get_attachment_url( $attach_id_bg );
}

if( $opt_themes['import_screenshots'] ){		
	if ( !empty($gets_data['images_GP']) ) { 
	global $opt_themes;
	$get_screenshots	= $gets_data['images_GP'];  
	$import_ss_limit	= $opt_themes['import_ss_limit'] ? $opt_themes['import_ss_limit'] : '5';
	$gallery_image	= array(); 
		for ($i = 0; $i < $import_ss_limit; $i++) 
		{
			if ( '' != $get_screenshots[$i] ) 
			{				
				$title_APK		= sanitize_title_with_dashes_and_no_special_chars($gets_data['title_GP']);						
				$post_date 		= sanitize_title_with_dashes_and_no_special_chars(ex_themes_clean(date('Y/m/d g:i:s A')));	 
				$sha256_hash	= hash('sha256', $title_APK);
				$md5_hash 		= hash('md5', $title_APK); 
				$randomNumber 	= rand();
				$md5_date 		= md5(date('Y-m-d'));
				$md5_title 		= md5($title_APK);
				$AlphaNums 		= RandomAlphaNumeric(5);		
				$ss_name 		= $title_APK .'-'. $AlphaNums .'=screenshot-' . ($i + 1);
				$ss_id 			= upload_images_to_wp(esc_url($get_screenshots[$i]), 'webp', $ss_name); 
				$gallery_image['image_url'][] = esc_url(wp_get_attachment_url($ss_id));
			}
		}
		update_post_meta( $post_id, 'gallery_data', $gallery_image ); 
	}
	
} else {
	
	if ( !empty($gets_data['images_GP']) ) 
	{ 
		global $opt_themes;		
		$import_ss_limit	= $opt_themes['import_ss_limit'] ? $opt_themes['import_ss_limit'] : '5';
		$gallery_image	= array();			
		for ($i = 0; $i < $import_ss_limit; $i++ ) 
		{
			if ( '' != $gets_data['images_GP'][$i] ) 
			{
				$gallery_image['image_url'][] = $gets_data['images_GP'][$i];
			}
		}
		update_post_meta( $post_id, 'gallery_data', $gallery_image ); 
	}
} 
 
