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

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


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


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


// Plugin uninstall
function uninstall() {
    \NeoDuplicateFull\NeoGlobal\call_interface_func_implemented('\NeoDuplicateFull\NeoFreemius\interface_freemius_uninstall_hook_20260210')(\NeoDuplicateFull\NeoGlobal\plugin_slug(), \NeoDuplicateFull\NeoGlobal\plugin_edition(), \NeoDuplicateFull\NeoGlobal\plugin_version());
    \NeoDuplicateFull\NeoGlobal\add_action_hook("shutdown:max", function() {
        \NeoDuplicateFull\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Uninstall plugin " . \NeoDuplicateFull\NeoGlobal\plugin_slug() . "-" . \NeoDuplicateFull\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(\NeoDuplicateFull\NeoGlobal\plugin_entry_file_path())) { continue; }
            if (!str_starts_with($plugin_file, "neo-")) { continue; }
            return;
        }

        \NeoDuplicateFull\NeoGlobal\delete_all_neo_options();
    });
}

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


// Clean up plugin cache folder


function clear_plugin_cache($delete_all = false, $force = false) {
    \NeoDuplicateFull\NeoGlobal\global_log_with_module_name("_global-activate-deactivate-uninstall", "Clear plugin cache " . \NeoDuplicateFull\NeoGlobal\plugin_slug() . "-" . \NeoDuplicateFull\NeoGlobal\plugin_edition());

    $cache_path = \NeoDuplicateFull\NeoGlobal\cache_path(only_get_path: true);
    if (count(\NeoDuplicateFull\NeoGlobal\fs_glob("$cache_path/custom-*")) > 0 && !$force) { return; }

    $failed_delete_items = [];
    if ($delete_all) {
        $delete_success = \NeoDuplicateFull\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 = \NeoDuplicateFull\NeoGlobal\array_filter_better(array_merge(\NeoDuplicateFull\NeoGlobal\fs_glob("$cache_path/*", GLOB_MARK), \NeoDuplicateFull\NeoGlobal\fs_glob("$cache_path/.*", GLOB_MARK)), function ($item) use ($exceptions) { if (in_array(basename($item), [".", ".."])) { return; } foreach ($exceptions as $ex) { if (\NeoDuplicateFull\NeoGlobal\fs_fnmatch($ex, basename($item))) return false; } return true; });
            foreach ($items as $item) { $delete_success = \NeoDuplicateFull\NeoGlobal\delete_all($item); if (!$delete_success) { $failed_delete_items[]= $item; } }
        };

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