<?php
/**
 * Mavix Beat Notice Handler
 *
 * @package Mavix Beat 
 * @since Mavix Beat 1.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Enqueue getting started styles and scripts
 */
add_action( 'admin_enqueue_scripts', 'mavix_beat_widgets_backend_enqueue' );
function mavix_beat_widgets_backend_enqueue( $hook_suffix ) {

    // Load only on specific admin pages (adjust as needed)
    $allowed_pages = array(
        'appearance_page_ct-options',
        'themes.php',
        'toplevel_page_ct-options'
    );

    if ( ! in_array( $hook_suffix, $allowed_pages, true ) ) {
        return;
    }

    wp_enqueue_style(
        'mavix-beat-getting-started',
        get_template_directory_uri() . '/inc/getting-started/getting-started.css'
    );

    wp_register_script(
        'mavix-beat-custom-widgets',
        get_template_directory_uri() . '/inc/getting-started/getting-started.js',
        array( 'jquery' ),
        false,
        true
    );

    $translation = array(
        'btn_text'  => esc_html__( 'Processing...', 'mavix-beat' ),
        'nonce'     => wp_create_nonce( 'mavix_beat_demo_import_nonce' ),
        'noncen'    => 'mavix_beat_demo_import_nonce',
        'adminurl'  => esc_url_raw( admin_url() ),
    );

    wp_localize_script(
        'mavix-beat-custom-widgets',
        'mavix_beat_adi_install',
        $translation
    );

    wp_enqueue_media();
    wp_enqueue_script( 'mavix-beat-custom-widgets' );
}

/**
 * Class to handle notices and Advanced Demo Import
 *
 * Class mavix_beat_Notice_Handler
 */
class mavix_beat_Notice_Handler {

    public static $nonce;

    /**
     * Empty Constructor
     */
    public function __construct() {
        /* activation notice */
        add_action( 'switch_theme', array( $this, 'flush_dismiss_status' ) );
        add_action( 'admin_init', array($this,'getting_started_notice_dismissed' ) );
        add_action( 'admin_notices', array( $this, 'mavix_beat_theme_info_welcome_admin_notice' ), 3 );
        add_action( 'wp_ajax_mavix_beat_getting_started', array( $this, 'mavix_beat_getting_started' ) );
    }

    /**
     * Display an admin notice linking to the about page
     */
    public function mavix_beat_theme_info_welcome_admin_notice() {
        $theme = wp_get_theme();
        if ( is_admin() && !get_user_meta( get_current_user_id(), 'mavix_beat_gs_notice_dismissed' ) ){
            echo '<div class="updated notice notice-success is-dismissible getting-started">';
            if ( is_plugin_active( 'elementor/elementor.php' ) && is_plugin_active( 'elementskit-lite/elementskit-lite.php' ) && is_plugin_active( 'kirki/kirki.php' ) && is_plugin_active( 'creativ-demo-importer/creativ-demo-importer.php' ) ){
                echo ('<div class="fs-notice success"><p>'.esc_html__( 'All plugins are installed. To hide this notice click on dismiss.', 'mavix-beat' ).'</p></div>');
            }else{
                /* translators: %s - Theme name*/
                echo ( '<p><strong>' . sprintf( esc_html__( 'Congratulations!', 'mavix-beat' ), esc_attr( $theme->get( 'Name' ) ) ) . '</strong></p><p class="plugin-notice">'.esc_html__( 'Clicking on Get started with Mavix Beat will install and activate Elementor, ElementsKit Lite, Kirki and Creativ Demo Importer Plugin.', 'mavix-beat' ).'</p><p><a href="#" class="mavix-beat-install-plugins button button-primary">' . sprintf( esc_html__( 'Get Started','mavix-beat' ), esc_attr( $theme->get( 'Name' ) ) ) . '</a></p>' ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            }
            echo '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'gs-notice-dismissed', '1' ), 'mavix_beat_notice_dismiss' ) ) . '" class="getting-started-notice-dismiss">' . esc_html__( 'Dismiss', 'mavix-beat' ) . '</a>';
            echo '</div>';
        }
    }

    /**
     * Register dismissal of the getting started notification.
     * Acts on the dismiss link.
     * If clicked, the admin notice disappears and will no longer be visible to this user.
     */
    public function getting_started_notice_dismissed() {
        
        if ( isset( $_GET['gs-notice-dismissed'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'mavix_beat_notice_dismiss' )) {
            update_user_meta(
                get_current_user_id(), 'mavix_beat_gs_notice_dismissed', '1'
            );
        }
    }

    /**
     * Deletes the getting started notice's dismiss status upon theme switch. 
     */
    public function flush_dismiss_status(){
        delete_user_meta( get_current_user_id(), 'mavix_beat_gs_notice_dismissed', 'true' );
    }

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

        check_ajax_referer( 'mavix_beat_demo_import_nonce', 'security' );

        $slug = isset( $_POST['slug'] ) ? sanitize_key( wp_unslash( $_POST['slug'] ) ) : '';

        $main_file = isset( $_POST['main_file'] ) ? sanitize_file_name( wp_unslash( $_POST['main_file'] ) ) : $slug;

        if ( ! preg_match( '/^[a-z0-9\-_]+$/', $slug ) ) {
            $slug = '';
        }

        if ( ! preg_match( '/^[a-zA-Z0-9\-_]+$/', $main_file ) ) {
            $main_file = $slug;
        }

        $plugin = $slug . '/' . $main_file . '.php';

        $request = isset( $_POST['request'] ) ? absint( $_POST['request'] ) : 0;

        $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.', 'mavix-beat' );
            wp_send_json_error( $status );
        }

        if( $request > 4 ){
            wp_send_json_error( );
        }

        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( $request == 1 && strpos( $slug, 'elementor' ) === false ){
            wp_send_json_error( );
        }
        if( $request == 2 && strpos( $slug, 'elementskit-lite' ) === false ){
            wp_send_json_error( );
        }
        if( $request == 3 && strpos( $slug, 'kirki' ) === false ){
            wp_send_json_error( );
        }
        if( $request == 4 && strpos( $slug, 'creativ-demo-importer' ) === false ){
            wp_send_json_error( );
        }
        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.', 'mavix-beat' );

            // 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 );
    }
}
new mavix_beat_Notice_Handler;