-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayTop10PostsWidgets.php
More file actions
282 lines (246 loc) · 8.63 KB
/
Copy pathDisplayTop10PostsWidgets.php
File metadata and controls
282 lines (246 loc) · 8.63 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @package Top 10 Posts
* @version 0.1
*/
/*
Plugin Name:Display Top 10 Posts Widget
Plugin URI: http://notavailable/
Description: This is a plugin that displays the Top 10 posts in 7 day(s) , 1 month(s) , and 1 year(s)
Author: Arun G
Version: 0.1
Author URI: http://aruncyberspace.blogspot.in/
*/
/*imports*/
require 'gapi.class.php';
class Display_Top10_Posts_Widget extends WP_Widget {
public function __construct() {
// widget actual processes
/* Widget control settings. */
/* Widget settings. */
$widget_ops = array( 'classname' => 'Display_Top10_Posts_Widget', 'description' => 'Displays Top 10 Posts' );
/* Widget control settings. */
//$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'Display-Top-10-Posts-Widget' );
/* Create the widget. */
$this->WP_Widget( 'Display-Top-10-Posts-Widget', 'Display Top 10 Posts Widget', $widget_ops, $control_ops );
// parent::WP_Widget(false,'Display Top 10 Posts Widget','description=Description Widget');
}
public function form( $instance ) {
// outputs the options form on admin
// echo 'include html coding in here';
//for Google Analytics Email
if ( isset( $instance[ 'google_analytics_email' ] ) )
{
$gaEmail = $instance[ 'google_analytics_email' ];
}
else
{
$gaEmail ='';
}
//for Google Analytics Email Password
if ( isset( $instance[ 'google_analytics_email_password' ] ) )
{
$gaPassword = $instance[ 'google_analytics_email_password' ];
}
else
{
$gaPassword ='';
}
//for Google Analytics Profile Id
if ( isset( $instance[ 'google_analytics_ProfileId' ] ) )
{
$gaProfileId = $instance[ 'google_analytics_ProfileId' ];
}
else
{
$gaProfileId ='';
}
//for Google Analytics Max Results
if ( isset( $instance[ 'google_analytics_Max_Results' ] ) )
{
$gaMaxResults = $instance[ 'google_analytics_Max_Results' ];
}
else
{
$gaMaxResults ='';
}
//for Google Analytics Time Duration
if ( isset( $instance[ 'google_analytics_Time_Duration' ] ) )
{
$gaTimeDuration = $instance[ 'google_analytics_Time_Duration' ];
}
else
{
$gaTimeDuration ='';
}
$day_select="";
$month_select="";
$year_select="";
switch ($gaTimeDuration) {
case "7 DAY":
$day_select=" Selected ";
break;
case "1 MONTH":
$month_select=" Selected ";
break;
case "1 YEAR":
$year_select=" Selected ";
break;
}
//for Google Analytics Email
echo '<p>';
echo '<label for="'.$this->get_field_name( 'title' ) .'">' ._e( 'Google Analytics Email:' ).' </label> ';
echo '<input id="'. $this->get_field_id( 'google_analytics_email' ).'" name="'. $this->get_field_name( 'google_analytics_email' ).'" type="text" value="'. esc_attr( $gaEmail ). '" />';
echo '</p>';
//for Google Analytics Email Password
echo '<p>';
echo '<label for="'.$this->get_field_name( 'title' ) .'">' ._e( 'Google Analytics Password:' ).' </label> ';
echo '<input id="'. $this->get_field_id( 'google_analytics_email_password' ).'" name="'. $this->get_field_name( 'google_analytics_email_password' ).'" type="password" value="'. esc_attr( $gaPassword ). '" />';
echo '</p>';
//for Google Analytics Profile Id
echo '<p>';
echo '<label for="'.$this->get_field_name( 'title' ) .'">' ._e( 'Google Analytics ProfileId:' ).' </label> ';
echo '<input id="'. $this->get_field_id( 'google_analytics_ProfileId' ).'" name="'. $this->get_field_name( 'google_analytics_ProfileId' ).'" type="text" value="'. esc_attr( $gaProfileId ). '" />';
echo '</p>';
//for Google Analytics Max Results
echo '<p>';
echo '<label for="'.$this->get_field_name( 'title' ) .'">' ._e( 'Google Analytics Max Results:' ).' </label> ';
echo '<input id="'. $this->get_field_id( 'google_analytics_Max_Results' ).'" name="'. $this->get_field_name( 'google_analytics_Max_Results' ).'" type="text" value="'. esc_attr( $gaMaxResults ). '" />';
echo '</p>';
//for Google Analytics Time Duration
echo '<p>';
echo '<label>'._e( 'Google Analytics Time Duration:' ).'</label>';
echo '<select id="'. $this->get_field_id( 'google_analytics_Time_Duration' ).'" name="'. $this->get_field_name( 'google_analytics_Time_Duration' ).'">
<option value="7 DAY" '.$day_select.'>7 Days</option>
<option value="1 MONTH" '.$month_select.'>1 Month</option>
<option value="1 YEAR" '.$year_select.'>1 Year</option>
</select>';
echo '</p>';
}
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['google_analytics_email'] = strip_tags( $new_instance['google_analytics_email'] );
$instance['google_analytics_email_password'] = strip_tags( $new_instance['google_analytics_email_password'] );
$instance['google_analytics_ProfileId'] = $new_instance['google_analytics_ProfileId'];
$instance['google_analytics_Max_Results'] = $new_instance['google_analytics_Max_Results'];
$instance['google_analytics_Time_Duration'] = $new_instance['google_analytics_Time_Duration'];
return $instance;
}
public function widget( $args, $instance ) {
extract($args);
//for Google Analytics Email
if ( isset( $instance[ 'google_analytics_email' ] ) )
{
$gaEmail = $instance[ 'google_analytics_email' ];
}
else
{
$gaEmail ='';
}
//for Google Analytics Email Password
if ( isset( $instance[ 'google_analytics_email_password' ] ) )
{
$gaPassword = $instance[ 'google_analytics_email_password' ];
}
else
{
$gaPassword ='';
}
//for Google Analytics Profile Id
if ( isset( $instance[ 'google_analytics_ProfileId' ] ) )
{
$gaProfileId = $instance[ 'google_analytics_ProfileId' ];
}
else
{
$gaProfileId ='';
}
//for Google Analytics Max Results
if ( isset( $instance[ 'google_analytics_Max_Results' ] ) )
{
$gaMaxResults = $instance[ 'google_analytics_Max_Results' ];
}
else
{
$gaMaxResults ='';
}
//for Google Analytics Time Duration
if ( isset( $instance[ 'google_analytics_Time_Duration' ] ) )
{
$gaTimeDuration = $instance[ 'google_analytics_Time_Duration' ];
}
else
{
$gaTimeDuration ='';
}
echo $before_widget;
echo $before_title.'Top 10 posts in '.$gaTimeDuration.'(s)'. $after_title;
$filter=null;
$startDate;
switch($gaTimeDuration)
{
//calculating start date based on duration setting
case "7 DAY":
$startDate =Date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-7,date("Y")));
break;
case "1 MONTH":
$startDate =Date('Y-m-d', mktime(0, 0, 0, date("m")-1, date("d"),date("Y")));
break;
case "1 YEAR":
$startDate =Date('Y-m-d', mktime(0, 0, 0, date("m"), date("d"),date("Y")-1));
break;
default: //defaulting to 1 month
$startDate =Date('Y-m-d', mktime(0, 0, 0, date("m")-1, date("d"),date("Y")));
break;
}
$endDate = Date('Y-m-d',time());
$startIndex=1;
$count=0;
if(!empty ($gaEmail) && !empty($gaPassword))
{
try {
$ga = new gapi($gaEmail, $gaPassword);
$ga->requestReportData($gaProfileId, array('hostname', 'pagePath','pageTitle'), array('visits'), array('-visits'), $filter, $startDate, $endDate, $startIndex, $maxResults);
foreach ($ga->getResults() as $result) {
$count=$count+1;
$getHostname = $result->getHostname();
$getPagepath = $result->getPagepath();
$getPageTitle = $result->getPagetitle();
$postPagepath = 'http://'.$getHostname.$getPagepath;
$getPostID = url_to_postid($postPagepath);
if ($getPostID <= 0) {
$output .= '<p>';
$output .= '<a href='.$postPagepath.'>'.$getPageTitle.'</a>'."\n";
$output .= '</p>';
}
}
//If no data was returned we will reach here. So display no posts available
if($count==0)
{
$output.="No Posts Available";
}
}
//catch exception and display default error message
catch(Exception $ex)
{
$output.='<p>Could not get Report Data.</p><p> Error Ocurred. Please verify the settings are right</p>';
}
}
//we reach here when Google Analytics Email/ Password is not configurated. Display message to user
else
{
$output.="Please configure Google Analytics in Settings Page!";
}
echo $output;
echo $after_widget;
}
}
//registering the Widget
function DisplayTop10PostsWidgetInit() {
register_widget('Display_Top10_Posts_Widget');
}
//adding hook for registering the Widget
add_action('widgets_init', 'DisplayTop10PostsWidgetInit');
?>