<?php

if (!function_exists('check_new_versi')) {
	add_shortcode( 'new_versi', 'check_new_versi'  );   
	function check_new_versi() {
		global $post, $wpdb, $opt_themes, $pagenow;
			
		//if ( $pagenow === 'post.php' && isset( $_GET['action'] ) && $_GET['action'] === 'edit' ) { 
			
		$screen = get_current_screen();

			// Check if the current screen is the 'post' post type's edit or new post screen
		if ( $screen->post_type === 'post' && ( $screen->id === 'post' || $screen->id === 'edit-post' ) ) {
			if($opt_themes['check_updates_versi_post']){	
					
			@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'); 
			$scraper		= new Go_Curl();
			$appid			= get_post_meta( get_the_ID(), 'wp_GP_ID', true ); 
			$urlx 			= 'https://a-a.my.id/gps.php?pkgid='.$appid;  
			
			$g_data			= $scraper->scrape_sitex($urlx); 
			$n_version		= $scraper->match('/<div class="c_version">(.*?)<\/div>/ms', $g_data, 1);
			$a_version		= get_post_meta( get_the_ID(), 'available_version', true );
			$c_version		= get_post_meta( get_the_ID(), 'wp_version_GP', true ); 
				
				
			if (version_compare($n_version, $c_version ) > 0 ) {
				update_post_meta( get_the_ID(), 'update_available', 'yes' );
				update_post_meta( get_the_ID(), 'available_version', $n_version );
				//update_post_meta( $post_id, 'wp_sizes', $new_size );
				//update_post_meta( $post_id, 'wp_whatnews_GP', $new_whats_news );
				return true;
			} else {
				
			//if( isset( $_POST[ 'wp_version_GP' ] ) ) {
				delete_post_meta( get_the_ID(), 'update_available' );
				delete_post_meta( get_the_ID(), 'available_version' );
			//}
				return true;
				
			}
	
			
			} 
		} 
	} 
} 

if (!function_exists('my_post_type_notification')) {
	add_action( 'admin_notices', 'my_post_type_notification' ); 
	function my_post_type_notification() {
		global $post, $opt_themes, $pagenow;	 
		if ($opt_themes['check_updates_versi_post']){	
			if ( $pagenow === 'post.php' && isset( $_GET['action'] ) && $_GET['action'] === 'edit' ) { 
			
			//echo do_shortcode( '[new_versi]' );
			check_new_versi();
			
			@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'); 
			$n_version	 	= get_post_meta( get_the_ID(), 'available_version', true );
			$c_version 		= get_post_meta( get_the_ID(), 'wp_version_GP', true );
			$appname	 	= get_post_meta( get_the_ID(), 'wp_title_GP', true );
			$users 			= get_users( array( 'role__in' => array( 'administrator', 'editor' ) ) ); 
				
			if ( version_compare($n_version, $c_version) > 0 ) { 
			?>

			<div id="info_versi" class="notice notice-info is-dismissible marquee" style="background-color: cornsilk; ">
			<p class="marquee-content">📢 Hello <?php echo $users[0]->data->user_nicename; ?>... <b style="color: blue;" class="up-down-shake"><?php echo $appname; ?></b> 
			<?php if($c_version){?><b style="color: red;font-weight: 700;">v<?php echo $c_version; ?></b> <i>Available updates</i> to <?php } ?> <b style="color: green;font-weight: 700;">v<?php echo $n_version; ?></b> </p>
			</div>
						
			<div id="info_versi2" class=" is-dismissible marquee" style="background-color: cornsilk;text-align: center; ">
			<p class="marquee-contents">📢 Hello <?php echo $users[0]->data->user_nicename; ?>... <b style="color: blue;" class="up-down-shake"><?php echo $appname; ?></b> 
			<?php if($c_version){?><b style="color: red;font-weight: 700;">v<?php echo $c_version; ?></b> <i>Available updates</i> to <?php } ?> <b style="color: green;font-weight: 700;">v<?php echo $n_version; ?></b> </p>
			</div>
						 
			<p id="info_versi3" style="background-color: cornsilk;text-align: center; ">
			<b style="color: blue;" class="up-down-shake"><?php echo $appname; ?></b> 
			<?php if($c_version){?><b style="color: red;font-weight: 700;">v<?php echo $c_version; ?></b> <i>Available updates</i> to <?php } ?> <b style="color: green;font-weight: 700;">v<?php echo $n_version; ?></b> </p>
						 
<style>
.blink {animation: blink 1s step-end infinite;}@keyframes blink {0% {opacity: 1;}50% {opacity: 0;}100% {opacity: 1;}}.marquee {width: auto;overflow: hidden;white-space: nowrap;box-sizing: border-box;}.marquee-content {display: inline-block;animation: marquee 30s linear infinite;}@keyframes marquee {0% {transform: translateX(100%);}100% {transform: translateX(-100%);}}.marquee:hover .marquee-content,.marquee marquee--fit-content:hover .marquee-content {animation-play-state: paused;}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script> 

$(document).ready(function() {
    // Insert a paragraph before the div
    //$('#wp_apk_details').before('#info_versi2');
    
    // Or using insertBefore
    $('#info_versi2').insertBefore('.admin-inside-box.clearfix');
    $('#info_versi3').insertAfter('#misc-publishing-actions');
    //$('#info_versi2').insertAfter('ul.aps-features');
});
</script>
	<?php 	
			}
			
			}

		}
	}
}


if(!function_exists('count_apps_update')){
function count_apps_update() {
	global $wpdb; 
	/*
	https://stackoverflow.com/a/41538641
	"SELECT * FROM ".$wpdb->prefix."contentreports ORDER BY status='NEW' ASC"
	*/
	$wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'update_available' AND meta_value = 'yes')");
	return $wpdb->num_rows;
}
}
###### Register a custom menu bar. ###### 

if(!function_exists('wp_apps_update_admin_bar_link')){
function wp_apps_update_admin_bar_link() {
	global $wp_admin_bar;
	if ( !is_super_admin() || !is_admin_bar_showing() )
		return;
	
	$wp_admin_bar->add_menu( array(
		'id'    => 'wp_apps_updates',
		'title' => '<span class="ab-icon dashicons dashicons-info-outline"></span> Apps to update '.count_apps_update().'',
		'href' 	=> admin_url('/admin.php?page=wp_apps_to_updates'),
		),
	);
}
add_action( 'admin_bar_menu', 'wp_apps_update_admin_bar_link', 100 );
}
if(!function_exists('apps_to_updates_menu')){
function apps_to_updates_menu() {
    add_menu_page(
        __( 'Apps to update ', THEMES_NAMES ), 
		'Apps update <span class="awaiting-mod">'.count_apps_update().'</span> ',
        'manage_options',
        'wp_apps_to_updates',
        'wp_apps_to_updates',
        'dashicons-info-outline',
        //plugins_url( '/img/imdb.png' ),
        100
    );  
     
}
add_action('admin_menu', 'apps_to_updates_menu');
} 

function wp_apps_to_updates( $post ) {  
echo do_shortcode( '[css]' );	
global $opt_themes, $wpdb, $post, $wp_query; 
	@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'); 
		 
$list_post = get_posts( array(
	'posts_per_page'   	=> -1,
	'meta_key'   		=> 'update_available',
	'meta_value' 		=> 'yes',
) );

 
 
if ($list_post){ ?>
<div class="play_menu" style="text-transform:uppercase"><b>List all Available app to updates </b></div>
<?php } else { ?>
<div class="play_menu" style="text-transform:uppercase"><b>Welldone... No Available app to updates </b></div>
<?php } if ($list_post){ ?>
<section>
<div class="row">
<?php  
foreach($list_post as $p) {
$appnames				= wp_strip_all_tags(get_post_meta( $p->ID, 'wp_title_GP',  true )); 
$available_version		= get_post_meta($p->ID, 'available_version', true );
$current_version		= get_post_meta($p->ID, 'wp_version_GP', true );
$image_id				= get_post_thumbnail_id($p->ID); 
$image_url				= wp_get_attachment_image_src($image_id, 'thumbnail', true)[0]; 

if (version_compare($available_version, $current_version ) > 0 ) { 
?>
        <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 $image_url; ?>" class="app-logo " width="64" height="64">
                </div>

                <div class="app-info">
                    <h3 class="h5 font-weight-semibold w-100 app-title up-down-shake"><?php echo $appnames; ?></h3>

                    <div class="text-truncate text-muted app-desc">
                        <span class="clamp-1 w-100" style="color: black;font-weight: 700;"><i>Available updates</i> <b style="color: red;">v<?php echo $current_version; ?></b> to <b style="color: green;">v<?php echo $available_version; ?></b></span>
                    </div>
                </div>

                <div class="app-get">
                    <a style="font-size: 13px;  margin:0 12px;" href="<?php echo get_edit_post_link($p->ID); ?>" target="_blank" class="get-button rotate-shake">Update</a>
                </div>

            </span>
        </div>

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

<?php } ?>


<?php 

echo do_shortcode( '[ft_menu]' );
echo do_shortcode( '[ft_js]' ); 
} 
