<?php
/**
 * Admin class.
 *
 * This class ties together all admin classes.
 *
 * @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_Admin')):

	/**
	 * Admin Class
	 */
	class Business_Interface_Admin
	{

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

			/**
			 * Include admin files.
			 */
			$this->includes();

			/**
			 * Load admin assets.
			 */
			add_action('admin_enqueue_scripts', array($this, 'load_assets'));

			/**
			 * Add filters for WordPress header and footer text.
			 */
			add_filter('admin_footer_text', array($this, 'filter_admin_footer_text'), 50);

			/**
			 * Admin page header.
			 */
			add_action('in_admin_header', array($this, 'admin_header'), 100);

			/**
			 * Admin page footer.
			 */
			add_action('in_admin_footer', array($this, 'admin_footer'), 100);

			/**
			 * Add notices.
			 */
			add_action('admin_notices', array($this, 'admin_notices'));

			/**
			 * After admin loaded
			 */
			do_action('business_interface_admin_loaded');
		}

		/**
		 * Includes files.
		 *
		 * @since 1.0.0
		 */
		private function includes()
		{

			/**
			 * Include helper functions.
			 */
			require_once BUSINESS_INTERFACE_PATH . '/inc/admin/helpers.php'; // phpcs:ignore

			/**
			 * Include Business Interface welcome page.
			 */
			require_once BUSINESS_INTERFACE_PATH . '/inc/admin/class-interface-dashboard.php'; // phpcs:ignore

			/**
			 * Include Business Interface meta boxes.
			 */
			require_once BUSINESS_INTERFACE_PATH . '/inc/admin/metabox/class-interface-meta-boxes.php'; // phpcs:ignore

			/**
			 * Include upgrade to pro notice.
			 */
			require_once BUSINESS_INTERFACE_PATH . '/inc/admin/class-interface-upgrade-notice.php'; // phpcs:ignore
		}

		/**
		 * Load our required assets on admin pages.
		 *
		 * @since 1.0.0
		 * @param string $hook it holds the information about the current page.
		 */
		public function load_assets($hook)
		{

			/**
			 * Do not enqueue if we are not on one of our pages or showing the plugins notice on the dashboard/themes pages.
			 */
			$is_notice_page = in_array($hook, array('index.php', 'themes.php'), true) && !business_interface_is_notice_dismissed('business_interface_notice_recommended-plugins');
			if (!$is_notice_page && !business_interface_is_admin_page($hook)) {
				return;
			}

			// Script debug.
			$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';

			/**
			 * Enqueue admin pages stylesheet.
			 */
			if (business_interface_is_admin_page($hook)) {
				wp_enqueue_style(
					'business-interface-admin-styles',
					BUSINESS_INTERFACE_URI . '/inc/admin/assets/css/admin' . $suffix . '.css',
					false,
					BUSINESS_INTERFACE_VERSION
				);
			}

			/**
			 * Enqueue admin pages script.
			 */
			wp_enqueue_script(
				'business-interface-admin-script',
				BUSINESS_INTERFACE_URI . '/inc/admin/assets/js/admin' . $suffix . '.js',
				array('jquery', 'wp-util', 'updates'),
				BUSINESS_INTERFACE_VERSION,
				true
			);

			/**
			 * Localize admin strings.
			 */
			$texts = array(
				'install' => esc_html__('Install', 'business-interface'),
				'install-inprogress' => esc_html__('Installing...', 'business-interface'),
				'activate-inprogress' => esc_html__('Activating...', 'business-interface'),
				'deactivate-inprogress' => esc_html__('Deactivating...', 'business-interface'),
				'active' => esc_html__('Active', 'business-interface'),
				'retry' => esc_html__('Retry', 'business-interface'),
				'please_wait' => esc_html__('Please Wait...', 'business-interface'),
				'importing' => esc_html__('Importing... Please Wait...', 'business-interface'),
				'currently_processing' => esc_html__('Currently processing: ', 'business-interface'),
				'import' => esc_html__('Import', 'business-interface'),
				'import_demo' => esc_html__('Import Demo', 'business-interface'),
				'importing_notice' => esc_html__('The demo importer is still working. Closing this window may result in failed import.', 'business-interface'),
				'import_complete' => esc_html__('Import Complete!', 'business-interface'),
				'import_complete_desc' => esc_html__('The demo has been imported.', 'business-interface') . ' <a href="' . esc_url(get_home_url()) . '">' . esc_html__('Visit site.', 'business-interface') . '</a>',
			);

			$strings = array(
				'ajaxurl' => esc_url(admin_url('admin-ajax.php')),
				'wpnonce' => wp_create_nonce('business_interface_nonce'),
				'texts' => $texts,
				'color_pallete' => array('#3857f1', '#06cca6', '#2c2e3a', '#e4e7ec', '#f0b849', '#ffffff', '#000000'),
			);

			$strings = apply_filters('business_interface_admin_strings', $strings);

			wp_localize_script('business-interface-admin-script', 'business_interface_strings', $strings);
		}

		/**
		 * Filter WordPress footer left text to display our text.
		 *
		 * @since 1.0.0
		 * @param string $text Text that we're going to replace.
		 */
		public function filter_admin_footer_text($text)
		{

			if (business_interface_is_admin_page()) {
				return;
			}

			return $text;
		}

		/**
		 * Outputs the page admin header.
		 *
		 * @since 1.0.0
		 */
		public function admin_header()
		{

			$base = get_current_screen()->base;

			if (!business_interface_is_admin_page($base)) {
				return;
			}
			?>

			<div id="business-interface-header">
				<div class="interface-container">

					<a href="<?php echo esc_url(admin_url('admin.php?page=business-interface-dashboard')); ?>"
						class="business-interface-logo">
						<img src="<?php echo esc_url(BUSINESS_INTERFACE_URI . '/assets/images/business-interface-logo.svg'); ?>"
							alt="<?php echo esc_html('Business Interface'); ?>" />
					</a>

					<div class="business-interface-header-action">
                        <a href="<?php echo esc_url('https://wpinterface.com/themes/business-interface/#choose-pricing-plan'); ?>" class="interface-button primary" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-unlock"></span><span class="interface-button-label"><?php esc_html_e('Unlock Pro Features', 'business-interface'); ?></span></a>
                        <a href="<?php echo esc_url(admin_url('customize.php')); ?>" class="interface-button secondary"><span class="dashicons dashicons-admin-customizer"></span><span class="interface-button-label hide-button-label"><?php esc_html_e('Customize', 'business-interface'); ?></span></a>
                        <a href="<?php echo esc_url('https://wpinterface.com/support/'); ?>" class="interface-button secondary" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-editor-help"></span><span class="interface-button-label hide-button-label"><?php esc_html_e('Help Center', 'business-interface'); ?></span></a>
					</div>

				</div>
			</div><!-- END #business-interface-header -->
			<?php
		}

		/**
		 * Outputs the page admin footer.
		 *
		 * @since 1.0.0
		 */
		public function admin_footer()
		{

			$base = get_current_screen()->base;

			if (!business_interface_is_admin_page($base) || business_interface_is_admin_page($base, 'business_interface_wizard')) {
				return;
			}
			?>
			<div id="business-interface-footer">
				<ul>
					<li>
                        <a href="<?php echo esc_url('https://docs.wpinterface.com/business-interface'); ?>" target="_blank" rel="noopener noreferrer">
                            <?php esc_html_e('Help Articles', 'business-interface'); ?>
                        </a>
					</li>
					<li>
                        <a href="<?php echo esc_url('https://wordpress.org/support/theme/business-interface/reviews/#new-post'); ?>" target="_blank" rel="noopener noreferrer">
                            <span class="dashicons dashicons-heart" aria-hidden="true"></span><?php esc_html_e('Leave a Review', 'business-interface'); ?>
                        </a>
					</li>
				</ul>
			</div><!-- END #business-interface-footer -->

			<?php
		}

		/**
		 * Admin Notices
		 *
		 * @since 1.0.0
		 */
		public function admin_notices()
		{

			$screen = get_current_screen();

			// Display on Dashboard, Themes and Business Interface admin pages.
			if (!in_array($screen->base, array('dashboard', 'themes'), true) && !business_interface_is_admin_page()) {
				return;
			}

			// Display if not dismissed and not on Business Interface plugins page.
			if (!business_interface_is_notice_dismissed('business_interface_notice_recommended-plugins') && !business_interface_is_admin_page(false, 'business-interface-plugins')) {

				$plugins = business_interface_plugin_utilities()->get_recommended_plugins();
				$plugins = business_interface_plugin_utilities()->get_deactivated_plugins($plugins);

				$plugin_list = '';

				if (is_array($plugins) && !empty($plugins)) {

					$plugin_list = '<div class="plugins">';
					foreach ($plugins as $slug => $plugin) {
						if (business_interface_plugin_utilities()->is_installed($slug)) {
							$action = 'activate';
							$btn_text = esc_html__('Activate', 'business-interface') . ' ' . esc_html($plugin['name']);
						} else {
							$action = 'install';
							$btn_text = esc_html__('Install Now', 'business-interface') . ' ' . esc_html($plugin['name']);
						}

						$plugin_list .= '<a href="#" class="interface-button primary button-primary" style="margin-left:5px;margin-right:5px;" data-plugin="' . esc_attr($slug) . '" data-action="' . esc_attr($action) . '">' . $btn_text . '</a>';
					}
					$plugin_list .= '</div>';

					wp_enqueue_script('plugin-install');

					/* translators: %1$s <strong> tag, %2$s </strong> tag */
					$message = esc_html__('Business Interface theme recommends the following plugins to get the most out of your website:', 'business-interface') . $plugin_list;

					business_interface_print_notice(
						array(
							'type' => 'info',
							'message' => $message,
							'message_id' => 'recommended-plugins',
							'expires' => 7 * 24 * 60 * 60,
						)
					);
				}
			}

		}
	}
endif;
