/**
  * Set the base filename of the plugin.
  *
  * @since    1.0.1
  * @access   public
  */
 public function set_base_file($basefile)
 {
     self::$BASEFILE = $basefile;
 }
 /**
  * The main dashboard function.
  *
  * Query the CloudFlare API, output all the data to the dashboard widget.
  *
  * @since    1.0.0
  */
 public function display_dashboard_widget()
 {
     $this->plugin_options = get_option(CMD_Analytics_For_Cloudflare::PLUGIN_ID . "_settings");
     // Available dropdown display items
     $time_options = array('-1440' => __('Last 24 hours', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN), '-10080' => __('Last week', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN), '-43200' => __('Last month', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN));
     $display_options = array('requests' => __('Requests', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN), 'pageviews' => __('Pageviews', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN), 'uniques' => __('Unique Visitors', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN), 'bandwidth' => __('Bandwidth', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN));
     //Default - last week / requests
     $current_time = '-10080';
     $current_type = 'pageviews';
     //Check if the form was submitted to change the view
     if (isset($_REQUEST['cmd_analytics_for_cloudflare_dashboard_range']) && array_key_exists($_REQUEST['cmd_analytics_for_cloudflare_dashboard_range'], $time_options)) {
         $current_time = $_REQUEST['cmd_analytics_for_cloudflare_dashboard_range'];
     }
     if (isset($_REQUEST['cmd_analytics_for_cloudflare_dashboard_type']) && array_key_exists($_REQUEST['cmd_analytics_for_cloudflare_dashboard_type'], $display_options)) {
         $current_type = $_REQUEST['cmd_analytics_for_cloudflare_dashboard_type'];
     }
     //Set our caching options
     $options = get_option(CMD_Analytics_For_Cloudflare::PLUGIN_ID . "_settings");
     $cache_time = isset($this->plugin_options['cache_time']) ? $this->plugin_options['cache_time'] : '900';
     //Check our transiant for the analytics object for our current view, if not found then pull the data
     if ($cache_time == 0 || false === ($analytics = get_transient('cmd_afc_results_' . $current_time . '_' . $current_type))) {
         require_once 'class-analytics-for-cloudflare-api.php';
         $cloudflare = new CMD_Analytics_For_Cloudflare_Api();
         $analytics = $cloudflare->get_analytics(array('since' => $current_time));
         //If we encounter an error, show it and don't cache
         if (is_wp_error($analytics)) {
             echo '<h3 class=>' . __('Unable to connect to CloudFlare', CMD_Analytics_For_Cloudflare::TEXT_DOMAIN) . '</h3>';
             echo '<p>' . $analytics->get_error_message();
             '</p>';
             echo '<p>' . __(sprintf('View the %sCloudFlare For Analytics settings%s', '<a href="' . admin_url('options-general.php?page=cmd_analytics_for_cloudflare') . '">', '</a>'), CMD_Analytics_For_Cloudflare::TEXT_DOMAIN) . '</p>';
             return;
         }
         //Set the transient cache for the current view
         set_transient('cmd_afc_results_' . $current_time . '_' . $current_type, $analytics, $cache_time);
     }
     // Initialize all the javascript data
     $this->parse_analytics_to_js($analytics, $current_type, $current_time, $display_options);
     do_action('cmd_analytics_for_cloudflare_before_dashboard');
     // Render the display from the dashboard template file
     echo CMD_Analytics_For_Cloudflare::render_template('admin/cmd-afc-dashboard-widget.php', array('time_options' => $time_options, 'current_time' => $current_time, 'display_options' => $display_options, 'current_type' => $current_type, 'analytics' => $analytics));
     do_action('cmd_analytics_for_cloudflare_after_dashboard');
 }
/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_cmd_analytics_for_cloudflare()
{
    $plugin = new CMD_Analytics_For_Cloudflare();
    $plugin->set_base_file(__FILE__);
}