<?php
if ( ! class_exists( 'Ethical_Hacking_Plugin_Activation_WPElemento_Importer' ) ) {
    /**
     * Ethical_Hacking_Plugin_Activation_WPElemento_Importer initial setup
     *
     * @since 1.6.2
     */

    class Ethical_Hacking_Plugin_Activation_WPElemento_Importer {

        private static $ethical_hacking_instance;
        public $ethical_hacking_action_count;
        public $ethical_hacking_recommended_actions;

        /** Initiator **/
        public static function get_instance() {
          if ( ! isset( self::$ethical_hacking_instance) ) {
            self::$ethical_hacking_instance = new self();
          }
          return self::$ethical_hacking_instance;
        }

        /*  Constructor */
        public function __construct() {

            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'ethical_hacking_recommended_plugins', array($this, 'ethical_hacking_recommended_elemento_importer_plugins_array') );

            $ethical_hacking_actions                   = $this->ethical_hacking_get_recommended_actions();
            $this->ethical_hacking_action_count        = $ethical_hacking_actions['count'];
            $this->ethical_hacking_recommended_actions = $ethical_hacking_actions['actions'];

            add_action( 'wp_ajax_create_pattern_setup_builder', array( $this, 'create_pattern_setup_builder' ) );
        }

        public function ethical_hacking_recommended_elemento_importer_plugins_array($ethical_hacking_plugins){
            $ethical_hacking_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'ethical-hacking'),
                    'slug'     =>  'wpelemento-importer',
                    'function' => 'WPElemento_Importer_ThemeWhizzie',
                    'desc'     => esc_html__('We highly recommend installing the WPElemento Importer plugin for importing the demo content with Elementor.', 'ethical-hacking'),               
            );
            return $ethical_hacking_plugins;
        }

        public function enqueue_scripts() {
            wp_enqueue_script('updates');      
            wp_register_script( 'ethical-hacking-plugin-activation-script', esc_url(get_template_directory_uri()) . '/includes/getstart/js/plugin-activation.js', array('jquery') );
            wp_localize_script('ethical-hacking-plugin-activation-script', 'ethical_hacking_plugin_activate_plugin',
                array(
                    'installing' => esc_html__('Installing', 'ethical-hacking'),
                    'activating' => esc_html__('Activating', 'ethical-hacking'),
                    'error' => esc_html__('Error', 'ethical-hacking'),
                    'ajax_url' => esc_url(admin_url('admin-ajax.php')),
                    'wpelementoimpoter_admin_url' => esc_url(admin_url('admin.php?page=wpelemento-importer-tgmpa-install-plugins')),
                    'addon_admin_url' => esc_url(admin_url('admin.php?page=wpelementoimporter-wizard'))
                )
            );
            wp_enqueue_script( 'ethical-hacking-plugin-activation-script' );

        }

        // --------- Plugin Actions ---------
        public function ethical_hacking_get_recommended_actions() {

            $ethical_hacking_act_count  = 0;
            $ethical_hacking_actions_todo = get_option( 'recommending_actions', array());

            $ethical_hacking_plugins = $this->ethical_hacking_get_recommended_plugins();

            if ($ethical_hacking_plugins) {
                foreach ($ethical_hacking_plugins as $ethical_hacking_key => $ethical_hacking_plugin) {
                    $ethical_hacking_action = array();
                    if (!isset($ethical_hacking_plugin['slug'])) {
                        continue;
                    }

                    $ethical_hacking_action['id']   = 'install_' . $ethical_hacking_plugin['slug'];
                    $ethical_hacking_action['desc'] = '';
                    if (isset($ethical_hacking_plugin['desc'])) {
                        $ethical_hacking_action['desc'] = $ethical_hacking_plugin['desc'];
                    }

                    $ethical_hacking_action['name'] = '';
                    if (isset($ethical_hacking_plugin['name'])) {
                        $ethical_hacking_action['title'] = $ethical_hacking_plugin['name'];
                    }

                    $ethical_hacking_link_and_is_done  = $this->ethical_hacking_get_plugin_buttion($ethical_hacking_plugin['slug'], $ethical_hacking_plugin['name'], $ethical_hacking_plugin['function']);
                    $ethical_hacking_action['link']    = $ethical_hacking_link_and_is_done['button'];
                    $ethical_hacking_action['is_done'] = $ethical_hacking_link_and_is_done['done'];
                    if (!$ethical_hacking_action['is_done'] && (!isset($ethical_hacking_actions_todo[$ethical_hacking_action['id']]) || !$ethical_hacking_actions_todo[$ethical_hacking_action['id']])) {
                        $ethical_hacking_act_count++;
                    }
                    $ethical_hacking_recommended_actions[] = $ethical_hacking_action;
                    $ethical_hacking_actions_todo[]        = array('id' => $ethical_hacking_action['id'], 'watch' => true);
                }
                return array('count' => $ethical_hacking_act_count, 'actions' => $ethical_hacking_recommended_actions);
            }

        }

        public function ethical_hacking_get_recommended_plugins() {

            $ethical_hacking_plugins = apply_filters('ethical_hacking_recommended_plugins', array());
            return $ethical_hacking_plugins;
        }

        public function ethical_hacking_get_plugin_buttion($slug, $name, $function) {
                $ethical_hacking_is_done      = false;
                $ethical_hacking_button_html  = '';
                $ethical_hacking_is_installed = $this->is_plugin_installed($slug);
                $ethical_hacking_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $ethical_hacking_is_activeted = (class_exists($function)) ? true : false;
                if (!$ethical_hacking_is_installed) {
                    $ethical_hacking_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $ethical_hacking_plugin_install_url = wp_nonce_url($ethical_hacking_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $ethical_hacking_button_html        = sprintf('<a class="ethical-hacking-plugin-install install-now button-secondary button" data-slug="%1$s" href="%2$s" aria-label="%3$s" data-name="%4$s">%5$s</a>',
                        esc_attr($slug),
                        esc_url($ethical_hacking_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'ethical-hacking'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'ethical-hacking')
                    );
                } elseif ($ethical_hacking_is_installed && !$ethical_hacking_is_activeted) {

                    $ethical_hacking_plugin_activate_link = add_query_arg(
                        array(
                            'action'        => 'activate',
                            'plugin'        => rawurlencode($ethical_hacking_plugin_path),
                            'plugin_status' => 'all',
                            'paged'         => '1',
                            '_wpnonce'      => wp_create_nonce('activate-plugin_' . $ethical_hacking_plugin_path),
                        ), self_admin_url('plugins.php')
                    );

                    $ethical_hacking_button_html = sprintf('<a class="ethical-hacking-plugin-activate activate-now button-primary button" data-slug="%1$s" href="%2$s" aria-label="%3$s" data-name="%4$s">%5$s</a>',
                        esc_attr($slug),
                        esc_url($ethical_hacking_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'ethical-hacking'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'ethical-hacking')
                    );
                } elseif ($ethical_hacking_is_activeted) {
                    $ethical_hacking_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'ethical-hacking'));
                    $ethical_hacking_is_done     = true;
                }

                return array('done' => $ethical_hacking_is_done, 'button' => $ethical_hacking_button_html);
            }
        public function is_plugin_installed($slug) {
            $ethical_hacking_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $ethical_hacking_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($ethical_hacking_installed_plugins[$ethical_hacking_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $ethical_hacking_keys = array_keys($this->get_installed_plugins());
            foreach ($ethical_hacking_keys as $ethical_hacking_key) {
                if (preg_match('|^' . $slug . '/|', $ethical_hacking_key)) {
                    return $ethical_hacking_key;
                }
            }
            return $slug;
        }

        public function get_installed_plugins() {

            if (!function_exists('get_plugins')) {
                require_once ABSPATH . 'wp-admin/includes/plugin.php';
            }

            return get_plugins();
        }
        public function create_pattern_setup_builder() {

            $edit_page = admin_url().'post-new.php?post_type=page&create_pattern=true';
            echo json_encode(['page_id'=>'','edit_page_url'=> $edit_page ]);

            exit;
        }

    }
}
/**
 * Kicking this off by calling 'get_instance()' method
 */
Ethical_Hacking_Plugin_Activation_WPElemento_Importer::get_instance();