forked from Open-Web-Analytics/Open-Web-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_plugin.php
653 lines (520 loc) · 16.5 KB
/
wp_plugin.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<?php
/*
Plugin Name: Open Web Analytics
Plugin URI: http://www.openwebanalytics.com
Description: This plugin enables Wordpress blog owners to use the Open Web Analytics Framework.
Author: Peter Adams
Version: v1.5.1
Author URI: http://www.openwebanalytics.com
*/
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2008 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
require_once('owa_env.php');
// Filter and Action hook assignments
add_action('template_redirect', 'owa_main');
add_action('wp_head', 'owa_insertPageTags',100);
add_filter('the_permalink_rss', 'owa_post_link');
add_action('init', 'owa_handleSpecialActionRequest');
add_filter('bloginfo_url', 'add_feed_sid');
add_action('admin_menu', 'owa_dashboard_menu');
add_action('comment_post', 'owa_logComment',10,2);
add_action('transition_comment_status', 'owa_logCommentEdit',10,3);
add_action('admin_menu', 'owa_options_menu');
add_action('user_register', 'owa_userRegistrationActionTracker');
add_action('wp_login', 'owa_userLoginActionTracker');
add_action('profile_update', 'owa_userProfileUpdateActionTracker', 10,2);
add_action('password_reset', 'owa_userPasswordResetActionTracker');
add_action('trackback_post', 'owa_trackbackActionTracker');
add_action('add_attachment', 'owa_newAttachmentActionTracker');
add_action('edit_attachment', 'owa_editAttachmentActionTracker');
add_action('transition_post_status', 'owa_postActionTracker', 10, 3);
add_action('wpmu_new_blog', 'owa_newBlogActionTracker', 10, 5);
add_action('wpmu_new_blog', 'owa_createTrackedSiteForNewBlog', 10, 6);
// Installation hook
register_activation_hook(__FILE__, 'owa_install');
/////////////////////////////////////////////////////////////////////////////////
/**
* New Blog Action Tracker
*/
function owa_newBlogActionTracker($blog_id, $user_id, $domain, $path, $site_id) {
$owa = owa_getInstance();
$owa->trackAction('wordpress', 'Blog Created', $domain);
}
function owa_createTrackedSiteForNewBlog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
$owa = owa_getInstance();
$sm = owa_coreAPI::supportClassFactory( 'base', 'siteManager' );
$sm->createNewSite( $domain, $domain, '', '');
}
/**
* Edit Post Action Tracker
*/
function owa_editPostActionTracker($post_id, $post) {
// we don't want to track autosaves...
if(wp_is_post_autosave($post)) {
return;
}
$owa = owa_getInstance();
$label = $post->post_title;
$owa->trackAction('wordpress', $post->post_type.' edited', $label);
}
/**
* Post Action Tracker
*/
function owa_postActionTracker($new_status, $old_status, $post) {
// we don't want to track autosaves...
if(wp_is_post_autosave($post)) {
return;
}
if ($new_status === 'draft' && $old_status === 'draft') {
return;
} elseif ($new_status === 'publish' && $old_status != 'publish') {
$action_name = $post->post_type.' publish';
} elseif ($new_status === $old_status) {
$action_name = $post->post_type.' edit';
}
if ($action_name) {
$owa = owa_getInstance();
owa_coreAPI::debug(sprintf("new: %s, old: %s, post: %s", $new_status, $old_status, print_r($post, true)));
$label = $post->post_title;
$owa->trackAction('wordpress', $action_name, $label);
}
}
/**
* New Attachment Action Tracker
*/
function owa_editAttachmentActionTracker($post_id) {
$owa = owa_getInstance();
$post = get_post($post_id);
$label = $post->post_title;
$owa->trackAction('wordpress', 'Attachment Edit', $label);
}
/**
* New Attachment Action Tracker
*/
function owa_newAttachmentActionTracker($post_id) {
$owa = owa_getInstance();
$post = get_post($post_id);
$label = $post->post_title;
$owa->trackAction('wordpress', 'Attachment Created', $label);
}
/**
* User Registration Action Tracker
*/
function owa_userRegistrationActionTracker($user_id) {
$owa = owa_getInstance();
$user = get_userdata($user_id);
if (!empty($user->first_name) && !empty($user->last_name)) {
$label = $user->first_name.' '.$user->last_name;
} else {
$label = $user->display_name;
}
$owa->trackAction('wordpress', 'User Registration', $label);
}
/**
* User Login Action Tracker
*/
function owa_userLoginActionTracker($user_id) {
$owa = owa_getInstance();
$label = $user_id;
$owa->trackAction('wordpress', 'User Login', $label);
}
/**
* Profile Update Action Tracker
*/
function owa_userProfileUpdateActionTracker($user_id, $old_user_data = '') {
$owa = owa_getInstance();
$user = get_userdata($user_id);
if (!empty($user->first_name) && !empty($user->last_name)) {
$label = $user->first_name.' '.$user->last_name;
} else {
$label = $user->display_name;
}
$owa->trackAction('wordpress', 'User Profile Update', $label);
}
/**
* Password Reset Action Tracker
*/
function owa_userPasswordResetActionTracker($user) {
$owa = owa_getInstance();
$label = $user->display_name;
$owa->trackAction('wordpress', 'User Password Reset', $label);
}
/**
* Trackback Action Tracker
*/
function owa_trackbackActionTracker($comment_id) {
$owa = owa_getInstance();
$label = $comment_id;
$owa->trackAction('wordpress', 'Trackback', $label);
}
/**
* Singleton Method
*
* Returns an instance of OWA
*
* @return $owa object
*/
function &owa_getInstance() {
static $owa;
if( empty( $owa ) ) {
require_once(OWA_BASE_CLASSES_DIR.'owa_wp.php');
// create owa instance w/ config
$owa = new owa_wp();
$owa->setSiteId( md5( get_settings( 'siteurl' ) ) );
$owa->setSetting( 'base', 'report_wrapper', 'wrapper_wordpress.tpl' );
$owa->setSetting( 'base', 'link_template', '%s&%s' );
$owa->setSetting( 'base', 'main_url', '../wp-admin/index.php?page=owa' );
$owa->setSetting( 'base', 'main_absolute_url', get_bloginfo('url').'/wp-admin/index.php?page=owa' );
$owa->setSetting( 'base', 'action_url', get_bloginfo('url').'/index.php?owa_specialAction' );
$owa->setSetting( 'base', 'api_url', get_bloginfo('url').'/index.php?owa_apiAction' );
$owa->setSetting( 'base', 'is_embedded', true );
// needed as old installs might have this turned on by default...
$owa->setSetting( 'base', 'delay_first_hit', false );
// Access WP current user object to check permissions
//$current_user = owa_getCurrentWpUser();
//print_r($current_user);
// Set OWA's current user info and mark as authenticated so that
// downstream controllers don't have to authenticate
//$cu->isInitialized = true;
// register allowedSitesList filter
$dispatch = owa_coreAPI::getEventDispatch();
// alternative auth method, sets auth status, role, and allowed sites list.
$dispatch->attachFilter('auth_status', 'owa_wpAuthUser',0);
//print_r( $current_user );
}
return $owa;
}
/**
* OWA authentication filter method
*
* This filter function authenticates the user and populates the
* the current user in OWA with the proper role, and allowed sites list.
*
* This method kicks in after all over OWA's built in auth methods have failed
* in the owa_auth class.
*
* @param $auth_status boolean
* @return $auth_status boolean
*/
function owa_wpAuthUser($auth_status) {
$current_user = wp_get_current_user();
if ( $current_user instanceof WP_User ) {
// logged in, authenticated
$cu = owa_coreAPI::getCurrentUser();
$cu->setAuthStatus(true);
if (isset($current_user->user_login)) {
$cu->setUserData('user_id', $current_user->user_login);
owa_coreAPI::debug("Wordpress User_id: ".$current_user->user_login);
}
if (isset($current_user->user_email)) {
$cu->setUserData('email_address', $current_user->user_email);
}
if (isset($current_user->first_name)) {
$cu->setUserData('real_name', $current_user->first_name.' '.$current_user->last_name);
$cu->setRole(owa_translate_role($current_user->roles));
}
owa_coreAPI::debug("Wordpress User Role: ".print_r($current_user->roles, true));
owa_coreAPI::debug("Wordpress Translated OWA User Role: ".$cu->getRole());
// fetch the list of allowed blogs from WP
$domains = array();
$allowedBlogs = get_blogs_of_user($current_user->ID);
foreach ( $allowedBlogs as $blog) {
$domains[] = $blog->siteurl;
}
// check to see if we are installing before trying to load sites
// other wise you run into a race condition as config file
// might not be created.
if (! defined('OWA_INSTALLING') ) {
// load assigned sites list by domain
$cu->loadAssignedSitesByDomain($domains);
}
$cu->setInitialized();
return true;
} else {
// not logged in to WP and therefor not authenticated
return false;
}
}
function owa_getCurrentWpUser() {
// Access WP current user object to check permissions
global $current_user;
get_currentuserinfo();
return $current_user;
}
// translates wordpress roles to owa roles
function owa_translate_role($roles) {
if (!empty($roles)) {
if (in_array('administrator', $roles)) {
$owa_role = 'admin';
} elseif (in_array('editor', $roles)) {
$owa_role = 'viewer';
} elseif (in_array('author', $roles)) {
$owa_role = 'viewer';
} elseif (in_array('contributor', $roles)) {
$owa_role = 'viewer';
} elseif (in_array('subscriber', $roles)) {
$owa_role = 'everyone';
} else {
$owa_role = 'everyone';
}
} else {
$owa_role = 'everyone';
}
return $owa_role;
}
function owa_handleSpecialActionRequest() {
$owa = owa_getInstance();
owa_coreAPI::debug("hello from WP special action handler");
return $owa->handleSpecialActionRequest();
}
function owa_logComment($id, $comment_data = '') {
if ( $comment_data === 'approved' || $comment_data === 1 ) {
$owa = owa_getInstance();
$label = '';
$owa->trackAction('wordpress', 'comment', $label);
}
}
function owa_logCommentEdit($new_status, $old_status, $comment) {
if ($new_status === 'approved') {
if (isset($comment->comment_author)) {
$label = $comment->comment_author;
} else {
$label = '';
}
$owa = owa_getInstance();
$owa->trackAction('wordpress', 'comment', $label);
}
}
/**
* Prints helper page tags to the <head> of pages.
*
*/
function owa_insertPageTags() {
// Don't log if the page request is a preview - Wordpress 2.x or greater
if (function_exists('is_preview')) {
if (is_preview()) {
return;
}
}
$owa = owa_getInstance();
$page_properties = $owa->getAllEventProperties($owa->pageview_event);
$cmds = '';
if ( $page_properties ) {
$page_properties_json = json_encode( $page_properties );
$cmds .= "owa_cmds.push( ['setPageProperties', $page_properties_json] );";
}
//$wgOut->addInlineScript( $cmds );
$options = array( 'cmds' => $cmds );
$owa->placeHelperPageTags(true, $options);
}
/**
* This is the main logging controller that is called on each request.
*
*/
function owa_main() {
//global $user_level;
$owa = owa_getInstance();
owa_coreAPI::debug('wp main request method');
//Check to see if this is a Feed Reeder
if( $owa->getSetting('base', 'log_feedreaders') && is_feed() ) {
$event = $owa->makeEvent();
$event->setEventType('base.feed_request');
$event->set('feed_format', $_GET['feed']);
// Process the request by calling owa
return $owa->trackEvent($event);
}
// Set the type and title of the page
$page_type = owa_get_page_type();
$owa->setPageType( $page_type );
// Get Title of Page
$owa->setPageTitle( owa_get_title( $page_type ) );
}
/**
* Determines the title of the page being requested
*
* @param string $page_type
* @return string $title
*/
function owa_get_title($page_type) {
if ($page_type == "Home"):
$title = get_bloginfo('name');
elseif ($page_type == "Search Results"):
$title = "Search Results for \"".$_GET['s']."\"";
elseif ($page_type == "Page" || "Post"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Author"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Category"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Month"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Day"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Year"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Time"):
$title = wp_title($sep = '', $display = 0);
elseif ($page_type == "Feed"):
$title = wp_title($sep = '', $display = 0);
endif;
return $title;
}
/**
* Determines the type of page being requested
*
* @return string $type
*/
function owa_get_page_type() {
if (is_home()):
$type = "Home";
elseif (is_attachment()):
$type = "Attachment";
elseif (is_page()):
$type = "Page";
// general page catch, should be after more specific post types
elseif (is_single()):
$type = "Post";
elseif (is_feed()):
$type = "Feed";
elseif (is_author()):
$type = "Author";
elseif (is_category()):
$type = "Category";
elseif (is_search()):
$type = "Search Results";
elseif (is_month()):
$type = "Month";
elseif (is_day()):
$type = "Day";
elseif (is_year()):
$type = "Year";
elseif (is_time()):
$type = "Time";
elseif (is_tag()):
$type = "Tag";
elseif (is_tax()):
$type = "Taxonomy";
// general archive catch, should be after specific archive types
elseif (is_archive()):
$type = "Archive";
else:
$type = '(not set)';
endif;
return $type;
}
/**
* Wordpress filter function adds a GUID to the feed URL.
*
* @param array $binfo
* @return string $newbinfo
*/
function add_feed_sid($binfo) {
$owa = owa_getInstance();
$test = strpos($binfo, "feed=");
if ($test == true):
$newbinfo = $owa->add_feed_tracking($binfo);
else:
$newbinfo = $binfo;
endif;
return $newbinfo;
}
/**
* Adds tracking source param to links in feeds
*
* @param string $link
* @return string
*/
function owa_post_link($link) {
$owa = owa_getInstance();
return $owa->add_link_tracking($link);
}
/**
* Schema and setting installation
*
*/
function owa_install() {
define('OWA_INSTALLING', true);
$params = array();
//$params['do_not_fetch_config_from_db'] = true;
$owa = owa_getInstance($params);
$owa->setSetting('base', 'cache_objects', false);
$public_url = get_bloginfo('wpurl').'/wp-content/plugins/owa/';
$install_params = array('site_id' => md5(get_settings('siteurl')),
'name' => get_bloginfo('name'),
'domain' => get_settings('siteurl'),
'description' => get_bloginfo('description'),
'action' => 'base.installEmbedded',
'db_type' => 'mysql',
'db_name' => DB_NAME,
'db_host' => DB_HOST,
'db_user' => DB_USER,
'db_password' => DB_PASSWORD,
'public_url' => $public_url
);
$owa->handleRequest($install_params);
}
/**
* Adds Analytics sub tab to admin dashboard screens.
*
*/
function owa_dashboard_menu() {
if (function_exists('add_submenu_page')):
add_submenu_page('index.php', 'OWA Dashboard', 'Analytics', 1, dirname(__FILE__), 'owa_pageController');
endif;
return;
}
/**
* Main page handler.
*
*/
function owa_pageController() {
$owa = owa_getInstance();
echo $owa->handleRequest();
}
/**
* Adds Options page to admin interface
*
*/
function owa_options_menu() {
if (function_exists('add_options_page')):
add_options_page('Options', 'OWA', 8, basename(__FILE__), 'owa_options_page');
endif;
return;
}
/**
* Generates Options Management Page
*
*/
function owa_options_page() {
$owa = owa_getInstance();
$params = array();
$params['view'] = 'base.options';
$params['subview'] = 'base.optionsGeneral';
echo $owa->handleRequest($params);
return;
}
/**
* Parses string to get the major and minor version of the
* instance of wordpress that is running
*
* @param string $version
* @return array
*/
function owa_parse_version($version) {
$version_array = explode(".", $version);
return $version_array;
}
?>