<?php

/**
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
} // Exit if accessed directly

if( !class_exists( 'jass_welcome' ) ) {

	class jass_welcome {

		public function __construct( $fields = array() ) {
	
			$this->theme_fields = $fields;

			add_action ('admin_init' , array( &$this, 'admin_scripts' ) );
			add_action('admin_menu',array( &$this, 'welcome_page_menu'));

		}

		public function admin_scripts() {
	
			global $pagenow;

			$file_dir = get_template_directory_uri() . '/core/assets/';

			if ( $pagenow == 'themes.php' && isset($_GET['page']) && $_GET['page'] == 'jass-welcome-page') {
				
				wp_enqueue_style (
					'jass-welcome-page-style',
					$file_dir . 'css/welcome-page.css',
					array(), '1.0.0'
				);
				 
			}

		}
		
        public function welcome_page_menu() {
            
			add_theme_page(
				esc_html__('About Jass', 'jass'),
				esc_html__('About Jass', 'jass'), 
				'edit_theme_options', 
				'jass-welcome-page', 
				array( &$this, 'welcome_page' )
			);
		
		}
		
        public function check_installed_plugin($slug, $filename) {
			return file_exists( ABSPATH . 'wp-content/plugins/' . $slug . '/' . $filename . '.php' ) ? true : false;
		}

		private function call_plugin_api( $slug ) {
			
			include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
	
			$call_api = get_transient( 'jass_plugin_info_' . $slug );
	
			if ( false === $call_api ) {
				$call_api = plugins_api(
					'plugin_information',
					array(
						'slug'   => $slug,
						'fields' => array(
							'downloaded'        => false,
							'rating'            => false,
							'description'       => false,
							'short_description' => true,
							'donate_link'       => false,
							'tags'              => false,
							'sections'          => true,
							'homepage'          => true,
							'added'             => false,
							'last_updated'      => false,
							'compatibility'     => false,
							'tested'            => false,
							'requires'          => false,
							'downloadlink'      => false,
							'icons'             => true,
							'banners'           => true,
						),
					)
				);
				set_transient( 'jass_plugin_info_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS );
			}
	
			return $call_api;
		}

        public function welcome_page() {

            $tabs = array(
                'getting_started' => esc_html__('Getting Started', 'jass'),
                'recommended_plugins' => esc_html__('Recommended Plugins', 'jass'),
                'support' => esc_html__('Support', 'jass'),
            );
			
        ?>
            
            <div class="wrap about-wrap access-wrap">
                <div class="abt-promo-wrap clearfix">
                    <div class="abt-theme-wrap">
                        
                        <h1><?php printf(esc_html__('Welcome to %1$s - Version %2$s', 'jass'), 'Jass', '1.0.2'); ?></h1>
                        
                        <div class="about-text">
							<?php echo esc_html__('Jass is a clean and responsive WordPress theme with an optional carousel of sticky posts.', 'jass'); ?>
                        </div>
                    
                    </div>
                
                </div>

                <div class="nav-tab-wrapper clearfix">
                    
					<?php 

						$tabHTML = '';

						foreach ($tabs as $id => $label) :

							$target = '';
							$nav_class = 'nav-tab';
							$section = isset($_GET['section']) ? $_GET['section'] : 'getting_started';
							
							if ($id == $section) {
								$nav_class .= ' nav-tab-active';
							}

							switch ($id) {
								
								case 'support':
									$target = 'target="_blank"';
									$url = esc_url('https://wordpress.org/support/theme/jass/');
								break;

								case 'getting_started':
									$url = esc_url(admin_url('themes.php?page=jass-welcome-page'));
								break;

								default:
									$url = esc_url(admin_url('themes.php?page=jass-welcome-page&section=' . $id));
								break;

							}

							$tabHTML .= '<a ';
							$tabHTML .= $target;
							$tabHTML .= ' href="' . $url. '"';
							$tabHTML .= ' class="' . esc_attr($nav_class). '"';
							$tabHTML .= '>';
							$tabHTML .= esc_html($label);
							$tabHTML .= '</a>';
					
						endforeach;
						
						echo $tabHTML;
						
					?>
                    
                </div>

                <div class="welcome-section-wrapper">
                    
                    <div class="welcome-section getting_started clearfix">

                    	<?php
						
							$section = isset($_GET['section']) ? $_GET['section'] : 'getting_started';
							
							switch ($section) {
								
								case 'recommended_plugins':
									$this->recommended_plugins();
								break;

								case 'getting_started':
								default:
									$this->getting_started();
								break;

							}
						
						?>

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

        public function getting_started() {

		?>
    
			<div class="getting-started-top-wrap clearfix">
                        
				<div class="theme-steps-list">

					<div class="theme-steps">
                                
						<h3><?php echo esc_html__('Step 1 - Ensure Your Page Home Page is set Your latest posts', 'jass'); ?></h3>
						<ol>
							<li><?php echo esc_html__('Go to Settings > Reading > General settings > Your homepage displays', 'jass'); ?></li>
							<li><?php echo esc_html__('Set "Your homepage displays" to Your latest posts', 'jass'); ?></li>
							<li><?php echo esc_html__('Save changes', 'jass'); ?></li>
						</ol>
						<a class="button button-primary" target="_blank" href="<?php echo esc_url(admin_url('options-reading.php')); ?>"><?php echo esc_html__('Assign Static Page', 'jass'); ?></a>
					</div>
                        
					<div class="theme-steps">
						<h3><?php echo esc_html__('Step 2 - Customizer Options Panel', 'jass'); ?></h3>
						<p><?php echo esc_html__('Now go to Customizer Page. Using the WordPress Customizer you can easily set up the home page and customize the theme.', 'jass'); ?></p>
						<a class="button button-primary" href="<?php echo esc_url(admin_url('customize.php')); ?>"><?php echo esc_html__('Go to Customizer Panels', 'jass'); ?></a>
					</div>
                        
				</div>
                            
			</div>

        <?php
		
		}
		
		public function recommended_plugins() {

			$plugins = array(
	
				array(
					'filename'	=> 'init',
					'slug'      => 'internal-linking-of-related-contents',
				),
				array(
					'filename'	=> 'init',
					'slug'      => 'content-snippet-manager',
				),
				array(
					'filename'	=> 'init',
					'slug'      => 'wa-chatbox-manager',
				),
	
			);
			
		?>

			<div class="required-plugin-top-wrap clearfix">
                        
				<div class="required-plugin-list">

				<?php
                    
                    foreach ( $plugins as $plugin ) {
                        
                        $slug = $plugin['slug'];
                        $filename = $plugin['filename'];
        
                        $plugin_info = $this->call_plugin_api( $slug );
                        $plugin_desc = $plugin_info->short_description;
                        $plugin_img  = ( !isset($plugin_info->icons['1x']) ) ? $plugin_info->icons['default'] : $plugin_info->icons['1x'];
                        $plugin_banner  = $plugin_info->banners['low'];

                ?>

					<div class="required-plugin">
                    
                        <div class="required-plugin-head">
                        
							<img class="plugin-banner" src="<?php echo $plugin_banner;?>">
                            
                        </div>
                        
                        <div class="required-plugin-desc">
                            
                            <h3><?php echo $plugin_info->name; ?></h3>
							<?php echo $plugin_info->short_description; ?>
                            
						</div>
                           
                        <div class="required-plugin-footer">

							<span>
								
								<?php 
									echo esc_html__('v', 'jass');
									echo $plugin_info->version;
									echo esc_html__(' by ', 'jass');
									echo html_entity_decode( wp_strip_all_tags( $plugin_info->author ) );
								?>
                                
							</span>

							<?php if ( $this->check_installed_plugin( $slug, $filename ) ) : ?>
                                    
                            	<button type="button" class="button button-disabled" disabled="disabled">
                            		<?php esc_html_e( 'Installed', 'jass' ); ?>
                            	</button>
                                
                            <?php else : ?>
                    
                            	<a class="install-now button-primary" href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin='. $slug ), 'install-plugin_'. $slug ) ); ?>" >
                            		<?php esc_html_e( 'Install Now', 'jass' ); ?>
                            	</a>							
                                
                            <?php endif; ?>
                            
						</div>
                           
					</div>

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

}

new jass_welcome();

?>