'', 'path' => '', 'blog_id' => false, ); if ( is_array( $options ) && !empty( $options ) ) $options = array_merge( $defaults, $options ); else $options = $defaults; // Send new bloginfo with gmt_offset if ( $options['version'] < 3 ) $update_bloginfo = true; $options['version'] = STATS_VERSION; stats_set_options( $options ); if ( $update_bloginfo ) stats_update_bloginfo(); return $options; } function stats_footer() { global $wp_the_query, $current_user; $options = stats_get_options(); if ( !empty($current_user->ID) || empty($options['blog_id']) ) return; $a['blog'] = $options['blog_id']; $a['v'] = 'ext'; if ( $wp_the_query->is_single || $wp_the_query->is_page ) $a['post'] = $wp_the_query->get_queried_object_id(); else $a['post'] = '0'; ?> $v ) $jskvs[] = "$k:'$v'"; return join(',', $jskvs); } function stats_admin_menu() { if ( stats_get_option('blog_id') ) { $hook = add_submenu_page('index.php', __('Site Stats'), __('Site Stats'), 'publish_posts', 'stats', 'stats_reports_page'); add_action("load-$hook", 'stats_reports_load'); } $hook = add_submenu_page('plugins.php', __('WordPress.com Stats Plugin'), __('WordPress.com Stats'), 'manage_options', 'wpstats', 'stats_admin_page'); add_action("load-$hook", 'stats_admin_load'); add_action("admin_head-$hook", 'stats_admin_head'); add_action('admin_notices', 'stats_admin_notices'); } function stats_reports_load() { add_action('admin_head', 'stats_reports_head'); } function stats_reports_head() { ?> 'true', 'proxy' => '', 'page' => 'stats', 'key' => $key, 'day' => $day, 'blog' => $blog_id, 'charset' => get_option('blog_charset'), ); $args = array( 'view' => array('referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table'), 'numdays' => 'int', 'day' => 'date', 'unit' => array(1, 7, 31), 'summarize' => null, 'post' => 'int', 'width' => 'int', 'height' => 'int', 'data' => 'data', ); foreach ( $args as $var => $vals ) { if ( ! isset($_GET[$var]) ) continue; if ( is_array($vals) ) { if ( in_array($_GET[$var], $vals) ) $q[$var] = $_GET[$var]; } elseif ( $vals == 'int' ) { $q[$var] = intval($_GET[$var]); } elseif ( $vals == 'date' ) { if ( preg_match('/^\d{4}-\d{2}-\d{2}$/', $_GET[$var]) ) $q[$var] = $_GET[$var]; } elseif ( $vals == null ) { $q[$var] = ''; } elseif ( $vals == 'data' ) { if ( substr($_GET[$var], 0, 9) == 'index.php' ) $q[$var] = $_GET[$var]; } } if ( isset( $_GET['chart'] ) ) { if ( preg_match('/^[a-z0-9-]+$/', $_GET['chart']) ) $url = "https://dashboard.wordpress.com/wp-includes/charts/{$_GET['chart']}.php"; } else { $url = "https://dashboard.wordpress.com/wp-admin/index.php"; } $url = add_query_arg($q, $url); $get = wp_remote_get($url, array('timeout'=>300)); if ( is_wp_error($get) || empty($get['body']) ) { $http = $_SERVER['https'] ? 'https' : 'http'; $day = $day ? "&day=$day" : ''; echo ""; } else { $body = convert_post_titles($get['body']); $body = convert_swf_urls($body); echo $body; } if ( isset( $_GET['noheader'] ) ) die; } function convert_swf_urls($html) { if ( version_compare($wp_version, '2.8', '<') ) { $path = dirname(plugin_basename(__FILE__)); if ( $path == '.' ) $path = ''; $swf_url = trailingslashit( plugins_url( $path ) ) . 'open-flash-chart.swf?data='; } else { $swf_url = trailingslashit( plugins_url( '', __FILE__) ) . 'open-flash-chart.swf?data='; } $html = preg_replace('!( implode(',', $matches[1]), 'post_type' => 'any', 'numberposts' => -1, )); foreach ( $posts as $post ) $stats_posts[$post->ID] = $post; $html = preg_replace_callback("!$pattern!", 'convert_post_title', $html); return $html; } function convert_post_title($matches) { global $stats_posts; $post_id = $matches[1]; if ( isset($stats_posts[$post_id]) ) return ''.get_the_title($post_id).''; return $matches[0]; } function stats_admin_load() { global $plugin_page; if ( ! empty( $_POST['action'] ) && $_POST['_wpnonce'] == wp_create_nonce('stats') ) { switch( $_POST['action'] ) { case 'reset' : stats_set_options(array()); wp_redirect( "plugins.php?page=$plugin_page" ); exit; case 'enter_key' : stats_check_key( $_POST['api_key'] ); wp_redirect( "plugins.php?page=$plugin_page" ); exit; case 'add_or_replace' : $key_check = stats_get_option('key_check'); stats_set_api_key($key_check[0]); if ( isset($_POST['add']) ) { stats_get_blog_id($key_check[0]); } else { extract( parse_url( get_option( 'home' ) ) ); $path = rtrim( $path, '/' ); if ( empty( $path ) ) $path = '/'; $options = stats_get_options(); $options['blog_id'] = intval($_POST['blog_id']); $options['api_key'] = $key_check[0]; $options['host'] = $host; $options['path'] = $path; stats_set_options($options); stats_update_bloginfo(); } if ( stats_get_option('blog_id') ) stats_set_option('key_check', false); wp_redirect( "plugins.php?page=$plugin_page" ); exit; } } $options = stats_get_options(); if ( empty( $options['blog_id']) && empty( $options['key_check'] ) && stats_get_api_key() ) stats_check_key( stats_get_api_key() ); } function stats_admin_notices() { if ( stats_get_api_key() ) return; echo "

" . sprintf(__('WordPress.com Stats needs attention: please enter an API key or disable the plugin.'), "plugins.php?page=wpstats") . "

"; } function stats_admin_head() { ?>

add this as a new blog on your WordPress.com account or replace an existing blog and inherit its stats history.'); ?>

Get your key here.)'); ?>

your Dashboard to see your site stats. If you are asked to log in, use your WordPress.com username and password.'), 'index.php?page=stats'); ?>

'stats_get_posts', 'wpStats.get_blog' => 'stats_get_blog' ); return array_merge( $methods, $my_methods ); } function stats_get_posts( $args ) { list( $post_ids ) = $args; $post_ids = array_map( 'intval', (array) $post_ids ); $r = 'include=' . join(',', $post_ids); $posts = get_posts( $r ); $_posts = array(); foreach ( $post_ids as $post_id ) $_posts[$post_id] = stats_get_post($post_id); return $_posts; } function stats_get_blog( ) { $home = parse_url( get_option('home') ); $blog = array( 'host' => $home['host'], 'path' => $home['path'], 'name' => get_option('blogname'), 'description' => get_option('blogdescription'), 'siteurl' => get_option('siteurl'), 'gmt_offset' => get_option('gmt_offset'), 'version' => STATS_VERSION ); return array_map('wp_specialchars', $blog); } function stats_get_post( $post_id ) { $post = get_post( $post_id ); if ( empty( $post ) ) $post = get_page( $post_id ); $_post = array( 'id' => $post->ID, 'permalink' => get_permalink($post->ID), 'title' => $post->post_title, 'type' => $post->post_type ); return array_map('wp_specialchars', $_post); } function stats_client() { require_once( ABSPATH . WPINC . '/class-IXR.php' ); $client = new IXR_ClientMulticall( STATS_XMLRPC_SERVER ); $client->useragent = 'WordPress/' . $client->useragent; return $client; } function stats_add_call() { global $stats_xmlrpc_client; if ( empty($stats_xmlrpc_client) ) { $stats_xmlrpc_client = stats_client(); ignore_user_abort(true); add_action('shutdown', 'stats_multicall_query'); } $args = func_get_args(); call_user_method_array( 'addCall', $stats_xmlrpc_client, $args ); } function stats_multicall_query() { global $stats_xmlrpc_client; $stats_xmlrpc_client->query(); } function stats_update_bloginfo() { stats_add_call( 'wpStats.update_bloginfo', stats_get_api_key(), stats_get_option('blog_id'), stats_get_blog() ); } function stats_update_post( $post_id ) { if ( !in_array( get_post_type($post_id), array('post', 'page', 'attachment') ) ) return; stats_add_call( 'wpStats.update_postinfo', stats_get_api_key(), stats_get_option('blog_id'), stats_get_post($post_id) ); } function stats_flush_posts() { stats_add_call( 'wpStats.flush_posts', stats_get_api_key(), stats_get_option('blog_id') ); } // WP < 2.5 function stats_activity() { if ( did_action( 'rightnow_end' ) ) return; $options = stats_get_options(); if ( $options['blog_id'] ) { ?>

' . __('your Global Dashboard') . ''); ?>

query( 'wpStats.check_key', $api_key, stats_get_blog() ); if ( $client->isError() ) { if ( $client->getErrorCode() == -32300 ) $options['error'] = __('Your blog was unable to connect to WordPress.com. Please ask your host for help. (' . $client->getErrorMessage() . ')'); else $options['error'] = $client->getErrorMessage(); stats_set_options( $options ); return false; } else { $options['error'] = false; } $options['key_check'] = $client->getResponse(); stats_set_options($options); return true; } function stats_get_blog_id($api_key) { $options = stats_get_options(); require_once( ABSPATH . WPINC . '/class-IXR.php' ); $client = new IXR_Client( STATS_XMLRPC_SERVER ); extract( parse_url( get_option( 'home' ) ) ); $path = rtrim( $path, '/' ); if ( empty( $path ) ) $path = '/'; $client->query( 'wpStats.get_blog_id', $api_key, stats_get_blog() ); if ( $client->isError() ) { if ( $client->getErrorCode() == -32300 ) $options['error'] = __('Your blog was unable to connect to WordPress.com. Please ask your host for help. (' . $client->getErrorMessage() . ')'); else $options['error'] = $client->getErrorMessage(); stats_set_options( $options ); return false; } else { $options['error'] = false; } $response = $client->getResponse(); $blog_id = isset($response['blog_id']) ? (int) $response['blog_id'] : false; $options[ 'host' ] = $host; $options[ 'path' ] = $path; $options[ 'blog_id' ] = $blog_id; stats_set_options( $options ); stats_set_api_key( $api_key ); return $blog_id; } function stats_activate() { $options = stats_get_options(); if ( empty($options['blog_id']) && $api_key = stats_get_api_key() ) stats_get_blog_id($api_key); } function stats_deactivate() { delete_option('stats_options'); delete_option('stats_dashboard_widget'); } /* Dashboard Stuff: WP >= 2.5 */ function stats_register_dashboard_widget() { if ( ( !$blog_id = stats_get_option('blog_id') ) || !stats_get_api_key() || !current_user_can( 'manage_options' ) ) return; // wp_dashboard_empty: we load in the content after the page load via JS wp_register_sidebar_widget( 'dashboard_stats', __( 'Stats' ), 'wp_dashboard_empty', array( 'width' => 'full' ) ); wp_register_widget_control( 'dashboard_stats', __( 'Stats' ), 'stats_register_dashboard_widget_control', array(), array( 'widget_id' => 'dashboard_stats', ) ); add_action( 'admin_head', 'stats_dashboard_head' ); } function stats_dashboard_widget_options() { $defaults = array( 'chart' => 1, 'top' => 7, 'search' => 7, 'active' => 7 ); if ( ( !$options = get_option( 'stats_dashboard_widget' ) ) || !is_array($options) ) $options = array(); return array_merge( $defaults, $options ); } function stats_register_dashboard_widget_control() { $periods = array( '1' => __('day'), '7' => __('week'), '31' => __('month') ); $intervals = array( '-1' => __('all time'), '1' => __('the past day'), '7' => __('the past week'), '31' => __('the past month'), '90' => __('the past quarter'), '365' => __('the past year') ); $options = stats_dashboard_widget_options(); if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'dashboard_stats' == $_POST['widget_id'] ) { if ( isset($periods[$_POST['chart']]) ) $options['chart'] = $_POST['chart']; foreach ( array( 'top', 'search', 'active' ) as $key ) if ( isset($intervals[$_POST[$key]]) ) $options[$key] = $_POST[$key]; update_option( 'stats_dashboard_widget', $options ); } ?>

.

.

.

.

false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); $args = wp_parse_args( $args, $defaults ); $args['table'] = $table; $args['blog_id'] = $blog_id; $args['api_key'] = $key; $stats_csv_url = add_query_arg( $args, 'http://stats.wordpress.com/csv.php' ); $key = md5( $stats_csv_url ); // Get cache $stats_cache = get_option( 'stats_cache' ); if ( !$stats_cache || !is_array($stats_cache) ) $stats_cache = array(); // Return or expire this key if ( isset($stats_cache[$key]) ) { $time = key($stats_cache[$key]); if ( time() - $time < 300 ) return $stats_cache[$key][$time]; unset( $stats_cache[$key] ); } $stats_rows = array(); do { if ( !$stats = stats_get_remote_csv( $stats_csv_url ) ) break; $labels = array_shift( $stats ); if ( 0 === stripos( $labels[0], 'error' ) ) break; $stats_rows = array(); for ( $s = 0; isset($stats[$s]); $s++ ) { $row = array(); foreach ( $labels as $col => $label ) $row[$label] = $stats[$s][$col]; $stats_rows[] = $row; } } while(0); // Expire old keys foreach ( $stats_cache as $k => $cache ) if ( !is_array($cache) || 300 < time() - key($cache) ) unset($stats_cache[$k]); // Set cache $stats_cache[$key] = array( time() => $stats_rows ); update_option( 'stats_cache', $stats_cache ); return $stats_rows; } function stats_get_remote_csv( $url ) { $url = clean_url( $url, null, 'url' ); // Yay! if ( ini_get('allow_url_fopen') ) { $fp = @fopen($url, 'r'); if ( $fp ) { //stream_set_timeout($fp, $timeout); // Requires php 4.3 $data = array(); while ( $remote_read = fgetcsv($fp, 1000) ) $data[] = $remote_read; fclose($fp); return $data; } } // Boo - we need to use wp_remote_fopen for maximium compatibility if ( !$csv = wp_remote_fopen( $url ) ) return false; return stats_str_getcsv( $csv ); } // rather than parsing the csv and its special cases, we create a new file and do fgetcsv on it. function stats_str_getcsv( $csv ) { if ( !$temp = tmpfile() ) // tmpfile() automatically unlinks return false; $data = array(); fwrite($temp, $csv, strlen($csv)); fseek($temp, 0); while ( false !== $row = fgetcsv($temp, 1000) ) $data[] = $row; fclose($temp); return $data; } function stats_dashboard_widget_content() { $blog_id = stats_get_option('blog_id'); if ( ( !$width = (int) ( $_GET['width'] / 2 ) ) || $width < 250 ) $width = 370; if ( ( !$height = (int) $_GET['height'] - 36 ) || $height < 230 ) $height = 230; $_width = $width - 5; $_height = $height - ( $GLOBALS['is_winIE'] ? 16 : 5 ); // hack! $options = stats_dashboard_widget_options(); $q = array( 'noheader' => 'true', 'proxy' => '', 'page' => 'stats', 'blog' => $blog_id, 'key' => stats_get_api_key(), 'chart' => '', 'unit' => $options['chart'], 'width' => $_width, 'height' => $_height, ); $url = 'https://dashboard.wordpress.com/wp-admin/index.php'; $url = add_query_arg($q, $url); $get = wp_remote_get($url, array('timeout'=>300)); if ( is_wp_error($get) || empty($get['body']) ) { $http = $_SERVER['https'] ? 'https' : 'http'; $src = clean_url( "$http://dashboard.wordpress.com/wp-admin/index.php?page=estats&blog=$blog_id&noheader=true&chart&unit=$options[chart]&width=$_width&height=$_height" ); echo ""; } else { $body = convert_swf_urls($get['body']); echo $body; } $post_ids = array(); if ( version_compare( '2.7-z', $GLOBALS['wp_version'], '<=' ) ) { $csv_args = array( 'top' => '&limit=8', 'active' => '&limit=5', 'search' => '&limit=5' ); $printf = __( '%s %s Views' ); } else { $csv_args = array( 'top' => '', 'active' => '', 'search' => '' ); $printf = __( '%s, %s views' ); } foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $post ) $post_ids[] = $post['post_id']; foreach ( $active_posts = stats_get_csv( 'postviews', "days=$options[active]$csv_args[active]" ) as $post ) $post_ids[] = $post['post_id']; // cache get_posts( array( 'include' => join( ',', array_unique($post_ids) ) ) ); $searches = array(); foreach ( $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" ) as $search_term ) $searches[] = wp_specialchars($search_term['searchterm']); ?>

' . get_the_title( $post['post_id'] ) . '', // '' . $post['post_title'] . '', number_format_i18n( $post['views'] ) ); ?>

' . get_the_title( $post['post_id'] ) . '', // '' . $post['post_title'] . '', number_format_i18n( $post['views'] ) ); ?>


= 0; $t-- ) { $a = floor( $num / pow( 62, $t ) ); $out = $out . substr( $index, $a, 1 ); $num = $num - ( $a * pow( 62, $t ) ); } return $out; } } if ( ! function_exists('get_shortlink') ) : function get_shortlink( $post_id, $force_numeric = false ) { $blog_id = stats_get_option('blog_id'); // Return link to blog home if no post id if ( empty($post_id) ) return 'http://wp.me/' . dec2sixtwo($blog_id); $post = get_post($post_id); $type = ''; if ( !$force_numeric && 'publish' == $post->post_status && 'post' == $post->post_type && strlen($post->post_name) <= 8 && false === strpos($post->post_name, '%') && false === strpos($post->post_name, '-') ) { $id = $post->post_name; $type = 's'; } else { $id = dec2sixtwo($post_id); if ( 'page' == $post->post_type ) $type = 'P'; elseif ( 'post' == $post->post_type ) $type = 'p'; elseif ( 'attachment' == $post->post_type ) $type = 'a'; } if ( empty($type) ) return ''; return 'http://wp.me/' . $type . dec2sixtwo($blog_id) . '-' . $id; } function shortlink_wp_head() { global $wp_query; if ( ! ( is_singular() || is_front_page() ) ) return; $shortlink = get_shortlink($wp_query->get_queried_object_id()); echo ''; } function shortlink_header() { global $wp_query; if ( headers_sent() ) return; if ( ! ( is_singular() || is_front_page() ) ) return; $shortlink = get_shortlink($wp_query->get_queried_object_id()); header('Link: <' . $shortlink . '>; rel=shortlink'); } add_action('wp_head', 'shortlink_wp_head'); add_action('wp', 'shortlink_header'); function get_shortlink_html($html, $post_id) { $url = get_shortlink($post_id); $html .= '' . __('Get Shortlink') . ''; return $html; } add_filter( 'get_sample_permalink_html', 'get_shortlink_html', 10, 2 ); endif; add_action( 'wp_dashboard_setup', 'stats_register_dashboard_widget' ); add_filter( 'wp_dashboard_widgets', 'stats_add_dashboard_widget' ); // Boooooooooooring init stuff register_activation_hook(__FILE__, 'stats_activate'); register_deactivation_hook(__FILE__, 'stats_deactivate'); add_action( 'admin_menu', 'stats_admin_menu' ); add_action( 'activity_box_end', 'stats_activity', 1 ); // WP < 2.5 // Plant the tracking code in the footer add_action( 'wp_footer', 'stats_footer', 101 ); // Tell HQ about changed settings add_action( 'update_option_home', 'stats_update_bloginfo' ); add_action( 'update_option_siteurl', 'stats_update_bloginfo' ); add_action( 'update_option_blogname', 'stats_update_bloginfo' ); add_action( 'update_option_blogdescription', 'stats_update_bloginfo' ); add_action( 'update_option_timezone_string', 'stats_update_bloginfo' ); add_action( 'add_option_timezone_string', 'stats_update_bloginfo' ); add_action( 'update_option_gmt_offset', 'stats_update_bloginfo' ); // Tell HQ about changed posts add_action( 'save_post', 'stats_update_post', 10, 1 ); // Tell HQ to drop all post info for this blog add_action( 'update_option_permalink_structure', 'stats_flush_posts' ); // Teach the XMLRPC server how to dance properly add_filter( 'xmlrpc_methods', 'stats_xmlrpc_methods' ); define( 'STATS_VERSION', '3' ); define( 'STATS_XMLRPC_SERVER', 'http://wordpress.com/xmlrpc.php' );