# firebox/1.0.0/Inc/Core/Admin/Analytics/MetricData.php

FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin &amp; Cart Abandonment, version 1.0.0. 261 lines.

- Page: https://pluginprobe.com/plugins/firebox/1.0.0/code/Inc/Core/Admin/Analytics/MetricData.php
- Raw: https://pluginprobe.com/plugins/firebox/1.0.0/raw/Inc/Core/Admin/Analytics/MetricData.php
- Modified: 2021-02-25T13:39:50+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/firebox/1.0.0/code/Inc/Core/Admin/Analytics/MetricData.php#L10-L20`.

```php
<?php
/**
 * @package         FireBox
 * @version         1.0.0 Free
 * 
 * @author          FirePlugins <info@fireplugins.com>
 * @link            https://www.fireplugins.com
 * @copyright       Copyright © 2021 FirePlugins All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

namespace FireBox\Core\Admin\Analytics;

if (!defined('ABSPATH'))
{
	exit; // Exit if accessed directly.
}

class MetricData
{
	/**
	 * Toolbar
	 * 
	 * @var  Toolbar
	 */
    private $toolbar;
    
	/**
	 * Toolbar Items
	 * 
	 * @var  array
	 */
	private $toolbar_items;

	/**
	 * Selected Toolbar items
	 * 
	 * @var  array
	 */
    private $selected_toolbar_items;

    /**
     * Analytics DB Object
     * 
     * @var  DB
     */
    private $db;

    /**
     * Metric
     * 
     * @var  Metric
     */
    private $metric;

    /**
     * Query Data that will be used to merge with
     * the currently used query data.
     * 
     * @var  array
     */
    private $modify_query_data;
	
	/**
	 * MetricData Constructor
	 * 
	 * @param   Metric   $metric
	 * @param   Toolbar  $toolbar
	 * @param   DB       $db
	 * 
	 * @return  void
	 */
	public function __construct($metric = null, $toolbar = null, $db = null)
	{
        $this->metric = $metric;
        
        if (!$toolbar)
        {
            $toolbar = new Toolbar();
        }
        $this->toolbar = $toolbar;

        if (!$db)
        {
            $db = new DB();
        }
        $this->db = $db;
        
        $this->toolbar_items = $this->toolbar->getToolbarItems();
        $this->selected_toolbar_items = $this->toolbar->getSelectedToolbarItems();
	}
	
	/**
	 * Returns the payload used to retrieve data for current period
	 * 
     * @param   mixed  $previous_period
     * 
	 * @return  array
	 */
    public function getResultsQueryData($previous_period = false)
    {
        $query_data = [];

        foreach ($this->selected_toolbar_items as $key => &$value)
        {
            if (empty($value))
            {
                continue;
            }

            $suffix = '';

            // get previous period
            if ($previous_period)
            {
                if ($key == 'date')
                {
                    $value = $this->toolbar_items['date']->getPreviousPeriodDateObject($value);
                }
                else
                {
                    $suffix .= '_prev_period';
                }
            }
            
            $method = $value['method'];

            $data = [
                'type' => $key,
                'method' => $method,
                'options_key' => $value['options_key'] . $suffix,
                'value' => isset($value['value']) ? $value['value'] : '',
                'previous_period' => $previous_period
            ];

            // get query data for each selected toolbar item
            $toolbarItemData = $this->toolbar_items[$key]->getQueryData($this->metric, $query_data, $data);

            /**
             * Get the manipulated query data from the toolbar item and use it.
             * The toolbar may alter the query data for its own purposes.
             */
            if (isset($toolbarItemData['query_data']))
            {
                // we receive the query data in case we have modified it within each toolbar item
                $query_data = $toolbarItemData['query_data'];

                unset($toolbarItemData['query_data']);
            }

            // merge the query data
            $query_data = array_merge_recursive($toolbarItemData, $query_data);
        }
        
        return $query_data;
    }

    /**
     * Get Current Metric Data
     * 
     * @return  int
     */
    public function getCurrent($payload = null)
    {
        if (!$payload)
        {
            $payload = $this->getResultsQueryData();
        }

        if (!isset($payload['select']))
        {
            return [];
        }

        if (isset($this->modify_query_data))
        {
            $payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true));
        }

        return $this->db->getResults($payload);
	}

    /**
     * Get Last Period Metric Data
     * 
     * @return  int
     */
    public function getLastPeriod($payload = null)
    {
        if (!$payload)
        {
            $payload = $this->getResultsQueryData(true);
        }

        if (!isset($payload['select']))
        {
            return [];
        }

        if (isset($this->modify_query_data))
        {
            $payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true));
        }

        return $this->db->getResults($payload);
	}
	
    /**
     * Get percentage change over previous period(month)
     * 
     * @param   int    $current_total
     * 
     * @return  float
     */
    public function getPercentageChangeWithPreviousPeriod($current_total)
    {
        if ($current_total <= 0)
        {
            return 0;
        }

        $previous_period = $this->toolbar_items['date']->getPreviousPeriodDateObject($this->selected_toolbar_items['date']);

        $this->selected_toolbar_items['date'] = $previous_period;
        
        $payload = $this->getResultsQueryData(true);

        $results = $this->db->getResults($payload);

        $previous_period_total = isset($results[0]->total) ? (int) $results[0]->total : 0;

        if ($previous_period_total <= 0)
        {
            return 0;
        }

        $percentage = (($current_total - $previous_period_total) / $previous_period_total) * 100;

        return $percentage;
    }

    public function setSelectedToolbarItems($items)
    {
        $this->selected_toolbar_items = $items;
    }

    public function getSelectedItems()
    {
        return $this->selected_toolbar_items;
    }

    public function setModifyQueryData($data)
    {
        $this->modify_query_data = $data;
    }

    public function getModifyQueryData()
    {
        return $this->modify_query_data;
    }
}
```
