File: /var/www/html/wp-content/plugins/bsx/be/bsx-rest-post.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 (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if($pt == 'contract') {
$ysym = get_field("ysym", $id);
$symex = explode('.',$ysym);
if(count($symex)==1) {
update_field("symbol",$symex[0],$id);
update_field("exch",'',$id);
} else {
update_field("symbol",$symex[0],$id);
update_field("exch",$symex[1],$id);
}
} else if($pt == 'campaign') {
$kpis = getKpis($id);
$pst = $wpdb->get_row($wpdb->prepare("select cid from wp_bsx_kpi where cid = %s",$id));
$kpis = ($pst->cid > 0) ? updateKpis($id) : initKpis($id);
}
// 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) {
global $wpdb;
$cassid = get_field("cassandra", $id)[0]->ID;
$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['mngd'] = get_field("managed", $id) ? 1:0;
$kpis['cass_id'] = $cassid;
$kpis['ls'] = $ls;
$kpis['pdt'] = get_gmt_from_date(get_field('publish_date',$id));
$kpis['edt'] = get_gmt_from_date(get_field('entry_date', $id));
$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) {
global $wpdb;
$c = [];
$c['cid'] = $id;
$c['proc'] = 'change';
$c['stat'] = getStatus($id);
$c['mngd'] = get_field("managed", $id) ? 1:0;
$c['pdt'] = get_gmt_from_date(get_field('publish_date',$id));
$c['edt'] = get_gmt_from_date(get_field('entry_date', $id));
$c['eprc'] = get_field("entry_price", $id);
$c['dt'] = current_time('mysql',true);
return $c;
}