/**
  * Get the instance
  *
  * @return Yoast_GA_Dashboards
  */
 public static function get_instance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * Init function for the settings of GA
  */
 public function init_settings()
 {
     $this->options = $this->get_options();
     $this->api = Yoast_Api_Libs::load_api_libraries(array('google', 'googleanalytics'));
     $dashboards = Yoast_GA_Dashboards::get_instance();
     // Listener for reconnecting with google analytics
     $this->google_analytics_listener();
     if (is_null($this->get_tracking_code()) && $this->show_admin_warning()) {
         add_action('admin_notices', array('Yoast_Google_Analytics_Notice', 'config_warning'));
     }
     // Check if something has went wrong with GA-api calls
     $has_tracking_code = !is_null($this->get_tracking_code()) && empty($this->options['manual_ua_code_field']);
     if ($has_tracking_code && $this->show_admin_dashboard_warning()) {
         Yoast_Google_Analytics::get_instance()->check_for_ga_issues();
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         $this->handle_ga_post_request($dashboards);
     }
     /**
      * Show the notifications if we have one
      */
     $this->show_notification('ga_notifications');
     // Load the Google Analytics Dashboards functionality
     $dashboards->init_dashboards($this->get_current_profile());
 }
 /**
  * Ajax call: Fetch new data from Google Analytics
  */
 public function yst_ga_refresh_data()
 {
     check_ajax_referer('yst_ga_dashboards_ajax_nonce', 'security');
     Yoast_GA_Dashboards::get_instance()->aggregator->aggregate_data();
     /**
      * Add a notification
      */
     set_transient('yst-ga-premium-ajax-refresh-message', true, MINUTE_IN_SECONDS * 5);
     die('done');
 }
 /**
  * Loading the assets for dashboard
  */
 public static function enqueue_dashboard_assets()
 {
     wp_enqueue_script('ga-admin-dashboard', self::get_asset_path('assets/js/yoast_ga_admin_dashboard') . self::file_ext('.js'), array(), GAWP_VERSION);
     wp_enqueue_style('ga-admin-dashboard-css', self::get_asset_path('assets/css/yoast_ga_admin_dashboard') . self::file_ext('.css'), array(), GAWP_VERSION);
     // Enqueue the d3 js file
     wp_enqueue_script('d3_js', self::get_asset_path('assets/dependencies/rickshaw/d3.v3.min.js'), array(), GAWP_VERSION, true);
     // Enqueue the ricksaw js file
     wp_enqueue_script('rickshaw_js', self::get_asset_path('assets/dependencies/rickshaw/rickshaw.min.js'), array(), GAWP_VERSION, true);
     // Enqueue the rickshaw css
     wp_enqueue_style('rickshaw_css', self::get_asset_path('assets/dependencies/rickshaw/rickshaw.min.css'), array(), GAWP_VERSION);
     // Enqueue the datatables js file
     wp_enqueue_script('datatables_js', self::get_asset_path('assets/dependencies/datatables/js/jquery.dataTables.min.js'), array(), GAWP_VERSION, true);
     // Enqueue the datatables css
     wp_enqueue_style('datatables_css', self::get_asset_path('assets/dependencies/datatables/css/jquery.dataTables.min.css'), array(), GAWP_VERSION);
     Yoast_GA_Dashboards::get_instance()->add_dashboard_js_translations();
 }
 /**
  * Init function for the settings of GA
  */
 public function init_settings()
 {
     $this->options = $this->get_options();
     $this->api = Yoast_Api_Libs::load_api_libraries(array('google', 'googleanalytics'));
     $dashboards = Yoast_GA_Dashboards::get_instance();
     // Listener for reconnecting with google analytics
     $this->google_analytics_listener();
     if (is_null($this->get_tracking_code()) && $this->show_admin_warning()) {
         add_action('admin_notices', array('Yoast_Google_Analytics_Notice', 'config_warning'));
     }
     // Check if something has went wrong with GA-api calls
     $has_tracking_code = !is_null($this->get_tracking_code()) && empty($this->options['manual_ua_code_field']);
     if ($has_tracking_code && $this->show_admin_dashboard_warning()) {
         Yoast_Google_Analytics::get_instance()->check_for_ga_issues();
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (!function_exists('wp_verify_nonce')) {
             require_once ABSPATH . 'wp-includes/pluggable.php';
         }
         if (isset($_POST['ga-form-settings']) && wp_verify_nonce($_POST['yoast_ga_nonce'], 'save_settings')) {
             if (!isset($_POST['ignore_users'])) {
                 $_POST['ignore_users'] = array();
             }
             $dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled();
             if ($dashboards_disabled == false && isset($_POST['dashboards_disabled'])) {
                 $dashboards->reset_dashboards_data();
             }
             // Post submitted and verified with our nonce
             $this->save_settings($_POST);
         }
     }
     /**
      * Show the notifications if we have one
      */
     $this->show_notification('ga_notifications');
     // Load the Google Analytics Dashboards functionality
     $dashboards->init_dashboards($this->get_current_profile());
 }
 /**
  * Init function for the settings of GA
  */
 public function init_settings()
 {
     $this->options = $this->get_options();
     $this->api = Yoast_Api_Libs::load_api_libraries(array('google', 'googleanalytics'));
     // Listener for reconnecting with google analytics
     $this->google_analytics_listener();
     if (is_null($this->get_tracking_code()) && $this->show_admin_warning()) {
         add_action('admin_notices', array($this, 'config_warning'));
     }
     $last_run = get_option('yst_ga_last_wp_run');
     if ($last_run === false || Yoast_GA_Utils::hours_between(strtotime($last_run), time()) >= 48) {
         // Show error, something went wrong
         if (!is_null($this->get_tracking_code()) && empty($this->options['manual_ua_code_field']) && $this->show_admin_dashboard_warning()) {
             add_action('admin_notices', array($this, 'warning_fetching_data'));
         }
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (!function_exists('wp_verify_nonce')) {
             require_once ABSPATH . 'wp-includes/pluggable.php';
         }
         if (isset($_POST['ga-form-settings']) && wp_verify_nonce($_POST['yoast_ga_nonce'], 'save_settings')) {
             if (!isset($_POST['ignore_users'])) {
                 $_POST['ignore_users'] = array();
             }
             // Post submitted and verified with our nonce
             $this->save_settings($_POST);
         }
     }
     /**
      * Show the notifications if we have one
      */
     $this->show_notification('ga_notifications');
     // Load the Google Analytics Dashboards functionality
     $dashboards = Yoast_GA_Dashboards::get_instance();
     $dashboards->init_dashboards($this->get_current_profile());
 }
 /**
  * Handle the post requests in the admin form of the GA plugin
  *
  * @param Yoast_GA_Dashboards $dashboards
  */
 private function handle_ga_post_request($dashboards)
 {
     if (!function_exists('wp_verify_nonce')) {
         require_once ABSPATH . 'wp-includes/pluggable.php';
     }
     if (isset($_POST['ga-form-settings']) && wp_verify_nonce($_POST['yoast_ga_nonce'], 'save_settings')) {
         if (!isset($_POST['ignore_users'])) {
             $_POST['ignore_users'] = array();
         }
         $dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled();
         if ($dashboards_disabled == false && isset($_POST['dashboards_disabled']) || $this->ga_profile_changed($_POST)) {
             $dashboards->reset_dashboards_data();
         }
         // Post submitted and verified with our nonce
         $this->save_settings($_POST);
     }
 }
 /**
  * Check if the register items valid
  *
  * @covers Yoast_GA_Dashboards::register()
  */
 public function test_register()
 {
     $register = array('sessions', 'bounceRate');
     $result = $this->class_instance->register($register);
     $this->assertTrue($result);
 }
 /**
  * Ajax call: Fetch new data from Google Analytics
  */
 public static function yst_ga_refresh_data()
 {
     check_ajax_referer('yst_ga_dashboards_ajax_nonce', 'security');
     Yoast_GA_Dashboards::get_instance()->aggregator->aggregate_data();
     die('done');
 }