<?php
if (!function_exists('wr_output_guard_start')) {
    function wr_output_guard_start(){ if(ob_get_level()<1) ob_start(); $GLOBALS['WR_OUTPUT_GUARD_STARTED']=true; return ob_get_level(); }
}
if (!function_exists('wr_output_guard_clean_api_noise')) {
    function wr_output_guard_clean_api_noise(){
        if(!wr_runtime_is_api()) return '';
        $noise=''; while(ob_get_level()>0){ $part=ob_get_clean(); if($part!==false) $noise=$part.$noise; }
        ob_start();
        if(trim($noise)!=='') wr_runtime_log('WARNING','Suppressed output before API JSON',array('bytes'=>strlen($noise),'preview'=>substr($noise,0,500)),__FILE__,__LINE__);
        return $noise;
    }
}
if (!function_exists('wr_output_guard_headers_sent')) { function wr_output_guard_headers_sent(){ $f='';$l=0;$sent=headers_sent($f,$l);return array('sent'=>$sent,'file'=>$f,'line'=>$l); } }
if (!function_exists('wr_output_guard_safe_flush')) {
    function wr_output_guard_safe_flush(){ if(!empty($GLOBALS['WR_OUTPUT_GUARD_FLUSHED'])) return false; $GLOBALS['WR_OUTPUT_GUARD_FLUSHED']=true; if(ob_get_level()>0){ @ob_end_flush(); return true; } return false; }
}
