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
















\NeoMotionBeta\NeoGlobal\frontend_image_hook_register_callback(function ($get_attr, $set_attr, $image_file_path) {
    if (!is_user_logged_in()) { return; }
    $set_attr("src", \NeoMotionBeta\NeoGlobal\add_query_param($get_attr("src"), "neo-image-cachebust", \NeoMotionBeta\NeoGlobal\fs_filemtime($image_file_path)));
});



function interface_update_last_image_cachebust_change_date_20260303() { static $first_run = true; if (!$first_run) { return; } $first_run = false; \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date(time()); }
function interface_get_last_image_cachebust_change_date_20260604() { return \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date(); }
\NeoMotionBeta\NeoGlobal\add_action_hook("add_attachment",    function ($attachment_id) { interface_update_last_image_cachebust_change_date_20260303(); });
\NeoMotionBeta\NeoGlobal\add_action_hook("edit_attachment",   function ($attachment_id) { interface_update_last_image_cachebust_change_date_20260303(); });
\NeoMotionBeta\NeoGlobal\add_action_hook("delete_attachment", function ($attachment_id) { interface_update_last_image_cachebust_change_date_20260303(); });



\NeoMotionBeta\NeoGlobal\add_filter_hook("wp_get_attachment_image_attributes", function ($attr, $attachment, $size) {
    if (!is_admin()) { return $attr; }
    if (!array_key_exists("src", $attr)) { return $attr; }
    $image_file_path = get_attached_file($attachment->ID);
    if ($image_file_path === false || !\NeoMotionBeta\NeoGlobal\fs_file_exists($image_file_path)) { return $attr; }
    $attr["src"] = \NeoMotionBeta\NeoGlobal\add_query_param($attr["src"], "neo-image-cachebust--attachment", \NeoMotionBeta\NeoGlobal\fs_filemtime($image_file_path));
    return $attr;
});
\NeoMotionBeta\NeoGlobal\add_action_hook("current_screen", function () {
    if ((get_current_screen()?->base ?? "") !== "post") { return; }
    if ((get_current_screen()?->post_type ?? "") !== "attachment") { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend("neoImageCachebustLastChangeDateAttachmentEditPage", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--attachment-edit-page.js");
});




\NeoMotionBeta\NeoGlobal\add_action_hook("template_redirect", "current_screen", function () {
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend_and_frontend("neoImageCachebustLastChangeDateWpMediaSelector", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
});
\NeoMotionBeta\NeoGlobal\add_action_hook("wp_enqueue_media", function () {
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--wp-media-selector.js");
});




\NeoMotionBeta\NeoGlobal\add_action_hook("template_redirect", function () {
    if (\NeoMotionBeta\NeoGlobal\query_param("brickspreview") === null) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_frontend("neoImageCachebustLastChangeDateBricksEditorPreview", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--bricks-editor.js");
});



\NeoMotionBeta\NeoGlobal\add_action_hook("template_redirect", function () {
    if (\NeoMotionBeta\NeoGlobal\query_param("et_fb") === null) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_frontend("neoImageCachebustLastChangeDateDiviEditor", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--divi-editor.js");
});
\NeoMotionBeta\NeoGlobal\add_action_hook("current_screen", function () {
    if ((get_current_screen()?->base ?? "") !== "post") { return; }
    if (!function_exists("et_pb_is_pagebuilder_used") || !et_pb_is_pagebuilder_used(intval(\NeoMotionBeta\NeoGlobal\query_param("post") ?? 0))) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend("neoImageCachebustLastChangeDateDiviEditor", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--divi-editor.js");
});



\NeoMotionBeta\NeoGlobal\add_action_hook("current_screen", function () {
    if ((get_current_screen()?->base ?? "") !== "post") { return; }
    if (!(get_current_screen()?->is_block_editor ?? false)) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend("neoImageCachebustLastChangeDateGutenbergEditor", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
});
\NeoMotionBeta\NeoGlobal\add_action_hook("enqueue_block_editor_assets", function () {
    if ((get_current_screen()?->base ?? "") !== "post") { return; }
    if (!(get_current_screen()?->is_block_editor ?? false)) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--gutenberg-editor.js");
});



\NeoMotionBeta\NeoGlobal\add_action_hook("current_screen", function () {
    if ((get_current_screen()?->base ?? "") !== "post") { return; }
    if (get_current_screen()?->is_block_editor ?? false) { return; }
    \NeoMotionBeta\NeoGlobal\enqueue_js_variable_backend("neoImageCachebustLastChangeDateClassicEditor", \NeoMotionBeta\NeoGlobal\option__neo_image_cachebust__last_change_date());
    \NeoMotionBeta\NeoGlobal\enqueue_js("neo-image-cachebust--classic-editor.js");
});


\NeoMotionBeta\NeoGlobal\add_action_hook("save_post", function (int $post_id, \WP_Post $post, bool $update) {
    $clean_image_cachebust = function ($data) use (&$clean_image_cachebust) {
        if (is_string($data)) {
            if (is_serialized($data)) { $unserialized_data = maybe_unserialize($data); return serialize($clean_image_cachebust($unserialized_data)); }
            return \NeoMotionBeta\NeoGlobal\preg_replace_callback_better("/https?:(?:\\\\)?\\/(?:\\\\)?\\/[^\\s\"']+/", function ($m) {
                $url = $m[0];
                $url = \NeoMotionBeta\NeoGlobal\preg_replace_better("/([?&])neo-image-cachebust(?:--[^=]*)?=\\d+(?:&|$)/", "$1", $url) ?? $url;
                $url = str_replace(["?&", "&&"], ["?", "&"], $url);
                $url = \NeoMotionBeta\NeoGlobal\preg_replace_better("/[?&]$/", "", $url) ?? $url;
                return $url;
            }, $data) ?? $data;
        }
        if (is_array($data))  { foreach ($data as $k => $v) { $data[$k] = $clean_image_cachebust($v); } return $data; }
        if (is_object($data)) { $cleaned_object = clone $data; foreach ($cleaned_object as $k => $v) { $cleaned_object->$k = $clean_image_cachebust($v); } return $cleaned_object; }
        return $data;
    };
    static $is_running = false; if ($is_running) { return; } $is_running = true;
    try {
        
        $cleaned_content = $clean_image_cachebust($post->post_content); if (is_string($cleaned_content) && $cleaned_content !== $post->post_content) { wp_update_post(["ID" => $post_id, "post_content" => wp_slash($cleaned_content)]); }
        foreach (get_post_meta($post_id) as $meta_key => $values) { foreach ($values as $value) { $value_before_cleaning = maybe_serialize($value); $cleaned_value = $clean_image_cachebust($value); if (maybe_serialize($cleaned_value) !== $value_before_cleaning) { update_post_meta($post_id, (string)$meta_key, wp_slash($cleaned_value), $value); } } }
    } finally { $is_running = false; }
});
