<?php

class WKFE_PRO_Update_Notification{
    private static $instance = null;
    private $api_url = 'https://widgetkit.themesgrove.com/wp-json/wk/changelog';

    public static function init(){
        if(null === self::$instance ){
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function __construct(){
        add_filter ('pre_set_site_transient_update_plugins', [$this, 'wkpro_update_notification'] );
    }

    /**
     * update notification
     */
    public function wkpro_update_notification ($transient)
    {   
        $latest_version = $this->wkpro_get_latest_version_number();
        $latest_version = $latest_version['plugin_version'];
        $current_version = WKP_VERSION;

        if($latest_version > $current_version ){
            $obj = new stdClass();
            $obj->slug = 'widgetkit-pro';
            $obj->new_version = $latest_version;
            $obj->url = 'https://themesgrove.com';
            $obj->package = 'https://themesgrove.com/account/';
            $transient->response['widgetkit-pro/widgetkit-pro.php'] = $obj;
            return $transient;
        }else{
            return $transient;
        }
        
    }

    public function wkpro_get_latest_version_number(){

        $remote_api_data = wp_remote_get($this->api_url);
        $response_data = wp_remote_retrieve_body($remote_api_data);
        $data_arr = json_decode($response_data, true);
        $changes_data = $data_arr['changes'][0];


        return $changes_data;
    }
    
}

?>