<?php
/**
 * Welcome Notice
 */

/**
 * Whether to load notice
 */
function hootubix_load_notices() {
	global $pagenow;
	$subpage = isset( $_GET['page'] ) ? $_GET['page'] : 'subpage';
	$slug = hootubix_abouttag( 'slug' );
	return (
		$slug && is_string( $slug )
		&& ! get_option( "{$slug}-dismiss-welcome" )
		&& (
			in_array( $pagenow, array( 'themes.php', 'plugins.php', 'upload.php' ) )
			|| ( $pagenow === 'edit.php' && ( empty( $_GET['post_type'] ) || $_GET['post_type'] === 'page' ) )
		)
		&& ! in_array( $subpage, array( "{$slug}-welcome", 'hootkit', 'hoot-import' ) )
	);
}

/**
 * Skip notice for already installed themes
 */
function hootubix_theme_update_action( $upgrader_object, $options ) {
	if ( $options['type'] === 'theme' && $options['action'] === 'update' ) {
		$updated_themes = $options['themes'];
		$current_theme = get_option( 'stylesheet' ); // Get the active theme's slug (directory name)
		if ( in_array( $current_theme, $updated_themes ) && function_exists( 'get_theme_mod' ) && get_theme_mod('accent_color') ) {
			$slug = hootubix_abouttag( 'slug' );
			update_option( "{$slug}-dismiss-welcome", 1 );
		}
	}
}
add_action( 'upgrader_process_complete', 'hootubix_theme_update_action', 10, 2 );

/**
 * Add a welcome notice if not already dismissed
 */
function hootubix_welcome_notice() {
	$slug = hootubix_abouttag( 'slug' );
	if ( hootubix_load_notices() ) {
		add_action( 'admin_notices', 'hootubix_add_welcome_notice' );
	}
	if ( !empty( $_GET['hootubix-noticereset'] ) ) {
		update_option( "{$slug}-dismiss-welcome", 0 );
		update_option( "{$slug}-dismiss-import", 0 );
	}
}
add_action( 'admin_head', 'hootubix_welcome_notice' );

/**
 * Enqueue CSS
 *
 * @since 1.0
 * @access public
 * @return void
 */
function hootubix_admin_enqueue_notice_styles( $hook ) {
	$slug = hootubix_abouttag( 'slug' );
	if ( hootubix_load_notices() || $hook === "appearance_page_{$slug}-welcome" ) {
		wp_enqueue_style( 'hootubix-admin-notice', HYBRIDEXTEND_INCURI . 'admin/css/notice.css', array(), HYBRIDEXTEND_VERSION );
		wp_enqueue_script( 'hootubix-admin-notice', HYBRIDEXTEND_INCURI . 'admin/js/notice.js', array( 'jquery' ), HYBRIDEXTEND_VERSION, true );
		wp_localize_script( 'hootubix-admin-notice', 'hootubix_admin_notice', array(
							'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
							'nonce' => wp_create_nonce( 'hoottheme-welcome-action' ),
							'dismiss_action' => 'hootubix_dismiss_welcome_notice',
							'hootubix_processplugin_action' => 'hootubix_processplugin',
							'hootubix_processplugin_btntext' => esc_html__( 'Please Wait. This may take a while...', 'hoot-ubix' ),
						) );
	}
}
add_action( 'admin_enqueue_scripts', 'hootubix_admin_enqueue_notice_styles' );

/**
 * Display admin notice
 */
function hootubix_add_welcome_notice() {
	$slug = hootubix_abouttag( 'slug' );
	$themename = hootubix_abouttag( 'name' );
	$imgshot = hootubix_abouttag( 'fullshot' );
	$issmall = false;
	if ( ! $imgshot ) {
		$imgshot = hootubix_abouttag( 'shot' );
		$issmall = true;
	}
	$import_config = apply_filters( 'hootimport_theme_config', array() ); // Hoot Import has been configured for active theme
	$display_import = ! empty( $import_config ) && ! get_option( "{$slug}-dismiss-import" );
	?>
	<div id="hootubix-welcome-msg" class="hootubix-welcome-msg notice notice-success is-dismissible">
		<div class="hootubix-welcome-content">
			<?php if ( $imgshot ) : ?>
				<a class="hootubix-welcome-img <?php if ( $display_import ) { echo 'hootubix-welcome-img--large'; } if ( $issmall ) { echo ' hootubix-welcome-img--ss'; } ?>" href="<?php echo esc_url( "https://demo.wphoot.com/hoot-ubix" ); ?>" target="_blank">
					<img class="hootubix-welcome-screenshot <?php if ( ! $issmall ) { echo 'hootubix-welcome-scrollImg'; } ?>" src="<?php echo esc_url( $imgshot ); ?>" alt="<?php echo esc_attr( $themename ); ?>" />
				</a>
			<?php endif; ?>
			<div class="hootubix-welcome-text">
				<h1><?php
					/* Translators: 1 is the theme name */
					printf( esc_html__( 'Thank you for choosing %1$s!', 'hoot-ubix' ), $themename );
				?></h1>
				<p><?php
					/* Translators: 1 is the link start markup, 2 is link markup end */
					printf( esc_html__( 'To get started and fully take advantage of our theme, please make sure you visit the welcome page for the %1$sQuick Start Guide%2$s.', 'hoot-ubix' ), '<a href="' . esc_url( admin_url( "themes.php?page={$slug}-welcome&tab=qstart" ) ) . '">', '</a>' );
				?></p>
				<?php if ( $display_import ) : ?>
					<p><?php _e( 'Or you can import the demo data by clicking the button below to help you get familiar with the theme.', 'hoot-ubix' ); ?></p>
					<p><a class="button button-primary hootubix-welcome-btn hootubix-btn-processplugin" href="#"><?php esc_html_e( 'Import Demo Content', 'hoot-ubix' ); ?></a></p>
					<?php if ( ! class_exists( 'HootImport' ) ) : ?><p class="hootubix-welcome-note"><?php _e( 'Clicking the button will install and activate the "Hoot Import" plugin.', 'hoot-ubix' ); ?></p><?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<?php
}

/**
 * Ajax callback to set dismissable notice
 */
function hootubix_dismiss_welcome_notice() {
	check_ajax_referer( 'hoottheme-welcome-action', 'nonce' );
	$slug = hootubix_abouttag( 'slug' );
	update_option( "{$slug}-dismiss-welcome", 1 );
	wp_die();
}
add_action( 'wp_ajax_hootubix_dismiss_welcome_notice', 'hootubix_dismiss_welcome_notice' );

/**
 * Ajax callback to import and activate Hoot Import plugin
 */
function hootubix_processplugin() {
	check_ajax_referer( 'hoottheme-welcome-action', 'nonce' );
	$slug = hootubix_abouttag( 'slug' );
	$state = '';
	$response = array();
	$plugin = isset( $_POST['plugin'] ) && in_array( $_POST['plugin'], array( 'hoot-import', 'hootkit' ) ) ? $_POST['plugin'] : '';
	if (
		( $plugin === 'hoot-import' && class_exists( 'HootImport' ) ) ||
		( $plugin === 'hootkit' && class_exists( 'HootKit' ) )
	 ) {
		$state = 'activated';
	} elseif ( file_exists( WP_PLUGIN_DIR . "/{$plugin}/{$plugin}.php" ) ) {
		$state = 'installed';
	} else {
		/** Install plugin. **/
		wp_enqueue_style( 'plugin-install' );
		wp_enqueue_script( 'plugin-install' );
		include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
		include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
		include_once( ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php' );
		include_once( ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' );
		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => sanitize_key( wp_unslash( $plugin ) ),
				'fields' => array(
					'short_description' => false,
					'sections' => false,
					'requires' => false,
					'rating' => false,
					'ratings' => false,
					'downloaded' => false,
					'last_updated' => false,
					'added' => false,
					'tags' => false,
					'compatibility' => false,
					'homepage' => false,
					'donate_link' => false,
				),
			)
		);
		if ( is_wp_error( $api ) ) {
			$response['errorInstall'] = $api->get_error_message();
		} else {
			$skin     = new WP_Ajax_Upgrader_Skin();
			$upgrader = new Plugin_Upgrader( $skin );
			$result   = $upgrader->install( $api->download_link );
			if ( $result ) {
				$state = 'installed';
			} else {
				$pluginname = ucwords( str_replace( '-', ' ', $plugin ) );
				/* Translators: 1 is line break, 2 is the link start markup, 3 is link markup end, 4 is the plugin name, 5 and 6 are strong tags */
				$errormsg = sprintf( esc_html__( 'WordPress encountered an unexpected error during the plugin installation.%1$sPlease %2$sclick this link%3$s to install the %5$s%4$s plugin%6$s directly.', 'hoot-ubix' ), '<br />', '<a href="' . esc_url( admin_url( "plugin-install.php?s={$plugin}&tab=search&type=term" ) ) . '">', '</a>', $pluginname, '<strong>', '</strong>' );
				$response['errorInstall'] = $errormsg;
			}
		}
	}

	if ( 'installed' === $state ) {
		if ( current_user_can( 'activate_plugin' ) ) {
			$result = activate_plugin( "{$plugin}/{$plugin}.php" );
			if ( ! is_wp_error( $result ) ) {
				$state = 'activated';
			} else {
				$response['errorCode']    = $result->get_error_code();
				$response['errorMessage'] = $result->get_error_message();
			}
		}
	}
	if ( 'activated' === $state ) {
		if ( $plugin === 'hoot-import' ) {
			$response['redirect'] = admin_url( 'themes.php?page=hoot-import' );
			update_option( "{$slug}-dismiss-import", 1 );
		} elseif ( $plugin === 'hootkit' ) {
			$response['redirect'] = admin_url( 'options-general.php?page=hootkit' );
		}
	}

	wp_send_json( $response );
	exit();
}
add_action( 'wp_ajax_hootubix_processplugin', 'hootubix_processplugin' );