<?php
/**************************
 *   Plugin Installer
 **************************/

// Admin Enqueue for Admin
function one_elementor_admin_enqueue_scripts( $hook ){
    // Enqueue only on relevant admin pages if needed, or globally in wp-admin for installers
    wp_enqueue_script( 'one-elementor-plugin-installer', get_theme_file_uri( '/resource/js/plugins-installer.js' ), array( 'jquery' ), ONE_ELEMENTOR_THEME_VERSION, true );
    wp_localize_script(
        'one-elementor-plugin-installer',
        'one_elementor_ajax_object',
        array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'one_elementor_plugin_installer_nonce' ),
        )
    );
}
add_action( 'admin_enqueue_scripts', 'one_elementor_admin_enqueue_scripts' );

add_action( 'wp_ajax_install_act_plugin', 'one_elementor_admin_install_plugin' );

function one_elementor_admin_install_plugin() {
    // 1. Security Check: Nonce Verification
    check_ajax_referer( 'one_elementor_plugin_installer_nonce', 'nonce' );

    // 2. Security Check: Capability Verification
    if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
        wp_send_json_error( array( 'message' => __( 'Permission denied.', 'one-elementor' ) ) );
    }

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

    $plugins_to_install = array(
        'rs-wp-themes-one-click-demo-content' => 'rs-wp-themes-one-click-demo-content/rs-wp-themes-one-click-demo-content.php',
        'advanced-import'                     => 'advanced-import/advanced-import.php',
        'rs-author-info-box'                  => 'rs-author-info-box/rs-author-info-box.php',
        'rs-wp-books-showcase'                => 'rs-wp-books-showcase/rs-wp-books-showcase.php',
        'elementor'                           => 'elementor/elementor.php',
        'breadcrumb-navxt'                    => 'breadcrumb-navxt/breadcrumb-navxt.php',
        'header-footer-elementor'             => 'header-footer-elementor/header-footer-elementor.php',
        'contact-form-7'                      => 'contact-form-7/wp-contact-form-7.php',
        'mailchimp-for-wp'                    => 'mailchimp-for-wp/mailchimp-for-wp.php',
    );

    foreach ( $plugins_to_install as $slug => $file_path ) {
        if ( ! file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
            $api = plugins_api( 'plugin_information', array(
                'slug'   => sanitize_key( $slug ),
                'fields' => array( 'sections' => false ),
            ) );

            if ( ! is_wp_error( $api ) && isset( $api->download_link ) ) {
                $skin     = new WP_Ajax_Upgrader_Skin();
                $upgrader = new Plugin_Upgrader( $skin );
                $upgrader->install( $api->download_link );
            }
        }

        if ( file_exists( WP_PLUGIN_DIR . '/' . $file_path ) && ! is_plugin_active( $file_path ) ) {
            activate_plugin( $file_path );
        }
    }

    wp_send_json_success( array( 'message' => __( 'Plugins installed and activated successfully.', 'one-elementor' ) ) );
}