<?php
/**
 * Business Interface About page class.
 *
 * @package     Business Interface
 * @author      WPInterface Team
 * @since       1.0.0
 */

/**
 * Do not allow direct script access.
 */
if (!defined('ABSPATH')) {
	exit;
}

if (!class_exists('Business_Interface_Dashboard')):
	/**
	 * Business Interface Dashboard page class.
	 */
	final class Business_Interface_Dashboard
	{

		/**
		 * Singleton instance of the class.
		 *
		 * @since 1.0.0
		 * @var object
		 */
		private static $instance;

		/**
		 * Main Business Interface Dashboard Instance.
		 *
		 * @since 1.0.0
		 * @return Business_Interface_Dashboard
		 */
		public static function instance()
		{

			if (!isset(self::$instance) && !(self::$instance instanceof Business_Interface_Dashboard)) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Primary class constructor.
		 *
		 * @since 1.0.0
		 */
		public function __construct()
		{

			/**
			 * Register admin menu item under Appearance menu item.
			 */
			add_action('admin_menu', array($this, 'add_to_menu'), 10);
			add_filter('submenu_file', array($this, 'highlight_submenu'));

			/**
			 * Ajax activate & deactivate plugins.
			 */
			add_action('wp_ajax_business-interface-plugin-activate', array($this, 'activate_plugin'));
			add_action('wp_ajax_business-interface-plugin-deactivate', array($this, 'deactivate_plugin'));

			/**
			 * Set admin page title for hidden pages.
			 */
			add_action('admin_init', array($this, 'set_admin_page_title'));
		}

		/**
		 * Register our custom admin menu item.
		 *
		 * @since 1.0.0
		 */
		public function add_to_menu()
		{

			/**
			 * Dashboard page.
			 */
			add_theme_page(
				esc_html__('Business Interface', 'business-interface'),
				'Business Interface',
				apply_filters('business_interface_manage_cap', 'edit_theme_options'),
				'business-interface-dashboard',
				array($this, 'render_dashboard')
			);

			/**
			 * Plugins page.
			 */
			add_theme_page(
				esc_html__('Plugins', 'business-interface'),
				'Plugins',
				apply_filters('business_interface_manage_cap', 'edit_theme_options'),
				'business-interface-plugins',
				array($this, 'render_plugins')
			);

			// Hide from admin navigation.
			remove_submenu_page('themes.php', 'business-interface-plugins');

			/**
			 * Changelog page.
			 */
			add_theme_page(
				esc_html__('Changelog', 'business-interface'),
				'Changelog',
				apply_filters('business_interface_manage_cap', 'edit_theme_options'),
				'business-interface-changelog',
				array($this, 'render_changelog')
			);

			// Hide from admin navigation.
			remove_submenu_page('themes.php', 'business-interface-changelog');

			/**
			 * Starter Demos page.
			 */
			add_theme_page(
				esc_html__('Starter Demos', 'business-interface'),
				'Starter Demos',
				apply_filters('business_interface_manage_cap', 'edit_theme_options'),
				'business-interface-starter-demos',
				array($this, 'render_starter_demos')
			);

			// Hide from admin navigation.
			remove_submenu_page('themes.php', 'business-interface-starter-demos');
		}

		/**
		 * Set admin page title for hidden pages.
		 *
		 * @since 1.0.0
		 */
		public function set_admin_page_title()
		{
			global $title, $plugin_page;

			if (isset($plugin_page) && empty($title)) {
				switch ($plugin_page) {
					case 'business-interface-plugins':
						$title = esc_html__('Plugins', 'business-interface');
						break;
					case 'business-interface-starter-demos':
						$title = esc_html__('Starter Demos', 'business-interface');
						break;
					case 'business-interface-changelog':
						$title = esc_html__('Changelog', 'business-interface');
						break;
				}
			}
		}

		/**
		 * Render dashboard page.
		 *
		 * @since 1.0.0
		 */
		public function render_dashboard()
		{

			// Render dashboard navigation.
			$this->render_navigation();

			?>
			<div class="interface-container">

				<div class="business-interface-section-title">
					<h2 class="business-interface-section-title"><?php esc_html_e('Getting Started', 'business-interface'); ?></h2>
				</div><!-- END .business-interface-section-title -->

				<div class="business-interface-section business-interface-columns">

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i
									class="dashicons dashicons-admin-plugins"></i><?php esc_html_e('Install Plugins', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Explore recommended plugins. These free plugins provide additional features and customization options.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="<?php echo esc_url(menu_page_url('business-interface-plugins', false)); ?>"
									class="interface-button secondary"
									role="button"><?php esc_html_e('Install Plugins', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i
									class="dashicons dashicons-layout"></i><?php esc_html_e('Start with a Template', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Don&rsquo;t want to start from scratch? Import a pre-built demo website in 1-click and get a head start.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="<?php echo esc_url(menu_page_url('business-interface-starter-demos', false)); ?>"
									class="interface-button secondary"
									role="button"><?php esc_html_e('Starter Demos', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i
									class="dashicons dashicons-palmtree"></i><?php esc_html_e('Upload Your Logo', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Kick off branding your new site by uploading your logo. Simply upload your logo and customize as you need.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="<?php echo esc_url(admin_url('customize.php?autofocus[control]=custom_logo')); ?>"
									class="interface-button secondary" target="_blank"
									rel="noopener noreferrer"><?php esc_html_e('Upload Logo', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i
									class="dashicons dashicons-welcome-widgets-menus"></i><?php esc_html_e('Change Menus', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Customize menu links and choose what&rsquo;s displayed in available theme menu locations.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="<?php echo esc_url(admin_url('nav-menus.php')); ?>" class="interface-button secondary"
									target="_blank"
									rel="noopener noreferrer"><?php esc_html_e('Go to Menus', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i class="dashicons dashicons-art"></i><?php esc_html_e('Change Colors', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Replace the default theme colors and make your website color scheme match your brand design.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="<?php echo esc_url(admin_url('customize.php?autofocus[section]=business_interface_section_colors')); ?>"
									class="interface-button secondary" target="_blank"
									rel="noopener noreferrer"><?php esc_html_e('Change Colors', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>

					<div class="business-interface-column">
						<div class="business-interface-box">
							<h4><i
									class="dashicons dashicons-editor-help"></i><?php esc_html_e('Need Help?', 'business-interface'); ?>
							</h4>
							<p><?php esc_html_e('Head over to our site to learn more about the Business Interface theme, read help articles and get support.', 'business-interface'); ?>
							</p>

							<div class="business-interface-buttons">
								<a href="https://docs.wpinterface.com/business-interface" target="_blank" rel="noopener noreferrer"
									class="interface-button secondary"><?php esc_html_e('Help Articles', 'business-interface'); ?></a>
							</div><!-- END .business-interface-buttons -->
						</div>
					</div>
				</div><!-- END .business-interface-section -->

				<div class="business-interface-section large-section">
					<div class="business-interface-hero">
						<img src="<?php echo esc_url(BUSINESS_INTERFACE_URI . '/assets/images/upgrade.svg'); ?>"
							alt="<?php echo esc_html('Customize'); ?>" />
					</div>

					<h2><?php esc_html_e('Let‘s customize your website', 'business-interface'); ?></h2>
					<p><?php esc_html_e('There are many changes you can make to customize your website. Explore Business Interface customization options and make it unique.', 'business-interface'); ?>
					</p>

					<div class="business-interface-buttons">
						<a href="<?php echo esc_url(admin_url('customize.php')); ?>"
							class="interface-button primary large-button"><?php esc_html_e('Start Customizing', 'business-interface'); ?></a>
					</div><!-- END .business-interface-buttons -->

				</div><!-- END .business-interface-section -->

				<?php do_action('business_interface_about_content_after'); ?>

			</div><!-- END .interface-container -->

			<?php
		}

		/**
		 * Render the recommended plugins page.
		 *
		 * @since 1.0.0
		 */
		public function render_plugins()
		{

			// Render dashboard navigation.
			$this->render_navigation();

			$plugins = business_interface_plugin_utilities()->get_recommended_plugins();
			?>
			<div class="interface-container">

				<div class="business-interface-section-title">
					<h2 class="business-interface-section-title"><?php esc_html_e('Recommended Plugins', 'business-interface'); ?>
					</h2>
				</div><!-- END .business-interface-section-title -->

				<div class="business-interface-section business-interface-columns plugins">

					<?php if (is_array($plugins) && !empty($plugins)) { ?>
						<?php foreach ($plugins as $plugin) { ?>

							<?php
							// Check plugin status.
							if (business_interface_plugin_utilities()->is_activated($plugin['slug'])) {
								$btn_class = 'interface-button secondary disabled';
								$btn_text = esc_html__('Already Activated', 'business-interface');
								$action = '';
								$notice = '<span class="si-active-plugin"><span class="dashicons dashicons-yes"></span>' . esc_html__('Plugin activated', 'business-interface') . '</span>';
							} elseif (business_interface_plugin_utilities()->is_installed($plugin['slug'])) {
								$btn_class = 'interface-button primary';
								$btn_text = esc_html__('Activate', 'business-interface');
								$action = 'activate';
								$notice = '';
							} else {
								$btn_class = 'interface-button primary';
								$btn_text = esc_html__('Install & Activate', 'business-interface');
								$action = 'install';
								$notice = '';
							}
							?>

							<div class="business-interface-column column-6">
								<div class="business-interface-box">

									<div class="plugin-image">
										<img src="<?php echo esc_url($plugin['thumb']); ?>"
											alt="<?php echo esc_html($plugin['name']); ?>" />
									</div>

									<div class="plugin-info">
										<h4><?php echo esc_html($plugin['name']); ?></h4>
										<p><?php echo esc_html($plugin['desc']); ?></p>
										<div class="business-interface-buttons">
											<?php echo (wp_kses_post($notice)); ?>
											<a href="#" class="<?php echo esc_attr($btn_class); ?>"
												data-plugin="<?php echo esc_attr($plugin['slug']); ?>"
												data-action="<?php echo esc_attr($action); ?>"><?php echo esc_html($btn_text); ?></a>
										</div>
									</div>

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

				</div><!-- END .business-interface-section -->

				<?php do_action('business_interface_recommended_plugins_after'); ?>

			</div><!-- END .interface-container -->

			<?php
		}

		/**
		 * Render the starter demos page.
		 *
		 * @since 1.0.0
		 */
		public function render_starter_demos()
		{

			// Render dashboard navigation.
			$this->render_navigation();

			?>
			<div class="interface-container">
				<?php
				if (function_exists('smartocs_display_smart_import')) {
					smartocs_display_smart_import(array(
						'wrapper_class' => 'interface-smartocs',  // Custom CSS class for wrapper
						'show_header' => false,  // Hide plugin header
						'show_sidebar' => false,  // Hide theme card sidebar
						'show_smart_import_tabs' => false,  // Hide the tabs
						'show_intro_text' => false,  // Hide the intro text
					));
				} else {
					?>
					<h2 class="business-interface-section-title"><?php esc_html_e('Starter Demos', 'business-interface'); ?></h2>

					<div class="business-interface-box">
						<p><?php esc_html_e('Please install and activate the Smart One Click Setup plugin to import demo content.', 'business-interface'); ?>
						</p>
						<div class="business-interface-buttons">
							<a href="<?php echo esc_url(menu_page_url('business-interface-plugins', false)); ?>"
								class="interface-button primary"
								role="button"><?php esc_html_e('Install Plugin', 'business-interface'); ?></a>
						</div>
					</div>

					<?php
				}
				?>
			</div><!-- END .interface-container -->
			<?php
		}

		/**
		 * Render the changelog page.
		 *
		 * @since 1.0.0
		 */
		public function render_changelog()
		{

			// Render dashboard navigation.
			$this->render_navigation();

			$changelog = BUSINESS_INTERFACE_PATH . '/changelog.txt';

			if (!file_exists($changelog)) {
				$changelog = esc_html__('Changelog file not found.', 'business-interface');
			} elseif (!is_readable($changelog)) {
				$changelog = esc_html__('Changelog file not readable.', 'business-interface');
			} else {
				global $wp_filesystem;

				// Check if the the global filesystem isn't setup yet.
				if (is_null($wp_filesystem)) {
					WP_Filesystem();
				}

				$changelog = $wp_filesystem->get_contents($changelog);
			}

			?>
			<div class="interface-container">

				<div class="business-interface-section-title">
					<h2 class="business-interface-section-title">
						<span><?php esc_html_e('Business Interface Changelog', 'business-interface'); ?></span>
						<span class="changelog-version"><?php echo esc_html(sprintf('v%1$s', BUSINESS_INTERFACE_VERSION)); ?></span>
					</h2>

				</div><!-- END .business-interface-section-title -->

				<div class="business-interface-section business-interface-columns">

					<div class="business-interface-column column-12">
						<div class="business-interface-box business-interface-changelog">
							<pre><?php echo esc_html($changelog); ?></pre>
						</div>
					</div>
				</div><!-- END .business-interface-columns -->

				<?php do_action('business_interface_after_changelog'); ?>

			</div><!-- END .interface-container -->
			<?php
		}

		/**
		 * Render admin page navigation tabs.
		 *
		 * @since 1.0.0
		 */
		public function render_navigation()
		{

			// Get navigation items.
			$menu_items = $this->get_navigation_items();

			?>
			<div class="interface-container">

				<div class="business-interface-tabs">
					<ul>
						<?php
						// Determine current tab.
						$base = $this->get_current_page();

						// Display menu items.
						foreach ($menu_items as $item) {

							// Check if we're on a current item.
							$current = false !== strpos($base, $item['id']) ? 'current-item' : '';
							?>

							<li class="<?php echo esc_attr($current); ?>">
								<a href="<?php echo esc_url($item['url']); ?>">
									<?php echo esc_html($item['name']); ?>

									<?php
									if (isset($item['icon']) && $item['icon']) {
										business_interface_print_admin_icon($item['icon']);
									}
									?>
								</a>
							</li>

						<?php } ?>
					</ul>
				</div><!-- END .business-interface-tabs -->

			</div><!-- END .interface-container -->
			<?php
		}

		/**
		 * Return the current Business Interface Dashboard page.
		 *
		 * @since 1.0.0
		 * @return string $page Current dashboard page slug.
		 */
		public function get_current_page()
		{

			$page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : 'dashboard'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$page = str_replace('business-interface-', '', $page);
			$page = apply_filters('business_interface_dashboard_current_page', $page);

			return esc_html($page);
		}

		/**
		 * Print admin page navigation items.
		 *
		 * @since 1.0.0
		 * @return array $items Array of navigation items.
		 */
		public function get_navigation_items()
		{

			$items = array(
				'dashboard' => array(
					'id' => 'dashboard',
					'name' => esc_html__('About', 'business-interface'),
					'icon' => '',
					'url' => menu_page_url('business-interface-dashboard', false),
				),
				'plugins' => array(
					'id' => 'plugins',
					'name' => esc_html__('Recommended Plugins', 'business-interface'),
					'icon' => '',
					'url' => menu_page_url('business-interface-plugins', false),
				),
				'starter-demos' => array(
					'id' => 'starter-demos',
					'name' => esc_html__('Starter Demos', 'business-interface'),
					'icon' => '',
					'url' => menu_page_url('business-interface-starter-demos', false),
				),
				'changelog' => array(
					'id' => 'changelog',
					'name' => esc_html__('Changelog', 'business-interface'),
					'icon' => '',
					'url' => menu_page_url('business-interface-changelog', false),
				),
			);

			return apply_filters('business_interface_dashboard_navigation_items', $items);
		}

		/**
		 * Activate plugin.
		 *
		 * @since 1.0.0
		 */
		public function activate_plugin()
		{

			// Security check.
			check_ajax_referer('business_interface_nonce');

			// Plugin data.
			$plugin = isset($_POST['plugin']) ? sanitize_text_field(wp_unslash($_POST['plugin'])) : '';

			if (empty($plugin)) {
				wp_send_json_error(esc_html__('Missing plugin data', 'business-interface'));
			}

			if ($plugin) {

				$response = business_interface_plugin_utilities()->activate_plugin($plugin);

				if (is_wp_error($response)) {
					wp_send_json_error($response->get_error_message(), $response->get_error_code());
				}

				wp_send_json_success();
			}

			wp_send_json_error(esc_html__('Failed to activate plugin. Missing plugin data.', 'business-interface'));
		}

		/**
		 * Deactivate plugin.
		 *
		 * @since 1.0.0
		 */
		public function deactivate_plugin()
		{

			// Security check.
			check_ajax_referer('business_interface_nonce');

			// Plugin data.
			$plugin = isset($_POST['plugin']) ? sanitize_text_field(wp_unslash($_POST['plugin'])) : '';

			if (empty($plugin)) {
				wp_send_json_error(esc_html__('Missing plugin data', 'business-interface'));
			}

			if ($plugin) {
				$response = business_interface_plugin_utilities()->deactivate_plugin($plugin);

				if (is_wp_error($response)) {
					wp_send_json_error($response->get_error_message(), $response->get_error_code());
				}

				wp_send_json_success();
			}

			wp_send_json_error(esc_html__('Failed to deactivate plugin. Missing plugin data.', 'business-interface'));
		}

		/**
		 * Highlight dashboard page for plugins page.
		 *
		 * @since 1.0.0
		 * @param string $submenu_file The submenu file.
		 */
		public function highlight_submenu($submenu_file)
		{

			global $pagenow;

			// Check if we're on business-interface plugins or changelog page.
			if ('themes.php' === $pagenow) {
				if (isset($_GET['page'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					if ('business-interface-plugins' === $_GET['page'] || 'business-interface-changelog' === $_GET['page'] || 'business-interface-starter-demos' === $_GET['page']) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
						$submenu_file = 'business-interface-dashboard';
					}
				}
			}

			return $submenu_file;
		}
	}
endif;

/**
 * The function which returns the one Business_Interface_Dashboard instance.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $business_interface_dashboard = business_interface_dashboard(); ?>
 *
 * @since 1.0.0
 * @return object
 */
function business_interface_dashboard()
{
	return Business_Interface_Dashboard::instance();
}

business_interface_dashboard();
