get_data( array( 'default' => true ) ) return ''; } public function additional_data() { return array(); } public function requirements( $error = false, $args = array(), $name = '' ) { return $error; } public function show_report( $args = array() ) { if ( ! current_user_can( 'exactmetrics_view_dashboard' ) ) { return exactmetrics_get_message( 'error', esc_html__( 'Access denied', 'google-analytics-dashboard-for-wp' ) ); } if ( exactmetrics_get_option( 'dashboard_disabled', false ) ) { if ( current_user_can( 'exactmetrics_save_settings' ) ) { $url = is_network_admin() ? network_admin_url( 'admin.php?page=exactmetrics_settings' ) : admin_url( 'admin.php?page=exactmetrics_settings' ); // Translators: Placeholders add a link to the settings panel. return exactmetrics_get_message( 'error', sprintf( esc_html__( 'Please %1$senable the dashboard%2$s to see report data.', 'google-analytics-dashboard-for-wp' ), '', '' ) ); } else { return exactmetrics_get_message( 'error', esc_html__( 'The dashboard is disabled.', 'google-analytics-dashboard-for-wp' ) ); } } if ( exactmetrics_is_pro_version() ) { if ( ! ExactMetrics()->license->has_license() ) { $url = is_network_admin() ? network_admin_url( 'admin.php?page=exactmetrics_settings' ) : admin_url( 'admin.php?page=exactmetrics_settings' ); // Translators: Placeholders add a link to the settings panel. return exactmetrics_get_message( 'error', esc_html__( 'You do not have an active license. Please %1$scheck your license configuration.%2$s', 'google-analytics-dashboard-for-wp' ), '', '' ); } else if ( ExactMetrics()->license->license_has_error() ) { return exactmetrics_get_message( 'error', $this->get_license_error() ); } } if ( ! ( ExactMetrics()->auth->is_authed() || ExactMetrics()->auth->is_network_authed() ) ) { if ( current_user_can( 'exactmetrics_save_settings' ) ) { $url = is_network_admin() ? network_admin_url( 'admin.php?page=exactmetrics_settings' ) : admin_url( 'admin.php?page=exactmetrics_settings' ); // Translators: Placeholders add a link to the settings panel. return exactmetrics_get_message( 'error', sprintf( esc_html__( 'Please %1$sauthenticate %2$swith Google Analytics to allow the plugin to fetch data.', 'google-analytics-dashboard-for-wp' ), '', '' ) ); } else { return exactmetrics_get_message( 'error', esc_html__( 'The Google oAuth authentication needs to be re-authenticated to view data.', 'google-analytics-dashboard-for-wp' ) ); } } if ( exactmetrics_is_pro_version() ) { if ( ! ExactMetrics()->license->license_can( $this->level ) ) { return $this->get_upsell_notice(); } } $error = $this->requirements( false, array(), $this->name ); if ( ! empty( $error ) ) { return exactmetrics_get_message( 'error', $error ); } if ( ! empty( $args['error'] ) ) { return exactmetrics_get_message( 'error', $args['error'] ); } if ( empty( $args['data'] ) || ! is_array( $args['data'] ) ) { if ( exactmetrics_is_pro_version() ) { return ''; } else { // Try to get default data. $args = $this->get_data( array( 'default' => true ) ); if ( empty( $args['data'] ) || is_array( $args['data'] ) ) { return exactmetrics_get_message( 'error', __( 'No data found', 'google-analytics-dashboard-for-wp' ) ); } if ( ! empty( $args['error'] ) ) { return exactmetrics_get_message( 'error', $args['error'] ); } } } return $this->get_report_html( $args['data'] ); } // Deletes the report data from the cache public function delete_cache( $where = 'site' ) { if ( $where === 'site' || $where === 'both' ) { delete_option( 'exactmetrics_report_data_' . $this->name ); } if ( $where === 'network' || $where === 'both' ) { delete_option( 'exactmetrics_network_report_data_' . $this->name ); } } // Get report data public function get_data( $args = array() ) { if ( ! empty( $args['default'] ) ) { $args['start'] = $this->default_start_date(); $args['end'] = $this->default_end_date(); } $start = ! empty( $args['start'] ) && $this->is_valid_date( $args['start'] ) ? $args['start'] : ''; $end = ! empty( $args['end'] ) && $this->is_valid_date( $args['end'] ) ? $args['end'] : ''; if ( exactmetrics_is_pro_version() && ! ExactMetrics()->license->license_can( $this->level ) ) { return array( 'success' => true, 'upgrade' => true, 'data' => array(), ); } if ( ! $this->is_valid_date_range( $start, $end ) ) { return array( 'success' => false, 'error' => __( 'Invalid date range.', 'google-analytics-dashboard-for-wp' ), 'data' => array(), ); } if ( ( $start !== $this->default_start_date() || $end !== $this->default_end_date() ) && ! exactmetrics_is_pro_version() ) { $start = $this->default_start_date(); $end = $this->default_end_date(); // return array( // 'success' => false, // 'error' => __( 'Please upgrade to ExactMetrics Pro to use custom date ranges.', 'google-analytics-dashboard-for-wp' ), // 'data' => array(), // ); } $error = apply_filters( 'exactmetrics_reports_abstract_get_data_pre_cache', false, $args, $this->name ); if ( $error ) { return apply_filters( 'exactmetrics_reports_handle_error_message', array( 'success' => false, 'error' => $error, 'data' => array(), ) ); } $check_cache = ( $start === $this->default_start_date() && $end === $this->default_end_date() ) || apply_filters( 'exactmetrics_report_use_cache', false, $this->name ); $site_auth = ExactMetrics()->auth->get_viewname(); $ms_auth = is_multisite() && ExactMetrics()->auth->get_network_viewname(); $transient = 'exactmetrics_report_' . $this->name . '_' . $start . '_' . $end; // Set to same time as MI cache. MI caches same day to 15 and others to 1 day, so there's no point pinging MI before then. $expiration = date( 'Y-m-d' ) === $end ? apply_filters( 'exactmetrics_report_transient_expiration', 15 * MINUTE_IN_SECONDS, $this->name ) : HOUR_IN_SECONDS; // Default date range, check. if ( $site_auth || $ms_auth ) { // Single site or MS with auth at subsite $option_name = $site_auth ? 'exactmetrics_report_data_' . $this->name : 'exactmetrics_network_report_data_' . $this->name; $p = $site_auth ? ExactMetrics()->auth->get_viewid() : ExactMetrics()->auth->get_network_viewid(); $data = array(); if ( $check_cache ) { $data = ! $site_auth && $ms_auth ? get_site_option( $option_name, array() ) : get_option( $option_name, array() ); } else { $data = ! $site_auth && $ms_auth ? get_site_transient( $transient ) : get_transient( $transient ); } if ( ! empty( $data ) && ! empty( $data['expires'] ) && $data['expires'] >= time() && ! empty( $data['data'] ) && ! empty( $data['p'] ) && $data['p'] === $p ) { return $this->prepare_report_data( array( 'success' => true, 'data' => $data['data'], ) ); } // Nothing in cache, either not saved before, expired or mismatch. Let's grab from API $api_options = array( 'start' => $start, 'end' => $end ); if ( ! $site_auth && $ms_auth ) { $api_options['network'] = true; } $api = new ExactMetrics_API_Request( 'analytics/reports/' . $this->name . '/', $api_options, 'GET' ); // Use a report source indicator for requests. if ( ! empty( $this->source ) ) { $api->set_additional_data( array( 'source' => $this->source, ) ); } $additional_data = $this->additional_data(); if ( ! empty( $additional_data ) ) { $api->set_additional_data( $additional_data ); } $ret = $api->request(); if ( is_wp_error( $ret ) ) { return array( 'success' => false, 'error' => $ret->get_error_message(), 'data' => array(), ); } else { // Success $data = array( 'expires' => time() + $expiration, 'p' => $p, 'data' => $ret['data'], ); if ( $check_cache ) { ! $site_auth && $ms_auth ? update_site_option( $option_name, $data ) : update_option( $option_name, $data ); } else { ! $site_auth && $ms_auth ? set_site_transient( $transient, $data, $expiration ) : set_transient( $transient, $data, $expiration ); } return $this->prepare_report_data( array( 'success' => true, 'data' => $ret['data'], ) ); } } else { return array( 'success' => false, 'error' => __( 'You must authenticate with ExactMetrics to use reports.', 'google-analytics-dashboard-for-wp' ), 'data' => array(), ); } } public function default_start_date() { return date( 'Y-m-d', strtotime( '-30 days' ) ); } public function default_end_date() { return date( 'Y-m-d', strtotime( '-1 day' ) ); } // Checks to see if date range is valid. Should be 30-yesterday always for lite & any valid date range to today for Pro. public function is_valid_date_range( $start, $end ) { $start = strtotime( $start ); $end = strtotime( $end ); if ( $start > strtotime( 'now' ) || $end > strtotime( 'now' ) || $start < strtotime( '01 January 2005' ) || $end < strtotime( '01 January 2005' ) ) { return false; } // return false if the start date is after the end date return ( $start > $end ) ? false : true; } // Is a valid date value public function is_valid_date( $date = '' ) { $d = ExactMetricsDateTime::createFromFormat( 'Y-m-d', $date ); return $d && $d->format( 'Y-m-d' ) === $date; } /** * Do not use the functions below this. They are unused and are just here so people * with out of date ExactMetrics addons won't get fatal errors. */ protected function get_api_max_limit() { return 300; } protected function get_date_range() { return array(); } public function get_upsell_notice() { $has_level = exactmetrics_is_pro_version() ? ExactMetrics()->license->get_license_type() : false; $has_level = $has_level ? $has_level : 'lite'; // Translators: Placeholders add the license level and the report title. $message = sprintf( __( 'You currently have a %1$s level license, but this report requires at least a %2$s level license to view the %3$s. Please upgrade to view this report.', 'google-analytics-dashboard-for-wp' ), $has_level, $this->level, $this->title ); ob_start(); ?>
' ); ?>
' . $this->title, '', '' . $this->level, '' ); ?>
name . '-report-upsell-license-link', 'https://exactmetrics.com/my-account/' ) . '">', '', '', '' ); ?>
' ); ?>
' . $this->title, '', '' . $this->level, '' ); ?>
name . '-report-upsell-license-link' ) . '">', '', '', '' ); ?>