File: /var/www/html/wp-content/plugins/bsx/ass/func.php
<?php
// ---------------------------------------------------------------------------------------
// get from campaign_id
function getStatus($id) {
$status = get_the_terms($id, 'campaign-status')[0]->name;
return $status;
}
function getTypes($id){
$ctypes = get_the_terms($id, 'campaign-type');
$ct = array();
if (!empty($ctypes) && !is_wp_error($ctypes)) {
$ctypes = bsx_order_hierarchy($ctypes);
foreach($ctypes as $x) $ct[] = $x->name;
}
return $ct;
}
function getRegions($id) {
$comp_id = get_field("company",$id)[0]->ID;
$geoterms = get_the_terms($comp_id, 'region');
$reg = array();
if (!empty($geoterms) && !is_wp_error($geoterms)) {
$wpregs = bsx_order_hierarchy($geoterms);
foreach($wpregs as $wpreg) $reg[] = $wpreg->name;
}
return $reg;
}
function getKpis($id) {
global $wpdb;
$kpis = $wpdb->get_row($wpdb->prepare(
"select * from wp_bsx_kpi where cid = %d", $id));
return $kpis;
}
function getYsym($id) {
$cntr = get_field("contract",$id);
$cntr_id = $cntr[0]->ID;
$ysym = get_field("ysym", $cntr_id);
return $ysym;
}
// ---------------------------------------------------------------------------------------
// get id from another
// possible are: camp, comp, cass, cntr
function getId($id, $give = 'camp_id', $get = 'cntr_id') {
global $wpdb;
$res = $wpdb->get_results($wpdb->prepare(
"select %i from x_rel where %i = %d", [$get,$give,$id])
);
return $res[0];
}
// ---------------------------------------------------------------------------------------
// get indirect data
// get reports belonging to a campaign_id ($id)
function getCampReps($id) {
$reps = get_posts(array(
'post_type'=>'report',
'posts_per_page'=> -1,
'meta_key' => 'published',
'orderby' => 'meta_value',
'order' => 'DESC'
));
$crps = [];
foreach($reps as $rep) {
$cmp = get_field("campaign",$rep->ID);
if($cmp[0]->ID != $id) continue;
$crps[] = $rep;
}
return $crps;
}
// get campaigns from one cassandra ($cass_id)
function getCassCamps($cass_id) {
$camps = get_posts(array(
'post_type'=>'campaign',
'post_status'=>'publish',
'posts_per_page'=> -1,
'meta_key' => 'publish_date',
'orderby' => 'meta_value',
'order' => 'DESC'
));
$cc = [];
foreach($camps as $cmp) {
if(empty($cmp)) continue;
$cass = get_field("cassandra",$cmp->ID);
if($cass[0]->ID != $cass_id) continue;
$cc[] = $cmp;
}
return $cc;
}
// get all reports of a cassandra
function getCassReps($cass_id) {
$rcs = getCassCamps($cass_id);
$args = array(
'post_type' => 'report',
'post_per_page' => -1,
'meta_query' => [
[
'key' => 'campaign',
'value' => array_column('ID',$rcs),
'compare' => 'IN'
]
]
);
$reps = get_posts($args);
return $reps;
}
// ==========================================================================================
function bsx_last_pub_date($id) {
$reps = get_posts(array(
'post_type'=>'report',
'posts_per_page'=> 1,
'meta_query'=>array(
array(
'key' => 'campaign',
'value' => '"' . $id . '"',
'compare' => 'LIKE'
),
),
'meta_key' => 'pub_date',
'orderby' => 'meta_value',
'order' => 'DESC'
));
$lpd = null;
if(!empty($reps)) $lpd = get_field('pub_date', $reps[0]->ID);
return $lpd;
}
// ==========================================================================================
// handle hierarchical terms
// Helper: Get hierarchical terms
function bsx_get_hierarchical_terms($taxonomy) {
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => 0,
));
if (!is_array($terms) || is_wp_error($terms)) {
return array();
}
$result = array();
foreach ($terms as $term) {
$result[] = array(
'id' => $term->term_id,
'name' => $term->name,
'parent' => 0,
'children' => bsx_get_child_terms($taxonomy, $term->term_id)
);
}
return $result;
}
function bsx_get_child_terms($taxonomy, $parent_id) {
$children = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $parent_id,
));
return array_map(function($term) {
return array(
'id' => $term->term_id,
'name' => $term->name,
'parent' => $term->parent
);
}, $children);
}
//
function bsx_order_hierarchy($h) {
if($h[0]->parent!=0) {
if( count($h) > 1) {
$h=array_reverse($h);
} else {
$h[1] = '';
}
}
return $h;
}
// ---------------------------------------------------------------------------------------
//
function enqueue_apex(){
// ApexCharts Script load
wp_enqueue_script(
'apexcharts',
'https://cdn.jsdelivr.net/npm/apexcharts@3.45.1/dist/apexcharts.min.js',
[],
'3.45.1',
true
);
}