<?php 
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class CFKEF_License_Helper {
    
    private static $allowed_pages = array(
        'cool-formkit',
    );

    public function __construct(){
        add_action( 'after_plugin_row_' . CFKEF_PLUGIN_BASENAME, array( $this, 'cfkef_plugin_custom_notice' ), 10, 2 );
        add_filter( 'site_transient_update_plugins', array( $this, 'cfkef_hide_plugin_update_notice' ) );
    }

    public function cfkef_plugin_custom_notice($plugin_file, $plugin_data){
        $text_domain = 'cool-formkit';
		if ( ! class_exists('Cool_Form_Kit_For_Elementor_Forms_Base') ) {
			return; 
		}
    	// Get license info and update info for the plugin
		$license_info = Cool_Form_Kit_For_Elementor_Forms_Base::GetRegisterInfo();
		$update_info = Cool_Form_Kit_For_Elementor_Forms_Base::getInstance()->el_plugin_update_info();
		// Get version available message using common helper
		$version_available_message = CoolFormKitForElementorForms::getVersionAvailableMessage();
		$plugin_basename = CFKEF_PLUGIN_BASENAME;

		if ( ! CoolFormKitForElementorForms::$form_status &&  $plugin_basename === CFKEF_PLUGIN_BASENAME ) {	
			$renew_link = wp_kses_post(
				__( $version_available_message.' Please <a href="admin.php?page=cool-formkit&tab=license">enter your license key</a> to enable automatic updates and premium support for Cool FormKit for Elementor Forms', $text_domain ),
				array(
					'a' => array(
						'href' => array()
					)
				)
			);

		} else {
			// Check if new version is available and license is invalid/expired            

			if (empty( $update_info->download_link ) && ($this->tmdivi_is_support_expired($license_info) === true || $license_info->is_valid === 'license_expired')) {	
				$is_expired = (empty($update_info->is_downloadable) && $this->tmdivi_is_support_expired($license_info) !== true) ? 'license' : 'support';
                if($this->tmdivi_is_support_expired($license_info) === true && $license_info->is_valid === 'license_expired'){
                    $is_expired = 'license';
                }
				$message = ' Your ' . $is_expired . ' has expired,';
				$renew_text = ( ! empty( $license_info->market ) && $license_info->market === 'E' ) 
				? 'Please renew your ' . $is_expired . ' to continue receiving automatic updates and priority support.'
				: 'Please <a href="https://my.coolplugins.net/account/subscriptions/" target="_blank">Renew now</a> to continue receiving automatic updates and priority support.';	
				$renew_link = $version_available_message . $message . ' ' . $renew_text;
			}
		}

		if ( ! empty( $renew_link )) {
			?>
            <style>
					.plugins tr.active[data-plugin="cool-formkit-for-elementor-forms/cool-formkit-for-elementor-forms.php"]
					th,
					.plugins tr.active[data-plugin="cool-formkit-for-elementor-forms/cool-formkit-for-elementor-forms.php"]
					td 
					{
					 box-shadow: none;
					}
			</style>
			<tr class="plugin-update-tr active">
				<td colspan="4" class="plugin-update colspanchange">
					<div class="update-message notice inline notice-warning notice-alt"> 
						<p><?php echo wp_kses_post( $renew_link ); ?></p>
					</div>
				</td>
			</tr>
			<?php
		}   
    }

    public function cfkef_hide_plugin_update_notice($transient){
		if ( ! class_exists('Cool_Form_Kit_For_Elementor_Forms_Base') ) {
			return $transient; 
		}

        $update_info = Cool_Form_Kit_For_Elementor_Forms_Base::getInstance()->el_plugin_update_info();
		$license_info = Cool_Form_Kit_For_Elementor_Forms_Base::GetRegisterInfo();

		if ( empty($update_info->download_link) || $license_info == null){

			if ( isset( $transient->response ) && isset( $transient->response[CFKEF_PLUGIN_BASENAME] ) ) {
					unset( $transient->response[CFKEF_PLUGIN_BASENAME] );
				}
		}
		
		return $transient;
    }

    public function tmdivi_is_support_expired($license_info) {
        return $license_info->support_end === 'no support' || 
            ($license_info->is_valid === 'support_expired' || 
            (strtolower($license_info->support_end) !== 'unlimited' && 
            strtotime($license_info->support_end) < time()));
    }
}
new CFKEF_License_Helper;