<?php
/**
 * Page assets compiler.
 *
 * @since   1.0.0
 * @ignore
 * @access  private
 *
 * @package SplendorStudio\Admin\Welcome
 */
final class Fancify_Lite_Fancify_Lite_Core extends Fancify_Lite_Admin {

	/**
	 * hold tgmpa instance
	 * @var status
	 */
	public $tgmpa;

	/**
	 * Constructor
	 *
	 * @since    1.0.0
	 */
	protected function __construct() {
		// Include.
		include_once FANCIFY_API_PATH . 'tgmpa/class-tgm-plugin-activation.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound

		// Tgmpa
		$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );

		$this->register_plugin();

		add_action( 'tgmpa_register', array( $this, 'register_menu' ) );

		$this->register_hook_callbacks();
	}

	/**
	 * Register tgmpa plugin
	 *
	 * @since    1.0.0
	 */
	public function register_plugin() {
		$plugins = array(
			array(
				'name'      => 'Fancify Core',
				'slug'      => 'fancify-core',
				'required'  => false,
			)
		);

		$config = array(
			'id'           => 'fancify_lite_',
			'menu'         => 'tgmpa-install-plugins',
			'has_notices'  => false,
			'dismissable'  => false,
			'dismiss_msg'  => '',
			'is_automatic' => false,
			'message'      => '',
		);

		tgmpa( $plugins, $config );
	}

	/**
	 * Register tgmpa plugin
	 *
	 * @since    1.0.0
	 */
	public function register_menu() {
		add_action( 'admin_menu', array( $this, 'theme_menu' ) );
	}

	/**
	 * Add theme page.
	 *
	 * @since    1.0.0
	 */
	public function theme_menu() {
				// FANCIFY-LITE
		$menu_name = __( 'Fancify Lite', 'fancify-lite' );
		// FANCIFY-LITE-END
		add_theme_page( $menu_name, $menu_name, 'install_themes', 'fancify_lite_', array( $this, 'welcome_render' ) );
	}

	/**
	 * Add theme page.
	 *
	 * @since    1.0.0
	 */
	public function welcome_render() {
		echo static::render_template(// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this is argument passed and escaped on welcome.php
			'general/admin/welcome.php',
			array(
				'logo' 		=> FANCIFY_ADMIN_ASSETS_URL . 'images/logo.png',// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- its escaped on welcome.php
				'page' 		=> sanitize_text_field( wp_unslash( $_GET['page'] ) ),// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- it would be check on welcome.php
				'source'	=> $this->get_tgmpa_url()// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- it would be escape on welcome.php
			)
		); 
	}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since    1.0.0
	 */
	protected function register_hook_callbacks() {
		Fancify_Lite_Actions::add_smart_action( 'admin_notices', array( $this, 'install_notice' ) );
		Fancify_Lite_Actions::add_smart_action( 'wp_ajax_install_fancify_lite_core_plugin', array( $this, 'install_fancify_lite_core' ) );
		add_action( 'wp_ajax_fancify_lite_dismiss_notice', array( $this, 'dismiss_handler' ) );
	}

	/**
	 * Print admin notice.
	 *
	 * @since 1.0.0
	 */
	public function install_notice() {
		if ( get_option( 'core_plugin_dismiss_notice', false ) ) {
			return;
		}

		$screen = get_current_screen();
		
		if ( $screen->id === 'appearance_page_fancify_lite_' ) {
			return;
		}

		echo static::render_template(// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this is argument passed and escaped on welcome-notice.php
			'general/admin/welcome-notice.php',
			array(
				'source'	=>	$this->get_tgmpa_url()// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- it would be escape on welcome-notice.php
			)
		);
	}

	/**
	 * Install plugin via ajax.
	 *
	 * @since 1.0.0
	 */
	public function install_fancify_lite_core() {
		// Check ajax nounce
		$type     = isset( $_GET['type'] )  ? sanitize_text_field( wp_unslash( $_GET['type'] ) )   : '';
		check_admin_referer( 'tgmpa-' . $type, 'tgmpa-nonce' );

		$url      = '';
		$htmlspecialchars_decode = '';

		// Simple check
		if ( '' === $type ) {
			wp_send_json( array( 'error' => true, 'message' => esc_html__( 'Invalid request', 'fancify-lite' ) ) );
		}

		if ( 'activate' === $type ) {
			$type = 'Activated';
		}

		if ( 'update' === $type ) {
			$type = 'activate';
		}

		if ( 'install' === $type ) {
			$update_info = $this->tgmpa->does_plugin_have_update( 'fancify-core' );
			if ( $update_info ) {
				$type = 'update';
			} else {
				$type = 'activate';
			}

		}

		$this->tgmpa->install_plugins_page();

		if ( 'Activated' != $type ) {    
			$url = wp_nonce_url(
				add_query_arg(
					array(
						'plugin'            => urlencode( 'fancify-core' ),
						'tgmpa-' . $type    => $type . '-plugin',
					),
					$this->tgmpa->get_tgmpa_url()
				),
				'tgmpa-' . $type,
				'tgmpa-nonce'
			);

			$htmlspecialchars_decode =  wp_specialchars_decode( $url );
		}

		echo json_encode( array( 'error' => false, 'action' => $type, 'source' => $htmlspecialchars_decode, 'url' => esc_url( admin_url( 'admin.php?page=fancify-core' ) ) ) );

		die();
	}

	/**
	 * Print admin notice.
	 *
	 * @since 1.0.0
	 */
	public function get_tgmpa_url() {
		if ( $this->tgmpa->is_plugin_installed( 'fancify-core' ) ) {
			$type = 'activate';
		} else {
			$type = 'install';
			$update_info = $this->tgmpa->does_plugin_have_update( 'fancify-core' );
			if ( $update_info ) {
				$type = 'update';
			}
		}

		$url = wp_nonce_url(
			add_query_arg(
				array(
					'plugin'            => urlencode( 'fancify-core' ),
					'tgmpa-' . $type    => $type . '-plugin',
				),
				$this->tgmpa->get_tgmpa_url()
			),
			'tgmpa-' . $type,
			'tgmpa-nonce'
		);

		return array( 'url' => $url, 'type' => $type );
	}

	/**
	 * Core plugin dismiss notification.
	 *
	 * @since 1.0.2
	 */
	public function dismiss_handler() {
		// Ajax security check
		check_ajax_referer( 'fancify_lite_admin_ajax_nounce', 'security' );

		update_option( 'core_plugin_dismiss_notice', true, false );
	}

}
