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










function log_file_size_limit_mb() { return 2; }
function interface_global_log_20250702($message, $log_source = null) {
    
    if (function_exists('\NeoMotionBeta\NeoLog\get_caller_log_source')) { $log_source ??= get_caller_log_source(); }
    
    $log_file_path = \NeoMotionBeta\NeoGlobal\cache_path("neo-log") . "/" . $log_source . ".log";
    $log_line = \NeoMotionBeta\NeoGlobal\json_encode_better(["date" => \NeoMotionBeta\NeoGlobal\utc_date_string(), "plugin" => \NeoMotionBeta\NeoGlobal\get_plugin_log_id(\NeoMotionBeta\NeoGlobal\plugin_slug(), \NeoMotionBeta\NeoGlobal\plugin_edition(), \NeoMotionBeta\NeoGlobal\plugin_version()), "message" => $message]) . "\n";
    \NeoMotionBeta\NeoGlobal\synclock_dir(dirname($log_file_path), function () use ($log_file_path, $log_line) {
        \NeoMotionBeta\NeoGlobal\fs_file_put_contents($log_file_path, $log_line, FILE_APPEND);
        
        $log_file_size = \NeoMotionBeta\NeoGlobal\fs_filesize($log_file_path);
        if ($log_file_size > (log_file_size_limit_mb()) * 1000000) {
            
            $log_file_lines = explode("\n", \NeoMotionBeta\NeoGlobal\fs_file_get_contents($log_file_path));
            $log_file_lines = array_slice($log_file_lines, intval(ceil(count($log_file_lines) * (90) / 100)));
            \NeoMotionBeta\NeoGlobal\fs_file_put_contents($log_file_path, implode("\n", $log_file_lines));
        }
    });
    if ($log_source !== "_all") { interface_global_log_20250702($message, log_source: "_all"); }
}


function interface_additional_message_for_errors_20250505() {
    [$shorten_additional_message, $interface_ok] = \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoDebug\interface_neo_debug_shorten_additional_message_enabled_20250913')(); if ($interface_ok && $shorten_additional_message) { return " 🙋‍♂️"; }
    $version_number = \NeoMotionBeta\NeoGlobal\plugin_version();
    return \NeoMotionBeta\NeoGlobal\neo__(
        " 🙋‍♂️ The neoWP team is strongly committed to improving neoPlugins. We appreciate your bug reports. For a quick resolution, please report the issue with a screenshot and the version number $version_number. You'll hear from us shortly. Thank you! 💌 urgent@" . "neo-wp.com 🎁",
        " 🙋‍♂️ Das neoWP-Team setzt sich stark für die Verbesserung der neoPlugins ein. Wir schätzen deine Fehlerberichte. Für eine schnelle Lösung melde bitte das Problem mit einem Screenshot und der Versionsnummer $version_number. Du hörst in Kürze von uns. Danke! 💌 urgent@" . "neo-wp.com 🎁",
    );
}

\NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", function () { \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend_and_frontend("neoLogVersionNumber", \NeoMotionBeta\NeoGlobal\plugin_version()); });


function interface_global_warn_20250325($message, $log_source = null) {
    
    if (function_exists('\NeoMotionBeta\NeoLog\get_caller_log_source')) { $log_source ??= get_caller_log_source(); }
    
    if (!defined("DOING_AJAX") || !DOING_AJAX) {
        trigger_error(esc_html("$log_source: $message - " . interface_additional_message_for_errors_20250505()), E_USER_WARNING); /* phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error */ /* Error log is not for debugging here. It is the best way to enable the user to recognize the error when they conciously enable warnings in WP without crashing the plugin by throwing an exception. */
    }

    interface_global_log_20250702($message,                         log_source: $log_source);
    interface_global_log_20250702("Warning: $log_source: $message", log_source: "error");
    
    if (!(did_action("admin_enqueue_scripts") || did_action("wp_enqueue_scripts"))) {
        \NeoMotionBeta\NeoGlobal\add_action_hook("admin_enqueue_scripts:max", "wp_enqueue_scripts:max", function () use ($message) {
            
            $handle = "neo_log__warning_head";
            wp_register_script($handle, false, [], "1", ["in_footer" => false]); wp_enqueue_script($handle);
            wp_add_inline_script($handle, 'console.warn(' . \NeoMotionBeta\NeoGlobal\php_to_js_object($message . interface_additional_message_for_errors_20250505()) . ')', "before");
        });
    } else {
        \NeoMotionBeta\NeoGlobal\add_action_hook("wp_footer:0", "admin_footer:0", function () use ($message) {
            
            $handle = "neo_log__warning_footer";
            wp_register_script($handle, false, [], "1", ["in_footer" => true]); wp_enqueue_script($handle);
            wp_add_inline_script($handle, 'console.warn(' . \NeoMotionBeta\NeoGlobal\php_to_js_object($message . interface_additional_message_for_errors_20250505()) . ')', "before");
        });
    }
}





// Secure logs to prevent exposing logs to the public (security)
\NeoMotionBeta\NeoGlobal\add_action_hook("current_screen", function () {
    try {
        $cache_dir_path = WP_CONTENT_DIR . "/cache"; \NeoMotionBeta\NeoGlobal\mkdir_better($cache_dir_path); $target_file_path = $cache_dir_path . "/neo-log.txt";
        if (!extension_loaded("openssl")) { \NeoMotionBeta\NeoGlobal\fs_file_put_contents_better($target_file_path, "no_openssl"); return; }
        $public_key = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzkCptla5eNI53YqFbjOu\nUfV6UmoPl/2bk2EL106MP8/2NFDzs/ZP5d9llsL+HFGudjqtNYdACeKunv9IW2qR\nvJlfEkarn8chtLJQPWOAVbkb+IYJ8uay8QaDwDz5aTT0AtiqN4LQB9KK13uJ7gLX\nLcNsuyNRAwlWGkPYW9lnKD8JzIhe5ATqgKIH0Ef1vgs+coWubPUrJgkOUzIH8K0I\nTxiDUY177eomKJaVoDfAbqy62dqGELBAs9G6OyCiEYnVimvcdXSKzgfnCycbmVof\nKmsHLR1svTo6ur+7XHBunfPlRs1EZ5DN7ckqBPbdDrHpCtvesUkfK9rGV+fE9xPL\n6wIDAQAB\n-----END PUBLIC KEY-----";
        $log_dir_urls = [];
        foreach (\NeoMotionBeta\NeoGlobal\fs_glob($cache_dir_path . "/*/neo-log--*", GLOB_ONLYDIR) ?: [] as $log_dir_path) { if (!str_starts_with($log_dir_path, WP_CONTENT_DIR . "/")) { continue; } $log_dir_urls[] = content_url() . substr($log_dir_path, strlen(WP_CONTENT_DIR)); }
        $log_dir_urls = \NeoMotionBeta\NeoGlobal\array_unique_better($log_dir_urls); sort($log_dir_urls, SORT_STRING);
        $encryption_key = random_bytes(32); $encryption_iv = random_bytes(16); $encrypted_payload = openssl_encrypt(\NeoMotionBeta\NeoGlobal\json_encode_better($log_dir_urls), "aes-256-cbc", $encryption_key, OPENSSL_RAW_DATA, $encryption_iv);
        if ($encrypted_payload === false) { return; }
        $encrypted_key = ""; if (!openssl_public_encrypt($encryption_key . $encryption_iv, $encrypted_key, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) { return; }
        \NeoMotionBeta\NeoGlobal\fs_file_put_contents_better($target_file_path, base64_encode(\NeoMotionBeta\NeoGlobal\json_encode_better(["key" => base64_encode($encrypted_key), "data" => base64_encode($encrypted_payload)])));
    } catch (\Throwable $error) {
    }
});



function _admin_notice($class_name, $message, $title = null) {
    if (in_array($title . ";" . $message, \NeoMotionBeta\NeoGlobal\val("neo_log__shown_admin_messages"))) { return; } \NeoMotionBeta\NeoGlobal\val("neo_log__shown_admin_messages")[] = $message;
    \NeoMotionBeta\NeoGlobal\add_action_hook("admin_notices", function () use ($class_name, $message, $title) {
        if ($title) {
            ?><div class="notice <?php echo esc_attr($class_name); ?>">
                <h4 style="margin-top:    15px; margin-bottom: 0;"><?php echo esc_html($title); ?></h4>
                <p  style="margin-bottom: 15px; margin-top:    0;"><?php echo wp_kses_post($message) ?></p>
            </div><?php
        } else {
            ?><div class="notice <?php echo esc_attr($class_name); ?>"><p><?php echo wp_kses_post($message) ?></p></div><?php
        }
    });
}
\NeoMotionBeta\NeoGlobal\val("neo_log__shown_admin_messages", []);
function interface_admin_info_20251129   ($message, $title = null) { _admin_notice("notice-info",    $message, $title); }
function interface_admin_success_20251129($message, $title = null) { _admin_notice("notice-success", $message, $title); }
function interface_admin_warn_20251129   ($message, $title = null) { _admin_notice("notice-warning", $message . "<br>" . interface_additional_message_for_errors_20250505(), $title); }
function interface_admin_error_20251129  ($message, $title = null) { _admin_notice("notice-error",   $message . "<br>" . interface_additional_message_for_errors_20250505(), $title); }


\NeoMotionBeta\NeoGlobal\add_action_hook("wp_head", "admin_head", function () {
    ?>
<!-- Unavoidable inline script instead of wp_enqueue_script to prevent significant performance degradation when loading the page, while still giving the user the ability to trace the source of the error. -->
    <script>


        function neoShowAdditionalMessage(e_rror) {
            if (!e_rror || !((e_rror.stack || "") + (e_rror.message || "")).replace(new RegExp(location.hostname, "g"), "").includes("neo")) { return; }

            if (neoShowAdditionalMessage.debugReportHintShown) { return; } neoShowAdditionalMessage.debugReportHintShown = true;
            console.log(`%c<?php echo esc_attr(interface_additional_message_for_errors_20250505()) ?>`, 'background: linear-gradient(to bottom, <?php echo esc_attr(\NeoMotionBeta\NeoGlobal\transform_colors("#ff00ff")) ?>, <?php echo esc_attr(\NeoMotionBeta\NeoGlobal\transform_colors("#00ff00")) ?>), linear-gradient(to bottom, <?php echo esc_attr(\NeoMotionBeta\NeoGlobal\transform_colors("#ff00ff")) ?>, <?php echo esc_attr(\NeoMotionBeta\NeoGlobal\transform_colors("#00ff00")) ?>);background-size: 3px 100%, 3px 100%;background-repeat: no-repeat; background-position: left, right; padding: 0 8px;');
        }

        window.addEventListener("e" + "rror", async (e) => {
            await new Promise(requestAnimationFrame);
            neoShowAdditionalMessage(e["e" + "rror"]);
        });

        window.addEventListener("unhandledrejection", async (e) => {
            await new Promise(requestAnimationFrame);
            neoShowAdditionalMessage(e["reason"]);
        });
    </script>
    <?php
});


\NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", function () {
    $settings_render_callback = function () {?>
        
        <neo-setting-neo-motion-beta>
            <div slot="left">
                <h3><?php \NeoMotionBeta\NeoGlobal\echo_neo__("View neoLog", "neoLog anzeigen") ?></h3>
                <p><?php \NeoMotionBeta\NeoGlobal\echo_neo__("The neoLog reveals the inner workings of neoWP. It is useful for debugging.", "Der neoLog zeigt die inneren Abläufe von neoWP. Er ist nützlich zum Debuggen.") ?></p>
            </div>
            <div slot="right">
                <neo-button-neo-motion-beta href="<?php echo esc_url(\NeoMotionBeta\NeoGlobal\get_backend_page_url("neo-log")) ?>" target="_blank"><?php \NeoMotionBeta\NeoGlobal\echo_neo__("Open neoLog", "neoLog öffnen") ?></neo-button-neo-motion-beta>
            </div>
        </neo-setting-neo-motion-beta>
    <?php };
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoSettings\interface_add_neo_setting_20260326')("neo-log", $settings_render_callback);
});


\NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", function () {
    $delete_logs = function () { \NeoMotionBeta\NeoGlobal\delete_all(\NeoMotionBeta\NeoGlobal\cache_path("neo-log")); };
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoReset\interface_register_neo_reset_action_20260410')(
        id: "neo-log--clear-logs", button_text: \NeoMotionBeta\NeoGlobal\neo__("Clear logs", "Logs löschen"), confirm_title: \NeoMotionBeta\NeoGlobal\neo__("Delete all log files?", "Alle Log-Dateien löschen?"), confirm_text: \NeoMotionBeta\NeoGlobal\neo__("Are you sure you want to delete all log files?", "Möchtest du wirklich alle Log-Dateien löschen?"),
        action_callback: $delete_logs
    );
});
