<?php 

    class WK_Pro_License_Deactivate{

        private static $instance;
        private $message = 'No action happen';

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

        public function initiate($license=''){
            global $wp_version;
            // check_admin_referer('wk-pro-license'); 
    
            $api_params = array(
                'edd_action' => 'deactivate_license',
                'license'    => $license,
                'item_name'  => urlencode( WKP_PRODUCT_NAME ), // the name of our product in EDD
                'url'        => home_url()
            );
    
            // Call the custom API.
            $response = wp_remote_post( WKP_STORE_URL, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
            
            // make sure the response came back okay
            if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {

                if ( is_wp_error( $response ) ) {
                    $this->message = $response->get_error_message();
                } else {
                    $this->message = __( 'An error occurred, please try again.' );
                }

            }

            // decode the license data
            $license_data = json_decode( wp_remote_retrieve_body( $response ) );

            // $license_data->license will be either "deactivated" or "failed"
            if( $license_data->license == 'deactivated' or $license_data->license == 'failed' ) {
                delete_option( 'wk_pro_license_key' );
                delete_option( 'wk_pro_user_license_info' );
                delete_option( 'wk_pro_user_license_status' );
            }

            if( true == $license_data->success ){
                return true;
            }else{
                return $this->message;
            }
            
        }
    }

?>