root/trunk/wp_plugin.php

Revision 558, 11.1 kB (checked in by padams, 3 days ago)

fixed several bugs in serviceUser
renamed 'Administrator' role to 'admin' to maintain backwards compatability
fixed bogus error msg when applying module updates successfully

Line 
1<?php 
2
3/*
4Plugin Name: Open Web Analytics
5Plugin URI: http://www.openwebanalytics.com
6Description: This plugin enables Wordpress blog owners to use the Open Web Analytics Framework.
7Author: Peter Adams
8Version: v1.2
9Author URI: http://www.openwebanalytics.com
10*/
11
12//
13// Open Web Analytics - An Open Source Web Analytics Framework
14//
15// Copyright 2008 Peter Adams. All rights reserved.
16//
17// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
18//
19// Unless required by applicable law or agreed to in writing, software
20// distributed under the License is distributed on an "AS IS" BASIS,
21// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22// See the License for the specific language governing permissions and
23// limitations under the License.
24//
25// $Id$
26//
27
28require_once('owa_env.php');
29
30// Public folder URI
31define('OWA_PUBLIC_URL', '../wp-content/plugins/owa/public/');
32
33// Check to see what version of wordpress is running
34$owa_wp_version = owa_parse_version($wp_version);
35
36// hack for eliminating WP from printing db errors prior to WP v2.1
37if ($owa_wp_version[0] == '2'):
38        if ($owa_wp_version[1] == '0'):
39                $wpdb->hide_errors();
40        endif;
41endif;
42
43// Filter and Action hook assignments
44add_action('template_redirect', 'owa_main');
45add_action('wp_footer', 'owa_footer');
46add_filter('the_permalink_rss', 'owa_post_link');
47add_action('init', 'owa_handleSpecialActionRequest');
48add_filter('bloginfo_url', 'add_feed_sid');
49add_action('admin_menu', 'owa_dashboard_menu');
50add_action('comment_post', 'owa_logComment');
51add_action('admin_menu', 'owa_options_menu');
52// Installation hook
53register_activation_hook(__FILE__,'owa_install');
54/////////////////////////////////////////////////////////////////////////////////
55
56
57/**
58 * Singleton Method
59 *
60 * Returns an instance of OWA
61 *
62 * @return $owa object
63 */
64
65function owa_getInstance($params = array()) {
66       
67        static $owa;
68
69        if(!empty($owa)):
70                return $owa;
71        else:
72       
73                require_once(OWA_BASE_CLASSES_DIR.'owa_wp.php');
74               
75                // Build the OWA wordpress specific config overrides array
76                $owa_config = array();
77               
78                // OWA DATABASE CONFIGURATION
79                // Will use Wordpress config unless there is a config file present.
80                // OWA uses this to setup it's own DB connection seperate from the one
81                // that Wordpress uses.
82                $owa_config['db_type'] = 'mysql';
83                $owa_config['db_name'] = DB_NAME;
84                $owa_config['db_host'] = DB_HOST;
85                $owa_config['db_user'] = DB_USER;
86                $owa_config['db_password'] = DB_PASSWORD;
87               
88                $owa_config['report_wrapper'] = 'wrapper_wordpress.tpl';
89                //'../wp-content/plugins/owa/public/i/';
90                $owa_config['images_url'] = OWA_PUBLIC_URL.'i/';
91                //'../wp-content/plugins/owa/public/i/';
92                $owa_config['images_absolute_url'] = get_bloginfo('url').'/wp-content/plugins/owa/public/i/';
93                $owa_config['main_url'] = '../wp-admin/index.php?page=owa/public/wp.php';
94                $owa_config['main_absolute_url'] = get_bloginfo('url').'/wp-admin/index.php?page=owa/public/wp.php';
95                $owa_config['action_url'] = get_bloginfo('url').'/index.php?owa_specialAction';
96                $owa_config['log_url'] = get_bloginfo('url').'/index.php?owa_logAction=1';
97                $owa_config['link_template'] = '%s&%s';
98                $owa_config['site_id'] = md5(get_settings('siteurl'));
99                $owa_config['is_embedded'] = true;
100                $owa_config['delay_first_hit'] = true;
101       
102                $config = array_merge($owa_config, $params);
103               
104                $owa = new owa_wp($config);
105               
106                // adds wordpress specific user priviledge info to the request params
107                global $current_user;
108        get_currentuserinfo();
109       
110        /*
111       
112        //print_r($current_user);
113                $owa->params['caller']['wordpress']['user_data'] = array(
114       
115                'user_roles'    => $current_user->roles,
116                'user_ID'               => $current_user->user_ID,
117                'user_login'    => $current_user->user_login,
118                'user_email'    => $current_user->user_email,
119                'user_identity' => $current_user->user_identity,
120                'user_password' => 'xxxxxxxxx');
121               
122                $owa->params['u'] = $current_user->user_login;
123                $owa->params['p'] = 'xxxxxxxxx';
124               
125                */
126               
127                // preemptively set the current user info and mark as authenticated so that
128                // downstream controllers don't have to authenticate
129                $cu =&owa_coreAPI::getCurrentUser();
130                $cu->setUserData('user_id', $current_user->user_ID);
131                $cu->setUserData('email_address', $current_user->user_email);
132                $cu->setUserData('real_name', $current_user->user_identity);
133                $cu->setRole(owa_translate_role($current_user->roles[0]));
134                $cu->setAuthStatus(true);
135                               
136                return $owa;
137               
138        endif;
139       
140}
141
142// translates wordpress roles to owa roles
143function owa_translate_role($role) {
144        $role = strtolower($role);
145        switch ($role) {
146                case "administrator":
147                        $owa_role = 'admin';
148                        break;
149                case "editor":
150                        $owa_role = 'viewer';
151                        break;
152                case "author":
153                        $owa_role = 'viewer';
154                        break;
155                case "contributor":
156                        $owa_role = 'viewer';
157                        break;
158                case "subscriber":
159                        $owa_role = 'everyone';
160                        break;
161                default:
162                        $owa_role = 'everyone';
163       
164        }
165
166        return $owa_role;
167}
168
169
170function owa_handleSpecialActionRequest() {
171
172        $owa = owa_getInstance();
173        return $owa->handleSpecialActionRequest();
174}
175
176function owa_logComment() {
177
178        $owa = owa_getInstance();
179        return $owa->logComment();
180}
181
182
183
184/**
185 * Prints helper page tags to the footers of templates.
186 *
187 */
188function owa_footer() {
189       
190        $owa = owa_getInstance();
191       
192        $owa->placeHelperPageTags();
193       
194       
195        return;
196       
197}       
198
199/**
200 * This is the main logging controller that is called on each request.
201 *
202 */
203function owa_main() {
204       
205        global $user_level;
206       
207        $owa = owa_getInstance();
208       
209        // Don't log if the page request is a preview - Wordpress 2.x or greater
210        if (function_exists(is_preview)):
211                if (is_preview()):
212                        $owa->params['do_not_log'] = true;
213                endif;
214        endif;
215       
216        // Don't Log if user is an admin
217        if($user_level == '10'):
218                if ($owa->config['do_not_log_admins'] == true):
219                        $owa->params['do_not_log'] = true;
220                endif;
221        endif;
222       
223        // WORDPRESS SPECIFIC DATA //
224       
225        // Get the type of page
226        $app_params['page_type'] = owa_get_page_type();
227       
228        //Check to see if this is a Feed Reeder
229        if(is_feed()):
230                $app_params['is_feedreader'] = true;
231                $app_params['feed_format'] = $_GET['feed'];
232        endif;
233       
234        $app_params[$owa->config['source_param']] = $_GET[$owa->config['ns'].$owa->config['source_param']];
235       
236        // Track users by the email address of that they used when posting a comment
237        $app_params['user_email'] = $_COOKIE['comment_author_email_'.COOKIEHASH]; 
238       
239        // Track users who have a named account
240        $app_params['user_name'] = $_COOKIE['wordpressuser_'.COOKIEHASH];
241       
242        // Get Title of Page
243        $app_params['page_title'] = owa_get_title($app_params['page_type']);
244       
245        // Process the request by calling owa
246        $owa->log($app_params);
247       
248        return;
249}
250
251/**
252 * Determines the title of the page being requested
253 *
254 * @param string $page_type
255 * @return string $title
256 */
257function owa_get_title($page_type) {
258
259        if ($page_type == "Home"):
260                $title = get_bloginfo('name');
261        elseif ($page_type == "Search Results"):
262                $title = "Search Results for \"".$_GET['s']."\"";       
263        elseif ($page_type == "Page" || "Post"):
264                $title = wp_title($sep = '', $display = 0);
265        elseif ($page_type == "Author"):
266                $title = wp_title($sep = '', $display = 0);
267        elseif ($page_type == "Category"):
268                $title = wp_title($sep = '', $display = 0);
269        elseif ($page_type == "Month"):
270                $title = wp_title($sep = '', $display = 0);
271        elseif ($page_type == "Day"):
272                $title = wp_title($sep = '', $display = 0);
273        elseif ($page_type == "Year"):
274                $title = wp_title($sep = '', $display = 0);
275        elseif ($page_type == "Time"):
276                $title = wp_title($sep = '', $display = 0);
277        elseif ($page_type == "Feed"):
278                $title = wp_title($sep = '', $display = 0);
279        endif; 
280       
281        return $title;
282}
283
284/**
285 * Determines the type of page being requested
286 *
287 * @return string $type
288 */
289function owa_get_page_type() { 
290       
291        if (is_home()):
292                $type = "Home";
293        elseif (is_single()):
294                $type = "Post";
295        elseif (is_page()):
296                $type = "Page";
297        elseif (is_author()):
298                $type = "Author";
299        elseif (is_category()):
300                $type = "Category";
301        elseif (is_search()):
302                $type = "Search Results";
303        elseif (is_month()):
304                $type = "Month";
305        elseif (is_day()):
306                $type = "Day";
307        elseif (is_year()):
308                $type = "Year";
309        elseif (is_time()):
310                $type = "Time";
311        elseif (is_archive()):
312                $type = "Archive";
313        elseif (is_feed()):
314                $type = "Feed";
315        endif;
316       
317        return $type;
318}
319
320/**
321 * Wordpress filter function adds a GUID to the feed URL.
322 *
323 * @param array $binfo
324 * @return string $newbinfo
325 */
326function add_feed_sid($binfo) {
327       
328        $owa = owa_getInstance();
329       
330        $test = strpos($binfo, "feed=");
331       
332        if ($test == true):
333                $newbinfo = $owa->add_feed_tracking($binfo);
334       
335        else: 
336               
337                $newbinfo = $binfo;
338               
339        endif;
340       
341        return $newbinfo;
342
343}
344
345/**
346 * Adds tracking source param to links in feeds
347 *
348 * @param string $link
349 * @return string
350 */
351function owa_post_link($link) {
352
353        $owa = owa_getInstance();
354
355        return $owa->add_link_tracking($link);
356               
357}
358
359/**
360 * Schema and setting installation
361 *
362 */
363function owa_install() {
364
365        global $user_level;
366       
367        $params = array();
368        $params['do_not_fetch_config_from_db'] = true;
369
370        $owa = owa_getInstance($params);
371       
372        //check to see if the user has permissions to install or not...
373        get_currentuserinfo();
374       
375        if ($user_level < 8):
376        return;
377    else:
378        $owa->config['fetch_config_from_db'] = false;
379       
380        $owa->config['db_type'] = 'mysql';
381       
382        $install_params = array('site_id' => md5(get_settings('siteurl')), 
383                                                        'name' => get_bloginfo('name'),
384                                                        'domain' => get_settings('siteurl'), 
385                                                        'description' => get_bloginfo('description'),
386                                                        'action' => 'base.installEmbedded');
387                                                       
388        $owa->handleRequest($install_params);
389        endif;
390
391        return;
392}
393
394/**
395 * Adds Analytics sub tab to admin dashboard screens.
396 *
397 */
398function owa_dashboard_menu() {
399
400        if (function_exists('add_submenu_page')):
401                add_submenu_page('index.php', 'OWA Dashboard', 'Analytics', 1, dirname(__FILE__), 'owa_dashboard_report');
402    endif;
403   
404    return;
405
406}
407
408/**
409 * Produces the analytics dashboard
410 *
411 */
412function owa_dashboard_report() {
413       
414        $owa = owa_getInstance();
415       
416        $params = array();
417        $params['do'] = 'base.reportDashboard';
418        echo $owa->handleRequest($params);
419       
420        return;
421       
422}
423
424/**
425 * Adds Options page to admin interface
426 *
427 */
428function owa_options_menu() {
429       
430        if (function_exists('add_options_page')):
431                add_options_page('Options', 'OWA', 8, basename(__FILE__), 'owa_options_page');
432        endif;
433   
434    return;
435}
436
437/**
438 * Generates Options Management Page
439 *
440 */
441function owa_options_page() {
442       
443        $owa = owa_getInstance();
444       
445        $params = array();
446        $params['view'] = 'base.options';
447        $params['subview'] = 'base.optionsGeneral';
448        echo $owa->handleRequest($params);
449       
450        return;
451}
452
453/**
454 * Parses string to get the major and minor version of the
455 * instance of wordpress that is running
456 *
457 * @param string $version
458 * @return array
459 */
460function owa_parse_version($version) {
461       
462        $version_array = explode(".", $version);
463   
464   return $version_array;
465       
466}
467
468?>
Note: See TracBrowser for help on using the browser.