<?php
/**
 * Wisnia Notice Handler For WordPress Admin Screens
 *
 * @author  Wisnia
 * @package Wisnia
 * @since   1.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class to handle notices and Advanced Demo Import
 *
 * Class Wisnia_Theme_Notice
 */
class Wisnia_Theme_Notice {

	/**
	 * Empty Constructor
	 */
	public function __construct() { }

	/**
	 * Gets an instance of this object.
	 * Prevents duplicate instances which avoid artefacts and improves performance.
	 *
	 * @static
	 * @access public
	 * @since 1.0.0
	 * @return object
	 */
	public static function instance() {
		// Store the instance locally to avoid private static replication
		static $instance = null;

		// Only run these methods if they haven't been ran previously
		if ( null === $instance ) {
			$instance = new self();
		}

		// Always return the instance
		return $instance;
	}

	/**
	 * Initialize the class
	 *
	 * 3 Different Process
	 */
	public function run() {
		$this->get_started_notice();
		$this->review_notice();
	}

	/**
	 * Get Started Notice
	 *
	 * Handle Getting Started Functions
	 * return void
	 */
	private function get_started_notice() {

		add_action( 'wp_loaded', array( $this, 'admin_notice' ), 20 );
		add_action( 'wp_loaded', array( $this, 'hide_notices' ), 15 );
		add_action( 'wp_ajax_wisnia_getting_started', array( $this, 'install_advanced_import' ) );
	}

	/**
	 * Get Started Notice
	 * Active callback of wp_loaded
	 * return void
	 */
	public function admin_notice() {
		/*Check for notice nag*/
		$notice_nag = get_option( 'wisnia_admin_notice_welcome' );
		if ( ! $notice_nag ) {
			wp_enqueue_style( 'wisnia-notice', WISNIA_URL . '/inc/admin/notice.css', array(), '1.0.0' );
			wp_enqueue_script( 'wisnia-adi-install', WISNIA_URL . '/inc/admin/notice.js', array( 'jquery' ), '', true );

			$translation = array(
				'btn_text' => esc_html__( 'Processing...', 'wisnia' ),
				'nonce'    => wp_create_nonce( 'wisnia_demo_import_nonce' ),
			);
			wp_localize_script( 'wisnia-adi-install', 'wisnia_adi_install', $translation );

			/*admin notice hook*/
			add_action( 'admin_notices', array( $this, 'getting_started' ) );
		}
	}

	/**
	 * Get Started Notice
	 * Active callback of wp_loaded
	 * return void
	 */
	public static function hide_notices() {

		if ( isset( $_GET['wisnia-gsm-hide-notice'] ) && isset( $_GET['wisnia_gsm_admin_notice_nonce'] ) ) {
			if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['wisnia_gsm_admin_notice_nonce'] ) ), 'wisnia_gsm_hide_notices_nonce' ) ) {
				wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'wisnia' ) );
			}

			if ( ! current_user_can( 'manage_options' ) ) {
				wp_die( esc_html__( 'Cheatin&#8217; huh?', 'wisnia' ) );
			}

			$notice_type = sanitize_text_field( wp_unslash( $_GET['wisnia-gsm-hide-notice'] ) );
			update_option( 'wisnia_admin_notice_' . $notice_type, 1 );

			/*Update to Hide.*/
			if ( 'welcome' === $_GET['wisnia-gsm-hide-notice'] ) {
				update_option( 'wisnia_admin_notice_' . $notice_type, 1 );
			} else { // Show.
				delete_option( 'wisnia_admin_notice_' . $notice_type );
			}
		}
	}

	/**
	 * Get Started Notice
	 * Active callback of admin_notices
	 * return void
	 */
	public function getting_started() {
		?>
		<div id="wisnia-gsm" class="updated notice-info wisnia-gsm">
			<a class="wisnia-gsm-close notice-dismiss" href="<?php echo esc_url( wp_nonce_url( remove_query_arg( array( 'activated' ), add_query_arg( 'wisnia-gsm-hide-notice', 'welcome' ) ), 'wisnia_gsm_hide_notices_nonce', 'wisnia_gsm_admin_notice_nonce' ) ); ?>">
				<?php esc_html_e( 'Dismiss', 'wisnia' ); ?>
			</a>
			<div class="wisnia-gsm-container">
				<img class="wisnia-gsm-screenshot" src="<?php echo esc_url( WISNIA_URL . '/screenshot.png' ); ?>" alt="<?php esc_attr_e( 'Wisnia', 'wisnia' ); ?>" />
				<div class="wisnia-gsm-notice">
					<h2>
						<?php
						printf(
						/* translators: 1: welcome page link starting html tag, 2: welcome page link ending html tag. */
							esc_html__( 'Welcome! Thank you for choosing %1$s! You can configure the setup of this theme using the %2$swelcome page%3$s or following the instructions below:', 'wisnia' ),
							wp_kses_post( '<strong>' . wp_get_theme()->get( 'Name' ) . '</strong>' ),
							'<a href="' . esc_url( admin_url( 'themes.php?page=wisnia-intro' ) ) . '">',
							'</a>'
						);
						?>
					</h2>

					<p class="plugin-install-notice">
						<?php esc_html_e( 'Wisnia is now installed, to get the most out of this Theme we recommended that you complete the following steps:', 'wisnia' ); ?>
						<ul>
							<li><?php esc_html_e( '1. Click Download the Demo Zip and grab your zip file from the page', 'wisnia' ); ?></li>
							<li><?php esc_html_e( '2. Next click Get Started below to start the setup wizard', 'wisnia' ); ?></li>
							<li><?php esc_html_e( '3. On the following screen browse and select the 3 files (by file type) that you\'ll find in the zip you downloaded previously', 'wisnia' ); ?></li>
							<li><?php esc_html_e( '4. Finally click Import Demo Data to ensure your theme looks similar to the preview', 'wisnia' ); ?></li>
							<li><br></li>
							<li><em><?php esc_html_e( 'The entire process only takes a few clicks to complete and will speed up your setup by installing and activating the required plugins for things to display correctly. As well as importing example pages and layouts that can be easily customized.', 'wisnia' ); ?></em></li>
						</ul>
					</p>

					<a class="button button-hero" href="<?php echo esc_url( __( 'https://www.individualobligation.com/wordpress-theme-wisnia/#demo-setup', 'wisnia' ) ); ?>" target="_blank"><?php esc_html_e( 'Download the Demo Zip', 'wisnia' ); ?></a>
					<a class="wisnia-gsm-btn button button-primary button-hero" href="#" data-name="" data-slug="" aria-label="<?php esc_attr_e( 'Get Started With Wisnia', 'wisnia' ); ?>">
						<?php esc_html_e( 'Get started with Wisnia', 'wisnia' ); ?>
					</a>

				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Get Started Notice
	 * Active callback of wp_ajax
	 * return void
	 */

	public function install_advanced_import() {

		check_ajax_referer( 'wisnia_demo_import_nonce', 'security' );

		$slug   = 'one-click-demo-import';
		$plugin = 'one-click-demo-import/one-click-demo-import.php';

		$status             = array(
			'install' => 'plugin',
			'slug'    => sanitize_key( wp_unslash( $slug ) ),
		);

		// check if plugin is already active, if it is then send success status
		if ( is_plugin_active_for_network( $plugin ) || is_plugin_active( $plugin ) ) {
			// Plugin is activated
			wp_send_json_success( $status );
		}

		// check if user is allowed to install plugins, if not send error status
		if ( ! current_user_can( 'install_plugins' ) ) {
			$status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.', 'wisnia' );
			wp_send_json_error( $status );
		}

		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
		include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound

		// If plugin looks like a plugin is installed, but not active. Activate the plugin and send success status
		if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
			$plugin_data          = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
			$status['plugin']     = $plugin;
			$status['pluginName'] = $plugin_data['Name'];

			if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) {
				$result = activate_plugin( $plugin );

				if ( is_wp_error( $result ) ) {
					$status['errorCode']    = $result->get_error_code();
					$status['errorMessage'] = $result->get_error_message();
					wp_send_json_error( $status );
				}
				wp_send_json_success( $status );
			}
		}

		// Get plugin information about URL slug
		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => sanitize_key( wp_unslash( $slug ) ),
				'fields' => array(
					'sections' => false,
				),
			)
		);

		// Check for error and send error message if true
		if ( is_wp_error( $api ) ) {
			$status['errorMessage'] = $api->get_error_message();
			wp_send_json_error( $status );
		}

		$status['pluginName'] = $api->name;

		$skin     = new WP_Ajax_Upgrader_Skin();
		$upgrader = new Plugin_Upgrader( $skin );
		$result   = $upgrader->install( $api->download_link );

		// Check for errors and debug
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			$status['debug'] = $skin->get_upgrade_messages();
		}

		if ( is_wp_error( $result ) ) {
			$status['errorCode']    = $result->get_error_code();
			$status['errorMessage'] = $result->get_error_message();
			wp_send_json_error( $status );
		} elseif ( is_wp_error( $skin->result ) ) {
			$status['errorCode']    = $skin->result->get_error_code();
			$status['errorMessage'] = $skin->result->get_error_message();
			wp_send_json_error( $status );
		} elseif ( $skin->get_errors()->get_error_code() ) {
			$status['errorMessage'] = $skin->get_error_messages();
			wp_send_json_error( $status );
		} elseif ( is_null( $result ) ) {
			require_once( ABSPATH . 'wp-admin/includes/file.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
			WP_Filesystem();
			global $wp_filesystem;

			$status['errorCode']    = 'unable_to_connect_to_filesystem';
			$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'wisnia' );

			// Pass through the error from WP_Filesystem if one was raised.
			if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
				$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
			}

			wp_send_json_error( $status );
		}

		$install_status = install_plugin_install_status( $api );

		if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
			$result = activate_plugin( $install_status['file'] );

			if ( is_wp_error( $result ) ) {
				$status['errorCode']    = $result->get_error_code();
				$status['errorMessage'] = $result->get_error_message();
				wp_send_json_error( $status );
			}
		}
		wp_send_json_success( $status );

	}

	/**
	 * Get Started Notice
	 *
	 * Handle Rating/Review Notice
	 * return void
	 */
	private function review_notice() {
		add_action( 'after_setup_theme', array( $this, 'setup_review_notice' ) );
		add_action( 'switch_theme', array( $this, 'remove_review_notice' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'review_notice_enqueue' ) );
	}

	/**
	 * Set the Time
	 * Aso call other necessary functions
	 *
	 * Active callback of after_setup_theme
	 * return void
	 */
	public function setup_review_notice() {

		// Set the installed time in `wisnia_theme_installed_time` option table.
		$option = get_option( 'wisnia_theme_installed_time' );

		if ( ! $option ) {
			update_option( 'wisnia_theme_installed_time', time() );
		}

		add_action( 'admin_notices', array( $this, 'display_review_notice' ), 0 );
		add_action( 'admin_init', array( $this, 'remove_theme_review_notice' ), 0 );
		add_action( 'admin_init', array( $this, 'remove_theme_review_notice_partially' ), 0 );
	}

	/**
	 * Display review notice
	 * Aso call other necessary functions
	 *
	 * Active callback of after_setup_theme
	 * return void
	 */
	public function display_review_notice() {

		global $current_user;
		$user_id                  = $current_user->ID;
		$ignored_notice           = get_user_meta( $user_id, 'remove_theme_review_notice', true );
		$ignored_notice_partially = get_user_meta( $user_id, 'nag_remove_theme_review_notice_partially', true );

		/**
		 * Return from notice display if:
		 *
		 * 1. The theme installed is less than 15 days ago.
		 * 2. If the user has ignored the message partially for 15 days.
		 * 3. Dismiss always if clicked on 'I Already Did' button.
		 */
		if ( ( get_option( 'wisnia_theme_installed_time' ) > strtotime( '-15 day' ) ) || ( $ignored_notice_partially > strtotime( '-15 day' ) ) || ( $ignored_notice ) ) {
			return;
		}
		?>
		<div class="notice updated wisnia-review-notice">
			<p>
				<?php
				printf(
				/* Translators: %1$s current user display name. */
					esc_html__(
						'Howdy, %1$s! It seems that you have been using this theme for more than 15 days. We hope you are happy with everything that the theme has to offer. If you can spare a minute, please help us by leaving a 5-star review on WordPress.org.  By spreading the love, we can continue to develop new amazing features in the future, for free!',
						'wisnia'
					),
					'<strong>' . esc_html( $current_user->display_name ) . '</strong>'
				);
				?>
			</p>

			<div class="links">
				<a href="https://wordpress.org/support/theme/wisnia/reviews/?filter=5#new-post" class="btn button-primary" target="_blank">
					<span class="dashicons dashicons-thumbs-up"></span>
					<span><?php esc_html_e( 'Sure', 'wisnia' ); ?></span>
				</a>

				<a href="?nag_remove_theme_review_notice_partially=0" class="btn wisnia-danger-btn">
					<span class="dashicons dashicons-calendar"></span>
					<span><?php esc_html_e( 'Maybe later', 'wisnia' ); ?></span>
				</a>

				<a href="?nag_remove_theme_review_notice=0" class="btn wisnia-success-btn">
					<span class="dashicons dashicons-smiley"></span>
					<span><?php esc_html_e( 'I already did', 'wisnia' ); ?></span>
				</a>

				<a href="<?php echo esc_url( 'https://wordpress.org/support/theme/wisnia/' ); ?>" class="btn wisnia-default-btn" target="_blank">
					<span class="dashicons dashicons-edit"></span>
					<span><?php esc_html_e( 'Got theme support question?', 'wisnia' ); ?></span>
				</a>
			</div>

			<a class="notice-dismiss" style="text-decoration:none;" href="?nag_remove_theme_review_notice_partially=0"></a>
		</div>
		<?php
	}

	/**
	 * Remove notice permanently
	 *
	 * Active callback of after_setup_theme
	 * return void
	 */
	public function remove_theme_review_notice() {

		global $current_user;
		$user_id = $current_user->ID;

		/* If user clicks to ignore the notice, add info to user meta */
		if ( isset( $_GET['nag_remove_theme_review_notice'] ) && '0' == $_GET['nag_remove_theme_review_notice'] ) {
			add_user_meta( $user_id, 'remove_theme_review_notice', 'true', true );
		}
	}

	/**
	 * Remove notice partially
	 *
	 * Active callback of after_setup_theme
	 * return void
	 */
	public function remove_theme_review_notice_partially() {

		global $current_user;
		$user_id = $current_user->ID;

		/* If user clicks to ignore the notice, add that to their user meta */
		if ( isset( $_GET['nag_remove_theme_review_notice_partially'] ) && '0' == $_GET['nag_remove_theme_review_notice_partially'] ) {
			update_user_meta( $user_id, 'nag_remove_theme_review_notice_partially', time() );
		}
	}

	/**
	 * Remove the data set after the theme has been switched to other theme.
	 *
	 * Active callback of after_setup_theme
	 * return void
	 */
	public function remove_review_notice() {

		global $current_user;
		$user_id                  = $current_user->ID;
		$theme_installed_time     = get_option( 'wisnia_theme_installed_time' );
		$ignored_notice           = get_user_meta( $user_id, 'remove_theme_review_notice', true );
		$ignored_notice_partially = get_user_meta( $user_id, 'nag_remove_theme_review_notice_partially', true );

		// Delete options data.
		if ( $theme_installed_time ) {
			delete_option( 'wisnia_theme_installed_time' );
		}

		// Delete permanent notice remove data.
		if ( $ignored_notice ) {
			delete_user_meta( $user_id, 'remove_theme_review_notice' );
		}

		// Delete partial notice remove data.
		if ( $ignored_notice_partially ) {
			delete_user_meta( $user_id, 'nag_remove_theme_review_notice_partially' );
		}
	}

	/**
	 * Enqueue the required CSS file for theme review notice on admin page.
	 */
	public function review_notice_enqueue() {
		wp_enqueue_style( 'wisnia-review-notice', WISNIA_URL . '/inc/admin/notice.css' );
	}
}

/**
 * Begins execution of the hooks.
 *
 * @since    1.0.0
 */
function wisnia_theme_notice() {
	return Wisnia_Theme_Notice::instance();
}
wisnia_theme_notice()->run();
