-
Notifications
You must be signed in to change notification settings - Fork 1
/
activityView.php
317 lines (283 loc) · 10.5 KB
/
activityView.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<meta http-equiv="refresh" content="300">
<?php
/*
* Plugin Name: Activity of Links
* Description: A table containing activity from blogs that user's blog is linked to under Links Manager.
* Author: Kinzie Brooks (Josh Stemmler & Paul Underwood, initial template)
* Version: 1.0
*/
if(is_admin())
{
new Activities_View_Table();
if( ! function_exists('getUserId') ){
include(ABSPATH . 'wp-content/plugins/sharedFunctions.php' );
}
}
/**
* Activities_View_Table class will create the page to load the table
*/
class Activities_View_Table
{
/**
* Display the list table page
*
* @return Void
*/
public function list_table_page()
{
$activityViewTable = new Activity_View_Table();
$activityViewTable->prepare_items();
?>
<div class="wrap">
<div id="icon-users" class="icon32"></div>
<h2>Activity of Linked Blogs</h2>
<?php $actvityViewTable->display(); ?>
</div>
<?php
}
}
// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'activity_WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-content/plugins/activity-class-wp-list-table.php' );
}
/**
* Create a new table class that will extend the WP_List_Table
*/
class Activity_View_Table extends activity_WP_List_Table
{
/**
* Prepare the items for the table to process
*
* @return Void
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
usort( $data, array( &$this, 'sort_data' ) );
$perPage = 10;
$currentPage = $this->get_pagenum();
$totalItems = count($data);
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $perPage
) );
$data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = array(
'blog_title' => "Blog",
'post_title' => "Post Title",
'activity_author' => "Activity Author",
'activity_type' => "Activity Type",
'recent_activity' => 'Activity Content',
'comment_date' => 'Date',
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return array('activity_author' => array('activity_author', false), 'activity_type' => array('activity_type', false), 'post_title' => array('post_title', false), 'blog_title' => array('blog_title', false), 'comment_date' => array('comment_date', false));
}
private function table_data()
{
global $wpdb;
$userId = getUserId();
if(get_current_blog_id() == 1){
$linkquery = "SELECT link_url FROM ". $wpdb->base_prefix ."links";
}
else{
$linkquery = "SELECT link_url FROM ". $wpdb->base_prefix. get_current_blog_id() ."_links";
}
$domainPathQuery = "SELECT domain, path FROM " . $wpdb->base_prefix . "blogs";
$domainPath = $wpdb->get_results($domainPathQuery);
$link_list = $wpdb->get_results($linkquery);
$first = 1;
foreach($link_list as $link){
foreach($domainPath as $paths) {
$url = "http://".$paths->domain . $paths->path;
if(substr($link->link_url, -1) != "/"){
$compUrl = $link->link_url."/";
}
else{
$compUrl = $link->link_url;
}
if ($url == $compUrl){
if( $first == 1){
$matchQuery = "SELECT blog_id FROM " . $wpdb->base_prefix . "blogs WHERE domain = \"{$paths->domain}\" AND path = \"{$paths->path}\"";
$uni = '';
$first = 0;
}
else{
$uni = ' union ';
$matchQuery .= $uni . "SELECT blog_id FROM " . $wpdb->base_prefix . "blogs WHERE domain = \"{$paths->domain}\" AND path = \"{$paths->path}\"";
}
}
}
}
$match = $wpdb->get_results($matchQuery);
$first = 1;
// gets all comments
foreach($match as $blogids){
if($first == 1){
$postsQuery = "SELECT ".$blogids->blog_id. " as blog_id, \"comment\" as type, comment_content as content, comment_date_gmt AS date, comment_author AS author, (SELECT post_title FROM ". $wpdb->base_prefix ."posts WHERE ID = comment_post_id) AS postTitle, (SELECT guid FROM ". $wpdb->base_prefix ."posts WHERE ID = comment_post_id) AS url FROM wp_comments";
$uni = '';
$first = 0;
}
else{
$uni = ' UNION ';
$postsQuery .= $uni . "SELECT ".$blogids->blog_id. " as blog_id, \"comment\" as type, comment_content as content, comment_date_gmt AS date, comment_author AS author,(SELECT post_title FROM ". $wpdb->base_prefix.$blogids->blog_id."_posts WHERE ID = comment_post_id) AS postTitle, (SELECT guid FROM ". $wpdb->base_prefix.$blogids->blog_id ."_posts WHERE ID = comment_post_id) AS url FROM ".$wpdb->base_prefix.$blogids->blog_id."_comments";
}
}
//gets all posts
foreach($match as $blogids){
if($blogids->blog_id == 1){
$postsQuery .= " UNION SELECT " . $blogids->blog_id . " as blog_id, \"post\" as type, post_content as content, post_modified_gmt AS date, (SELECT user_login FROM ".$wpdb->base_prefix."users where id = post_author) as author, post_title AS postTitle, guid AS url FROM ".$wpdb->base_prefix."posts WHERE post_status = \"publish\"";
}
else{
$postsQuery .= " UNION SELECT " . $blogids->blog_id . " as blog_id, \"post\" as type, post_content as content, post_modified_gmt AS date, (SELECT user_login FROM wp_users where id = post_author) as author, post_title AS postTitle, guid AS url FROM ".$wpdb->base_prefix.$blogids->blog_id."_posts WHERE post_status = \"publish\"";
}
}
$limit = 50; //set your limit
$limit = ' LIMIT 0, '. $limit;
$postsQuery .= " ORDER BY date desc " . $limit;
//echo($postsQuery);
$activities = $wpdb->get_results($postsQuery);
$blognamequery1 = "SELECT option_value FROM ". $wpdb->base_prefix . "options WHERE option_name = \"blogname\"";
$postnamequery1 = "SELECT post_title FROM ". $wpdb->base_prefix ."posts WHERE ID = {$comment->comment_post_id}";
$blogurlquery1 = "SELECT option_value FROM ". $wpdb->base_prefix . "options WHERE option_name = \"siteurl\"";
$data = array();
foreach($activities as $activity){
if($activity->type == "post") {
$title = $wpdb->get_var("SELECT post_title FROM ". $wpdb->base_prefix . $activity->blog_id . "_posts WHERE post_modified_gmt = \"{$activity->date}\"");
} else {
$id = $wpdb->get_var("SELECT comment_post_ID FROM ". $wpdb->base_prefix . $activity->blog_id . "_comments WHERE comment_content = \"{$activity->content}\"");
//$title = $wpdb->get_var("SELECT post_title FROM ". $wpdb->base_prefix .$activity->blog_id."_posts WHERE ID = {$comment->comment_post_id}");
}
if($activity->blog_id !=1){
$data[] = array(
'activity_author' => $activity->author,
'recent_activity' => $activity->content,
'activity_type' => strtoupper($activity->type),
'comment_date' => date_i18n( get_option( 'date_format' ). " " .get_option('time_format'), strtotime($activity->date) ),
'post_title' => $activity->postTitle,
'blog_title' => $wpdb->get_var("SELECT option_value FROM ". $wpdb->base_prefix . $activity->blog_id . "_options WHERE option_name = \"blogname\""),
'blog_url' => $wpdb->get_var("SELECT option_value FROM ". $wpdb->base_prefix . $activity->blog_id . "_options WHERE option_name = \"siteurl\""),
'post_url' => $activity->url,
'activity_author' => $activity->author
);
}
else{
$data[] = array(
'activity_author' => $activity->author,
'recent_activity' => $activity->content,
'activity_type' => strtoupper($activity->type),
'comment_date' => date_i18n( get_option( 'date_format' ). " " .get_option('time_format'), strtotime($activity->date) ),
'blog_title' => $wpdb->get_var($blognamequery1),
'post_title' => $activity->postTitle,
'blog_url' => $wpdb->get_var($blogurlquery1),
'post_url' => $activity->url
);
}
}
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
switch( $column_name ) {
case 'activity_author':
return $item[$column_name];
case 'activity_type':
return $item[$column_name];
case 'blog_title':
return "<a href =\"". $item["blog_url"]."\" target=\"_blank\">" . $item[$column_name] . "</a>";
case 'post_title':
return "<a href =\"".$item["post_url"]."\" target=\"_blank\">" . $item[$column_name] . "</a>";
case 'recent_activity':
if(strlen($item[$column_name]) > 50){
return substr($item[$column_name],0,100)."...";
}
if(strlen($item[$column_name])<50){
return $item[$column_name];
}
case 'comment_date':
return $item[$column_name];
default:
return print_r( $item, true ) ;
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b )
{
// Set defaults
$orderby = 'blog_title';
$order = 'asc';
// If orderby is set, use this as the sort column
if(!empty($_GET['orderby']))
{
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if(!empty($_GET['order']))
{
$order = $_GET['order'];
}
$result = strcmp( $a[$orderby], $b[$orderby] );
if($order === 'asc')
{
return $result;
}
return -$result;
}
}
add_action( 'wp_dashboard_setup', 'activity_links_dashboard_setup' );
function activity_links_dashboard_setup() {
wp_add_dashboard_widget(
'activity-links-dashboard-widget',
'Activity of Linked Blogs',
'activity_links_dashboard_content',
$control_callback = null
);
}
function activity_links_dashboard_content() {
$activityViewTable = new Activity_View_Table();
$activityViewTable->prepare_items();
$activityViewTable->display();
}
?>