/**
  * Activate add-on license.
  *
  * This code is run only when an add-on requiring a license is installed and active.
  *
  * @since 3.0.0
  */
 public function manage_add_on_license()
 {
     $addon = isset($_POST['add_on']) ? sanitize_key($_POST['add_on']) : false;
     $action = isset($_POST['license_action']) ? esc_attr($_POST['license_action']) : false;
     $key = isset($_POST['license_key']) ? esc_attr($_POST['license_key']) : '';
     $nonce = isset($_POST['nonce']) ? esc_attr($_POST['nonce']) : '';
     // Verify that there are valid variables to process.
     if (false === $addon || !in_array($action, array('activate_license', 'deactivate_license'))) {
         wp_send_json_error(__('Add-on unspecified or invalid action.', 'google-calendar-events'));
     }
     // Verify this request comes from the add-ons licenses activation settings page.
     if (!wp_verify_nonce($nonce, 'simcal_license_manager')) {
         wp_send_json_error(sprintf(__('An error occurred: %s', 'google-calendar-events'), 'Nonce verification failed.'));
     }
     // Removes the prefix and converts simcal_{id_no} to {id_no}.
     $id = intval(substr($addon, 7));
     // Data to send in API request.
     $api_request = array('edd_action' => $action, 'license' => $key, 'item_id' => urlencode($id), 'url' => home_url());
     // Call the custom API.
     $response = wp_remote_post(defined('SIMPLE_CALENDAR_STORE_URL') ? SIMPLE_CALENDAR_STORE_URL : simcal_get_url('home'), array('timeout' => 15, 'sslverify' => false, 'body' => $api_request));
     // Update license in db.
     $keys = get_option('simple-calendar_settings_licenses', array());
     $new_keys = array_merge((array) $keys, array('keys' => array($addon => $key)));
     update_option('simple-calendar_settings_licenses', $new_keys);
     // Make sure there is a response.
     if (is_wp_error($response)) {
         wp_send_json_error(sprintf(__('There was an error processing your request: %s', 'google-calendar-events'), $response->get_error_message()));
     }
     // Decode the license data and save.
     $license_data = json_decode(wp_remote_retrieve_body($response));
     $status = simcal_get_license_status();
     if ('deactivated' == $license_data->license) {
         unset($status[$addon]);
         update_option('simple-calendar_licenses_status', $status);
         wp_send_json_success($license_data->license);
     } elseif (in_array($license_data->license, array('valid', 'invalid'))) {
         $status[$addon] = $license_data->license;
         update_option('simple-calendar_licenses_status', $status);
         $message = 'valid' == $license_data->license ? 'valid' : __('License key is invalid.', 'google-calendar-events');
         wp_send_json_success($message);
     } else {
         wp_send_json_error('');
     }
 }
    /**
     * Outputs the field markup.
     *
     * @since 3.0.0
     */
    public function html()
    {
        if (!empty($this->addon)) {
            $status = simcal_get_license_status($this->addon);
            if (empty($status) || in_array($status, array('valid', 'invalid', 'deactivated'))) {
                $display_activate = 'display: inline-block';
                $display_deactivate = 'display: none';
                $active = 'valid' == $status ? 'display: block' : 'display: none';
                $disabled = '';
            } else {
                $display_activate = $active = 'display: none';
                $display_deactivate = 'display: inline-block';
                $disabled = empty($this->value) ? '' : 'disabled="disabled"';
            }
            ?>
			<div class="simcal-addon-manage-license-field" data-addon="<?php 
            echo $this->addon;
            ?>
">

				<input type="text" <?php 
            echo $disabled;
            ?>
				       name="<?php 
            echo $this->name;
            ?>
"
				       id="<?php 
            echo $this->id;
            ?>
"
				       value="<?php 
            echo $this->value;
            ?>
"
				       class="<?php 
            echo $this->class;
            ?>
" />

				<span class="simcal-addon-manage-license-buttons">

					<button class="button-secondary simcal-addon-manage-license deactivate" data-add-on="<?php 
            echo $this->addon;
            ?>
" style="<?php 
            echo $display_deactivate;
            ?>
">
				        <i class="simcal-icon-spinner simcal-icon-spin" style="display: none;"></i><?php 
            _e('Deactivate', 'google-calendar-events');
            ?>
					</button>

					<button class="button-secondary simcal-addon-manage-license activate" data-add-on="<?php 
            echo $this->addon;
            ?>
" style="<?php 
            echo $display_activate;
            ?>
">
						<i class="simcal-icon-spinner simcal-icon-spin" style="display: none;"></i><?php 
            _e('Activate', 'google-calendar-events');
            ?>
					</button>

					<span class="error" style="color: red; display: none"> </span>

					<strong class="label" style="color:green; <?php 
            echo $active;
            ?>
"> <?php 
            _e('(active)', 'google-calendar-events');
            ?>
</strong>

				</span>

			</div>
			<?php 
        }
    }