<?php
namespace NeoAnimateBeta\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

register_activation_hook(\NeoAnimateBeta\NeoGlobal\plugin_entry_file_path(), function () {
    \NeoAnimateBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Activate plugin " . \NeoAnimateBeta\NeoGlobal\plugin_slug() . "-" . \NeoAnimateBeta\NeoGlobal\plugin_edition());
    \NeoAnimateBeta\NeoGlobal\call_interface_func_implemented('\NeoAnimateBeta\NeoFreemius\interface_freemius_activate_hook_20260210')(\NeoAnimateBeta\NeoGlobal\plugin_slug(), \NeoAnimateBeta\NeoGlobal\plugin_edition(), \NeoAnimateBeta\NeoGlobal\plugin_version());
});

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


// Plugin update
\NeoAnimateBeta\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"] === \NeoAnimateBeta\NeoGlobal\get_bundle_slug(\NeoAnimateBeta\NeoGlobal\plugin_slug(), \NeoAnimateBeta\NeoGlobal\plugin_edition()))) { return $response; }
    \NeoAnimateBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Clear cache because of plugin update " . \NeoAnimateBeta\NeoGlobal\plugin_slug() . "-" . \NeoAnimateBeta\NeoGlobal\plugin_edition());
    clear_plugin_cache(delete_all: false);
    \NeoAnimateBeta\NeoGlobal\flush_all_third_party_caches();
    return $response;
});


// Plugin uninstall
function uninstall() {
    \NeoAnimateBeta\NeoGlobal\call_interface_func_implemented('\NeoAnimateBeta\NeoFreemius\interface_freemius_uninstall_hook_20260210')(\NeoAnimateBeta\NeoGlobal\plugin_slug(), \NeoAnimateBeta\NeoGlobal\plugin_edition(), \NeoAnimateBeta\NeoGlobal\plugin_version());
    \NeoAnimateBeta\NeoGlobal\add_action_hook("shutdown:max", function() {
        \NeoAnimateBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Uninstall plugin " . \NeoAnimateBeta\NeoGlobal\plugin_slug() . "-" . \NeoAnimateBeta\NeoGlobal\plugin_edition());
        
        clear_plugin_cache(delete_all: true);
        if (\NeoAnimateBeta\NeoGlobal\fs_file_exists(WP_CONTENT_DIR . "/cache/neo-log.txt")) { \NeoAnimateBeta\NeoGlobal\fs_unlink(WP_CONTENT_DIR . "/cache/neo-log.txt"); }
        \NeoAnimateBeta\NeoGlobal\flush_all_third_party_caches();
        
        foreach (array_keys(get_plugins()) as $plugin_file) {
            if (dirname(WP_PLUGIN_DIR . "/" . $plugin_file) === dirname(\NeoAnimateBeta\NeoGlobal\plugin_entry_file_path())) { continue; } /* WP_PLUGIN_DIR has to be used because only WordPress can authoritatively resolve the installation-specific plugin root including MU context, symlink topology and hosting path sovereignty; any local derivation would not be semantically equivalent, could address user installations incorrectly and is therefore fundamentally not a reliable alternative. */
            if (!str_starts_with($plugin_file, "neo-")) { continue; }
            return;
        }

        \NeoAnimateBeta\NeoGlobal\delete_all_neo_options();
    });
}

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


// Clean up plugin cache folder


function clear_plugin_cache($delete_all = false, $force = false) {
    \NeoAnimateBeta\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Clear plugin cache " . \NeoAnimateBeta\NeoGlobal\plugin_slug() . "-" . \NeoAnimateBeta\NeoGlobal\plugin_edition());
    
    $cache_path = \NeoAnimateBeta\NeoGlobal\cache_path(only_get_path: true);
    if (count(\NeoAnimateBeta\NeoGlobal\fs_glob("$cache_path/custom-*")) > 0 && !$force) { return; }
    
    $failed_delete_items = [];
    if ($delete_all) {
        $delete_success = \NeoAnimateBeta\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 = \NeoAnimateBeta\NeoGlobal\array_filter_better(array_merge(\NeoAnimateBeta\NeoGlobal\fs_glob("$cache_path/*", GLOB_MARK), \NeoAnimateBeta\NeoGlobal\fs_glob("$cache_path/.*", GLOB_MARK)), function ($item) use ($exceptions) { if (in_array(basename($item), [".", ".."])) { return; } foreach ($exceptions as $ex) { if (\NeoAnimateBeta\NeoGlobal\fs_fnmatch($ex, basename($item))) return false; } return true; });
            foreach ($items as $item) { $delete_success = \NeoAnimateBeta\NeoGlobal\delete_all($item); if (!$delete_success) { $failed_delete_items[]= $item; } }
        };

        $delete_cache_folder_with_exceptions();
    }
    if (!empty($failed_delete_items)) { \NeoAnimateBeta\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")) {                               \NeoAnimateBeta\NeoGlobal\call_interface_func_implemented('\NeoAnimateBeta\NeoDraw\interface_draw_pagebuilder_elementor_inline_svg_db_clear_20251118')(); }
    else { \NeoAnimateBeta\NeoGlobal\add_action_hook("neo_init", function () { \NeoAnimateBeta\NeoGlobal\call_interface_func_implemented('\NeoAnimateBeta\NeoDraw\interface_draw_pagebuilder_elementor_inline_svg_db_clear_20251118')(); }); }
    
}
