get_post_types ();
add_filter ( 'posts_where', array ($this, 'date_range_filter' ) );
$querydata = new WP_Query ( array ('posts_per_page' => - 1, 'post_status' => 'publish', 'post_type' => $post_types ) );
remove_filter ( 'posts_where', array ($this, 'date_range_filter' ) );
$data = array ();
// foreach ($querydata as $querydatum ) {
if ($querydata->have_posts ()) {
while ( $querydata->have_posts () ) {
$querydata->the_post ();
global $post;
$item ['ID'] = $post->ID;
$item ['post_title'] = $post->post_title;
$item ['post_date'] = $post->post_date;
$item ['metrics_data'] = get_post_meta ( $post->ID, "essb_metrics_data", true );
$item ['permalink'] = get_permalink ( $post->ID );
if (is_array($item['metrics_data'])) {
$data[] = $item;
}
}
}
$this->data = $data;
$this->prepare_networks();
}
public function prepare_networks() {
foreach ($this->data as $post_data) {
if (count($post_data['metrics_data']) == 0) {
continue;
}
$network_counts = array();
$last_id = count($post_data['metrics_data']) - 1;
if (is_array($post_data['metrics_data'][$last_id])) {
foreach ($post_data['metrics_data'][$last_id] as $service => $value) {
if ($service == 'total' || $service == 'date') { continue; }
if (!in_array($service, $this->networks)) {
$this->networks[] = $service;
$this->network_totals[$service] = 0;
$this->past_network_totals[$service] = 0;
$this->top_content[$service] = array('value' => 0, 'title' => '', 'permalink' => '');
$network_counts[$service] = array('current' => 0, 'past' => 0);
}
$this->network_totals[$service] += intval($value);
$network_counts[$service]['current'] = intval($value);
if (isset($this->top_content[$service])) {
if ($value > $this->top_content[$service]['value']) {
$this->top_content[$service]['title'] = $post_data['post_title'];
$this->top_content[$service]['permalink'] = $post_data['permalink'];
$this->top_content[$service]['value'] = $value;
}
}
}
}
if (is_array($post_data['metrics_data'][0])) {
foreach ($post_data['metrics_data'][0] as $service => $value) {
if ($service == 'total' || $service == 'date') {
continue;
}
if (!in_array($service, $this->networks)) {
$this->networks[] = $service;
$this->network_totals[$service] = 0;
$this->past_network_totals[$service] = 0;
$network_counts[$service] = array('current' => 0, 'past' => 0);
}
$this->past_network_totals[$service] += intval($value);
$network_counts[$service]['past'] = intval($value);
}
foreach ($network_counts as $service => $data) {
$diff = intval(isset($data['current']) ? $data['current'] : 0) - intval(isset($data['past']) ? $data['past'] : 0);
if (!isset($this->trending_content[$service])) {
$this->trending_content[$service] = array('value' => 0, 'title' => '', 'permalink' => '');
}
if ($diff > $this->trending_content[$service]['value']) {
$this->trending_content[$service]['title'] = $post_data['post_title'];
$this->trending_content[$service]['permalink'] = $post_data['permalink'];
$this->trending_content[$service]['value'] = $diff;
}
}
}
}
$total = 0;
foreach ($this->network_totals as $network => $value) {
$total += intval($value);
}
$this->network_totals['total'] = $total;
$total = 0;
foreach ($this->past_network_totals as $network => $value) {
$total += intval($value);
}
$this->past_network_totals['total'] = $total;
arsort($this->network_totals);
// creating trending posts
$this->trending_content[$service] = array('value' => 0, 'title' => '', 'permalink' => '');
}
function date_range_filter($where = '') {
$range = (isset ( $_GET ['range'] )) ? $_GET ['range'] : '0';
if ($range <= 0)
return $where;
$range_bottom = " AND post_date >= '" . date ( "Y-m-d", strtotime ( '-' . $range . ' month' ) );
$range_top = "' AND post_date <= '" . date ( "Y-m-d" ) . "'";
$where .= $range_bottom . $range_top;
return $where;
}
public function get_post_types() {
$types_to_track = array ();
$pts = get_post_types ( array ('public' => true, 'show_ui' => true, '_builtin' => true ) );
$cpts = get_post_types ( array ('public' => true, 'show_ui' => true, '_builtin' => false ) );
// classical post type listing
foreach ( $pts as $pt ) {
$types_to_track [] = $pt;
}
// custom post types listing
if (is_array ( $cpts ) && ! empty ( $cpts )) {
foreach ( $cpts as $cpt ) {
$types_to_track [] = $cpt;
}
}
return $types_to_track;
}
public function network_name($key) {
$all_networks = essb_available_social_networks();
$name = '';
foreach ($all_networks as $network => $data ) {
$network_name = isset($data['name']) ? $data['name'] : $network;
if ($network == $key) {
$name = $network_name;
break;
}
}
return $name;
}
public function compare_value_change_class($past_value, $new_value) {
$diff = intval($new_value) - intval($past_value);
$result = '';
if ($diff > 0) {
$result = 'up-bg';
}
if ($diff < 0) {
$result = 'down-bg';
}
return $result;
}
public function compare_value_change($past_value, $new_value, $plain_output = false) {
$diff = intval($new_value) - intval($past_value);
$result = '';
if ($diff > 0) {
$result = '('.number_format($diff).')';
}
if ($diff < 0) {
$result = '('.number_format($diff).')';
}
if ($diff == 0) {
$result = '(0)';
}
if ($plain_output) {
$result = number_format($diff);
}
return $result;
}
public function output_total_content() {
echo '
';
echo '';
echo '';
echo '';
foreach ($this->networks as $key) {
if (!isset($this->top_content[$key])) {
continue;
}
$single_value = $this->top_content[$key]['value'];
$title = $this->top_content[$key]['title'];
$permalink = $this->top_content[$key]['permalink'];
echo '';
echo ''.$this->network_name($key).' | ';
echo ''.number_format($single_value).' | ';
echo ''.$title.' | ';
echo '
';
}
echo '
';
}
public function output_trending_content() {
echo '';
echo '';
echo '';
echo '';
foreach ($this->networks as $key) {
if (!isset($this->trending_content[$key])) {
continue;
}
$single_value = $this->trending_content[$key]['value'];
$title = $this->trending_content[$key]['title'];
$permalink = $this->trending_content[$key]['permalink'];
echo '';
echo ''.$this->network_name($key).' | ';
echo ''.number_format($single_value).' | ';
echo ''.$title.' | ';
echo '
';
}
echo '
';
}
public function output_total_result_modern() {
$total = $this->network_totals['total'];
$past_total = $this->past_network_totals['total'];
$key = 'total';
?>
network_totals as $key => $value) {
if ($key == 'total') {
continue;
}
$past_value = isset($this->past_network_totals[$key]) ? $this->past_network_totals[$key] : 0;
$single_value = $value;
if ($total != 0) {
$display_percent = number_format($single_value * 100 / $total, 2);
$percent = number_format($single_value * 100 / $total);
}
else {
$display_percent = "0.00";
$percent = "0";
}
if (intval($percent) == 0 && intval($single_value) != 0) {
$percent = 1;
}
?>
network_totals['total'];
$past_total = $this->past_network_totals['total'];
echo '';
echo '';
echo '';
echo '';
echo '';
echo 'TOTAL: | ';
echo ''.number_format($total). $this->compare_value_change($past_total, $total) . ' | ';
echo ' | ';
echo '
';
foreach ($this->network_totals as $key => $value) {
if ($key == 'total') { continue; }
$past_value = isset($this->past_network_totals[$key]) ? $this->past_network_totals[$key] : 0;
$single_value = $value;
if ($total != 0) {
$display_percent = number_format($single_value * 100 / $total, 2);
$percent = number_format($single_value * 100 / $total);
}
else {
$display_percent = "0.00";
$percent = "0";
}
if (intval($percent) == 0 && intval($single_value) != 0) {
$percent = 1;
}
echo '';
echo ''.$this->network_name($key).' '.$display_percent.' % | ';
echo ''.number_format($single_value). $this->compare_value_change($past_value, $value) .' | ';
echo ' | ';
echo '
';
}
echo '
';
}
public function create_array_total($data) {
$total = 0;
if (is_array($data)) {
foreach ($data as $key => $value) {
if ($key == 'total' || $key == 'date') { continue; }
$total += intval($value);
}
}
return $total;
}
public function output_main_result() {
echo '';
echo '';
echo '';
echo 'Post | ';
echo 'Total | ';
foreach ($this->networks as $key) {
echo ''.$this->network_name($key).' | ';
}
echo '
';
echo '';
echo ' | ';
echo ' | ';
foreach ($this->networks as $key) {
echo ' | ';
echo ' | ';
}
echo '
';
echo '';
echo '';
foreach ($this->data as $item) {
if (count($item['metrics_data']) == 0) {
continue;
}
$last_id = count($item['metrics_data']) - 1;
$data = $item['metrics_data'][$last_id];
$past_data = $item['metrics_data'][0];
$total = $this->create_array_total($data);
$past_total = $this->create_array_total($past_data);
if (intval($total) == 0) {
continue;
}
echo '';
echo ''.$item['post_title'].' (id:'.$item['ID'].', last update: '.$data['date'].') '.sprintf('Open edit post to see historical data',$item['ID'],'edit',$item['ID']) .' | ';
echo ''.number_format($total).' | ';
echo ''.$this->compare_value_change($past_total, $total, true).' | ';
foreach ($this->networks as $key) {
$current_value = isset($data[$key]) ? $data[$key] : 0;
$past_value = isset($past_data[$key]) ? $past_data[$key] : 0;
echo ''.number_format($current_value).' | ';
echo ''.$this->compare_value_change($past_value, $current_value, true).' | ';
}
echo '
';
}
/*foreach ($this->data_report as $item) {
$total_value = number_format(intval($item['esml_socialcount_total']));
$item_actions = sprintf('Edit Post',$item['ID'],'edit',$item['ID']) .
' Update Stats ' .
sprintf('Updated %s',EasySocialMetricsLite::timeago($item['esml_socialcount_LAST_UPDATED']));
echo '';
echo '';
printf('%1$s (id:%2$s) %3$s',
$item['post_title'],
$item['ID'],
$item_actions);
echo ' | ';
echo ''.$total_value.' | ';
foreach ($this->services as $key => $text) {
if ($key == "diggs") {
continue;
}
echo ''.number_format(intval($item['esml_socialcount_'.$key])).' | ';
}
echo '
';
}*/
echo '';
echo '
';
}
}