<?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.


add_filter( 'manage_posts_columns', function( $columns ) {
    $columns['app_update_status'] = 'App Updatex';
    return $columns;
});

add_action( 'manage_posts_custom_column', function( $column, $post_id ) {

    if ( $column !== 'app_update_status' ) {
        return;
    }

    $has_update   = get_post_meta( $post_id, 'available_updated', true );
    $new_version  = get_post_meta( $post_id, 'available_version', true );
    $current      = get_post_meta( $post_id, 'wp_version_GP', true );
    $last_check   = get_post_meta( $post_id, '_time_scrape_app_update', true );

    if ( $has_update === 'yes' && $new_version ) {
        echo '<span class="app-badge app-badge-update">';
        echo 'UPDATE → <strong>' . esc_html( $new_version ) . '</strong>';
        echo '</span>';

    } elseif ( $current ) {
        echo '<span class="app-badge app-badge-ok">UP TO DATE</span>';

    } else {
        echo '<span class="app-badge app-badge-unknown">UNKNOWN</span>';
    }

    if ( $last_check ) {
        echo '<div class="app-last-check">';
        echo 'Last check:<br><small>' . date_i18n( 'Y-m-d H:i', $last_check ) . '</small>';
        echo '</div>';
    }

}, 10, 2 );
add_action( 'admin_head', function() {
    ?>
    <style>
        .app-badge {
            display:inline-block;
            padding:3px 8px;
            border-radius:4px;
            font-size:12px;
            font-weight:600;
            color:#fff;
        }
        .app-badge-update { background:#d63638; }
        .app-badge-ok     { background:#2271b1; }
        .app-badge-unknown{ background:#8c8f94; }

        .app-last-check {
            margin-top:4px;
            font-size:11px;
            color:#555;
        }
    </style>
    <?php
});

add_filter( 'manage_edit-post_sortable_columns', function( $columns ) {
    $columns['app_update_status'] = 'available_updated';
    return $columns;
});

/* 
add_action( 'restrict_manage_posts', function() {
    ?>
    <select name="app_update_filter">
        <option value="">All Apps</option>
        <option value="update"<?php selected($_GET['app_update_filter'] ?? '', 'update'); ?>>
            Update Available
        </option>
    </select>
    <?php
});
*/

add_action( 'pre_get_posts', function( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) return;

    if ( ($_GET['app_update_filter'] ?? '') === 'update' ) {
        $query->set( 'meta_query', [
            [
                'key'   => 'available_updated',
                'value' => 'yes',
            ]
        ]);
    }
});


/* --------------------------------------------------------------------
* Add the column 
* ------------------------------------------------------------------ 
*/	 

if ( ! function_exists( 'apk_update_status_column' ) ) 
{
	//add_filter('manage_post_posts_columns', 'apk_update_status_column', 999);
	function apk_update_status_column($columns) 
	{
		// Insert after title, before date (or replace any column you want)
		$new_columns = [];
		foreach ($columns as $key => $value) {
			$new_columns[$key] = $value;
			if ($key === 'version') {
				$new_columns['apk_update'] = 'Update Available';
			}
		}
		return $new_columns;
	}
}
 
// Add column
//add_filter( 'manage_edit-your_cpt_name_columns', 'apk_update_add_column' );
//add_filter( 'manage_posts_columns', 'apk_update_add_column' ); // if using regular posts

if ( ! function_exists( 'apk_update_add_column' ) ) 
{
	function apk_update_add_column( $columns ) 
	{
		$columns['apk_update'] = 'Update Available';
		return $columns;
	}
}

/* --------------------------------------------------------------------
* apk_update column 
* ------------------------------------------------------------------ 
*/	 

if ( ! function_exists( 'apk_update_fill_column' ) ) 
{
	//add_action( 'manage_posts_custom_column', 'apk_update_fill_column', 10, 2 );
	function apk_update_fill_column( $column, $post_id ) 
	{
		if ( $column === 'apk_update' ) {
			if ( get_post_meta( $post_id, 'available_updated', true ) ) {
				echo '<span style="color:green;font-weight:bold;">v.'.get_post_meta( $post_id, 'available_version', true ).'</span>';
			} else {
				echo '—';
			}
		}
	}
}
 
 
/**
 * 2. Handle Bulk Action
 */
add_filter( 'handle_bulk_actions-edit-post', function(
    $redirect_url,
    $action,
    $post_ids
) {

    if ( $action !== 'check_app_update' ) {
        return $redirect_url;
    }

    if ( ! current_user_can( 'edit_posts' ) ) {
        return $redirect_url;
    }

    $checked = 0;

    foreach ( $post_ids as $post_id ) {
        $post_id = (int) $post_id;

        if ( function_exists( 'app_update_available' ) ) {
            // false = jangan tampilkan notice per app
            app_update_available( $post_id, false );
            $checked++;
        }
    }

    // Kirim hasil ke redirect
    $redirect_url = add_query_arg(
        [
            'app_update_checked' => $checked,
        ],
        $redirect_url
    );

    return $redirect_url;

}, 10, 3 );
