<?php
// includes/beaver-builder.php
defined('ABSPATH') || exit;

/**
 * After updating builder data, keep draft in sync and clear BB caches.
 */
function blog_maker_bb_sync_and_clear_cache($post_id) {
    // Keep draft in sync with data so editor + front-end match
    $data = get_post_meta($post_id, '_fl_builder_data', true);
    if (!empty($data)) {
        update_post_meta($post_id, '_fl_builder_draft', $data);
    }

    // Clear Beaver Builder caches AFTER mutations
    if (class_exists('FLBuilderModel')) {
        if (method_exists('FLBuilderModel', 'delete_asset_cache')) {
            FLBuilderModel::delete_asset_cache($post_id);
        }
        if (method_exists('FLBuilderModel', 'delete_layout_asset_cache')) {
            FLBuilderModel::delete_layout_asset_cache($post_id);
        }
        if (method_exists('FLBuilderModel', 'update_layout_css')) {
            FLBuilderModel::update_layout_css($post_id);
        }
        if (method_exists('FLBuilderModel', 'update_layout_js')) {
            FLBuilderModel::update_layout_js($post_id);
        }
    }
}
