<?php
namespace NeoLibraryBeta\NeoGlobal; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }



// Carefully implemented activation, deactivation and uninstall hooks with late cache cleanup and selective deletion of neo_ options.



































// Plugin activation

\NeoLibraryBeta\NeoGlobal\val("neo_global__after_activation_callbacks", []);
function register_after_activation_callback($plugin_slug, $callback) { \NeoLibraryBeta\NeoGlobal\val("neo_global__after_activation_callbacks", array_merge(\NeoLibraryBeta\NeoGlobal\val("neo_global__after_activation_callbacks"), [["plugin_slug" => $plugin_slug, "callback" => $callback]])); }
function run_after_activation_callbacks($plugin_slug) { foreach (\NeoLibraryBeta\NeoGlobal\val("neo_global__after_activation_callbacks") as $after_activation_callback) { if (!($after_activation_callback["plugin_slug"] === $plugin_slug)) { continue; } ($after_activation_callback["callback"])(); } }
register_activation_hook(\NeoLibraryBeta\NeoGlobal\plugin_entry_file_path(), function () {
    \NeoLibraryBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Activate plugin " . \NeoLibraryBeta\NeoGlobal\plugin_slug() . "-" . \NeoLibraryBeta\NeoGlobal\plugin_edition());
    \NeoLibraryBeta\NeoGlobal\call_interface_func_implemented('\NeoLibraryBeta\NeoFreemius\interface_freemius_activate_hook_20260210')(\NeoLibraryBeta\NeoGlobal\plugin_slug(), \NeoLibraryBeta\NeoGlobal\plugin_edition(), \NeoLibraryBeta\NeoGlobal\plugin_version());
    if (!(str_starts_with(\NeoLibraryBeta\NeoGlobal\request_uri(), "/wp-admin/plugins.php") && str_contains(\NeoLibraryBeta\NeoGlobal\request_uri(), "action=activate"))) { return; }
    \NeoLibraryBeta\NeoGlobal\option__neo_global__after_activation_redirect_neo_library_beta(true);
});
\NeoLibraryBeta\NeoGlobal\add_action_hook("current_screen", function () {
    if (!\NeoLibraryBeta\NeoGlobal\option__neo_global__after_activation_redirect_neo_library_beta()) { return; }
    \NeoLibraryBeta\NeoGlobal\option__neo_global__after_activation_redirect_neo_library_beta(delete: true);
    run_after_activation_callbacks(\NeoLibraryBeta\NeoGlobal\plugin_slug());
    wp_safe_redirect(admin_url("admin.php?page=" . \NeoLibraryBeta\NeoGlobal\plugin_settings_page_slug())); exit;
});

// Plugin deactivation
register_deactivation_hook(\NeoLibraryBeta\NeoGlobal\plugin_entry_file_path(), function () {
    \NeoLibraryBeta\NeoGlobal\call_interface_func_implemented('\NeoLibraryBeta\NeoFreemius\interface_freemius_deactivate_hook_20260210')(\NeoLibraryBeta\NeoGlobal\plugin_slug(), \NeoLibraryBeta\NeoGlobal\plugin_edition(), \NeoLibraryBeta\NeoGlobal\plugin_version());
    \NeoLibraryBeta\NeoGlobal\add_action_hook("shutdown:max", function() {
        \NeoLibraryBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Deactivate plugin " . \NeoLibraryBeta\NeoGlobal\plugin_slug() . "-" . \NeoLibraryBeta\NeoGlobal\plugin_edition());
        clear_plugin_cache(delete_all: false);
        \NeoLibraryBeta\NeoGlobal\flush_all_third_party_caches();
    });
});


// Plugin update
\NeoLibraryBeta\NeoGlobal\add_filter_hook("upgrader_post_install", function ($response, $hook_extra, $result) {
    if (!is_array($result) || !isset($result["destination_name"])) { return $response; }
    if (!($result["destination_name"] === \NeoLibraryBeta\NeoGlobal\get_bundle_slug(\NeoLibraryBeta\NeoGlobal\plugin_slug(), \NeoLibraryBeta\NeoGlobal\plugin_edition()))) { return $response; }
    \NeoLibraryBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Clear cache because of plugin update " . \NeoLibraryBeta\NeoGlobal\plugin_slug() . "-" . \NeoLibraryBeta\NeoGlobal\plugin_edition());
    clear_plugin_cache(delete_all: false);
    \NeoLibraryBeta\NeoGlobal\flush_all_third_party_caches();
    return $response;
});


// Plugin uninstall
function uninstall() {
    \NeoLibraryBeta\NeoGlobal\call_interface_func_implemented('\NeoLibraryBeta\NeoFreemius\interface_freemius_uninstall_hook_20260210')(\NeoLibraryBeta\NeoGlobal\plugin_slug(), \NeoLibraryBeta\NeoGlobal\plugin_edition(), \NeoLibraryBeta\NeoGlobal\plugin_version());
    \NeoLibraryBeta\NeoGlobal\add_action_hook("shutdown:max", function() {
        \NeoLibraryBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Uninstall plugin " . \NeoLibraryBeta\NeoGlobal\plugin_slug() . "-" . \NeoLibraryBeta\NeoGlobal\plugin_edition());
        
        clear_plugin_cache(delete_all: true);
        
        foreach (array_keys(get_plugins()) as $plugin_file) {
            if (dirname(WP_PLUGIN_DIR . "/" . $plugin_file) === dirname(\NeoLibraryBeta\NeoGlobal\plugin_entry_file_path())) { continue; }
            if (!str_starts_with($plugin_file, "neo-")) { continue; }
            return;
        }

        \NeoLibraryBeta\NeoGlobal\delete_all_neo_options();
    });
}

if (!did_action("pre_uninstall_plugin")) {
    register_uninstall_hook(\NeoLibraryBeta\NeoGlobal\plugin_entry_file_path(), "\\" . __NAMESPACE__ . "\\" . "uninstall");
}


// Clean up plugin cache folder


function clear_plugin_cache($delete_all = false, $force = false) {
    \NeoLibraryBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Clear plugin cache " . \NeoLibraryBeta\NeoGlobal\plugin_slug() . "-" . \NeoLibraryBeta\NeoGlobal\plugin_edition());
    
    $cache_path = \NeoLibraryBeta\NeoGlobal\cache_path(only_get_path: true);
    if (count(\NeoLibraryBeta\NeoGlobal\fs_glob("$cache_path/custom-*")) > 0 && !$force) { return; }
    
    $failed_delete_items = [];
    if ($delete_all) {
        $delete_success = \NeoLibraryBeta\NeoGlobal\delete_all($cache_path);
        if (!$delete_success) { $failed_delete_items[]= $cache_path; }
    } else {
        $delete_cache_folder_with_exceptions = function () use (&$cache_path, &$failed_delete_items) {
            
            $exceptions = [
                ".version",
                ".lock*",
                "neo-log--*",
                "neo-tool-passkey",
                "neo-gpt-engine",
                "cache-hash.json",
                "index.php",
            ];
            $items = \NeoLibraryBeta\NeoGlobal\array_filter_better(array_merge(\NeoLibraryBeta\NeoGlobal\fs_glob("$cache_path/*", GLOB_MARK), \NeoLibraryBeta\NeoGlobal\fs_glob("$cache_path/.*", GLOB_MARK)), function ($item) use ($exceptions) { if (in_array(basename($item), [".", ".."])) { return; } foreach ($exceptions as $ex) { if (\NeoLibraryBeta\NeoGlobal\fs_fnmatch($ex, basename($item))) return false; } return true; });
            foreach ($items as $item) { $delete_success = \NeoLibraryBeta\NeoGlobal\delete_all($item); if (!$delete_success) { $failed_delete_items[]= $item; } }
        };

        $delete_cache_folder_with_exceptions();
    }
    if (!empty($failed_delete_items)) { \NeoLibraryBeta\NeoGlobal\global_warn_with_module_name("_global-activate-deactivate-uninstall", "Could not delete cache folder(s) " . implode(", ", $failed_delete_items)); }
    
    if (did_action("neo_init")) {                               \NeoLibraryBeta\NeoGlobal\call_interface_func_implemented('\NeoLibraryBeta\NeoDraw\interface_draw_pagebuilder_elementor_inline_svg_db_clear_20251118')(); }
    else { \NeoLibraryBeta\NeoGlobal\add_action_hook("neo_init", function () { \NeoLibraryBeta\NeoGlobal\call_interface_func_implemented('\NeoLibraryBeta\NeoDraw\interface_draw_pagebuilder_elementor_inline_svg_db_clear_20251118')(); }); }
    \NeoLibraryBeta\NeoGlobal\flush_all_third_party_caches();
}
