forked from remyperona/google-analytics-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgad-admin-dashboard.php
124 lines (100 loc) · 5.08 KB
/
gad-admin-dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/* Copyright 2009 Carson McDonald ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once( dirname( __FILE__ ) . '/gad-admin-dashboard-ui.php' );
require_once( dirname( __FILE__ ) . '/gad-data-model.php' );
class GADAdminDashboard {
function GADAdminDashboard() {
$this->__construct();
}
function __construct() {
}
function register_for_actions_and_filters() {
add_action( 'wp_ajax_gad_set_preference', array( &$this, 'ajax_set_preference' ) );
add_action( 'wp_ajax_gad_fill_dp', array( &$this, 'fill_dashboard_placeholder' ) );
add_action( 'wp_dashboard_setup', array( &$this, 'register_dashboard_widget' ) );
add_filter( 'wp_dashboard_widgets', array( &$this, 'add_dashboard_widget' ) );
}
function ajax_set_preference() {
$current_user = $this->check_user_access();
if ( $current_user != null ) {
switch ( $_POST['pi'] ) {
case 'base-stats':
update_user_meta( $current_user->ID, 'gad_bs_toggle', $_POST['pv'] );
break;
case 'goal-stats':
update_user_meta( $current_user->ID, 'gad_gs_toggle', $_POST['pv'] );
break;
case 'extended-stats':
update_user_meta( $current_user->ID, 'gad_es_toggle', $_POST['pv'] );
break;
default:
die( "alert('Unknown option.')" );
}
}
die( "" ); // needed to end the process of returning the ajax data
}
function register_dashboard_widget() {
wp_register_sidebar_widget( 'dashboard_gad', __( 'Google Analytics Dashboard Widget', 'google-analytics-dashboard' ), array( &$this, 'create_dashboard_placeholder' ), array( 'width' => 'full', 'height' => 'single' ) );
}
function add_dashboard_widget( $widgets ) {
global $wp_registered_widgets;
$dashboard_display_level = get_option( 'gad_display_level' );
if ( ! isset( $wp_registered_widgets['dashboard_gad'] ) || ! current_user_can( $dashboard_display_level !== false ? $dashboard_display_level : 'manage_options' ) ) {
return $widgets;
}
array_splice( $widgets, sizeof( $widgets ) - 1, 0, 'dashboard_gad' );
return $widgets;
}
function create_dashboard_placeholder() {
if ( $this->check_user_access() != null ) {
echo '<div id="gad_dashboard_placeholder"><p class="hide-if-no-js">'.__('Loading…','google-analytics-dashboard').'</p><p class="describe hide-if-js">'.__('This widget requires JavaScript.','google-analytics-dashboard').'</p></div>';
}
}
function fill_dashboard_placeholder() {
$current_user = $this->check_user_access();
if ( $current_user != null ) {
$ui = new GADAdminDashboardUI();
$ui->ga_data = new GADDataModel();
if ( $ui->ga_data->fatal_error ) {
_e( 'Could not gather Google Analytics data.', 'google-analytics-dashboard' );
} else {
$bs_toggle_usermeta = get_user_meta( $current_user->ID, 'gad_bs_toggle' );
$bs_toggle_option = ! isset( $bs_toggle_usermeta ) || $bs_toggle_usermeta == '' ? get_option( 'gad_bs_toggle' ) : $bs_toggle_usermeta;
$bs_toggle_option = ! isset( $bs_toggle_option ) || $bs_toggle_option == '' ? 'hide' : $bs_toggle_option;
$gs_toggle_usermeta = get_user_meta( $current_user->ID, 'gad_gs_toggle' );
$gs_toggle_option = ! isset( $gs_toggle_usermeta ) || $gs_toggle_usermeta == '' ? get_option( 'gad_gs_toggle' ) : $gs_toggle_usermeta;
$gs_toggle_option = ! isset( $gs_toggle_option ) || $gs_toggle_option == '' ? 'hide' : $gs_toggle_option;
$es_toggle_usermeta = get_user_meta( $current_user->ID, 'gad_es_toggle' );
$es_toggle_option = ! isset( $es_toggle_usermeta ) || $es_toggle_usermeta == '' ? get_option( 'gad_es_toggle' ) : $es_toggle_usermeta;
$es_toggle_option = ! isset( $es_toggle_option ) || $es_toggle_option == '' ? 'hide' : $es_toggle_option;
$ui->display_all( $bs_toggle_option, $gs_toggle_option, $es_toggle_option );
}
}
die( "" ); // needed to end the process of returning the ajax data
}
function check_user_access() {
global $current_user;
get_currentuserinfo();
if ( get_option( 'gad_auth_token' ) === false || get_option( 'gad_account_id' ) === false ) {
if ( current_user_can( 'manage_options' ) ) {
printf( __( 'You need to log in and select an account in the %s$1options panel%s$2.', 'google-analytics-dashboard' ), '<a href="options-general.php?page=google-analytics-dashboard/gad-admin-options.php">', '</a>' );
} else {
_e( 'The administrator needs to log in and select a Google Analytics account.', 'google-analytics-dashboard' );
}
return null;
}
return $current_user;
}
}