| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.8 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Admin\Analytics\Overview\Items; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Boxestable extends Table |
| 20 |
{ |
| 21 |
public function parseData(&$metric_data) |
| 22 |
{ |
| 23 |
if (!count($metric_data['current_period']['data'])) |
| 24 |
{ |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$last_period_data = isset($metric_data['last_period']['data']) ? $metric_data['last_period']['data'] : []; |
| 29 |
|
| 30 |
$metrics = ['impressions']; |
| 31 |
|
| 32 |
// add the box toolbar item to the list of toolbar items that will be used in the link on each box label so we can directly |
| 33 |
// click on it and see analytics for this specific box |
| 34 |
$toolbar_items = $metric_data['toolbar_items']; |
| 35 |
$toolbar_items['box'] = [ |
| 36 |
'method' => 'filter', |
| 37 |
'options_key' => 'box' |
| 38 |
]; |
| 39 |
|
| 40 |
foreach ($metric_data['current_period']['data'] as $key => &$value) |
| 41 |
{ |
| 42 |
$value = (array) $value; |
| 43 |
$label = $value['label']; |
| 44 |
$total = $value['total']; |
| 45 |
|
| 46 |
$value['total'] = number_format($total, 0); |
| 47 |
|
| 48 |
$last_period_item = array_filter( |
| 49 |
$last_period_data, |
| 50 |
function ($e) use ($label, $value) { |
| 51 |
return $e->box_id == $value['box_id']; |
| 52 |
} |
| 53 |
); |
| 54 |
$last_period_item = array_values($last_period_item); |
| 55 |
$last_period_total = isset($last_period_item[0]->total) ? $last_period_item[0]->total : 0; |
| 56 |
|
| 57 |
$difference = $last_period_total ? (($total - $last_period_total) / $last_period_total) * 100 : 0; |
| 58 |
$prefix = $difference > 0 ? '+' : ''; |
| 59 |
|
| 60 |
$value['difference']['label'] = $prefix . number_format($difference, 0) . '%'; |
| 61 |
$value['difference']['class'] = $difference > 0 ? 'plus' : ($difference < 0 ? 'minus' : ''); |
| 62 |
|
| 63 |
$toolbar_items['box']['value'] = $value['box_id']; |
| 64 |
$value['label'] = []; |
| 65 |
$value['label']['label'] = $label; |
| 66 |
$value['label']['link'] = FBOX_LICENSE_TYPE === 'pro' ? admin_url('admin.php?page=firebox-analytics&toolbar_items=' . urlencode(json_encode($toolbar_items)) . '&metrics=' . urlencode(json_encode($metrics))) : ''; |
| 67 |
|
| 68 |
// Remove the box_id key,value as we don't want it to appear on the table. |
| 69 |
// It's used only to set the URL for each box in the table |
| 70 |
unset($value['box_id']); |
| 71 |
} |
| 72 |
} |
| 73 |
} |