-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgad-admin-pages-posts.php
142 lines (116 loc) · 5.18 KB
/
gad-admin-pages-posts.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/* Copyright 2009 Carson McDonald (carson@ioncannon.net)
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__ ) . '/ga-lib.php' );
require_once( dirname( __FILE__ ) . '/gauth-lib.php' );
require_once( dirname( __FILE__ ) . '/gad-admin-pages-posts-ui.php' );
class GADAdminPagesPosts {
function GADAdminPagesPosts() {
$this->__construct();
}
function __construct() {
}
function register_for_actions_and_filters() {
add_filter( 'manage_posts_columns', array( &$this, 'posts_pages_columns' ) );
add_filter( 'manage_pages_columns', array( &$this, 'posts_pages_columns' ) );
add_action( 'manage_pages_custom_column', array( &$this, 'posts_pages_custom_column' ), 10, 2 );
add_action( 'manage_posts_custom_column', array( &$this, 'posts_pages_custom_column' ), 10, 2 );
add_action( 'wp_ajax_gad_fill_ppp', array( &$this, 'fill_posts_pages_placeholder' ) );
}
function posts_pages_columns( $defaults ) {
$defaults['analytics'] = __( 'Analytics', 'google-analytics-dashboard' );
return $defaults;
}
function fill_posts_pages_placeholder() {
global $wpdb;
if ( ( $value = $this->security_check() ) !== true ) {
die( $value );
}
$post_id = intval( $_REQUEST['pid'] );
$count = intval( $_REQUEST['count'] );
// This is done to reduce the number of requests made at the same time
if ( $count % 2 == 0 ) {
usleep( 500000 );
}
if ( $count % 4 == 0 ) {
usleep( 500000 );
}
if ( get_option( 'gad_auth_token' ) == 'gad_see_oauth' ) {
$ga = new GALib( 'oauth', NULL, get_option( 'gad_oauth_token' ), get_option( 'gad_oauth_secret' ), get_option( 'gad_account_id' ), get_option( 'gad_cache_timeout' ) !== false ? get_option( 'gad_cache_timeout' ) : 60 );
} else {
$ga = new GALib( 'client', get_option( 'gad_auth_token' ), NULL, NULL, get_option( 'gad_account_id' ), get_option( 'gad_cache_timeout' ) !== false ? get_option( 'gad_cache_timeout' ) : 60 );
}
$link_value = get_permalink( $post_id );
$url_data = parse_url( $link_value );
$link_uri = substr( $url_data['path'] . ( isset( $url_data['query'] ) ? ( '?' . $url_data['query'] ) : '' ), - 20 );
$is_draft = $wpdb->get_var( $wpdb->prepare( "SELECT count(1) FROM $wpdb->posts WHERE post_status = 'draft' AND ID = %d", $post_id ) );
if ( $link_uri == '' || ( isset( $is_draft ) && $is_draft > 0 ) ) {
echo "";
} else {
$start_date = date( 'Y-m-d', time() - ( 60 * 60 * 24 * 30 ) );
$end_date = date( 'Y-m-d' );
$data = $ga->summary_by_partial_uri_for_date_period( $link_uri, $start_date, $end_date );
$error_type = gad_request_error_type( $ga );
if ( $error_type == 'perm' ) {
die( "Could not load data" );
} else {
if ( $error_type == 'retry' ) {
$data = $ga->summary_by_partial_uri_for_date_period( $link_uri, $start_date, $end_date );
}
}
$minvalue = 999999999;
$maxvalue = 0;
$pageviews = 0;
$exits = 0;
$uniques = 0;
$count = 0;
foreach ( $data as $date => $value ) {
if ( $minvalue > $value['ga:pageviews'] ) {
$minvalue = $value['ga:pageviews'];
}
if ( $maxvalue < $value['ga:pageviews'] ) {
$maxvalue = $value['ga:pageviews'];
}
$cvals .= $value['ga:pageviews'] . ( $count < sizeof( $data ) - 1 ? "," : "" );
$count ++;
$pageviews += $value['ga:pageviews'];
$exits += $value['ga:exits'];
$uniques += $value['ga:uniquePageviews'];
}
$ui = new GADAdminPagesPostsUI();
$ui->display_posts_pages_custom_column( $cvals, $minvalue, $maxvalue, $pageviews, $exits, $uniques );
}
die( "" );
}
function security_check() {
if ( get_option( 'gad_auth_token' ) === false || get_option( 'gad_account_id' ) === false ) {
if ( current_user_can( 'manage_options' ) ) {
return sprintf( __( 'You need to log in and select an account in the %1$soptions panel%2$s.', 'google-analytics-dashboard' ), '<a href="options-general.php?page=google-analytics-dashboard/gad-admin-options.php">', '</a>' );
} else {
return __( 'The administrator needs to log in and select a Google Analytics account.', 'google-analytics-dashboard' );
}
}
return true;
}
function posts_pages_custom_column( $column_name, $post_id ) {
if ( ( $value = $this->security_check() ) !== true ) {
echo $value;
} else {
if ( $column_name == 'analytics' ) {
echo '<div id="gad_ppp_' . $post_id . '" class="gad_ppp_loading"><p class="widget-loading hide-if-no-js">Loading…</p><p class="describe hide-if-js">' . __( 'This widget requires JavaScript.', 'google-analytics-dashboard' ) . '</p></div>';
}
}
}
}
?>