<?php
/**
 * Adds Theme page
 *
 * @package Consultare
 */

function consultare_about_admin_style( $hook ) {
	if ( 'appearance_page_consultare-about' === $hook ) {
		wp_enqueue_style( 'consultare-theme-about', get_theme_file_uri( 'css/theme-about.css' ), null, '1.0' );
	}
}
add_action( 'admin_enqueue_scripts', 'consultare_about_admin_style' );

/**
 * Add theme page
 */
function consultare_menu() {
	add_theme_page( esc_html__( 'About Theme', 'consultare' ), esc_html__( 'About Theme', 'consultare' ), 'edit_theme_options', 'consultare-about', 'consultare_about_display' );
}
add_action( 'admin_menu', 'consultare_menu' );

/**
 * Display About page
 */
function consultare_about_display() {
	$theme = wp_get_theme();
	?>
	<div class="wrap about-wrap full-width-layout">
		<h1><?php echo esc_html( $theme ); ?></h1>
		<div class="about-theme">
			<div class="theme-description">
				<div class="about-text">
					<?php
					echo wpautop( esc_html( $theme->get( 'Description' ) ) );
				?></div>
				<p class="actions">
					<a href="https://fireflythemes.com/themes/consultare-pro" class="button button-secondary" target="_blank"><?php esc_html_e( 'Info', 'consultare' ); ?></a>

					<a href="https://fireflythemes.com/documentation/consultare/" class="button button-primary" target="_blank"><?php esc_html_e( 'Documentation', 'consultare' ); ?></a>

					<a href="https://demo.fireflythemes.com/consultare" class="button button-primary green" target="_blank"><?php esc_html_e( 'Demo', 'consultare' ); ?></a>

					<a href="https://fireflythemes.com/support" class="button button-secondary" target="_blank"><?php esc_html_e( 'Support', 'consultare' ); ?></a>
				</p>
			</div>

			<div class="theme-screenshot">
				<img src="<?php echo esc_url( $theme->get_screenshot() ); ?>" />
			</div>

		</div>

		<nav class="nav-tab-wrapper wp-clearfix" aria-label="<?php esc_attr_e( 'Secondary menu', 'consultare' ); ?>">
			<a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'consultare-about' ), 'themes.php' ) ) ); ?>" class="nav-tab<?php echo ( isset( $_GET['page'] ) && 'consultare-about' === $_GET['page'] && ! isset( $_GET['tab'] ) ) ?' nav-tab-active' : ''; ?>"><?php esc_html_e( 'About', 'consultare' ); ?></a>

			<a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'consultare-about', 'tab' => 'changelog' ), 'themes.php' ) ) ); ?>" class="nav-tab<?php echo ( isset( $_GET['tab'] ) && 'changelog' === $_GET['tab'] ) ?' nav-tab-active' : ''; ?>"><?php esc_html_e( 'Changelog', 'consultare' ); ?></a>
		</nav>

		<?php
			consultare_main_screen();

			consultare_changelog_screen();
		?>

		<div class="return-to-dashboard">
			<?php if ( current_user_can( 'update_core' ) && isset( $_GET['updated'] ) ) : ?>
				<a href="<?php echo esc_url( self_admin_url( 'update-core.php' ) ); ?>">
					<?php is_multisite() ? esc_html_e( 'Return to Updates', 'consultare' ) : esc_html_e( 'Return to Dashboard &rarr; Updates', 'consultare' ); ?>
				</a> |
			<?php endif; ?>
			<a href="<?php echo esc_url( self_admin_url() ); ?>"><?php is_blog_admin() ? esc_html_e( 'Go to Dashboard &rarr; Home', 'consultare' ) : esc_html_e( 'Go to Dashboard', 'consultare' ); ?></a>
		</div>
	</div>
	<?php
}

/**
 * Output the main about screen.
 */
function consultare_main_screen() {
	if ( isset( $_GET['page'] ) && 'consultare-about' === $_GET['page'] && ! isset( $_GET['tab'] ) ) {
	?>
		<div class="feature-section two-col">
			<div class="col card">
				<h2 class="title"><?php esc_html_e( 'Theme Customizer', 'consultare' ); ?></h2>
				<p><?php esc_html_e( 'All Theme Options are available via Customize screen.', 'consultare' ) ?></p>
				<p><a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Customize', 'consultare' ); ?></a></p>
			</div>

			<div class="col card">
				<h2 class="title"><?php esc_html_e( 'Got theme support question?', 'consultare' ); ?></h2>
				<p><?php esc_html_e( 'Get genuine support from genuine people. Whether it\'s customization or compatibility, our seasoned developers deliver tailored solutions to your queries.', 'consultare' ) ?></p>
				<p><a href="https://fireflythemes.com/support" class="button button-primary"><?php esc_html_e( 'Support Forum', 'consultare' ); ?></a></p>
			</div>
		</div><!-- .changelog-wrapper -->
	<?php
	}
}

/**
 * Output the changelog screen.
 */
function consultare_changelog_screen() {
	if ( isset( $_GET['tab'] ) && 'changelog' === $_GET['tab'] ) {
		global $wp_filesystem;
	?>
		<div class="changelog-wrapper">
			<?php
				$changelog_file = apply_filters( 'consultare_changelog_file', get_template_directory() . '/readme.txt' );

				// Check if the changelog file exists and is readable.
				if ( $changelog_file && is_readable( $changelog_file ) ) {
					WP_Filesystem();
					$changelog = $wp_filesystem->get_contents( $changelog_file );
					$changelog_list = consultare_parse_changelog( $changelog );

					echo wp_kses_post( $changelog_list );
				}
			?>
		</div><!-- .changelog-wrapper -->
	<?php
	}
}

/**
 * Parse changelog from readme file.
 * @param  string $content
 * @return string
 */
function consultare_parse_changelog( $content ) {
	// Explode content with ==  to juse separate main content to array of headings.
	$content = explode ( '== ', $content );

	$changelog_isolated = '';

	// Get element with 'Changelog ==' as starting string, i.e isolate changelog.
	foreach ( $content as $key => $value ) {
		if (strpos( $value, 'Changelog ==') === 0) {
			$changelog_isolated = str_replace( 'Changelog ==', '', $value );
		}
	}

	// Now Explode $changelog_isolated to manupulate it to add html elements.
	$changelog_array = explode( '= ', $changelog_isolated );

	// Unset first element as it is empty.
	unset( $changelog_array[0] );

	$changelog = '<pre class="changelog">';
		
	foreach ( $changelog_array as $value) {
		// Replace all enter (\n) elements with </span><span> , opening and closing span will be added in next process.
		$value = preg_replace( '/\n+/', '</span><span>', $value );

		// Add openinf and closing div and span, only first span element will have heading class.
		$value = '<div class="block"><span class="heading">= ' . $value . '</span></div>';

		// Remove empty <span></span> element which newr formed at the end.
		$changelog .= str_replace( '<span></span>', '', $value );
	}

	$changelog .= '</pre>';

	return wp_kses_post( $changelog );
}
