<?php
/**
 *
 * Field: License
 *
 * @link       https://shapedplugin.com/
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin/views
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access directly.


if ( ! class_exists( 'SP_PCP_Field_license' ) ) {
	/**
	 *
	 * Field: license
	 *
	 * @since 2.1.0
	 * @version 2.1.0
	 */
	class SP_PCP_Field_license extends SP_PCP_Fields {
		/**
		 * The class constructor.
		 *
		 * @param array  $field The field type.
		 * @param string $value The values of the field.
		 * @param string $unique The unique ID for the field.
		 * @param string $where To where show the output CSS.
		 * @param string $parent The parent args.
		 */
		public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {

			parent::__construct( $field, $value, $unique, $where, $parent );
		}
		/**
		 * The render method.
		 *
		 * @return void
		 */
		public function render() {
			echo wp_kses_post( $this->field_before() );
			$type = ( ! empty( $this->field['attributes']['type'] ) ) ? $this->field['attributes']['type'] : 'text';

			$manage_license       = new Smart_Post_Show_Pro_License( SMART_POST_SHOW_PRO_FILE, SP_PCP_VERSION, 'ShapedPlugin', SP_SPSP_STORE_URL, SP_SPSP_ITEM_ID, SP_PCP_PLUGIN_NAME );
			$license_key          = $manage_license->get_license_key();
			$license_key_status   = $manage_license->get_license_status();
			$license_status       = ( is_object( $license_key_status ) ? $license_key_status->license : '' );
			$license_notices      = $manage_license->license_notices();
			$license_status_class = '';
			$license_active       = '';
			$license_data         = $manage_license->api_request();

			echo '<div class="spsp-license text-center">';
			echo '<h3>' . esc_html__( 'Smart Post Pro License Key', 'smart-post-show-pro' ) . '</h3>';
			if ( 'valid' === $license_status ) {
				$license_status_class = 'license-key-active';
				$license_active       = '<span>' . esc_html__( 'Active', 'smart-post-show-pro' ) . '</span>';
				echo '<p>' . esc_html__( 'Your license key is active.', 'smart-post-show-pro' ) . '</p>';
			} elseif ( 'expired' === $license_status ) {
				echo '<p style="color: red;">Your license key expired on ' . date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) . '. <a href="' . SP_SPSP_STORE_URL . '/checkout/?edd_license_key=' . $license_key . '&download_id=' . SP_SPSP_ITEM_ID . '&utm_campaign=smart_post_show_pro&utm_source=licenses&utm_medium=expired" target="_blank">Renew license key at discount.</a></p>';
			} else {
				echo '<p>Please activate your license key to make the plugin work. <a href="https://wpsmartpost.com/docs/installation-and-license-activation" target="_blank">How to activate license key?</a></p>';
			}
			echo '<div class="spsp-license-area">';
			echo '<div class="spsp-license-key"><input class="spsp-license-key-input ' . esc_attr( $license_status_class ) . '" type="' . esc_attr( $type ) . '" name="' . esc_attr( $this->field_name() ) . '" value="' . esc_attr( $this->value ) . '"' . $this->field_attributes() . ' />' . $license_active . '</div>';// phpcs:ignore
			wp_nonce_field( 'sp_spsp_nonce', 'sp_spsp_nonce' );
			if ( 'valid' === $license_status ) {
				echo '<input style="color: #dc3545; border-color: #dc3545;" type="submit" class="button-secondary btn-license-deactivate" name="sp_spsp_license_deactivate" value="' . esc_html__( 'Deactivate', 'smart-post-show-pro' ) . '"/>';
			} else {
				echo '<input type="submit" class="button-secondary btn-license-save-activate" name="' . esc_attr( $this->unique ) . '[_nonce][save]" value="' . esc_html__( 'Activate', 'smart-post-show-pro' ) . '"/>';
				echo '<input type="hidden" class="btn-license-activate" name="sp_spsp_license_activate" value="' . esc_html__( 'Activate', 'smart-post-show-pro' ) . '"/>';
			}
			echo '<br><div class="spsp-license-error-notices">' . esc_html( $license_notices ) . '</div>';
			echo '</div>';
			echo '</div>';
			echo wp_kses_post( $this->field_after() );
		}
	}
}
