<?php

/**
 * The Acme Themes theme hooks callback functionality of the plugin.
 *
 * @link       https://cosmoswp.com/
 * @since      1.0.0
 *
 * @package    Acme Themes
 * @subpackage Acme_Demo_Setup
 */

/**
 * The Acme Themes theme hooks callback functionality of the plugin.
 *
 * Since Acme Themes theme is hooks base theme, this file is main callback to add/remove/edit the functionality of the CosmosWP Theme
 *
 * @package    Acme Themes
 * @subpackage Acme_Demo_Setup
 * @author     Acme Themes <info@cosmoswp.com>
 */
class Acme_Demo_Setup_Hooks {

	/**
	 * current added Menu hook_suffix
	 *
	 * @since    1.0.0
	 * @access   public
	 * @var      array    $logs    Store logs and errors.
	 */
	private $hook_suffix;

	/**
	 * theme author name
	 *
	 * @since    1.0.0
	 * @access   public
	 * @var      array    $logs    Store logs and errors.
	 */
	private $theme_author = 'acmethemes';

	/**
	 * Main Instance
	 *
	 * Insures that only one instance of CosmosWP Pro exists in memory at any one
	 * time. Also prevents needing to define globals all over the place.
	 *
	 * @since    1.0.0
	 * @access   public
	 *
	 * @return object
	 */
	public static function instance() {

		// Store the instance locally to avoid private static replication
		static $instance = null;

		// Only run these methods if they haven't been ran previously
		if ( null === $instance ) {
			$instance = new self();
		}

		// Always return the instance
		return $instance;
	}

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {}

	/**
	 * Redirect to plugin page when plugin activated
	 *
	 * @since 1.0.0
	 */
	public static function redirect() {
		if ( get_option( '__acme_demo_setup_do_redirect' ) ) {
			update_option( '__acme_demo_setup_do_redirect', false );
			if ( ! is_multisite() ) {
				exit( wp_redirect( admin_url( 'themes.php?page=advanced-import' ) ) );
			}
		}
	}
	/**
	 * Add admin menus
	 *
	 * @access public
	 */
	public function import_menu() {
		if ( ! class_exists( 'Advanced_Import' ) ) {
			$this->hook_suffix[] = add_theme_page( esc_html__( 'Demo Import ', 'acme-demo-setup' ), esc_html__( 'Demo Import' ), 'manage_options', 'advanced-import', array( $this, 'demo_import_screen' ) );
		}
	}
	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles( $hook_suffix ) {
		if ( ! is_array( $this->hook_suffix ) || ! in_array( $hook_suffix, $this->hook_suffix ) ) {
			return;
		}
		wp_enqueue_style( ACME_DEMO_SETUP_PLUGIN_NAME, ACME_DEMO_SETUP_URL . 'assets/acme-demo-setup.css', array( 'wp-admin', 'dashicons' ), ACME_DEMO_SETUP_VERSION, 'all' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts( $hook_suffix ) {
		if ( ! is_array( $this->hook_suffix ) || ! in_array( $hook_suffix, $this->hook_suffix ) ) {
			return;
		}

		wp_enqueue_script( ACME_DEMO_SETUP_PLUGIN_NAME, ACME_DEMO_SETUP_URL . 'assets/acme-demo-setup.js', array( 'jquery' ), ACME_DEMO_SETUP_VERSION, true );
		wp_localize_script(
			ACME_DEMO_SETUP_PLUGIN_NAME,
			'acme_demo_setup',
			array(
				'btn_text' => esc_html__( 'Processing...', 'acme-demo-setup' ),
				'nonce'    => wp_create_nonce( 'acme_demo_setup_nonce' ),
			)
		);
	}

	/**
	 * Show the plugin recommended screen
	 *
	 * @access public
	 * @return void
	 */
	public function demo_import_screen() {
		?>
		<div id="ads-notice">
			<div class="ads-container">
				<img class="ads-screenshot" src="<?php echo esc_url( acme_demo_setup_get_theme_screenshot() ); ?>" alt="<?php esc_html( acme_demo_setup_get_theme_name() ); ?>" />
				<div class="ads-notice">
					<h2>
						<?php
						printf(
						/* translators: 1: welcome page link starting html tag, 2: welcome page link ending html tag. */
							esc_html__( 'Welcome! Thank you for choosing %1$s! To get started with ready-made starter site templates. Install the Advanced Import plugin and install Demo Starter Site within a single click', 'acme-demo-setup' ),
							'<strong>' . wp_get_theme()->get( 'Name' ) . '</strong>'
						);
						?>
					</h2>

					<p class="plugin-install-notice"><?php esc_html_e( 'Clicking the button below will install and activate the Advanced Import plugin.', 'acme-demo-setup' ); ?></p>

					<a class="ads-gsm-btn button button-primary button-hero" href="#" data-name="" data-slug="" aria-label="<?php esc_html_e( 'Get started with the Theme', 'acme-demo-setup' ); ?>">
						<?php esc_html_e( 'Get Started', 'acme-demo-setup' ); ?>
					</a>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Get Started Notice
	 * Active callback of wp_ajax
	 * return void
	 */
	public function install_advanced_import() {

		check_ajax_referer( 'acme_demo_setup_nonce', 'security' );

		$slug   = 'advanced-import';
		$plugin = 'advanced-import/advanced-import.php';

		$status             = array(
			'install' => 'plugin',
			'slug'    => sanitize_key( wp_unslash( $slug ) ),
		);
		$status['redirect'] = admin_url( '/themes.php?page=advanced-import&browse=all&at-gsm-hide-notice=welcome' );

		if ( is_plugin_active_for_network( $plugin ) || is_plugin_active( $plugin ) ) {
			// Plugin is activated
			wp_send_json_success( $status );
		}

		if ( ! current_user_can( 'install_plugins' ) ) {
			$status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.', 'acme-demo-setup' );
			wp_send_json_error( $status );
		}

		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
		include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

		// Looks like a plugin is installed, but not active.
		if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
			$plugin_data          = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
			$status['plugin']     = $plugin;
			$status['pluginName'] = $plugin_data['Name'];

			if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) {
				$result = activate_plugin( $plugin );

				if ( is_wp_error( $result ) ) {
					$status['errorCode']    = $result->get_error_code();
					$status['errorMessage'] = $result->get_error_message();
					wp_send_json_error( $status );
				}

				wp_send_json_success( $status );
			}
		}

		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => sanitize_key( wp_unslash( $slug ) ),
				'fields' => array(
					'sections' => false,
				),
			)
		);

		if ( is_wp_error( $api ) ) {
			$status['errorMessage'] = $api->get_error_message();
			wp_send_json_error( $status );
		}

		$status['pluginName'] = $api->name;

		$skin     = new WP_Ajax_Upgrader_Skin();
		$upgrader = new Plugin_Upgrader( $skin );
		$result   = $upgrader->install( $api->download_link );

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			$status['debug'] = $skin->get_upgrade_messages();
		}

		if ( is_wp_error( $result ) ) {
			$status['errorCode']    = $result->get_error_code();
			$status['errorMessage'] = $result->get_error_message();
			wp_send_json_error( $status );
		} elseif ( is_wp_error( $skin->result ) ) {
			$status['errorCode']    = $skin->result->get_error_code();
			$status['errorMessage'] = $skin->result->get_error_message();
			wp_send_json_error( $status );
		} elseif ( $skin->get_errors()->get_error_code() ) {
			$status['errorMessage'] = $skin->get_error_messages();
			wp_send_json_error( $status );
		} elseif ( is_null( $result ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			WP_Filesystem();
			global $wp_filesystem;

			$status['errorCode']    = 'unable_to_connect_to_filesystem';
			$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'acme-demo-setup' );

			// Pass through the error from WP_Filesystem if one was raised.
			if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
				$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
			}

			wp_send_json_error( $status );
		}

		$install_status = install_plugin_install_status( $api );

		if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
			$result = activate_plugin( $install_status['file'] );

			if ( is_wp_error( $result ) ) {
				$status['errorCode']    = $result->get_error_code();
				$status['errorMessage'] = $result->get_error_message();
				wp_send_json_error( $status );
			}
		}

		wp_send_json_success( $status );
	}

	/**
	 * Function to fetch templates.
	 *
	 * @param $current_demo_list array
	 * @return array
	 */
	public function add_demo_lists( $current_demo_list ) {

		if ( acme_demo_setup_get_current_theme_author() != $this->theme_author ) {
			return $current_demo_list;
		}

		$theme_slug = acme_demo_setup_get_current_theme_slug();

		$templates = acme_demo_setup_get_templates_lists( $theme_slug );

		return array_merge( $current_demo_list, $templates );
	}

	/**
	 * Add plugin menu items.
	 *
	 * @access public
	 *
	 * @since 1.0.0
	 * @param string[] $actions     An array of plugin action links. By default this can include
	 *                              'activate', 'deactivate', and 'delete'. With Multisite active
	 *                              this can also include 'network_active' and 'network_only' items.
	 * @param string   $plugin_file Path to the plugin file relative to the plugins directory.
	 * @param array    $plugin_data An array of plugin data. See get_plugin_data()
	 *                              and the {@see 'plugin_row_meta'} filter for the list
	 *                              of possible values.
	 * @param string   $context     The plugin context. By default this can include 'all',
	 *                              'active', 'inactive', 'recently_activated', 'upgrade',
	 *                              'mustuse', 'dropins', and 'search'.
	 * @return array settings schema for this plugin.
	 */
	public function add_plugin_links( $actions, $plugin_file, $plugin_data, $context ) {
		if ( function_exists( 'advanced_import_admin' ) ) {
			$actions[] = '<a href="' . esc_url( menu_page_url( 'advanced-import', false ) ) . '">' . esc_html__( 'Import', 'acme-demo-setup' ) . '</a>';
		}
		return $actions;
	}
}

/**
 * Begins execution of the hooks.
 *
 * @since    1.0.0
 */
function acme_demo_setup_hooks() {
	return Acme_Demo_Setup_Hooks::instance();
}
