HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux bsx-1-dev 6.8.0-101-generic #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 x86_64
User: www-data (33)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: //proc/3597422/root/home/gpv/sync_changes.php
<?php




add_action('acf/save_post', 'sync_changes', 10, 2);

function sync_changes($id) {
    global $wpdb;
    if (wp_is_post_revision($id)) return;
    $pt = get_post_type($id);
    if($pt != 'campaign') return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    $kpis = getKpis($id);
    $utcd = set_acf_utc($id);
    
    $pst = $wpdb->get_row($wpdb->prepare("select cid from wp_bsx_kpi where cid = %s",$id));
    error_log(print_r($pst,true));
    $kpis = ($pst->cid > 0) ?  updateKpis($id,$utcd) : initKpis($id,$utcd); 
    

    // POST to Backend
    $response = wp_remote_post(BACKEND_API_URL . '/changes', array(
        'headers' => array(
            'X-API-Key' => BACKEND_API_KEY,
            'Content-Type' => 'application/json'
        ),
        'body' => json_encode($kpis),
        'timeout' => 30
    ));


    sleep(1);
}


function initKpis($id,$utcd) {
    global $wpdb;

    $cassid = get_field("cassandra", $id)[0]->ID;
    error_log($cassid);

    $kpis = [];
    if(getTypes($id)[0]=="Long") {
        $ls = 1;
    } else if (getTypes($id)[0]=="Short"){
        $ls = -1;
    } else {
        $ls = 0;
    }

    $kpis['cid']      = $id;
    $kpis['proc']     = 'new';
    $kpis['ysym']     = getYsym($id);

    $kpis['stat']     = getStatus($id);
    $kpis['cass_id']  = $cassid;
    $kpis['ls']       = $ls;
        
    $kpis['pdt']      = $utcd['publish_date'];
    $kpis['edt']      = get_field('entry_date');
    $kpis['eprc']     = get_field("entry_price", $id);
    $kpis['cdt']      = $kpis['edt'];
    $kpis['cprc']     = $kpis['eprc'];

    $kpis['tenure']   = 0;
    $kpis['dt']       = current_time('mysql',true);
        
    $kpis['perf']     = 0;
    $kpis['perf_oe']  = 0;

    return $kpis;
}


function updateKpis($id,$utcd) {
    global $wpdb;

    $c = [];
    $c['cid']      = $id;
    $c['proc']     = 'change';
    $c['stat']     = getStatus($id);
    $c['pdt']      = $utcd['publish_date'];
    $c['edt']      = $utcd['entry_date'];
    $c['eprc']     = get_field("entry_price", $id);
    $c['dt']       = current_time('mysql',true);

    return $c;
}




function set_acf_utc($id) {

    $fields = ["entry_date", "publish_date"];
    $utc_dates = [];
    error_log("utc function");
    foreach($fields as $fld) {
        // ACF returns in WP local time (Europe/Berlin)
        $date_local = get_field( $fld, $id );
        // Convert to UTC right here, before any further use
        $wp_tz = new DateTimeZone( get_option('timezone_string') ?: '+00:00' );
        $utc_tz = new DateTimeZone( 'UTC' );

        $date_utc = ( new DateTime( $date_local, $wp_tz ) )
            ->setTimezone( $utc_tz )
            ->format( 'Y-m-d H:i:s' );
        $utc_dates[$fld] = $date_utc;
    }
    return $utc_dates;
}