diff --git a/owa_wp.php b/owa_wp.php index 86000145f..3b9761fda 100644 --- a/owa_wp.php +++ b/owa_wp.php @@ -44,49 +44,6 @@ function __construct($config = null) { return parent::__construct($config); } - - - function add_link_tracking($link) { - - // check for presence of '?' which is not present under URL rewrite conditions - - if ($this->config['track_feed_links'] == true): - - if (strpos($link, "?") === false): - // add the '?' if not found - $link .= '?'; - endif; - - // setup link template - $link_template = "%s&%s=%s&%s=%s"; - - return sprintf($link_template, - $link, - $this->config['ns'].'medium', - 'feed', - $this->config['ns'].$this->config['feed_subscription_param'], - $_GET[$this->config['ns'].$this->config['feed_subscription_param']]); - else: - return; - endif; - } - - /** - * Wordpress filter method. Adds tracking to feed links. - * - * @var string the feed link - * @return string link string with special tracking id - */ - function add_feed_tracking( $binfo ) { - - if ($this->config['track_feed_links'] == true) { - $guid = crc32(getmypid().microtime()); - - return $binfo."&".$this->config['ns'].$this->config['feed_subscription_param']."=".$guid; - } else { - return; - } - } } ?> \ No newline at end of file diff --git a/wp_plugin.php b/wp_plugin.php index 74c850d8a..567ff792d 100644 --- a/wp_plugin.php +++ b/wp_plugin.php @@ -5,7 +5,7 @@ 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.6.0 +Version: master Author URI: http://www.openwebanalytics.com */ @@ -25,396 +25,167 @@ // $Id$ // -require_once('owa_env.php'); - -// Filter and Action hook assignments - -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('admin_menu', 'owa_options_menu'); -add_action('wpmu_new_blog', 'owa_createTrackedSiteForNewBlog', 10, 6); // Hook package creation add_action('plugins_loaded', array( 'owa_wp_plugin', 'getInstance'), 10 ); -// Installation hook -register_activation_hook(__FILE__, 'owa_install'); - ///////////////////////////////////////////////////////////////////////////////// -// create a new tracked site. -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, '', ''); -} /** - * Singleton Method + * OWA WordPress Plugin Class * - * Returns an instance of OWA - * - * @return $owa object */ -function owa_getInstance() { +class owa_wp_plugin { - static $owa; + // cmd array + var $cmds = array(); + // plugin options + var $options = array( + + 'track_feed_links' => true, + 'feed_tracking_medium' => 'feed', + 'feed_subscription_param' => 'owa_sid' + ); - if( empty( $owa ) ) { + /** + * Constructor + * + */ + function __construct() { - require_once(OWA_BASE_CLASSES_DIR.'owa_wp.php'); + // needed??? + ob_start(); - // create owa instance w/ config - $owa = new owa_wp(); - $owa->setSiteId( md5( get_option( '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 ); + // fetch plugin options from DB and combine them with defaults. + $options = get_option( 'owa_wp_plugin' ); + + if ( $options ) { - // 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); + $this->options = array_merge_recursive($this->options, $options); } - if (isset($current_user->user_email)) { - $cu->setUserData('email_address', $current_user->user_email); - } + /* register WordPress hooks and filters. */ - 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)); - } + // insert javascript tracking tag + add_action('wp_head', array( $this,'insertTrackingTag' ), 100 ); - owa_coreAPI::debug("Wordpress User Role: ".print_r($current_user->roles, true)); - owa_coreAPI::debug("Wordpress Translated OWA User Role: ".$cu->getRole()); + // add tracking to feed entry permalinks + add_filter('the_permalink_rss', array( $this, 'decorateFeedEntryPermalink' ) ); - // 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; - } + // add tracking to feed subscription links + add_filter('bloginfo_url', array($this, 'decorateFeedSubscriptionLink' ) ); - // 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 - return wp_get_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'; - } + // register settings page + // - } 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(); - -} - -/** - * Prints helper page tags to the 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() ) { + // Actions if OWA is available as a library + if( $this->isOwaAvailable() ) { + + // handle API calls and other requests + add_action('init', array( $this, 'handleSpecialActionRequest' ) ); + + // @todo find a way for these methods to POST these to the OWA instance instead of via OWA's PHP Tracker + $this->defineActionHooks(); + + // Register admin pages + add_action('admin_menu', array( $this, 'registerAdminPages' ) ); + + // Create a new tracked site in OWA. + // @todo move this to REST API call when it's ready. + add_action('wpmu_new_blog', array($this, 'createTrackedSiteForNewBlog'), 10, 6); + + // Installation hook + register_activation_hook(__FILE__, 'owa_install'); - return; } } - // dont log customizer previews either. - if ( function_exists( 'is_customize_preview' ) ) { + /** + * Get an option value + */ + function getOption( $key ) { - if ( is_customize_preview() ) { + $options = array(); + $options = $this->options; + if ( array_key_exists( $key, $options ) ) { - return; + return $this->options[ $key ]; } } - // dont log requests for admin interface pages. - if ( function_exists( ' is_admin' ) && is_admin() ) { + /** + * Set an option value + */ + function setOption( $key, $value ) { - return; + $this->options[ $key ] = $value; } - - - // get instance of OWA - $owa = owa_getInstance(); - - // create a cmds object - $wp_cmds = owa_wp_plugin::getInstance(); - $wp_cmds->addTrackerToPage(); - - // convert cmds to string and feed to tracking tag template - $options = array( 'cmds' => $wp_cmds->cmdsToString() ); - - // place the tracking tag - $owa->placeHelperPageTags(true, $options); -} - - -/** - * 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; + /** + * Singelton + */ + static function getInstance() { - endif; + static $o; - 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); + if ( ! isset( $o ) ) { + + $o = new owa_wp_plugin(); + } -} - -/** - * 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_option('siteurl')), - 'name' => get_bloginfo('name'), - 'domain' => get_option('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'); - } -} - -/** - * 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'); + return $o; } -} - -/** - * 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); -} + /** + * Callback for admin_menu hook + */ + function registerAdminPages() { -/** - * 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; + if (function_exists('add_submenu_page')) { + + if ( $this->isOwaAvailable() ) { -} - -/** - * Main Plugin Class - * - * Should have no dependancies on OWA when loaded. - * - */ -class owa_wp_plugin { + add_submenu_page('index.php', 'OWA Dashboard', 'OWA Dashboard', 1, dirname(__FILE__), array( $this, 'pageController') ); + } + + if (function_exists('add_options_page')) { - // cmd array - var $cmds = array(); + add_options_page('OWA Options', 'OWA', 8, basename(__FILE__), array($this, 'options_page') ); + } + } + } /** - * Constructor - * - */ - function __construct() { + * Callback for reporting dashboard/pages + */ + function pageController() { + + if ( $this->isOwaAvailable() ) { - $this->defineActionHooks(); + $owa = $this->getOwaInstance(); + echo $owa->handleRequest(); + } } /** - * Singelton - * + * Callback for OWA settings page */ - static function getInstance() { - - static $o; + function options_page() { - if ( ! isset( $o ) ) { - - $o = new owa_wp_plugin(); - } + $owa = $this->getOwaInstance(); + + $params = array(); + $params['view'] = 'base.options'; + $params['subview'] = 'base.optionsGeneral'; + echo $owa->handleRequest($params); - return $o; } - + /** + * Hooks for tracking WordPress Admin actions + */ function defineActionHooks() { @@ -448,18 +219,56 @@ function defineActionHooks() { // track feeds - add_action('init', array( $this, 'addFeedTrackingQueryParams')); add_action( 'template_redirect', array( $this, 'trackFeedRequest'), 1 ); - - } // These hooks do NOT rely on OWA being accessable via PHP } + /** + * OWA Schema and setting installation + * + */ + function install() { + + define('OWA_INSTALLING', true); + + $params = array(); + //$params['do_not_fetch_config_from_db'] = true; + + $owa = $this->getOwaInstance($params); + $owa->setSetting('base', 'cache_objects', false); + $public_url = get_bloginfo('wpurl').'/wp-content/plugins/owa/'; + + $install_params = array('site_id' => md5(get_option('siteurl')), + 'name' => get_bloginfo('name'), + 'domain' => get_option('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); + } + + function handleSpecialActionRequest() { + + $owa = $this->getOwaInstance(); + + owa_coreAPI::debug("hello from WP special action handler"); + + return $owa->handleSpecialActionRequest(); + } + + // Add query vars to WordPress function addFeedTrackingQueryParams() { @@ -502,7 +311,7 @@ function setPageTitle() { } /** - * Determines the type of page being requested + * Determines the type of WordPress page * * @return string $type */ @@ -563,17 +372,7 @@ function cmdsToString() { return $out; } - - function getOption( $key ) { - $options = get_option( 'owa_wp_plugin' ); - - if ( $options && array_key_exists( $key, $options ) ) { - - return $options[ $key ]; - } - } - // check to see if OWA is available as a php library on the same server function isOwaAvailable() { @@ -585,23 +384,236 @@ function getOwaInstance() { static $owa; - if( empty( $owa ) ) { if ( $this->isOwaAvailable() ) { - - require_once(OWA_BASE_CLASSES_DIR.'owa_wp.php'); + + require_once('owa_env.php'); + require_once(OWA_BASE_CLASSES_DIR.'owa_php.php'); // create owa instance w/ config - $owa = new owa_wp(); + $owa = new owa_php(); $owa->setSiteId( md5( get_option( '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 ); + + + // register allowedSitesList filter + $dispatch = owa_coreAPI::getEventDispatch(); + // alternative auth method, sets auth status, role, and allowed sites list. + $dispatch->attachFilter('auth_status', 'owa_wp_plugin::wpAuthUser', 0); } } return $owa; } + /** + * OWA Authenication filter + * + * Uses WordPress priviledge system to determine OWA authentication levels. + * + * @return boolean + */ + static function 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_wp_plugin::translateAuthRole( $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; + } + } + + /** + * Translate WordPress to OWA Authentication Roles + * + * @param $roles array array of WP roles + * @return string + */ + static function translateAuthRole( $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; + } + + /** + * Insert Tracking Tag + * + * Adds javascript tracking tag int of all pages. + * + */ + function insertTrackingTag() { + + // Don't log if the page request is a preview - Wordpress 2.x or greater + if ( function_exists( 'is_preview' ) ) { + + if ( is_preview() ) { + + return; + } + } + + // dont log customizer previews either. + if ( function_exists( 'is_customize_preview' ) ) { + + if ( is_customize_preview() ) { + + return; + } + } + + // dont log requests for admin interface pages. + if ( function_exists( ' is_admin' ) && is_admin() ) { + + return; + } + + + // get instance of OWA + $owa = $this->getOwaInstance(); + + // set any cmds + $this->setPageType(); + $this->setPageTitle(); + + // convert cmds to string and pass to tracking tag template + $options = array( 'cmds' => $this->cmdsToString() ); + + // place the tracking tag + $owa->placeHelperPageTags(true, $options); + } + + /** + * Adds tracking source param to links in feeds + * + * @param string $link + * @return string + */ + function decorateFeedEntryPermalink($link) { + + // check for presence of '?' which is not present under URL rewrite conditions + + if ( $this->getOption( 'track_feed_links' ) ) { + + if ( strpos($link, "?") === false ) { + // add the '?' if not found + $link .= '?'; + } + + // setup link template + $link_template = "%s&%s=%s&%s=%s"; + + return sprintf($link_template, + $link, + 'owa_medium', + $this->getOption( 'feed_tracking_medium' ), + $this->getOption( 'feed_subscription_param' ), + $_GET[ $this->getOption( 'feed_subscription_param' ) ] + ); + } + } + + /** + * Wordpress filter function adds a GUID to the feed URL. + * + * @param array $binfo + * @return string $newbinfo + */ + function decorateFeedSubscriptionLink( $binfo ) { + + $is_feed = strpos($binfo, "feed="); + + if ( $is_feed && $this->getOption( 'track_feed_links' ) ) { + + $guid = crc32(getmypid().microtime()); + + $newbinfo = $binfo . "&" . $this->getOption('feed_subscription_param') . "=" . $guid; + + } else { + + $newbinfo = $binfo; + } + + return $newbinfo; + } + + // create a new tracked site. + function createTrackedSiteForNewBlog($blog_id, $user_id, $domain, $path, $site_id, $meta) { + + $owa = $this->getOwaInstance(); + $sm = owa_coreAPI::supportClassFactory( 'base', 'siteManager' ); + $sm->createNewSite( $domain, $domain, '', ''); + } + + /** * New Blog Action Tracker */ @@ -629,6 +641,8 @@ function trackedPostEditAction( $post_id, $post ) { /** * Post Action Tracker + * + * Trackes new and edited post actions. Including custom post types. */ function trackPostAction( $new_status, $old_status, $post ) { @@ -806,20 +820,6 @@ function trackFeedRequest() { } } - // adds the JavaScript Tracker cmds and script tag to the page. - function addTrackerToPage() { - - $this->setPageType(); - $this->setPageTitle(); - - //Output the script - - } - - function generateUniqueNumericId() { - - return crc32(getmypid().microtime()); - } } ?> \ No newline at end of file